diff --git a/README.adoc b/README.adoc index 66e76da..0ae5622 100644 --- a/README.adoc +++ b/README.adoc @@ -1239,6 +1239,8 @@ although `-E` is smarter: so you should almost always use it, unless you are really counting each cycle ;-) +This method prevents the BusyBox' init from launching a shell, so you cannot interact with the system afterwards. If you also want that, use: <>. + If the script is large, you can add it to a gitignored file and pass that to `-E` as in: .... @@ -1286,6 +1288,7 @@ But this fails when we are `init` itself! but why not just use your super simple and effective `/poweroff.out` and be done with it? +[[init-busybox]] === Run command at the end of BusyBox init If you rely on something that BusyBox' init set up for you like networking, you could do: diff --git a/kernel_module/README.adoc b/kernel_module/README.adoc index 6e9650c..3a1553d 100644 --- a/kernel_module/README.adoc +++ b/kernel_module/README.adoc @@ -36,6 +36,8 @@ .. link:character_device.c[] .. link:character_device_create.c[] .. link:virt_to_phys.c[] +. Utilities +.. kstrto . Hardware device drivers .. link:pci_min.c[] .. link:pci.c[] diff --git a/kernel_module/debugfs.c b/kernel_module/debugfs.c index efb1a26..5e8f405 100644 --- a/kernel_module/debugfs.c +++ b/kernel_module/debugfs.c @@ -21,6 +21,8 @@ Only the more basic fops can be implemented in debugfs, e.g. mmap is never calle static struct dentry *dir, *toplevel_file; static u32 value = 42; +/* This basically re-implents the write operation of debugfs_create_u32, + * it is just an excuse to illustrate a fop. */ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off) { int ret; diff --git a/kernel_module/kstrto.c b/kernel_module/kstrto.c new file mode 100644 index 0000000..f3cbdae --- /dev/null +++ b/kernel_module/kstrto.c @@ -0,0 +1,53 @@ +/* +https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658 + +Usage: + + /kstrto.sh +*/ + +#include +#include +#include +#include /* S_IRUSR */ + +static struct dentry *toplevel_file; + +static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off) +{ + int ret; + unsigned long long res; + ret = kstrtoull_from_user(buf, len, 10, &res); + if (ret) { + /* Negative error code. */ + pr_info("ko = %d\n", ret); + return ret; + } else { + pr_info("ok = %llu\n", res); + *off= len; + return len; + } +} + +static const struct file_operations fops = { + .owner = THIS_MODULE, + .write = write, +}; + +static int myinit(void) +{ + toplevel_file = debugfs_create_file("lkmc_kstrto", S_IWUSR, NULL, NULL, &fops); + if (!toplevel_file) { + return -1; + } + return 0; +} + +static void myexit(void) +{ + debugfs_remove(toplevel_file); +} + +module_init(myinit) +module_exit(myexit) +MODULE_LICENSE("GPL"); diff --git a/rootfs_overlay/kstrto.sh b/rootfs_overlay/kstrto.sh new file mode 100755 index 0000000..05628f5 --- /dev/null +++ b/rootfs_overlay/kstrto.sh @@ -0,0 +1,7 @@ +#!/bin/sh +insmod /kstrto.ko +cd /sys/kernel/debug +echo 1234 > lkmc_kstrto +# 1234 +echo foobar > lkmc_kstrto +# -22