chiark / gitweb /
execute: simplify appending to execution list
[elogind.git] / service.c
index ef10cdbf831a9c1a997207ade0fcec90a1bf4785..67950d4e649d587cf7f77930c926eee2093da7d7 100644 (file)
--- 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 <http://www.gnu.org/licenses/>.
+***/
+
 #include <errno.h>
 #include <signal.h>
 
@@ -9,7 +28,7 @@
 #include "load-dropin.h"
 #include "log.h"
 
-static const UnitActiveState state_table[_SERVICE_STATE_MAX] = {
+static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
         [SERVICE_DEAD] = UNIT_INACTIVE,
         [SERVICE_START_PRE] = UNIT_ACTIVATING,
         [SERVICE_START] = UNIT_ACTIVATING,
@@ -50,7 +69,7 @@ static void service_done(Unit *u) {
                 s->control_pid = 0;
         }
 
-        unit_unwatch_timer(u, &s->timer_id);
+        unit_unwatch_timer(u, &s->timer_watch);
 }
 
 static int service_load_sysv(Service *s) {
@@ -79,22 +98,25 @@ static int service_init(Unit *u) {
 
         exec_context_init(&s->exec_context);
 
-        s->timer_id = -1;
+        s->timer_watch.type = WATCH_INVALID;
 
         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);
+        RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
 
-        if (r < 0) {
+        /* Load a .service file */
+        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);
@@ -106,45 +128,19 @@ static int service_init(Unit *u) {
 
 static void service_dump(Unit *u, FILE *f, const char *prefix) {
 
-        static const char* const state_table[_SERVICE_STATE_MAX] = {
-                [SERVICE_DEAD] = "dead",
-                [SERVICE_START_PRE] = "start-pre",
-                [SERVICE_START] = "start",
-                [SERVICE_START_POST] = "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 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_table[s->state]);
+                prefix, service_state_to_string(s->state));
 
         if (s->pid_file)
                 fprintf(f,
@@ -160,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) {
@@ -198,6 +194,73 @@ static int service_load_pid_file(Service *s) {
         return 0;
 }
 
+static int service_get_sockets(Service *s, Set **_set) {
+        Set *set;
+        Iterator i;
+        char *t;
+        int r;
+
+        assert(s);
+        assert(_set);
+
+        /* Collects all Socket objects that belong to this
+         * service. Note that a service might have multiple sockets
+         * via multiple names. */
+
+        if (!(set = set_new(NULL, NULL)))
+                return -ENOMEM;
+
+        SET_FOREACH(t, UNIT(s)->meta.names, i) {
+                char *k;
+                Unit *p;
+
+                /* Look for all socket objects that go by any of our
+                 * units and collect their fds */
+
+                if (!(k = unit_name_change_suffix(t, ".socket"))) {
+                        r = -ENOMEM;
+                        goto fail;
+                }
+
+                p = manager_get_unit(UNIT(s)->meta.manager, k);
+                free(k);
+
+                if (!p) continue;
+
+                if ((r = set_put(set, p)) < 0)
+                        goto fail;
+        }
+
+        *_set = set;
+        return 0;
+
+fail:
+        set_free(set);
+        return r;
+}
+
+
+static int service_notify_sockets(Service *s) {
+        Iterator i;
+        Set *set;
+        Socket *sock;
+        int r;
+
+        assert(s);
+
+        /* Notifies all our sockets when we die */
+
+        if ((r = service_get_sockets(s, &set)) < 0)
+                return r;
+
+        SET_FOREACH(sock, set, i)
+                socket_notify_service_dead(sock);
+
+        set_free(set);
+
+        return 0;
+}
+
 static void service_set_state(Service *s, ServiceState state) {
         ServiceState old_state;
         assert(s);
@@ -216,15 +279,16 @@ static void service_set_state(Service *s, ServiceState state) {
             state != SERVICE_FINAL_SIGTERM &&
             state != SERVICE_FINAL_SIGKILL &&
             state != SERVICE_AUTO_RESTART)
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
+                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 &&
             state != SERVICE_STOP_SIGTERM &&
             state != SERVICE_STOP_SIGKILL)
-                if (s->main_pid >= 0) {
+                if (s->main_pid > 0) {
                         unit_unwatch_pid(UNIT(s), s->main_pid);
                         s->main_pid = 0;
                 }
@@ -239,7 +303,7 @@ static void service_set_state(Service *s, ServiceState state) {
             state != SERVICE_STOP_POST &&
             state != SERVICE_FINAL_SIGTERM &&
             state != SERVICE_FINAL_SIGKILL)
-                if (s->control_pid >= 0) {
+                if (s->control_pid > 0) {
                         unit_unwatch_pid(UNIT(s), s->control_pid);
                         s->control_pid = 0;
                 }
@@ -252,7 +316,20 @@ static void service_set_state(Service *s, ServiceState state) {
             state != SERVICE_STOP_POST)
                 s->control_command = NULL;
 
-        unit_notify(UNIT(s), state_table[old_state], state_table[s->state]);
+        if (state == SERVICE_DEAD ||
+            state == SERVICE_STOP ||
+            state == SERVICE_STOP_SIGTERM ||
+            state == SERVICE_STOP_SIGKILL ||
+            state == SERVICE_STOP_POST ||
+            state == SERVICE_FINAL_SIGTERM ||
+            state == SERVICE_FINAL_SIGKILL ||
+            state == SERVICE_MAINTAINANCE ||
+            state == SERVICE_AUTO_RESTART)
+                service_notify_sockets(s);
+
+        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]);
 }
 
 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
@@ -260,30 +337,21 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
         int r;
         int *rfds = NULL;
         unsigned rn_fds = 0;
-        char *t;
+        Set *set;
+        Socket *sock;
 
         assert(s);
         assert(fds);
         assert(n_fds);
 
-        SET_FOREACH(t, UNIT(s)->meta.names, i) {
-                char *k;
-                Unit *p;
+        if ((r = service_get_sockets(s, &set)) < 0)
+                return r;
+
+        SET_FOREACH(sock, set, i) {
                 int *cfds;
                 unsigned cn_fds;
 
-                /* Look for all socket objects that go by any of our
-                 * units and collect their fds */
-
-                if (!(k = unit_name_change_suffix(t, ".socket"))) {
-                        r = -ENOMEM;
-                        goto fail;
-                }
-
-                p = manager_get_unit(UNIT(s)->meta.manager, k);
-                free(k);
-
-                if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
+                if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
                         goto fail;
 
                 if (!cfds)
@@ -313,10 +381,15 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
 
         *fds = rfds;
         *n_fds = rn_fds;
+
+        set_free(set);
+
         return 0;
 
 fail:
+        set_free(set);
         free(rfds);
+
         return r;
 }
 
@@ -335,10 +408,10 @@ static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds
                         goto fail;
 
         if (timeout) {
-                if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_id)) < 0)
+                if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
                         goto fail;
         } else
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
                 goto fail;
@@ -356,7 +429,7 @@ fail:
         free(fds);
 
         if (timeout)
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         return r;
 }
