X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=service.c;h=70299dd065f472d1f0a52435b982f41d8632a990;hb=b19e7dc0104c8839835a90d7df20c2eeb631e07e;hp=2001b9f4d8e69e3b21da080b93f98bf51e53472c;hpb=3e33402a2dd0ba7aef23a8b7c760366dd5c4be87;p=elogind.git diff --git a/service.c b/service.c index 2001b9f4d..70299dd06 100644 --- a/service.c +++ b/service.c @@ -30,7 +30,7 @@ static const char* const state_string_table[_SERVICE_STATE_MAX] = { [SERVICE_DEAD] = "dead", [SERVICE_START_PRE] = "start-pre", [SERVICE_START] = "start", - [SERVICE_START_POST] = "post", + [SERVICE_START_POST] = "start-post", [SERVICE_RUNNING] = "running", [SERVICE_RELOAD] = "reload", [SERVICE_STOP] = "stop", @@ -101,17 +101,18 @@ static int service_init(Unit *u) { s->state = SERVICE_DEAD; /* Load a .service file */ - r = unit_load_fragment(u); - - /* Load a classic init script as a fallback */ - if (r == -ENOENT) - r = service_load_sysv(s); - - if (r < 0) { + if ((r = unit_load_fragment(u)) < 0) { service_done(u); return r; } + /* Load a classic init script as a fallback, if we couldn*t find anything */ + if (r == 0) + if ((r = service_load_sysv(s)) <= 0) { + service_done(u); + return r < 0 ? r : -ENOENT; + } + /* Load dropin directory data */ if ((r = unit_load_dropin(u)) < 0) { service_done(u); @@ -330,7 +331,7 @@ static void service_set_state(Service *s, ServiceState state) { state == SERVICE_AUTO_RESTART) service_notify_sockets(s); - log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); + log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); } @@ -471,13 +472,14 @@ static void service_enter_stop_post(Service *s, bool success) { if (!success) s->failure = true; - if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) { - + if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) goto fail; - service_set_state(s, SERVICE_STOP_POST); - } else + + service_set_state(s, SERVICE_STOP_POST); + + if (!s->control_command) service_enter_dead(s, true, true); return; @@ -518,9 +520,11 @@ static void service_enter_signal(Service *s, ServiceState state, bool success) { if (r < 0) goto fail; + } - service_set_state(s, state); - } else + service_set_state(s, state); + + if (s->main_pid <= 0 && s->control_pid <= 0) service_enter_dead(s, true, true); return; @@ -544,13 +548,13 @@ static void service_enter_stop(Service *s, bool success) { if (!success) s->failure = true; - if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) { - + if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) goto fail; - service_set_state(s, SERVICE_STOP); - } else + service_set_state(s, SERVICE_STOP); + + if (!s->control_command) service_enter_signal(s, SERVICE_STOP_SIGTERM, true); return; @@ -564,13 +568,14 @@ static void service_enter_start_post(Service *s) { int r; assert(s); - if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) { - + if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) goto fail; - service_set_state(s, SERVICE_START_POST); - } else + + service_set_state(s, SERVICE_START_POST); + + if (!s->control_command) service_set_state(s, SERVICE_RUNNING); return; @@ -592,6 +597,8 @@ static void service_enter_start(Service *s) { if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0) goto fail; + service_set_state(s, SERVICE_START); + if (s->type == SERVICE_SIMPLE) { /* For simple services we immediately start * the START_POST binaries. */ @@ -607,7 +614,6 @@ static void service_enter_start(Service *s) { s->control_pid = pid; s->control_command = s->exec_command[SERVICE_EXEC_START]; - service_set_state(s, SERVICE_START); } else assert_not_reached("Unknown service type"); @@ -623,13 +629,13 @@ static void service_enter_start_pre(Service *s) { assert(s); - if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) { - + if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) goto fail; - service_set_state(s, SERVICE_START_PRE); - } else + service_set_state(s, SERVICE_START_PRE); + + if (!s->control_command) service_enter_start(s); return; @@ -661,13 +667,13 @@ static void service_enter_reload(Service *s) { assert(s); - if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) { - + if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) goto fail; - service_set_state(s, SERVICE_RELOAD); - } else + service_set_state(s, SERVICE_RELOAD); + + if (!s->control_command) service_set_state(s, SERVICE_RUNNING); return;