aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <goliath@infraroot.at>2020-05-14 14:48:20 +0200
committerDavid Oberhollenzer <goliath@infraroot.at>2020-05-14 14:50:38 +0200
commit2961e8917f9c39e417e53b5048287f91b611392d (patch)
treefbaa249439788cd7aa12a16d5fc0af7a4afc0a82
parent8ce7d986283f5ef63377a0a447850c970c5cc493 (diff)
Minor cleanup: split initd main loop into smaller functions
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
-rw-r--r--initd/main.c81
1 files 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;