diff options
| author | Seth Jennings <sjenning@redhat.com> | 2014-12-16 12:58:20 -0500 |
|---|---|---|
| committer | Jiri Kosina <jkosina@suse.cz> | 2014-12-22 09:40:49 -0500 |
| commit | 13d1cf7e702596e0cd8ec62afa6bd49c431f2d0c (patch) | |
| tree | 0ee633d435755d634793462e0a6fc528205822dd /samples/livepatch/livepatch-sample.c | |
| parent | b700e7f03df5d92f85fa5247fe1f557528d3363d (diff) | |
livepatch: samples: add sample live patching module
Add a sample live patching module.
Signed-off-by: Seth Jennings <sjenning@redhat.com>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Reviewed-by: Petr Mladek <pmladek@suse.cz>
Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'samples/livepatch/livepatch-sample.c')
| -rw-r--r-- | samples/livepatch/livepatch-sample.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c new file mode 100644 index 000000000000..21f159d51074 --- /dev/null +++ b/samples/livepatch/livepatch-sample.c | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /* | ||
| 2 | * livepatch-sample.c - Kernel Live Patching Sample Module | ||
| 3 | * | ||
| 4 | * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU General Public License | ||
| 8 | * as published by the Free Software Foundation; either version 2 | ||
| 9 | * of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <linux/module.h> | ||
| 21 | #include <linux/kernel.h> | ||
| 22 | #include <linux/livepatch.h> | ||
| 23 | |||
| 24 | /* | ||
| 25 | * This (dumb) live patch overrides the function that prints the | ||
| 26 | * kernel boot cmdline when /proc/cmdline is read. | ||
| 27 | * | ||
| 28 | * Example: | ||
| 29 | * $ cat /proc/cmdline | ||
| 30 | * <your cmdline> | ||
| 31 | * $ insmod livepatch-sample.ko | ||
| 32 | * $ cat /proc/cmdline | ||
| 33 | * this has been live patched | ||
| 34 | * $ echo 0 > /sys/kernel/livepatch/klp_sample/enabled | ||
| 35 | * <your cmdline> | ||
| 36 | */ | ||
| 37 | |||
| 38 | #include <linux/seq_file.h> | ||
| 39 | static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) | ||
| 40 | { | ||
| 41 | seq_printf(m, "%s\n", "this has been live patched"); | ||
| 42 | return 0; | ||
| 43 | } | ||
| 44 | |||
| 45 | static struct klp_func funcs[] = { | ||
| 46 | { | ||
| 47 | .old_name = "cmdline_proc_show", | ||
| 48 | .new_func = livepatch_cmdline_proc_show, | ||
| 49 | }, { } | ||
| 50 | }; | ||
| 51 | |||
| 52 | static struct klp_object objs[] = { | ||
| 53 | { | ||
| 54 | /* name being NULL means vmlinux */ | ||
| 55 | .funcs = funcs, | ||
| 56 | }, { } | ||
| 57 | }; | ||
| 58 | |||
| 59 | static struct klp_patch patch = { | ||
| 60 | .mod = THIS_MODULE, | ||
| 61 | .objs = objs, | ||
| 62 | }; | ||
| 63 | |||
| 64 | static int livepatch_init(void) | ||
| 65 | { | ||
| 66 | int ret; | ||
| 67 | |||
| 68 | ret = klp_register_patch(&patch); | ||
| 69 | if (ret) | ||
| 70 | return ret; | ||
| 71 | ret = klp_enable_patch(&patch); | ||
| 72 | if (ret) { | ||
| 73 | WARN_ON(klp_unregister_patch(&patch)); | ||
| 74 | return ret; | ||
| 75 | } | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | static void livepatch_exit(void) | ||
| 80 | { | ||
| 81 | WARN_ON(klp_disable_patch(&patch)); | ||
| 82 | WARN_ON(klp_unregister_patch(&patch)); | ||
| 83 | } | ||
| 84 | |||
| 85 | module_init(livepatch_init); | ||
| 86 | module_exit(livepatch_exit); | ||
| 87 | MODULE_LICENSE("GPL"); | ||
