diff options
-rw-r--r-- | initd/main.c | 64 |
1 files changed, 22 insertions, 42 deletions
diff --git a/initd/main.c b/initd/main.c index 9c4bf78..0fcd3e0 100644 --- a/initd/main.c +++ b/initd/main.c @@ -1,41 +1,32 @@ /* SPDX-License-Identifier: ISC */ #include "init.h" -static void respawn(svc_run_data_t *rt) +static void handle_exited(svc_run_data_t *rt, int status) { - if (rt->svc->rspwn_limit > 0) { - rt->rspwn_count += 1; + rt->pid = -1; + rt->status = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; + rt->state = (rt->status == EXIT_SUCCESS) ? + STATE_COMPLETED : STATE_FAILED; - if (rt->rspwn_count >= rt->svc->rspwn_limit) - goto fail; - } - - rt->pid = runsvc(rt->svc); - if (rt->pid == -1) - goto fail; + if (rt->svc->type == SVC_RESPAWN && config_should_respawn()) { + rt->state = STATE_FAILED; - rt->state = STATE_RUNNING; - return; -fail: - rt->state = STATE_FAILED; - print_status(rt); - return; -} + if (rt->svc->rspwn_limit > 0) { + rt->rspwn_count += 1; -static void handle_exited(svc_run_data_t *rt) -{ - if (rt->svc->type == SVC_RESPAWN) { - if (config_should_respawn()) - respawn(rt); - } else { - if (rt->status == EXIT_SUCCESS) { - rt->state = STATE_COMPLETED; - } else { - rt->state = STATE_FAILED; + if (rt->rspwn_count >= rt->svc->rspwn_limit) + goto out_print; } - print_status(rt); + rt->pid = runsvc(rt->svc); + if (rt->pid == -1) + goto out_print; + + rt->state = STATE_RUNNING; + return; } +out_print: + print_status(rt); } static void start_service(svc_run_data_t *rt) @@ -78,17 +69,9 @@ static void handle_signal(int fd) case SIGCHLD: while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { rt = config_rt_data_by_pid(pid); - if (rt == NULL) - continue; - if (WIFEXITED(status)) { - rt->status = WEXITSTATUS(status); - } else { - rt->status = EXIT_FAILURE; - } - - rt->pid = -1; - handle_exited(rt); + if (rt != NULL) + handle_exited(rt, status); } break; case SIGHUP: @@ -142,11 +125,8 @@ int main(void) perror("cannot disable CTRL+ALT+DEL"); for (;;) { - rt = config_dequeue(); - - if (rt != NULL) { + while ((rt = config_dequeue()) != NULL) { start_service(rt); - continue; } ret = epoll_wait(epfd, &ev, 1, -1); |