chiark / gitweb /
service: read special startup dirs only on the respective distros
[elogind.git] / src / service.c
index fc773d20ebb194dc8678a01cac0df968a972e15f..ee1da2a7b32bcb1d746e3e36a28f0eedd69815ea 100644 (file)
@@ -49,20 +49,32 @@ static const struct {
         const char *target;
         const RunlevelType type;
 } rcnd_table[] = {
-        /* Standard SysV runlevels */
-        { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
+        /* Standard SysV runlevels for start-up */
         { "rc1.d",  SPECIAL_RESCUE_TARGET,    RUNLEVEL_UP },
         { "rc2.d",  SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
         { "rc3.d",  SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
         { "rc4.d",  SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
         { "rc5.d",  SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
-        { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN },
 
+#ifdef TARGET_SUSE
         /* SUSE style boot.d */
         { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
+#endif
 
+#ifdef TARGET_DEBIAN
         /* Debian style rcS.d */
         { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
+#endif
+
+        /* Standard SysV runlevels for shutdown */
+        { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
+        { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN }
+
+        /* Note that the order here matters, as we read the
+           directories in this order, and we want to make sure that
+           sysv_start_priority is known when we first load the
+           unit. And that value we only know from S links. Hence
+           UP/SYSINIT must be read before DOWN */
 };
 
 #define RUNLEVELS_UP "12345"
@@ -284,6 +296,7 @@ static int sysv_fix_order(Service *s) {
         LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
                 Service *t;
                 UnitDependency d;
+                bool special_s, special_t;
 
                 t = (Service*) other;
 
@@ -299,7 +312,14 @@ static int sysv_fix_order(Service *s) {
                     (!t->sysv_path || t->sysv_has_lsb))
                         continue;
 
-                if (t->sysv_start_priority < s->sysv_start_priority)
+                special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
+                special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
+
+                if (special_t && !special_s)
+                        d = UNIT_AFTER;
+                else if (special_s && !special_t)
+                        d = UNIT_BEFORE;
+                else if (t->sysv_start_priority < s->sysv_start_priority)
                         d = UNIT_AFTER;
                 else if (t->sysv_start_priority > s->sysv_start_priority)
                         d = UNIT_BEFORE;
@@ -546,8 +566,10 @@ static int service_load_sysv_path(Service *s, const char *path) {
 
                                         if (unit_name_to_type(m) == UNIT_SERVICE)
                                                 r = unit_add_name(u, m);
+                                        else if (s->sysv_start_priority >= 0)
+                                                r = unit_add_two_dependencies_by_name_inverse(u, UNIT_AFTER, UNIT_WANTS, m, NULL, true);
                                         else
-                                                r = unit_add_two_dependencies_by_name_inverse(u, UNIT_AFTER, UNIT_REQUIRES, m, NULL, true);
+                                                r = unit_add_dependency_by_name_inverse(u, UNIT_AFTER, m, NULL, true);
 
                                         free(m);
 
@@ -631,6 +653,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                         goto finish;
                                 }
 
+                                free(u->meta.description);
                                 u->meta.description = d;
 
                         } else if (startswith_no_case(t, "X-Interactive:")) {
@@ -1178,6 +1201,11 @@ static void service_set_state(Service *s, ServiceState state) {
                 service_connection_unref(s);
         }
 
+        /* For the inactive states unit_notify() will trim the cgroup,
+         * but for exit we have to do that ourselves... */
+        if (state == SERVICE_EXITED)
+                cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
+
         if (old_state != state)
                 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
 
@@ -1840,7 +1868,8 @@ static void service_run_next(Service *s, bool success) {
                                false,
                                !s->permissions_start_only,
                                !s->root_directory_start_only,
-                               false,
+                               s->control_command_id == SERVICE_EXEC_START_PRE ||
+                               s->control_command_id == SERVICE_EXEC_STOP_POST,
                                false,
                                &s->control_pid)) < 0)
                 goto fail;
@@ -1889,6 +1918,14 @@ static int service_start(Unit *u) {
                 return -ECANCELED;
         }
 
+        if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
+             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
+             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
+            s->socket_fd < 0) {
+                log_warning("%s can only be started with a per-connection socket.", u->meta.id);
+                return -EINVAL;
+        }
+
         s->failure = false;
         s->main_pid_known = false;
         s->allow_restart = true;
@@ -1902,12 +1939,9 @@ static int service_stop(Unit *u) {
 
         assert(s);
 
-        /* Cannot do this now */
-        if (s->state == SERVICE_START_PRE ||
-            s->state == SERVICE_START ||
-            s->state == SERVICE_START_POST ||
-            s->state == SERVICE_RELOAD)
-                return -EAGAIN;
+        /* This is a user request, so don't do restarts on this
+         * shutdown. */
+        s->allow_restart = false;
 
         /* Already on it */
         if (s->state == SERVICE_STOP ||
@@ -1918,16 +1952,24 @@ static int service_stop(Unit *u) {
             s->state == SERVICE_FINAL_SIGKILL)
                 return 0;
 
+        /* Don't allow a restart */
         if (s->state == SERVICE_AUTO_RESTART) {
                 service_set_state(s, SERVICE_DEAD);
                 return 0;
         }
 
-        assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
+        /* If there's already something running we go directly into
+         * kill mode. */
+        if (s->state == SERVICE_START_PRE ||
+            s->state == SERVICE_START ||
+            s->state == SERVICE_START_POST ||
+            s->state == SERVICE_RELOAD) {
+                service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
+                return 0;
+        }
 
-        /* This is a user request, so don't do restarts on this
-         * shutdown. */
-        s->allow_restart = false;
+        assert(s->state == SERVICE_RUNNING ||
+               s->state == SERVICE_EXITED);
 
         service_enter_stop(s, true);
         return 0;
@@ -1970,6 +2012,9 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
 
         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
 
+        if (s->status_text)
+                unit_serialize_item(u, f, "status-text", s->status_text);
+
         /* There's a minor uncleanliness here: if there are multiple
          * commands attached here, we will start from the first one
          * again */
@@ -2054,6 +2099,14 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                         log_debug("Failed to parse main-pid-known value %s", value);
                 else
                         s->main_pid_known = b;
+        } else if (streq(key, "status-text")) {
+                char *t;
+
+                if ((t = strdup(value))) {
+                        free(s->status_text);
+                        s->status_text = t;
+                }
+
         } else if (streq(key, "control-command")) {
                 ServiceExecCommand id;
 
@@ -2165,7 +2218,6 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         assert(pid >= 0);
 
         success = is_clean_exit(code, status);
-        s->failure = s->failure || !success;
 
         if (s->main_pid == pid) {
 
@@ -2175,9 +2227,14 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                 if (s->type != SERVICE_FORKING) {
                         assert(s->exec_command[SERVICE_EXEC_START]);
                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
+
+                        if (s->exec_command[SERVICE_EXEC_START]->ignore)
+                                success = true;
                 }
 
-                log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
+                log_full(success ? LOG_DEBUG : LOG_NOTICE,
+                         "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
+                s->failure = s->failure || !success;
 
                 /* The service exited, so the service is officially
                  * gone. */
@@ -2224,12 +2281,18 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
 
         } else if (s->control_pid == pid) {
 
-                if (s->control_command)
+                if (s->control_command) {
                         exec_status_exit(&s->control_command->exec_status, pid, code, status);
 
+                        if (s->control_command->ignore)
+                                success = true;
+                }
+
                 s->control_pid = 0;
 
-                log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
+                log_full(success ? LOG_DEBUG : LOG_NOTICE,
+                         "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
+                s->failure = s->failure || !success;
 
                 /* If we are shutting things down anyway we
                  * don't care about failing commands. */
@@ -2448,7 +2511,7 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
              s->state == SERVICE_RELOAD)) {
 
                 if (parse_pid(e + 8, &pid) < 0)
-                        log_warning("Failed to parse %s", e);
+                        log_warning("Failed to parse notification message %s", e);
                 else {
                         log_debug("%s: got %s", u->meta.id, e);
                         service_set_main_pid(s, pid);
@@ -2468,15 +2531,21 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
         if ((e = strv_find_prefix(tags, "STATUS="))) {
                 char *t;
 
-                if (!(t = strdup(e+7))) {
-                        log_error("Failed to allocate string.");
-                        return;
-                }
+                if (e[7]) {
+                        if (!(t = strdup(e+7))) {
+                                log_error("Failed to allocate string.");
+                                return;
+                        }
 
-                log_debug("%s: got %s", u->meta.id, e);
+                        log_debug("%s: got %s", u->meta.id, e);
+
+                        free(s->status_text);
+                        s->status_text = t;
+                } else {
+                        free(s->status_text);
+                        s->status_text = NULL;
+                }
 
-                free(s->status_text);
-                s->status_text = t;
         }
 }
 
@@ -2704,6 +2773,17 @@ int service_set_socket_fd(Service *s, int fd, Socket *sock) {
         return 0;
 }
 
+static void service_reset_maintenance(Unit *u) {
+        Service *s = SERVICE(u);
+
+        assert(s);
+
+        if (s->state == SERVICE_MAINTENANCE)
+                service_set_state(s, SERVICE_DEAD);
+
+        s->failure = false;
+}
+
 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
         [SERVICE_DEAD] = "dead",
         [SERVICE_START_PRE] = "start-pre",
@@ -2791,6 +2871,8 @@ const UnitVTable service_vtable = {
         .sigchld_event = service_sigchld_event,
         .timer_event = service_timer_event,
 
+        .reset_maintenance = service_reset_maintenance,
+
         .cgroup_notify_empty = service_cgroup_notify_event,
         .notify_message = service_notify_message,