From b81668e045b6a2105b7b0b9e2e5afa8e631e6ec5 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 7 Apr 2018 15:35:19 +0200 Subject: Minor cleanup Signed-off-by: David Oberhollenzer --- initd/main.c | 25 ++++++++----------------- initd/mksock.c | 15 ++------------- initd/runlst.c | 32 ++++++++++---------------------- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/initd/main.c b/initd/main.c index 2be2045..192a49a 100644 --- a/initd/main.c +++ b/initd/main.c @@ -38,17 +38,14 @@ static void handle_exited(service_t *svc) { switch (svc->type) { case SVC_RESPAWN: - if (target == TGT_REBOOT || target == TGT_SHUTDOWN) { - delsvc(svc); + if (target == TGT_REBOOT || target == TGT_SHUTDOWN) break; - } if (svc->rspwn_limit > 0) { svc->rspwn_limit -= 1; if (svc->rspwn_limit == 0) { print_status(svc->desc, STATUS_FAIL, false); - delsvc(svc); break; } } @@ -56,20 +53,18 @@ static void handle_exited(service_t *svc) svc->pid = runlst(svc->exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); - delsvc(svc); + break; } svclist_add(svc); - break; + return; case SVC_ONCE: print_status(svc->desc, svc->status == EXIT_SUCCESS ? STATUS_OK : STATUS_FAIL, false); - /* fall-through */ - default: - delsvc(svc); break; } + delsvc(svc); } static void handle_signal(int sigfd) @@ -86,11 +81,7 @@ static void handle_signal(int sigfd) switch (info.ssi_signo) { case SIGCHLD: - for (;;) { - pid = waitpid(-1, &status, WNOHANG); - if (pid <= 0) - break; - + while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { status = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; @@ -132,9 +123,6 @@ static void start_runlevel(int level) true); delsvc(svc); } else { - if (svc->type == SVC_RESPAWN) - print_status(svc->desc, STATUS_STARTED, false); - svc->pid = runlst(svc->exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); @@ -142,6 +130,9 @@ static void start_runlevel(int level) continue; } + if (svc->type == SVC_RESPAWN) + print_status(svc->desc, STATUS_STARTED, false); + svclist_add(svc); } } diff --git a/initd/mksock.c b/initd/mksock.c index 6a9cab0..0ec6b68 100644 --- a/initd/mksock.c +++ b/initd/mksock.c @@ -30,25 +30,14 @@ int mksock(void) { struct sockaddr_un un; - int fd, flags; + int fd; - fd = socket(AF_UNIX, SOCK_STREAM, 0); + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); if (fd < 0) { perror("socket"); return -1; } - flags = fcntl(fd, F_GETFD); - if (flags == -1) { - perror("socket F_GETFD"); - goto fail; - } - - if (fcntl(fd, F_SETFD, flags | O_CLOEXEC)) { - perror("socket F_SETFD"); - goto fail; - } - memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; diff --git a/initd/runlst.c b/initd/runlst.c index 49b7def..7840aff 100644 --- a/initd/runlst.c +++ b/initd/runlst.c @@ -16,26 +16,15 @@ * along with this program. If not, see . */ #include -#include #include #include -#include #include -#include -#include #include #include "init.h" extern char **environ; -static NORETURN void split_and_exec(exec_t *cmd) -{ - execve(cmd->argv[0], cmd->argv, environ); - perror(cmd->argv[0]); - exit(EXIT_FAILURE); -} - static int child_setup(const char *ctty) { sigset_t mask; @@ -77,7 +66,9 @@ int runlst_wait(exec_t *list, const char *ctty) if (pid == 0) { if (child_setup(ctty)) exit(EXIT_FAILURE); - split_and_exec(list); + execve(list->argv[0], list->argv, environ); + perror(list->argv[0]); + exit(EXIT_FAILURE); } if (pid == -1) { @@ -101,21 +92,18 @@ int runlst_wait(exec_t *list, const char *ctty) pid_t runlst(exec_t *list, const char *ctty) { - int status; - pid_t pid; - - pid = fork(); + pid_t pid = fork(); if (pid == 0) { if (child_setup(ctty)) exit(EXIT_FAILURE); - if (list->next != NULL) { - status = runlst_wait(list, NULL); - exit(status); - } else { - split_and_exec(list); - } + if (list->next != NULL) + exit(runlst_wait(list, NULL)); + + execve(list->argv[0], list->argv, environ); + perror(list->argv[0]); + exit(EXIT_FAILURE); } if (pid == -1) -- cgit v1.2.3