diff --git a/README.adoc b/README.adoc index 2c9c5c1..5e29d87 100644 --- a/README.adoc +++ b/README.adoc @@ -2664,17 +2664,68 @@ ____ And you can try it out with: .... -./run -e 'init=/init_env_poweroff.sh - asdf=qwer zxcv' +./run -e 'init=/init_env_poweroff.out - asdf=qwer zxcv' .... -Source: link:rootfs_overlay/init_env_poweroff.sh[]. - -Also note how the annoying dash `-` also gets passed as a parameter to `init`, which makes it impossible to use this method for most executables. - -Finally, the docs are lying, arguments with dots that come after `-` are still treated specially (of the form `subsystem.somevalue`) and disappear: +Output: .... -./run -e 'init=/init_env_poweroff.sh - /poweroff.out' +args: +/init_env_poweroff.out +- +zxcv + +env: +HOME=/ +TERM=linux +asdf=qwer +.... + +Source: link:kernel_module/user/init_env_poweroff.c[]. + +==== init environment args + +The annoying dash `-` gets passed as a parameter to `init`, which makes it impossible to use this method for most non custom executables. + +Arguments with dots that come after `-` are still treated specially (of the form `subsystem.somevalue`) and disappear, from args, e.g.: + +.... +./run -e 'init=/init_env_poweroff.out - /poweroff.out' +.... + +outputs: + +.... +args +/init_env_poweroff.out +- +ab +.... + +so see how `a.b` is gone. + +==== init environment env + +Wait, where do `HOME` and `TERM` come from? (greps the kernel). Ah, OK, the kernel sets those by default: https://github.com/torvalds/linux/blob/94710cac0ef4ee177a63b5227664b38c95bbf703/init/main.c#L173 + +.... +const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, }; +.... + +Furthermore, if you run something inside a shell: + +.... +./run -E '/usr/bin/env' +.... + +BusyBox also defines `SHLVL` and `PWD=`: + +.... +SHLVL=1 +HOME=/ +TERM=linux +lkmc_eval=L3Vzci9iaW4vZW52 +PWD=/ .... === Networking diff --git a/kernel_module/user/init_env_poweroff.c b/kernel_module/user/init_env_poweroff.c new file mode 100644 index 0000000..dc275f9 --- /dev/null +++ b/kernel_module/user/init_env_poweroff.c @@ -0,0 +1,26 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include + +int main(int argc, char **argv) +{ + int i; + + puts("args:"); + for (i = 0; i < argc; ++i) + puts(argv[i]); + puts(""); + + puts("env:"); + extern char **environ; + char **env = environ; + while (*env) { + printf("%s\n", *env); + env++; + } + puts(""); + + /* Poweroff. */ + reboot(RB_POWER_OFF); +} diff --git a/rootfs_overlay/init_env_poweroff.sh b/rootfs_overlay/init_env_poweroff.sh deleted file mode 100755 index 5c10aaa..0000000 --- a/rootfs_overlay/init_env_poweroff.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -echo "$@" -env -/poweroff.out