chiark / gitweb /
when shortcutting states do not skip state transitions
[elogind.git] / service.c
index e661dc7586cc7a2d0f3b9913f58d1ce861a2e041..1cbfb3dcebdc2db9db6204573a0bae4db2d76e41 100644 (file)
--- a/service.c
+++ b/service.c
@@ -3,29 +3,73 @@
 #include <errno.h>
 #include <signal.h>
 
-#include "name.h"
+#include "unit.h"
 #include "service.h"
 #include "load-fragment.h"
 #include "load-dropin.h"
 #include "log.h"
 
-static const NameActiveState state_table[_SERVICE_STATE_MAX] = {
-        [SERVICE_DEAD] = NAME_INACTIVE,
-        [SERVICE_START_PRE] = NAME_ACTIVATING,
-        [SERVICE_START] = NAME_ACTIVATING,
-        [SERVICE_START_POST] = NAME_ACTIVATING,
-        [SERVICE_RUNNING] = NAME_ACTIVE,
-        [SERVICE_RELOAD] = NAME_ACTIVE_RELOADING,
-        [SERVICE_STOP] = NAME_DEACTIVATING,
-        [SERVICE_STOP_SIGTERM] = NAME_DEACTIVATING,
-        [SERVICE_STOP_SIGKILL] = NAME_DEACTIVATING,
-        [SERVICE_STOP_POST] = NAME_DEACTIVATING,
-        [SERVICE_FINAL_SIGTERM] = NAME_DEACTIVATING,
-        [SERVICE_FINAL_SIGKILL] = NAME_DEACTIVATING,
-        [SERVICE_MAINTAINANCE] = NAME_INACTIVE,
-        [SERVICE_AUTO_RESTART] = NAME_ACTIVATING,
+static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
+        [SERVICE_DEAD] = UNIT_INACTIVE,
+        [SERVICE_START_PRE] = UNIT_ACTIVATING,
+        [SERVICE_START] = UNIT_ACTIVATING,
+        [SERVICE_START_POST] = UNIT_ACTIVATING,
+        [SERVICE_RUNNING] = UNIT_ACTIVE,
+        [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
+        [SERVICE_STOP] = UNIT_DEACTIVATING,
+        [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
+        [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
+        [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
+        [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
+        [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
+        [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
+        [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);
+
+        assert(s);
+
+        free(s->pid_file);
+        s->pid_file = NULL;
+
+        exec_context_done(&s->exec_context);
+        exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
+        s->control_command = NULL;
+
+        /* This will leak a process, but at least no memory or any of
+         * our resources */
+        if (s->main_pid > 0) {
+                unit_unwatch_pid(u, s->main_pid);
+                s->main_pid = 0;
+        }
+
+        if (s->control_pid > 0) {
+                unit_unwatch_pid(u, s->control_pid);
+                s->control_pid = 0;
+        }
+
+        unit_unwatch_timer(u, &s->timer_watch);
+}
+
 static int service_load_sysv(Service *s) {
         assert(s);
 
@@ -35,9 +79,9 @@ static int service_load_sysv(Service *s) {
         return -ENOENT;
 }
 
-static int service_init(Name *n) {
+static int service_init(Unit *u) {
         int r;
-        Service *s = SERVICE(n);
+        Service *s = SERVICE(u);
 
         assert(s);
 
@@ -52,72 +96,32 @@ static int service_init(Name *n) {
 
         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 = name_load_fragment(n);
+        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 < 0) {
+                service_done(u);
                 return r;
+        }
 
         /* Load dropin directory data */
-        if ((r = name_load_dropin(n)) < 0)
+        if ((r = unit_load_dropin(u)) < 0) {
+                service_done(u);
                 return r;
-
-        return 0;
-}
-
-static void service_done(Name *n) {
-        Service *s = SERVICE(n);
-
-        assert(s);
-
-        free(s->pid_file);
-        s->pid_file = NULL;
-
-        exec_context_done(&s->exec_context);
-        exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
-        s->control_command = NULL;
-
-        /* This will leak a process, but at least no memory or any of
-         * our resources */
-        if (s->main_pid > 0) {
-                name_unwatch_pid(n, s->main_pid);
-                s->main_pid = 0;
-        }
-
-        if (s->control_pid > 0) {
-                name_unwatch_pid(n, s->control_pid);
-                s->control_pid = 0;
         }
 
-        name_unwatch_timer(n, &s->timer_id);
+        return 0;
 }
 
-static void service_dump(Name *n, 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 void service_dump(Unit *u, FILE *f, const char *prefix) {
 
         static const char* const command_table[_SERVICE_EXEC_MAX] = {
                 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
@@ -129,13 +133,18 @@ static void service_dump(Name *n, FILE *f, const char *prefix) {
         };
 
         ServiceExecCommand c;
-        Service *s = SERVICE(n);
+        Service *s = SERVICE(u);
+        char *prefix2;
 
         assert(s);
 
+        prefix2 = strappend(prefix, "\t");
+        if (!prefix2)
+                prefix2 = "";
+
         fprintf(f,
                 "%sService State: %s\n",
-                prefix, state_table[s->state]);
+                prefix, state_string_table[s->state]);
 
         if (s->pid_file)
                 fprintf(f,
@@ -146,11 +155,17 @@ static void service_dump(Name *n, FILE *f, const char *prefix) {
         exec_context_dump(&s->exec_context, f, prefix);
 
         for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
-                ExecCommand *i;
 
-                LIST_FOREACH(command, i, s->exec_command[c])
-                        fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
+                if (!s->exec_command[c])
+                        continue;
+
+                fprintf(f, "%sā†’ %s:\n",
+                        prefix, command_table[c]);
+
+                exec_command_dump_list(s->exec_command[c], f, prefix2);
         }
+
+        free(prefix2);
 }
 
 static int service_load_pid_file(Service *s) {
@@ -183,6 +198,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 *socket;
+        int r;
+
+        assert(s);
+
+        /* Notifies all our sockets when we die */
+
+        if ((r = service_get_sockets(s, &set)) < 0)
+                return r;
+
+        SET_FOREACH(socket, set, i)
+                socket_notify_service_dead(socket);
+
+        set_free(set);
+
+        return 0;
+}
+
 static void service_set_state(Service *s, ServiceState state) {
         ServiceState old_state;
         assert(s);
@@ -201,7 +283,7 @@ static void service_set_state(Service *s, ServiceState state) {
             state != SERVICE_FINAL_SIGTERM &&
             state != SERVICE_FINAL_SIGKILL &&
             state != SERVICE_AUTO_RESTART)
-                name_unwatch_timer(NAME(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         if (state != SERVICE_START_POST &&
             state != SERVICE_RUNNING &&
@@ -209,8 +291,8 @@ 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) {
-                        name_unwatch_pid(NAME(s), s->main_pid);
+                if (s->main_pid > 0) {
+                        unit_unwatch_pid(UNIT(s), s->main_pid);
                         s->main_pid = 0;
                 }
 
@@ -224,8 +306,8 @@ 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) {
-                        name_unwatch_pid(NAME(s), s->control_pid);
+                if (s->control_pid > 0) {
+                        unit_unwatch_pid(UNIT(s), s->control_pid);
                         s->control_pid = 0;
                 }
 
@@ -237,37 +319,120 @@ static void service_set_state(Service *s, ServiceState state) {
             state != SERVICE_STOP_POST)
                 s->control_command = NULL;
 
-        name_notify(NAME(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)), 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) {
+        Iterator i;
+        int r;
+        int *rfds = NULL;
+        unsigned rn_fds = 0;
+        Set *set;
+        Socket *socket;
+
+        assert(s);
+        assert(fds);
+        assert(n_fds);
+
+        if ((r = service_get_sockets(s, &set)) < 0)
+                return r;
+
+        SET_FOREACH(socket, set, i) {
+                int *cfds;
+                unsigned cn_fds;
+
+                if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
+                        goto fail;
+
+                if (!cfds)
+                        continue;
+
+                if (!rfds) {
+                        rfds = cfds;
+                        rn_fds = cn_fds;
+                } else {
+                        int *t;
+
+                        if (!(t = new(int, rn_fds+cn_fds))) {
+                                free(cfds);
+                                r = -ENOMEM;
+                                goto fail;
+                        }
+
+                        memcpy(t, rfds, rn_fds);
+                        memcpy(t+rn_fds, cfds, cn_fds);
+                        free(rfds);
+                        free(cfds);
+
+                        rfds = t;
+                        rn_fds = rn_fds+cn_fds;
+                }
+        }
+
+        *fds = rfds;
+        *n_fds = rn_fds;
+
+        set_free(set);
+
+        return 0;
+
+fail:
+        set_free(set);
+        free(rfds);
+
+        return r;
 }
 
-static int service_spawn(Service *s, ExecCommand *c, bool timeout, pid_t *_pid) {
+static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
         pid_t pid;
         int r;
+        int *fds = NULL;
+        unsigned n_fds = 0;
 
         assert(s);
         assert(c);
         assert(_pid);
 
+        if (pass_fds)
+                if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
+                        goto fail;
+
         if (timeout) {
-                if ((r = name_watch_timer(NAME(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
-                name_unwatch_timer(NAME(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
-        if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
+        if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
                 goto fail;
 
-        if ((r = name_watch_pid(NAME(s), pid)) < 0)
+        if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
                 /* FIXME: we need to do something here */
                 goto fail;
 
+        free(fds);
         *_pid = pid;
 
         return 0;
 
 fail:
+        free(fds);
+
         if (timeout)
-                name_unwatch_timer(NAME(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         return r;
 }
@@ -283,7 +448,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 = name_watch_timer(NAME(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);
@@ -293,7 +458,7 @@ static void service_enter_dead(Service *s, bool success, bool allow_restart) {
         return;
 
 fail:
-        log_warning("%s failed to run install restart timer: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_dead(s, false, false);
 }
 
@@ -306,19 +471,20 @@ 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 ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        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;
 
 fail:
-        log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
 }
 
@@ -353,15 +519,17 @@ 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;
 
 fail:
-        log_warning("%s failed to kill processes: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
 
         if (sent)  {
                 s->failure = true;
@@ -379,19 +547,19 @@ 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 ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        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;
 
 fail:
-        log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
 }
 
@@ -399,19 +567,20 @@ static void service_enter_start_post(Service *s) {
         int r;
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
-
-                if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        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;
 
 fail:
-        log_warning("%s failed to run start-post executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_stop(s, false);
 }
 
@@ -424,9 +593,11 @@ static void service_enter_start(Service *s) {
         assert(s->exec_command[SERVICE_EXEC_START]);
         assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
 
-        if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, &pid)) < 0)
+        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. */
@@ -442,14 +613,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
                 assert_not_reached("Unknown service type");
 
         return;
 
 fail:
-        log_warning("%s failed to run start exectuable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_stop(s, false);
 }
 
@@ -458,19 +628,19 @@ static void service_enter_start_pre(Service *s) {
 
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
-
-                if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        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;
 
 fail:
-        log_warning("%s failed to run start-pre executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_dead(s, false, true);
 }
 
@@ -478,16 +648,16 @@ static void service_enter_restart(Service *s) {
         int r;
         assert(s);
 
-        if ((r = manager_add_job(NAME(s)->meta.manager, JOB_START, NAME(s), JOB_FAIL, false, NULL)) < 0)
+        if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
                 goto fail;
 
-        log_debug("%s scheduled restart job.", name_id(NAME(s)));
+        log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
         service_enter_dead(s, true, false);
         return;
 
 fail:
 
-        log_warning("%s failed to schedule restart job: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_dead(s, false, false);
 }
 
@@ -496,19 +666,19 @@ static void service_enter_reload(Service *s) {
 
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
-
-                if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        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;
 
 fail:
-        log_warning("%s failed to run reload executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
         service_enter_stop(s, false);
 }
 
@@ -524,13 +694,13 @@ static void service_run_next(Service *s, bool success) {
 
         s->control_command = s->control_command->command_next;
 
-        if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+        if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                 goto fail;
 
         return;
 
 fail:
-        log_warning("%s failed to run spawn next executable: %s", name_id(NAME(s)), strerror(-r));
+        log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
 
         if (s->state == SERVICE_STOP)
                 service_enter_stop_post(s, false);
@@ -540,8 +710,8 @@ fail:
                 service_enter_stop(s, false);
 }
 
-static int service_start(Name *n) {
-        Service *s = SERVICE(n);
+static int service_start(Unit *u) {
+        Service *s = SERVICE(u);
 
         assert(s);
 
@@ -570,8 +740,8 @@ static int service_start(Name *n) {
         return 0;
 }
 
-static int service_stop(Name *n) {
-        Service *s = SERVICE(n);
+static int service_stop(Unit *u) {
+        Service *s = SERVICE(u);
 
         assert(s);
 
@@ -592,8 +762,8 @@ static int service_stop(Name *n) {
         return 0;
 }
 
-static int service_reload(Name *n) {
-        Service *s = SERVICE(n);
+static int service_reload(Unit *u) {
+        Service *s = SERVICE(u);
 
         assert(s);
 
@@ -603,18 +773,18 @@ static int service_reload(Name *n) {
         return 0;
 }
 
-static bool service_can_reload(Name *n) {
-        Service *s = SERVICE(n);
+static bool service_can_reload(Unit *u) {
+        Service *s = SERVICE(u);
 
         assert(s);
 
         return !!s->exec_command[SERVICE_EXEC_RELOAD];
 }
 
-static NameActiveState service_active_state(Name *n) {
-        assert(n);
+static UnitActiveState service_active_state(Unit *u) {
+        assert(u);
 
-        return state_table[SERVICE(n)->state];
+        return state_translation_table[SERVICE(u)->state];
 }
 
 static int main_pid_good(Service *s) {
@@ -638,14 +808,14 @@ static bool control_pid_good(Service *s) {
         return s->control_pid > 0;
 }
 
-static void service_sigchld_event(Name *n, pid_t pid, int code, int status) {
-        Service *s = SERVICE(n);
+static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
+        Service *s = SERVICE(u);
         bool success;
 
         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) {
@@ -658,7 +828,7 @@ static void service_sigchld_event(Name *n, pid_t pid, int code, int status) {
                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
                 }
 
-                log_debug("%s: main process exited, code=%s status=%i", name_id(n), sigchld_code(code), status);
+                log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
 
                 /* The service exited, so the service is officially
                  * gone. */
@@ -695,7 +865,7 @@ static void service_sigchld_event(Name *n, 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", name_id(n), sigchld_code(code), status);
+                log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
 
                 /* If we are shutting things down anyway we
                  * don't care about failing commands. */
@@ -712,6 +882,8 @@ static void service_sigchld_event(Name *n, 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:
@@ -751,7 +923,7 @@ static void service_sigchld_event(Name *n, pid_t pid, int code, int status) {
                                          * executed. */
 
                                         if ((r = service_load_pid_file(s)) < 0)
-                                                log_warning("%s: failed to load PID file %s: %s", name_id(NAME(s)), s->pid_file, strerror(-r));
+                                                log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
                                 }
 
                                 /* Fall through */
@@ -799,13 +971,13 @@ static void service_sigchld_event(Name *n, pid_t pid, int code, int status) {
                 assert_not_reached("Got SIGCHLD for unkown PID");
 }
 
-static void service_timer_event(Name *n, int id, uint64_t elapsed) {
-        Service *s = SERVICE(n);
+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) {
 
@@ -813,17 +985,17 @@ static void service_timer_event(Name *n, int id, uint64_t elapsed) {
         case SERVICE_START:
         case SERVICE_START_POST:
         case SERVICE_RELOAD:
-                log_warning("%s operation timed out. Stopping.", name_id(n));
+                log_warning("%s operation timed out. Stopping.", unit_id(u));
                 service_enter_stop(s, false);
                 break;
 
         case SERVICE_STOP:
-                log_warning("%s stopping timed out. Terminating.", name_id(n));
+                log_warning("%s stopping timed out. Terminating.", unit_id(u));
                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
                 break;
 
         case SERVICE_STOP_SIGTERM:
-                log_warning("%s stopping timed out. Killing.", name_id(n));
+                log_warning("%s stopping timed out. Killing.", unit_id(u));
                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
                 break;
 
@@ -832,27 +1004,27 @@ static void service_timer_event(Name *n, int id, uint64_t elapsed) {
                  * Must be something we cannot kill, so let's just be
                  * weirded out and continue */
 
-                log_warning("%s still around after SIGKILL. Ignoring.", name_id(n));
+                log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
                 service_enter_stop_post(s, false);
                 break;
 
         case SERVICE_STOP_POST:
-                log_warning("%s stopping timed out (2). Terminating.", name_id(n));
+                log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
                 break;
 
         case SERVICE_FINAL_SIGTERM:
-                log_warning("%s stopping timed out (2). Killing.", name_id(n));
+                log_warning("%s stopping timed out (2). Killing.", unit_id(u));
                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
                 break;
 
         case SERVICE_FINAL_SIGKILL:
-                log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n));
+                log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
                 service_enter_dead(s, false, true);
                 break;
 
         case SERVICE_AUTO_RESTART:
-                log_debug("%s holdoff time over, scheduling restart.", name_id(n));
+                log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
                 service_enter_restart(s);
                 break;
 
@@ -861,7 +1033,7 @@ static void service_timer_event(Name *n, int id, uint64_t elapsed) {
         }
 }
 
-const NameVTable service_vtable = {
+const UnitVTable service_vtable = {
         .suffix = ".service",
 
         .init = service_init,