chiark / gitweb /
notify socket unit when service unit dies
[elogind.git] / service.c
index ef10cdbf831a9c1a997207ade0fcec90a1bf4785..2c3c0b233be3203aee90f6190d7546254f9c0c92 100644 (file)
--- a/service.c
+++ b/service.c
@@ -9,7 +9,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,
@@ -26,6 +26,23 @@ static const UnitActiveState state_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] = "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);
 
@@ -50,7 +67,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,7 +96,7 @@ 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;
 
@@ -106,23 +123,6 @@ 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",
@@ -144,7 +144,7 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
 
         fprintf(f,
                 "%sService State: %s\n",
-                prefix, state_table[s->state]);
+                prefix, state_string_table[s->state]);
 
         if (s->pid_file)
                 fprintf(f,
@@ -198,6 +198,34 @@ static int service_load_pid_file(Service *s) {
         return 0;
 }
 
+static int service_notify_sockets(Service *s) {
+        Iterator i;
+        char *t;
+
+        assert(s);
+
+        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")))
+                        return -ENOMEM;
+
+                p = manager_get_unit(UNIT(s)->meta.manager, k);
+                free(k);
+
+                if (!p)
+                        continue;
+
+                socket_notify_service_dead(SOCKET(p));
+        }
+
+        return 0;
+}
+
 static void service_set_state(Service *s, ServiceState state) {
         ServiceState old_state;
         assert(s);
@@ -216,7 +244,7 @@ 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 &&
             state != SERVICE_RUNNING &&
@@ -224,7 +252,7 @@ static void service_set_state(Service *s, ServiceState state) {
             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 +267,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 +280,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 changing %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]);
 }
 
 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
@@ -283,6 +324,9 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
                 p = manager_get_unit(UNIT(s)->meta.manager, k);
                 free(k);
 
+                if (!p)
+                        continue;
+
                 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
                         goto fail;
 
@@ -335,10 +379,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 +400,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 +416,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);
@@ -703,7 +747,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 +778,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) {
@@ -801,6 +845,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), state_string_table[s->state]);
+
                         switch (s->state) {
 
                         case SERVICE_START_PRE:
@@ -888,13 +934,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) {