@@ -372,7 +445,7 @@ static void service_enter_dead(Service *s, bool success, bool allow_restart) {
             (s->restart == SERVICE_RESTART_ALWAYS ||
              (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
 
-                if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_id)) < 0)
+                if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
                         goto fail;
 
                 service_set_state(s, SERVICE_AUTO_RESTART);
@@ -395,13 +468,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;
@@ -442,9 +516,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;
@@ -468,13 +544,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;
@@ -488,13 +564,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;
@@ -516,6 +593,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. */
@@ -531,7 +610,13 @@ 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 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");
 
@@ -547,13 +632,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;
@@ -585,13 +670,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;
@@ -652,6 +737,12 @@ static int service_start(Unit *u) {
 
         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
 
+        /* Make sure we don't enter a busy loop of some kind. */
+        if (!ratelimit_test(&s->ratelimit)) {
+                log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
+                return -EAGAIN;
+        }
+
         s->failure = false;
         s->main_pid_known = false;
 
@@ -703,7 +794,7 @@ static bool service_can_reload(Unit *u) {
 static UnitActiveState service_active_state(Unit *u) {
         assert(u);
 
-        return state_table[SERVICE(u)->state];
+        return state_translation_table[SERVICE(u)->state];
 }
 
 static int main_pid_good(Service *s) {
@@ -734,7 +825,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         assert(s);
         assert(pid >= 0);
 
-        success = code == CLD_EXITED || status == 0;
+        success = code == CLD_EXITED && status == 0;
         s->failure = s->failure || !success;
 
         if (s->main_pid == pid) {
@@ -742,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. */
@@ -761,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;
@@ -784,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. */
@@ -801,6 +902,8 @@ 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), service_state_to_string(s->state));
+
                         switch (s->state) {
 
                         case SERVICE_START_PRE:
@@ -888,13 +991,13 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                 assert_not_reached("Got SIGCHLD for unkown PID");
 }
 
-static void service_timer_event(Unit *u, int id, uint64_t elapsed) {
+static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
         Service *s = SERVICE(u);
 
         assert(s);
         assert(elapsed == 1);
 
-        assert(s->timer_id == id);
+        assert(w == &s->timer_watch);
 
         switch (s->state) {
 
@@ -950,6 +1053,52 @@ static void service_timer_event(Unit *u, int id, uint64_t elapsed) {
         }
 }
 
+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",