X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=service.c;h=67950d4e649d587cf7f77930c926eee2093da7d7;hp=2943da6e80405be1c5157fb173c487404430382f;hb=a6a80b4f440bcc1c6087572503c08a72ee674075;hpb=1e2e81336b2d7691d51a6f3e82df9ae20c22aee1 diff --git a/service.c b/service.c index 2943da6e8..67950d4e6 100644 --- a/service.c +++ b/service.c @@ -1,5 +1,24 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include @@ -26,23 +45,6 @@ static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, }; -static const char* const state_string_table[_SERVICE_STATE_MAX] = { - [SERVICE_DEAD] = "dead", - [SERVICE_START_PRE] = "start-pre", - [SERVICE_START] = "start", - [SERVICE_START_POST] = "start-post", - [SERVICE_RUNNING] = "running", - [SERVICE_RELOAD] = "reload", - [SERVICE_STOP] = "stop", - [SERVICE_STOP_SIGTERM] = "stop-sigterm", - [SERVICE_STOP_SIGKILL] = "stop-sigkill", - [SERVICE_STOP_POST] = "stop-post", - [SERVICE_FINAL_SIGTERM] = "final-sigterm", - [SERVICE_FINAL_SIGKILL] = "final-sigkill", - [SERVICE_MAINTAINANCE] = "maintainance", - [SERVICE_AUTO_RESTART] = "auto-restart", -}; - static void service_done(Unit *u) { Service *s = SERVICE(u); @@ -126,28 +128,19 @@ static int service_init(Unit *u) { static void service_dump(Unit *u, FILE *f, const char *prefix) { - static const char* const command_table[_SERVICE_EXEC_MAX] = { - [SERVICE_EXEC_START_PRE] = "ExecStartPre", - [SERVICE_EXEC_START] = "ExecStart", - [SERVICE_EXEC_START_POST] = "ExecStartPost", - [SERVICE_EXEC_RELOAD] = "ExecReload", - [SERVICE_EXEC_STOP] = "ExecStop", - [SERVICE_EXEC_STOP_POST] = "ExecStopPost", - }; - ServiceExecCommand c; Service *s = SERVICE(u); - char *prefix2; + const char *prefix2; + char *p2; assert(s); - prefix2 = strappend(prefix, "\t"); - if (!prefix2) - prefix2 = ""; + p2 = strappend(prefix, "\t"); + prefix2 = p2 ? p2 : prefix; fprintf(f, "%sService State: %s\n", - prefix, state_string_table[s->state]); + prefix, service_state_to_string(s->state)); if (s->pid_file) fprintf(f, @@ -163,12 +156,12 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) { continue; fprintf(f, "%s→ %s:\n", - prefix, command_table[c]); + prefix, service_exec_command_to_string(c)); exec_command_dump_list(s->exec_command[c], f, prefix2); } - free(prefix2); + free(p2); } static int service_load_pid_file(Service *s) { @@ -250,7 +243,7 @@ fail: static int service_notify_sockets(Service *s) { Iterator i; Set *set; - Socket *socket; + Socket *sock; int r; assert(s); @@ -260,8 +253,8 @@ static int service_notify_sockets(Service *s) { if ((r = service_get_sockets(s, &set)) < 0) return r; - SET_FOREACH(socket, set, i) - socket_notify_service_dead(socket); + SET_FOREACH(sock, set, i) + socket_notify_service_dead(sock); set_free(set); @@ -288,7 +281,8 @@ static void service_set_state(Service *s, ServiceState state) { state != SERVICE_AUTO_RESTART) unit_unwatch_timer(UNIT(s), &s->timer_watch); - if (state != SERVICE_START_POST && + if (state != SERVICE_START && + state != SERVICE_START_POST && state != SERVICE_RUNNING && state != SERVICE_RELOAD && state != SERVICE_STOP && @@ -333,7 +327,7 @@ static void service_set_state(Service *s, ServiceState state) { state == SERVICE_AUTO_RESTART) service_notify_sockets(s); - log_debug("%s changed %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)), service_state_to_string(old_state), service_state_to_string(state)); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); } @@ -344,7 +338,7 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { int *rfds = NULL; unsigned rn_fds = 0; Set *set; - Socket *socket; + Socket *sock; assert(s); assert(fds); @@ -353,11 +347,11 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { if ((r = service_get_sockets(s, &set)) < 0) return r; - SET_FOREACH(socket, set, i) { + SET_FOREACH(sock, set, i) { int *cfds; unsigned cn_fds; - if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0) + if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0) goto fail; if (!cfds) @@ -616,6 +610,13 @@ static void service_enter_start(Service *s) { s->control_pid = pid; s->control_command = s->exec_command[SERVICE_EXEC_START]; + } else if (s->type == SERVICE_FINISH) { + + /* For finishing services we wait until the start + * process exited, too, but it is our main process. */ + + s->main_pid = pid; + s->control_command = s->exec_command[SERVICE_EXEC_START]; } else assert_not_reached("Unknown service type"); @@ -832,12 +833,12 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { exec_status_fill(&s->main_exec_status, pid, code, status); s->main_pid = 0; - if (s->type == SERVICE_SIMPLE) { + if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) { assert(s->exec_command[SERVICE_EXEC_START]); s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status; } - log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status); + log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); /* The service exited, so the service is officially * gone. */ @@ -851,6 +852,16 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { * done */ break; + case SERVICE_START: + assert(s->type == SERVICE_FINISH); + + /* This was our main goal, so let's go on */ + if (success) + service_enter_start_post(s); + else + service_enter_stop(s, false); + break; + case SERVICE_RUNNING: service_enter_stop(s, success); break; @@ -874,7 +885,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { exec_status_fill(&s->control_command->exec_status, pid, code, status); s->control_pid = 0; - log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status); + log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); /* If we are shutting things down anyway we * don't care about failing commands. */ @@ -891,7 +902,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { /* No further commands for this step, so let's * figure out what to do next */ - log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]); + log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state)); switch (s->state) { @@ -1042,6 +1053,52 @@ static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { } } +static const char* const service_state_table[_SERVICE_STATE_MAX] = { + [SERVICE_DEAD] = "dead", + [SERVICE_START_PRE] = "start-pre", + [SERVICE_START] = "start", + [SERVICE_START_POST] = "start-post", + [SERVICE_RUNNING] = "running", + [SERVICE_RELOAD] = "reload", + [SERVICE_STOP] = "stop", + [SERVICE_STOP_SIGTERM] = "stop-sigterm", + [SERVICE_STOP_SIGKILL] = "stop-sigkill", + [SERVICE_STOP_POST] = "stop-post", + [SERVICE_FINAL_SIGTERM] = "final-sigterm", + [SERVICE_FINAL_SIGKILL] = "final-sigkill", + [SERVICE_MAINTAINANCE] = "maintainance", + [SERVICE_AUTO_RESTART] = "auto-restart", +}; + +DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); + +static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { + [SERVICE_ONCE] = "once", + [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success", + [SERVICE_RESTART_ALWAYS] = "restart-on-failure", +}; + +DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); + +static const char* const service_type_table[_SERVICE_TYPE_MAX] = { + [SERVICE_FORKING] = "forking", + [SERVICE_SIMPLE] = "simple", + [SERVICE_FINISH] = "finish" +}; + +DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); + +static const char* const service_exec_command_table[_SERVICE_EXEC_MAX] = { + [SERVICE_EXEC_START_PRE] = "ExecStartPre", + [SERVICE_EXEC_START] = "ExecStart", + [SERVICE_EXEC_START_POST] = "ExecStartPost", + [SERVICE_EXEC_RELOAD] = "ExecReload", + [SERVICE_EXEC_STOP] = "ExecStop", + [SERVICE_EXEC_STOP_POST] = "ExecStopPost", +}; + +DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); + const UnitVTable service_vtable = { .suffix = ".service",