diff options
-rw-r--r-- | initd/init.h | 4 | ||||
-rw-r--r-- | initd/main.c | 9 | ||||
-rw-r--r-- | initd/runlst.c | 53 | ||||
-rw-r--r-- | lib/include/service.h | 14 | ||||
-rw-r--r-- | lib/include/util.h | 2 | ||||
-rw-r--r-- | lib/src/delsvc.c | 17 | ||||
-rw-r--r-- | lib/src/rdsvc.c | 121 | ||||
-rw-r--r-- | lib/src/splitkv.c | 73 | ||||
-rw-r--r-- | lib/src/svc_tsort.c | 16 | ||||
-rw-r--r-- | servicecmd/list.c | 8 | ||||
-rw-r--r-- | services/loopback.in | 3 | ||||
-rw-r--r-- | services/reboot.in | 4 | ||||
-rw-r--r-- | services/shutdown.in | 4 | ||||
-rw-r--r-- | services/sigkill.in | 4 | ||||
-rw-r--r-- | services/sigterm.in | 5 | ||||
-rw-r--r-- | services/sync.in | 6 | ||||
-rwxr-xr-x | services/sysctl.in | 3 |
17 files changed, 205 insertions, 141 deletions
diff --git a/initd/init.h b/initd/init.h index cbe22aa..c8d16ba 100644 --- a/initd/init.h +++ b/initd/init.h @@ -48,7 +48,7 @@ enum { does not exit with EXIT_SUCCESS, processing of the list is aborted and the function returns the exit status of the failed process. */ -int runlst_wait(char **exec, size_t num, const char *ctty); +int runlst_wait(exec_t *list, const char *ctty); /* Does basically the same as runlst_wait, but asynchronously. @@ -60,7 +60,7 @@ int runlst_wait(char **exec, size_t num, const char *ctty); Alternatively, if num is 1, the child process directly exec()s the given command. */ -pid_t runlst(char **exec, size_t num, const char *ctty); +pid_t runlst(exec_t *list, const char *ctty); /********** setup_tty.c **********/ diff --git a/initd/main.c b/initd/main.c index 9ce23c0..5f85641 100644 --- a/initd/main.c +++ b/initd/main.c @@ -53,7 +53,7 @@ static void handle_exited(service_t *svc) } } - svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty); + svc->pid = runlst(svc->exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); delsvc(svc); @@ -115,7 +115,7 @@ static void start_runlevel(int level) svc = cfg.targets[level]; cfg.targets[level] = svc->next; - if (!svc->num_exec) { + if (svc->exec == NULL) { print_status(svc->desc, STATUS_OK, false); delsvc(svc); continue; @@ -124,8 +124,7 @@ static void start_runlevel(int level) if (svc->type == SVC_WAIT) { print_status(svc->desc, STATUS_WAIT, false); - status = runlst_wait(svc->exec, svc->num_exec, - svc->ctty); + status = runlst_wait(svc->exec, svc->ctty); print_status(svc->desc, status == EXIT_SUCCESS ? @@ -136,7 +135,7 @@ static void start_runlevel(int level) if (svc->type == SVC_RESPAWN) print_status(svc->desc, STATUS_STARTED, false); - svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty); + svc->pid = runlst(svc->exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); delsvc(svc); diff --git a/initd/runlst.c b/initd/runlst.c index ee8c5c5..49b7def 100644 --- a/initd/runlst.c +++ b/initd/runlst.c @@ -29,42 +29,10 @@ extern char **environ; -static NORETURN void split_and_exec(char *cmd) +static NORETURN void split_and_exec(exec_t *cmd) { - char *argv[128]; - size_t i = 0; - - while (*cmd != '\0') { - argv[i++] = cmd; /* FIXME: buffer overflow!! */ - - if (*cmd == '"') { - while (*cmd != '\0' && *cmd != '"') { - if (cmd[0] == '\\' && cmd[1] != '\0') - ++cmd; - - ++cmd; - } - - if (*cmd == '"') - *(cmd++) = '\0'; - - unescape(argv[i - 1]); - } else { - while (*cmd != '\0' && *cmd != ' ') - ++cmd; - - if (*cmd == ' ') - *(cmd++) = '\0'; - } - - while (*cmd == ' ') - ++cmd; - } - - argv[i] = NULL; - - execve(argv[0], argv, environ); - perror(argv[0]); + execve(cmd->argv[0], cmd->argv, environ); + perror(cmd->argv[0]); exit(EXIT_FAILURE); } @@ -98,19 +66,18 @@ static int child_setup(const char *ctty) return 0; } -int runlst_wait(char **exec, size_t num, const char *ctty) +int runlst_wait(exec_t *list, const char *ctty) { pid_t ret, pid; int status; - size_t i; - for (i = 0; i < num; ++i) { + for (; list != NULL; list = list->next) { pid = fork(); if (pid == 0) { if (child_setup(ctty)) exit(EXIT_FAILURE); - split_and_exec(exec[i]); + split_and_exec(list); } if (pid == -1) { @@ -132,7 +99,7 @@ int runlst_wait(char **exec, size_t num, const char *ctty) return EXIT_SUCCESS; } -pid_t runlst(char **exec, size_t num, const char *ctty) +pid_t runlst(exec_t *list, const char *ctty) { int status; pid_t pid; @@ -143,11 +110,11 @@ pid_t runlst(char **exec, size_t num, const char *ctty) if (child_setup(ctty)) exit(EXIT_FAILURE); - if (num > 1) { - status = runlst_wait(exec, num, NULL); + if (list->next != NULL) { + status = runlst_wait(list, NULL); exit(status); } else { - split_and_exec(exec[0]); + split_and_exec(list); } } diff --git a/lib/include/service.h b/lib/include/service.h index 76d48ce..593b221 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -44,21 +44,27 @@ enum { TGT_MAX }; +typedef struct exec_t { + char **argv; + char *raw_argv; + + struct exec_t *next; +} exec_t; + typedef struct service_t { int type; /* SVC_* service type */ int target; /* TGT_* service target */ char *name; /* canonical service name */ char *desc; /* description string */ - char **exec; /* command lines to execute */ - size_t num_exec; /* number of command lines */ + exec_t *exec; /* command lines to execute */ char *ctty; /* controlling tty or log file */ int rspwn_limit; /* maximum respawn count */ char **before; /* services that must be executed later */ - size_t num_before; char **after; /* services that must be executed first */ - size_t num_after; + char *raw_after; + char *raw_before; pid_t pid; int status; /* process exit status */ diff --git a/lib/include/util.h b/lib/include/util.h index 0ed7002..a6e96fa 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -73,6 +73,8 @@ char *rdline(int fd, int argc, const char *const *argv); */ int unescape(char *src); +char **split_argv(char *str); + /* Search through an array of enum_map_t entries to resolve a string to a numeric value. The end of the map is indicated by a sentinel entry diff --git a/lib/src/delsvc.c b/lib/src/delsvc.c index 3e077fb..7370653 100644 --- a/lib/src/delsvc.c +++ b/lib/src/delsvc.c @@ -21,16 +21,19 @@ void delsvc(service_t *svc) { - size_t i; + exec_t *e; - for (i = 0; i < svc->num_exec; ++i) - free(svc->exec[i]); + while (svc->exec != NULL) { + e = svc->exec; + svc->exec = e->next; - for (i = 0; i < svc->num_before; ++i) - free(svc->before[i]); + free(e->argv); + free(e->raw_argv); + free(e); + } - for (i = 0; i < svc->num_after; ++i) - free(svc->after[i]); + free(svc->raw_before); + free(svc->raw_after); free(svc->before); free(svc->after); diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c index 04cf30f..5058c76 100644 --- a/lib/src/rdsvc.c +++ b/lib/src/rdsvc.c @@ -38,6 +38,26 @@ static int try_unescape(char *arg, const char *filename, size_t lineno) return 0; } +static char **try_split_argv(char *str, const char *filename, size_t lineno) +{ + char **argv = split_argv(str); + + if (argv == NULL) { + switch (errno) { + case EINVAL: + fprintf(stderr, "%s: %zu: malformed string constant\n", + filename, lineno); + break; + default: + fprintf(stderr, "%s: %zu: %s\n", filename, lineno, + strerror(errno)); + break; + } + } + + return argv; +} + static int svc_desc(service_t *svc, char *arg, const char *filename, size_t lineno) { @@ -59,107 +79,108 @@ static int svc_tty(service_t *svc, char *arg, static int svc_exec(service_t *svc, char *arg, const char *filename, size_t lineno) { - char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1)); + exec_t *e, *end; + char **argv; - if (new == NULL) { + argv = try_split_argv(arg, filename, lineno); + if (argv == NULL) + return -1; + + e = calloc(1, sizeof(*e)); + if (e == NULL) { fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); + free(argv); return -1; } - svc->exec = new; - svc->exec[svc->num_exec++] = arg; + e->argv = argv; + e->raw_argv = arg; + + if (svc->exec == NULL) { + svc->exec = e; + } else { + for (end = svc->exec; end->next != NULL; end = end->next) + ; + end->next = e; + } return 0; } static int svc_before(service_t *svc, char *arg, const char *filename, size_t lineno) { - char **new; - - if (try_unescape(arg, filename, lineno)) + if (svc->before != NULL) { + fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n", + filename, lineno); return -1; + } - new = realloc(svc->before, sizeof(char*) * (svc->num_before + 1)); - - if (new == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); + svc->before = try_split_argv(arg, filename, lineno); + if (svc->before == NULL) return -1; - } - svc->before = new; - svc->before[svc->num_before++] = arg; + svc->raw_before = arg; return 0; } static int svc_after(service_t *svc, char *arg, const char *filename, size_t lineno) { - char **new; - - if (try_unescape(arg, filename, lineno)) + if (svc->after != NULL) { + fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n", + filename, lineno); return -1; + } - new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1)); - - if (new == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); + svc->after = try_split_argv(arg, filename, lineno); + if (svc->after == NULL) return -1; - } - svc->after = new; - svc->after[svc->num_after++] = arg; + svc->raw_after = arg; return 0; } static int svc_type(service_t *svc, char *arg, const char *filename, size_t lineno) { - char *extra; - int type; + char **args; + int i, type; - if (try_unescape(arg, filename, lineno)) - return -1; - - for (extra = arg; *extra != ' ' && *extra != '\0'; ++extra) - ; + args = try_split_argv(arg, filename, lineno); - if (*extra == ' ') { - *(extra++) = '\0'; - } else { - extra = NULL; - } + if (args == NULL) + return -1; - type = svc_type_from_string(arg); + type = svc_type_from_string(args[0]); if (type == -1) { fprintf(stderr, "%s: %zu: unknown service type '%s'\n", - filename, lineno, arg); + filename, lineno, args[0]); + free(args); return -1; } - if (extra != NULL) { + if (args[1] != NULL) { switch (type) { case SVC_RESPAWN: - if (strncmp(extra, "limit ", 6) != 0) + if (strcmp(args[1], "limit") != 0) goto fail_limit; - extra += 6; svc->rspwn_limit = 0; - if (!isdigit(*extra)) + if (!isdigit(args[2][0])) goto fail_limit; - while (isdigit(*extra)) { + for (i = 0; isdigit(args[2][i]); ++i) { svc->rspwn_limit *= 10; - svc->rspwn_limit += *(extra++) - '0'; + svc->rspwn_limit += args[2][i] - '0'; } - if (*extra != '\0') + if (args[2][i] != '\0') goto fail_limit; - break; + if (args[3] == NULL) + break; + /* fall-through */ default: fprintf(stderr, "%s: %zu: unexpected extra arguments " "for type '%s'\n", filename, lineno, arg); @@ -168,11 +189,13 @@ static int svc_type(service_t *svc, char *arg, } svc->type = type; + free(args); free(arg); return 0; fail_limit: fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n", filename, lineno); + free(args); return -1; } diff --git a/lib/src/splitkv.c b/lib/src/splitkv.c index 3631ea8..7d652d3 100644 --- a/lib/src/splitkv.c +++ b/lib/src/splitkv.c @@ -15,7 +15,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include <stdlib.h> #include <ctype.h> +#include <errno.h> #include "util.h" @@ -86,3 +88,74 @@ int unescape(char *src) *(dst++) = '\0'; return 0; } + +char **split_argv(char *str) +{ + size_t i = 0, cap = 0, new_cap; + char **argv = NULL, **new; + char *ptr; + + ptr = str; + + for (;;) { + if (*ptr == ' ') { + ++ptr; + continue; + } + + if (i == cap) { + new_cap = cap ? cap * 2 : 16; + new = realloc(argv, sizeof(argv[0]) * new_cap); + + if (new == NULL) { + free(argv); + errno = ENOMEM; + return NULL; + } + + cap = new_cap; + argv = new; + } + + if (*ptr == '\0') { + argv[i++] = NULL; + break; + } + + argv[i++] = ptr; + + if (*ptr == '"') { + ++ptr; + while (*ptr != '\0' && *ptr != '"') { + if (ptr[0] == '\\' && ptr[1] != '\0') + ++ptr; + + ++ptr; + } + + if (*ptr == '"') + ++ptr; + + if (*ptr == ' ') { + *(ptr++) = '\0'; + } else if (*ptr != '\0') { + goto fail_str; + } + + if (unescape(argv[i - 1])) + goto fail_str; + } else { + while (*ptr != '\0' && *ptr != ' ') + ++ptr; + + if (*ptr == ' ') + *(ptr++) = '\0'; + } + } + + return argv; +fail_str: + free(argv); + errno = EINVAL; + return NULL; +} diff --git a/lib/src/svc_tsort.c b/lib/src/svc_tsort.c index 50517f2..dcd1b4c 100644 --- a/lib/src/svc_tsort.c +++ b/lib/src/svc_tsort.c @@ -27,14 +27,18 @@ static bool has_dependencies(service_t *list, service_t *svc) size_t i; while (list != NULL) { - for (i = 0; i < svc->num_after; ++i) { - if (!strcmp(svc->after[i], list->name)) - return true; + if (svc->after != NULL) { + for (i = 0; svc->after[i] != NULL; ++i) { + if (!strcmp(svc->after[i], list->name)) + return true; + } } - for (i = 0; i < list->num_before; ++i) { - if (!strcmp(list->before[i], svc->name)) - return true; + if (list->before != NULL) { + for (i = 0; list->before[i] != NULL; ++i) { + if (!strcmp(list->before[i], svc->name)) + return true; + } } list = list->next; diff --git a/servicecmd/list.c b/servicecmd/list.c index 18b043f..404bddc 100644 --- a/servicecmd/list.c +++ b/servicecmd/list.c @@ -32,17 +32,17 @@ static void print_services(service_t *svc) if (svc->type == SVC_RESPAWN && svc->rspwn_limit > 0) printf("\tRespawn limit: %d\n", svc->rspwn_limit); - if (svc->num_before) { + if (svc->before != NULL) { fputs("\tMust be run before:\n", stdout); - for (i = 0; i < svc->num_before; ++i) + for (i = 0; svc->before[i] != NULL; ++i) printf("\t\t%s\n", svc->before[i]); } - if (svc->num_after) { + if (svc->after != NULL) { fputs("\tMust be run after:\n", stdout); - for (i = 0; i < svc->num_after; ++i) + for (i = 0; svc->after[i] != NULL; ++i) printf("\t\t%s\n", svc->after[i]); } } diff --git a/services/loopback.in b/services/loopback.in index c44306c..a01225a 100644 --- a/services/loopback.in +++ b/services/loopback.in @@ -2,8 +2,7 @@ description configure network loopback device type wait target boot before sysinit -after hwclock -after hostname +after hwclock hostname exec "@SBINPATH@/ip" addr add 127.0.0.1/8 dev lo brd + exec "@SBINPATH@/ip" link set lo up diff --git a/services/reboot.in b/services/reboot.in index 3c3c689..da11e2a 100644 --- a/services/reboot.in +++ b/services/reboot.in @@ -2,6 +2,4 @@ description system reboot exec "@SBINPATH@/shutdown" -nrf type wait target reboot -after sync -after sigkill -after sigterm +after sync sigkill sigterm diff --git a/services/shutdown.in b/services/shutdown.in index c357b36..6c28621 100644 --- a/services/shutdown.in +++ b/services/shutdown.in @@ -2,6 +2,4 @@ description system shutdown exec "@SBINPATH@/shutdown" -npf type wait target shutdown -after sync -after sigkill -after sigterm +after sync sigkill sigterm diff --git a/services/sigkill.in b/services/sigkill.in index 8f09dd5..cddd49d 100644 --- a/services/sigkill.in +++ b/services/sigkill.in @@ -3,6 +3,4 @@ exec "@SCRIPTDIR@/killall5" 9 type wait target %0 after sigterm -before sync -before shutdown -before reboot +before sync shutdown reboot diff --git a/services/sigterm.in b/services/sigterm.in index b00610c..45dda0e 100644 --- a/services/sigterm.in +++ b/services/sigterm.in @@ -3,7 +3,4 @@ exec "@SCRIPTDIR@/killall5" 15 exec "@BINPATH@/sleep" 5 type wait target %0 -before sigkill -before sync -before reboot -before shutdown +before sigkill sync reboot shutdown diff --git a/services/sync.in b/services/sync.in index 0ffb9a5..7f02287 100644 --- a/services/sync.in +++ b/services/sync.in @@ -2,7 +2,5 @@ description sync exec "@BINPATH@/sync" type wait target %0 -after sigkill -after sigterm -before reboot -before shutdown +after sigkill sigterm +before reboot shutdown diff --git a/services/sysctl.in b/services/sysctl.in index 61edaf3..2221480 100755 --- a/services/sysctl.in +++ b/services/sysctl.in @@ -3,5 +3,4 @@ exec "@SBINPATH@/sysctl" --system type wait target boot before sysinit -after hwclock -after hostname +after hwclock hostname |