From 2961e8917f9c39e417e53b5048287f91b611392d Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 14 May 2020 14:48:20 +0200 Subject: Minor cleanup: split initd main loop into smaller functions Signed-off-by: David Oberhollenzer --- initd/main.c | 81 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/initd/main.c b/initd/main.c index 9656daf..dbd4abf 100644 --- a/initd/main.c +++ b/initd/main.c @@ -44,7 +44,11 @@ static svc_run_data_t *wait_for_process(void) rt->status = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; rt->pid = -1; + return rt; +} +static void handle_exited(svc_run_data_t *rt) +{ if (rt->svc->type == SVC_RESPAWN) { if (config_should_respawn()) respawn(rt); @@ -60,8 +64,41 @@ static svc_run_data_t *wait_for_process(void) if (rt->svc->type == SVC_ONCE) config_singleshot_terminated(); } +} - return rt; +static void start_service(svc_run_data_t *rt) +{ + svc_run_data_t *terminated; + + if (!(rt->svc->flags & SVC_FLAG_HAS_EXEC)) { + rt->status = EXIT_SUCCESS; + rt->state = STATE_COMPLETED; + print_status(rt); + return; + } + + rt->pid = runsvc(rt->svc); + if (rt->pid == -1) { + rt->state = STATE_FAILED; + print_status(rt); + return; + } + + rt->state = STATE_RUNNING; + print_status(rt); + + switch (rt->svc->type) { + case SVC_WAIT: + do { + terminated = wait_for_process(); + if (terminated != NULL) + handle_exited(terminated); + } while (terminated != rt); + break; + case SVC_ONCE: + config_singleshot_started(); + break; + } } static void handle_signal(int signo) @@ -80,11 +117,8 @@ static void handle_signal(int signo) } } -static void check_target_completion(void) +static void handle_target_completed(void) { - if (!config_is_current_target_complete()) - return; - switch (config_get_current_target()) { case TGT_BOOT: break; @@ -101,7 +135,7 @@ static void check_target_completion(void) int main(void) { - svc_run_data_t *rt, *terminated; + svc_run_data_t *rt; struct sigaction act; if (config_load()) @@ -122,38 +156,17 @@ int main(void) rt = config_dequeue(); if (rt == NULL) { - terminated = wait_for_process(); - - if (terminated == NULL) + rt = wait_for_process(); + if (rt == NULL) continue; - } else if (rt->svc->flags & SVC_FLAG_HAS_EXEC) { - rt->pid = runsvc(rt->svc); - - if (rt->pid == -1) { - rt->state = STATE_FAILED; - print_status(rt); - } else { - rt->state = STATE_RUNNING; - print_status(rt); - - switch (rt->svc->type) { - case SVC_WAIT: - do { - terminated = wait_for_process(); - } while (terminated != rt); - break; - case SVC_ONCE: - config_singleshot_started(); - break; - } - } + + handle_exited(rt); } else { - rt->status = EXIT_SUCCESS; - rt->state = STATE_COMPLETED; - print_status(rt); + start_service(rt); } - check_target_completion(); + if (config_is_current_target_complete()) + handle_target_completed(); } return EXIT_SUCCESS; -- cgit v1.2.3