diff options
| author | Klaatu <[email protected]> | 2015-05-17 15:33:21 +1200 |
|---|---|---|
| committer | Klaatu <[email protected]> | 2015-05-17 15:33:21 +1200 |
| commit | b0de699679e8f1e39af847ed172d1ba605b4370c (patch) | |
| tree | 01dac00471d61f727394e508c613b29cff0ceae5 /contrib/conditional-init.c | |
bulk upload of source
Diffstat (limited to 'contrib/conditional-init.c')
| -rw-r--r-- | contrib/conditional-init.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/contrib/conditional-init.c b/contrib/conditional-init.c new file mode 100644 index 0000000..10c69cc --- /dev/null +++ b/contrib/conditional-init.c @@ -0,0 +1,47 @@ +#include <unistd.h> +#include <time.h> +#include <unistd.h> +#include <sys/reboot.h> +#include "../ninitfeatures.h" +#include "../uid.h" + +#define TM_OUT 1200 + +int main(int argc, char **argv) { + unsigned long ul; + char *stable, *test; + + if (argc < 4) { + carp("usage: conditional-init now /stable/init /test/init [args...]\n" + "\tnow must be the output of: date +%s"); + return 1; + } + + errmsg_argv0 = "conditional-init"; + stable=argv[2]; + test=argv[3]; + + if (scan_ulong(argv[1], &ul)==0 || ul + TM_OUT < (unsigned long)time(0)) { + carp("booting (stable): ", stable); + argv += 3; + argv[0] = stable; + execve(stable, argv, environ); + return 0; + } + + if (fork()==0) { + unsigned long deadline = (unsigned long)time(0) + TM_OUT; + while ((unsigned long)time(0) < deadline ) nano_sleep(1,0); + sync(); + set_reboot(RB_AUTOBOOT); + return 2; + } + + argv += 3; + argv[0]=test; + carp("booting (test): ", test); + execve(test, argv, environ); + nano_sleep(TM_OUT,0); /* only if test fails */ + set_reboot(RB_AUTOBOOT); + return 1; +} |
