From 5ab9c1c10322a4db21349555d901646259d26d6f Mon Sep 17 00:00:00 2001 From: sin Date: Thu, 6 Feb 2014 12:01:00 +0000 Subject: Factor out the rc commands --- config.def.h | 11 +++++++++-- sinit.c | 24 ++++++------------------ util.h | 1 + util/eprintf.c | 1 + 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/config.def.h b/config.def.h index 050d7e0..a257b45 100644 --- a/config.def.h +++ b/config.def.h @@ -1,6 +1,13 @@ /* See LICENSE file for copyright and license details. */ + +static const char *rcinitcmd[] = { "/bin/rc.init", NULL }; +static Arg rcinitarg = { .v = rcinitcmd }; + +static const char *rcrebootcmd[] = { "/bin/rc.shutdown", "reboot", NULL }; +static const char *rcpoweroffcmd[] = { "/bin/rc.shutdown", "poweroff", NULL }; + static const char *fifopath = "/var/run/sinit.fifo"; static Command commands[] = { - { "poweroff", cmdpoweroff, {0} }, - { "reboot", cmdreboot, {0} }, + { "poweroff", spawn, { .v = rcpoweroffcmd } }, + { "reboot", spawn, { .v = rcrebootcmd } }, }; diff --git a/sinit.c b/sinit.c index 69fd43e..a398e6e 100644 --- a/sinit.c +++ b/sinit.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ + #include #include #include @@ -23,10 +24,8 @@ typedef struct { const Arg arg; } Command; -static void cmdpoweroff(const Arg *); -static void cmdreboot(const Arg *); static void dispatchcmd(int); -static void spawn(const char *, char *const []); +static void spawn(const Arg *); #include "config.h" @@ -53,7 +52,7 @@ main(void) sigprocmask(SIG_UNBLOCK, &set, 0); - spawn("/bin/rc.init", (char *[]){ "rc.init", NULL }); + spawn(&rcinitarg); unlink(fifopath); umask(0); @@ -78,18 +77,6 @@ main(void) return EXIT_SUCCESS; } -static void -cmdpoweroff(const Arg *arg) -{ - spawn("/bin/rc.shutdown", (char *[]) { "rc", "poweroff", NULL }); -} - -static void -cmdreboot(const Arg *arg) -{ - spawn("/bin/rc.shutdown", (char *[]) { "rc", "reboot", NULL }); -} - static void dispatchcmd(int fd) { @@ -113,9 +100,10 @@ dispatchcmd(int fd) } static void -spawn(const char *file, char *const argv[]) +spawn(const Arg *arg) { pid_t pid; + const char *p = arg->v; pid = fork(); if (pid < 0) @@ -123,7 +111,7 @@ spawn(const char *file, char *const argv[]) if (pid == 0) { setsid(); setpgid(0, 0); - execvp(file, argv); + execvp(p, arg->v); _exit(errno == ENOENT ? 127 : 126); } } diff --git a/util.h b/util.h index af96e0e..3ce02d1 100644 --- a/util.h +++ b/util.h @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ + #define LEN(x) (sizeof (x) / sizeof *(x)) void enprintf(int, const char *, ...); diff --git a/util/eprintf.c b/util/eprintf.c index af05686..8dda0c8 100644 --- a/util/eprintf.c +++ b/util/eprintf.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ + #include #include #include -- cgit v1.2.3-70-g09d2