chiark / gitweb /
service: optionally call into PAM when dropping priviliges
[elogind.git] / src / service.c
index 2ce95f5a9f2016e38e619e4aeea405e02db6db15..a38c88076eeb46ded4fa938eec309141b26b15c2 100644 (file)
@@ -40,7 +40,7 @@
 typedef enum RunlevelType {
         RUNLEVEL_UP,
         RUNLEVEL_DOWN,
-        RUNLEVEL_BASIC
+        RUNLEVEL_SYSINIT
 } RunlevelType;
 
 static const struct {
@@ -58,10 +58,10 @@ static const struct {
         { "rc6.d",  SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
 
         /* SUSE style boot.d */
-        { "boot.d", SPECIAL_BASIC_TARGET,     RUNLEVEL_BASIC },
+        { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 
         /* Debian style rcS.d */
-        { "rcS.d",  SPECIAL_BASIC_TARGET,     RUNLEVEL_BASIC },
+        { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 };
 
 #define RUNLEVELS_UP "12345"
@@ -82,7 +82,7 @@ static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
         [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
         [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
         [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
-        [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
+        [SERVICE_MAINTENANCE] = UNIT_INACTIVE,
         [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
 };
 
@@ -149,6 +149,9 @@ static void service_done(Unit *u) {
         free(s->sysv_runlevels);
         s->sysv_runlevels = NULL;
 
+        free(s->status_text);
+        s->status_text = NULL;
+
         exec_context_done(&s->exec_context);
         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
         s->control_command = NULL;
@@ -675,7 +678,7 @@ static int service_load_sysv_name(Service *s, const char *name) {
             endswith(name, ".sh.service"))
                 return -ENOENT;
 
-        STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
+        STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
                 char *path;
                 int r;
 
@@ -726,7 +729,7 @@ static int service_load_sysv(Service *s) {
         /* Load service data from SysV init scripts, preferably with
          * LSB headers ... */
 
-        if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
+        if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
                 return 0;
 
         if ((t = UNIT(s)->meta.id))
@@ -907,6 +910,10 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
                 fprintf(f, "%sSysVRunLevels: %s\n",
                         prefix, s->sysv_runlevels);
 
+        if (s->status_text)
+                fprintf(f, "%sStatus Text: %s\n",
+                        prefix, s->status_text);
+
         free(p2);
 }
 
@@ -936,6 +943,9 @@ static int service_load_pid_file(Service *s) {
         if ((unsigned long) (pid_t) p != p)
                 return -ERANGE;
 
+        if (p <= 1)
+                return -ERANGE;
+
         if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
                 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
                             (unsigned long long) p, s->pid_file);
@@ -1069,7 +1079,7 @@ static void service_set_state(Service *s, ServiceState state) {
             state == SERVICE_STOP_POST ||
             state == SERVICE_FINAL_SIGTERM ||
             state == SERVICE_FINAL_SIGKILL ||
-            state == SERVICE_MAINTAINANCE ||
+            state == SERVICE_MAINTENANCE ||
             state == SERVICE_AUTO_RESTART)
                 service_notify_sockets_dead(s);
 
@@ -1117,7 +1127,9 @@ static int service_coldplug(Unit *u) {
 
                 if ((s->deserialized_state == SERVICE_START &&
                      (s->type == SERVICE_FORKING ||
-                      s->type == SERVICE_DBUS)) ||
+                      s->type == SERVICE_DBUS ||
+                      s->type == SERVICE_FINISH ||
+                      s->type == SERVICE_NOTIFY)) ||
                     s->deserialized_state == SERVICE_START_POST ||
                     s->deserialized_state == SERVICE_RUNNING ||
                     s->deserialized_state == SERVICE_RELOAD ||
@@ -1338,7 +1350,7 @@ static void service_enter_dead(Service *s, bool success, bool allow_restart) {
 
                 service_set_state(s, SERVICE_AUTO_RESTART);
         } else
-                service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
+                service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
 
         return;
 
@@ -1377,7 +1389,7 @@ static void service_enter_stop_post(Service *s, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
 }
 
@@ -1473,7 +1485,7 @@ static void service_enter_stop(Service *s, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
 }
 
@@ -1518,7 +1530,7 @@ static void service_enter_start_post(Service *s) {
         return;
 
 fail:
-        log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_stop(s, false);
 }
 
@@ -1538,7 +1550,7 @@ static void service_enter_start(Service *s) {
 
         if ((r = service_spawn(s,
                                s->exec_command[SERVICE_EXEC_START],
-                               s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
+                               s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
                                true,
                                true,
                                true,
@@ -1566,13 +1578,15 @@ static void service_enter_start(Service *s) {
                 service_set_state(s, SERVICE_START);
 
         } else if (s->type == SERVICE_FINISH ||
-                   s->type == SERVICE_DBUS) {
+                   s->type == SERVICE_DBUS ||
+                   s->type == SERVICE_NOTIFY) {
 
                 /* For finishing services we wait until the start
                  * process exited, too, but it is our main process. */
 
                 /* For D-Bus services we know the main pid right away,
-                 * but wait for the bus name to appear on the bus. */
+                 * but wait for the bus name to appear on the
+                 * bus. Notify services are similar. */
 
                 s->main_pid = pid;
                 s->main_pid_known = true;
@@ -1584,7 +1598,7 @@ static void service_enter_start(Service *s) {
         return;
 
 fail:
-        log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
 }
 
@@ -1613,7 +1627,7 @@ static void service_enter_start_pre(Service *s) {
         return;
 
 fail:
-        log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_dead(s, false, true);
 }
 
@@ -1660,7 +1674,7 @@ static void service_enter_reload(Service *s) {
         return;
 
 fail:
-        log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
         service_enter_stop(s, false);
 }
 
@@ -1690,7 +1704,7 @@ static void service_run_next(Service *s, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
+        log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
 
         if (s->state == SERVICE_START_PRE)
                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
@@ -1723,7 +1737,7 @@ static int service_start(Unit *u) {
             s->state == SERVICE_START_POST)
                 return 0;
 
-        assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
+        assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
 
         /* Make sure we don't enter a busy loop of some kind. */
         if (!ratelimit_test(&s->ratelimit)) {
@@ -1943,7 +1957,7 @@ 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 || s->type == SERVICE_FINISH) {
+                if (s->type != SERVICE_FORKING) {
                         assert(s->exec_command[SERVICE_EXEC_START]);
                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
                 }
@@ -1963,14 +1977,18 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                         break;
 
                 case SERVICE_START:
-                        assert(s->type == SERVICE_FINISH);
+                        if (s->type == SERVICE_FINISH) {
+                                /* This was our main goal, so let's go on */
+                                if (success)
+                                        service_enter_start_post(s);
+                                else
+                                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
+                                break;
+                        } else {
+                                assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
 
-                        /* This was our main goal, so let's go on */
-                        if (success)
-                                service_enter_start_post(s);
-                        else
-                                service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
-                        break;
+                                /* Fall through */
+                        }
 
                 case SERVICE_RUNNING:
                         service_enter_running(s, success);
@@ -2094,8 +2112,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                                 assert_not_reached("Uh, control process died at wrong time.");
                         }
                 }
-        } else
-                assert_not_reached("Got SIGCHLD for unkown PID");
+        }
 }
 
 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
@@ -2150,7 +2167,7 @@ static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
                 break;
 
         case SERVICE_FINAL_SIGKILL:
-                log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
+                log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
                 service_enter_dead(s, false, true);
                 break;
 
@@ -2188,6 +2205,57 @@ static void service_cgroup_notify_event(Unit *u) {
         }
 }
 
+static void service_notify_message(Unit *u, char **tags) {
+        Service *s = SERVICE(u);
+        const char *e;
+
+        assert(u);
+
+        log_debug("%s: Got message", u->meta.id);
+
+        /* Interpret MAINPID= */
+        if ((e = strv_find_prefix(tags, "MAINPID=")) &&
+            (s->state == SERVICE_START ||
+             s->state == SERVICE_START_POST ||
+             s->state == SERVICE_RUNNING ||
+             s->state == SERVICE_RELOAD)) {
+                unsigned long pid;
+
+                if (safe_atolu(e + 8, &pid) < 0 ||
+                    (unsigned long) (pid_t) pid != pid ||
+                    pid <= 1)
+                        log_warning("Failed to parse %s", e);
+                else {
+                        log_debug("%s: got %s", u->meta.id, e);
+                        s->main_pid = (pid_t) pid;
+                }
+        }
+
+        /* Interpret READY= */
+        if (s->type == SERVICE_NOTIFY &&
+            s->state == SERVICE_START &&
+            strv_find(tags, "READY=1")) {
+                log_debug("%s: got READY=1", u->meta.id);
+
+                service_enter_start_post(s);
+        }
+
+        /* Interpret STATUS= */
+        if ((e = strv_find_prefix(tags, "STATUS="))) {
+                char *t;
+
+                if (!(t = strdup(e+7))) {
+                        log_error("Failed to allocate string.");
+                        return;
+                }
+
+                log_debug("%s: got %s", u->meta.id, e);
+
+                free(s->status_text);
+                s->status_text = t;
+        }
+}
+
 static int service_enumerate(Manager *m) {
         char **p;
         unsigned i;
@@ -2197,7 +2265,7 @@ static int service_enumerate(Manager *m) {
 
         assert(m);
 
-        STRV_FOREACH(p, m->sysvrcnd_path)
+        STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
                         struct dirent *de;
 
@@ -2264,7 +2332,7 @@ static int service_enumerate(Manager *m) {
                                 }
 
                                 if (de->d_name[0] == 'S' &&
-                                    (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_BASIC))
+                                    (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
                                         SERVICE(service)->sysv_start_priority =
                                                 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
 
@@ -2283,7 +2351,9 @@ static int service_enumerate(Manager *m) {
                                         if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
                                                 goto finish;
 
-                                } else if (de->d_name[0] == 'K' && rcnd_table[i].type == RUNLEVEL_DOWN) {
+                                } else if (de->d_name[0] == 'K' &&
+                                           (rcnd_table[i].type == RUNLEVEL_DOWN ||
+                                            rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
                                         Unit *shutdown_target;
 
                                         /* We honour K links only for
@@ -2295,7 +2365,12 @@ static int service_enumerate(Manager *m) {
                                          * really distuingish here
                                          * between the runlevels 0 and
                                          * 6 and just add them to the
-                                         * special shutdown target. */
+                                         * special shutdown target. On
+                                         * SUSE the boot.d/ runlevel
+                                         * is also used for shutdown,
+                                         * so we add links for that
+                                         * too to the shutdown
+                                         * target.*/
 
                                         if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
                                                 goto finish;
@@ -2424,7 +2499,7 @@ static const char* const service_state_table[_SERVICE_STATE_MAX] = {
         [SERVICE_STOP_POST] = "stop-post",
         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
-        [SERVICE_MAINTAINANCE] = "maintainance",
+        [SERVICE_MAINTENANCE] = "maintenance",
         [SERVICE_AUTO_RESTART] = "auto-restart",
 };
 
@@ -2442,7 +2517,8 @@ static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
         [SERVICE_FORKING] = "forking",
         [SERVICE_SIMPLE] = "simple",
         [SERVICE_FINISH] = "finish",
-        [SERVICE_DBUS] = "dbus"
+        [SERVICE_DBUS] = "dbus",
+        [SERVICE_NOTIFY] = "notify"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
@@ -2488,6 +2564,7 @@ const UnitVTable service_vtable = {
         .timer_event = service_timer_event,
 
         .cgroup_notify_empty = service_cgroup_notify_event,
+        .notify_message = service_notify_message,
 
         .bus_name_owner_change = service_bus_name_owner_change,
         .bus_query_pid_done = service_bus_query_pid_done,