From 38c5b99ae73bd5812904e565a6daae9d2a3a8c73 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 13 May 2020 18:01:38 +0200 Subject: Some simplfications in initd main - Move the code of the supervisor main loop function into the main loop. - Remove the set target function and set the target variable directly in the signal handler. Signed-off-by: David Oberhollenzer --- initd/main.c | 132 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 59 insertions(+), 73 deletions(-) diff --git a/initd/main.c b/initd/main.c index e40fc32..332722c 100644 --- a/initd/main.c +++ b/initd/main.c @@ -111,20 +111,6 @@ static void supervisor_handle_exited(pid_t pid, int status) } } -static void supervisor_set_target(int next) -{ - if (target == TGT_REBOOT || target == TGT_SHUTDOWN || next == target) - return; - - if (queue_idx < queue_count[target]) { - if (next != TGT_REBOOT && next != TGT_SHUTDOWN) - return; - } - - target = next; - queue_idx = 0; -} - static void supervisor_init(void) { int status = STATE_FAILED; @@ -185,70 +171,20 @@ static pid_t wait_for_process(int *status) return pid; } -static void supervisor_process_queues(void) -{ - svc_run_data_t *rt; - service_t *svc; - size_t count; - int status; - pid_t pid; - - count = queue_count[target]; - - if (queue_idx >= count) { - pid = wait_for_process(&status); - supervisor_handle_exited(pid, status); - return; - } - - rt = rt_data + queue_start[target] + queue_idx++; - svc = rt->svc; - - if (svc->flags & SVC_FLAG_HAS_EXEC) { - rt->pid = runsvc(rt->svc); - - if (rt->pid == -1) { - rt->state = STATE_FAILED; - print_status(svc->desc, rt->state); - } else { - rt->state = STATE_RUNNING; - print_status(svc->desc, rt->state); - - switch (svc->type) { - case SVC_WAIT: - for (;;) { - pid = wait_for_process(&status); - if (pid == rt->pid) - break; - supervisor_handle_exited(pid, status); - } - - rt->state = status == EXIT_SUCCESS ? - STATE_COMPLETED : STATE_FAILED; - print_status(svc->desc, rt->state); - break; - case SVC_ONCE: - singleshot += 1; - break; - } - } - } else { - rt->status = EXIT_SUCCESS; - rt->state = STATE_COMPLETED; - print_status(svc->desc, rt->state); - } - - check_target_completion(); -} - static void handle_signal(int signo) { switch (signo) { case SIGTERM: - supervisor_set_target(TGT_SHUTDOWN); + if (target != TGT_REBOOT && target != TGT_SHUTDOWN) { + target = TGT_SHUTDOWN; + queue_idx = 0; + } break; case SIGINT: - supervisor_set_target(TGT_REBOOT); + if (target != TGT_REBOOT && target != TGT_SHUTDOWN) { + target = TGT_REBOOT; + queue_idx = 0; + } break; case SIGHUP: break; @@ -260,6 +196,11 @@ static void handle_signal(int signo) int main(void) { struct sigaction act; + svc_run_data_t *rt; + service_t *svc; + size_t count; + int status; + pid_t pid; supervisor_init(); @@ -275,7 +216,52 @@ int main(void) perror("cannot disable CTRL+ALT+DEL"); for (;;) { - supervisor_process_queues(); + count = queue_count[target]; + + if (queue_idx >= count) { + pid = wait_for_process(&status); + supervisor_handle_exited(pid, status); + continue; + } + + rt = rt_data + queue_start[target] + queue_idx++; + svc = rt->svc; + + if (svc->flags & SVC_FLAG_HAS_EXEC) { + rt->pid = runsvc(rt->svc); + + if (rt->pid == -1) { + rt->state = STATE_FAILED; + print_status(svc->desc, rt->state); + } else { + rt->state = STATE_RUNNING; + print_status(svc->desc, rt->state); + + switch (svc->type) { + case SVC_WAIT: + for (;;) { + pid = wait_for_process(&status); + if (pid == rt->pid) + break; + supervisor_handle_exited(pid, status); + } + + rt->state = status == EXIT_SUCCESS ? + STATE_COMPLETED : STATE_FAILED; + print_status(svc->desc, rt->state); + break; + case SVC_ONCE: + singleshot += 1; + break; + } + } + } else { + rt->status = EXIT_SUCCESS; + rt->state = STATE_COMPLETED; + print_status(svc->desc, rt->state); + } + + check_target_completion(); } return EXIT_SUCCESS; -- cgit v1.2.3