chiark / gitweb /
dbus: add introspection to midlevel paths
[elogind.git] / src / service.c
index 6a230e50c83e320e0c9d917f04b5ee39bcd9e9fd..d3852c76a13644570b7d8967e90e6570c6b2cb21 100644 (file)
@@ -39,6 +39,9 @@
 #define NEWLINES "\n\r"
 
 #ifdef HAVE_SYSV_COMPAT
+
+#define DEFAULT_SYSV_TIMEOUT_USEC (3*USEC_PER_MINUTE)
+
 typedef enum RunlevelType {
         RUNLEVEL_UP,
         RUNLEVEL_DOWN,
@@ -178,11 +181,11 @@ static void service_close_socket_fd(Service *s) {
 static void service_connection_unref(Service *s) {
         assert(s);
 
-        if (!s->socket)
+        if (!s->accept_socket)
                 return;
 
-        socket_connection_unref(s->socket);
-        s->socket = NULL;
+        socket_connection_unref(s->accept_socket);
+        s->accept_socket = NULL;
 }
 
 static void service_done(Unit *u) {
@@ -222,6 +225,8 @@ static void service_done(Unit *u) {
         service_close_socket_fd(s);
         service_connection_unref(s);
 
+        set_free(s->configured_sockets);
+
         unit_unwatch_timer(u, &s->timer_watch);
 }
 
@@ -250,7 +255,7 @@ static char *sysv_translate_name(const char *name) {
         return r;
 }
 
-static int sysv_translate_facility(const char *name, char **_r) {
+static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
 
         /* We silently ignore the $ prefix here. According to the LSB
          * spec it simply indicates whether something is a
@@ -289,28 +294,39 @@ static int sysv_translate_facility(const char *name, char **_r) {
 
         unsigned i;
         char *r;
+        const char *n;
 
-        for (i = 0; i < ELEMENTSOF(table); i += 2)
+        assert(name);
+        assert(_r);
 
-                if (streq(table[i], name) ||
-                    (*name == '$' && streq(table[i], name+1))) {
+        n = *name == '$' ? name + 1 : name;
 
-                        if (!table[i+1])
-                                return 0;
+        for (i = 0; i < ELEMENTSOF(table); i += 2) {
 
-                        if (!(r = strdup(table[i+1])))
-                                return -ENOMEM;
+                if (!streq(table[i], n))
+                        continue;
 
-                        goto finish;
-                }
+                if (!table[i+1])
+                        return 0;
+
+                if (!(r = strdup(table[i+1])))
+                        return -ENOMEM;
+
+                goto finish;
+        }
 
         /* If we don't know this name, fallback heuristics to figure
          * out whether something is a target or an service alias. */
 
         if (*name == '$')
-                r = unit_name_build(name+1, NULL, ".target");
+                /* Facilities starting with $ are most likely targets */
+                r = unit_name_build(n, NULL, ".target");
+        else if (filename && streq(name, filename))
+                /* Names equalling the file name of the services are redundant */
+                return 0;
         else
-                r = sysv_translate_name(name);
+                /* Everything else we assume to be normal service names */
+                r = sysv_translate_name(n);
 
         if (!r)
                 return -ENOMEM;
@@ -612,12 +628,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                                 goto finish;
                                         }
 
-                                        if (streq(n, file_name_from_path(path))) {
-                                                free(n);
-                                                continue;
-                                        }
-
-                                        r = sysv_translate_facility(n, &m);
+                                        r = sysv_translate_facility(n, file_name_from_path(path), &m);
                                         free(n);
 
                                         if (r < 0)
@@ -662,12 +673,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                                 goto finish;
                                         }
 
-                                        if (streq(n, file_name_from_path(path))) {
-                                                free(n);
-                                                continue;
-                                        }
-
-                                        r = sysv_translate_facility(n, &m);
+                                        r = sysv_translate_facility(n, file_name_from_path(path), &m);
                                         free(n);
 
                                         if (r < 0)
@@ -787,12 +793,13 @@ static int service_load_sysv_path(Service *s, const char *path) {
 
                 /* Don't timeout special services during boot (like fsck) */
                 s->timeout_usec = 0;
-        }
+        } else
+                s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
 
         /* Special setting for all SysV services */
         s->type = SERVICE_FORKING;
         s->remain_after_exit = true;
-        s->restart = SERVICE_ONCE;
+        s->restart = SERVICE_RESTART_NO;
         s->exec_context.std_output =
                 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
                 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
@@ -1176,6 +1183,9 @@ static int service_get_sockets(Service *s, Set **_set) {
         if (s->socket_fd >= 0)
                 return 0;
 
+        if (!set_isempty(s->configured_sockets))
+                return 0;
+
         /* Collects all Socket objects that belong to this
          * service. Note that a service might have multiple sockets
          * via multiple names. */
@@ -1215,23 +1225,30 @@ fail:
 
 static int service_notify_sockets_dead(Service *s) {
         Iterator i;
-        Set *set;
+        Set *set, *free_set = NULL;
         Socket *sock;
         int r;
 
         assert(s);
 
+        /* Notifies all our sockets when we die */
+
         if (s->socket_fd >= 0)
                 return 0;
 
-        /* Notifies all our sockets when we die */
-        if ((r = service_get_sockets(s, &set)) < 0)
-                return r;
+        if (!set_isempty(s->configured_sockets))
+                set = s->configured_sockets;
+        else {
+                if ((r = service_get_sockets(s, &free_set)) < 0)
+                        return r;
+
+                set = free_set;
+        }
 
         SET_FOREACH(sock, set, i)
                 socket_notify_service_dead(sock);
 
-        set_free(set);
+        set_free(free_set);
 
         return 0;
 }
@@ -1389,7 +1406,7 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
         int r;
         int *rfds = NULL;
         unsigned rn_fds = 0;
-        Set *set;
+        Set *set, *free_set = NULL;
         Socket *sock;
 
         assert(s);
@@ -1399,8 +1416,14 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
         if (s->socket_fd >= 0)
                 return 0;
 
-        if ((r = service_get_sockets(s, &set)) < 0)
-                return r;
+        if (!set_isempty(s->configured_sockets))
+                set = s->configured_sockets;
+        else {
+                if ((r = service_get_sockets(s, &free_set)) < 0)
+                        return r;
+
+                set = free_set;
+        }
 
         SET_FOREACH(sock, set, i) {
                 int *cfds;
@@ -1437,7 +1460,7 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
         *fds = rfds;
         *n_fds = rn_fds;
 
-        set_free(set);
+        set_free(free_set);
 
         return 0;
 
@@ -1604,7 +1627,11 @@ static void service_enter_dead(Service *s, bool success, bool allow_restart) {
         if (allow_restart &&
             !s->forbid_restart &&
             (s->restart == SERVICE_RESTART_ALWAYS ||
-             (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
+             (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) ||
+             (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) ||
+             (s->restart == SERVICE_RESTART_ON_ABORT && s->failure &&
+              (s->main_exec_status.code == CLD_KILLED ||
+               s->main_exec_status.code == CLD_DUMPED)))) {
 
                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
                         goto fail;
@@ -2083,14 +2110,6 @@ 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->forbid_restart = false;
@@ -2391,7 +2410,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         if (s->main_pid == pid) {
 
                 s->main_pid = 0;
-                exec_status_exit(&s->main_exec_status, pid, code, status);
+                exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id);
 
                 if (s->type != SERVICE_FORKING && s->control_command) {
                         s->control_command->exec_status = s->main_exec_status;
@@ -2468,7 +2487,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                 s->control_pid = 0;
 
                 if (s->control_command) {
-                        exec_status_exit(&s->control_command->exec_status, pid, code, status);
+                        exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
 
                         if (s->control_command->ignore)
                                 success = true;
@@ -2795,6 +2814,7 @@ static int service_enumerate(Manager *m) {
                 STRV_FOREACH(p, arch_daemons_split) {
 
                         free(name);
+                        name = NULL;
 
                         if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
                                 continue;
@@ -3060,7 +3080,7 @@ int service_set_socket_fd(Service *s, int fd, Socket *sock) {
 
         s->socket_fd = fd;
         s->got_socket_fd = true;
-        s->socket = sock;
+        s->accept_socket = sock;
 
         return 0;
 }
@@ -3097,9 +3117,11 @@ static const char* const service_state_table[_SERVICE_STATE_MAX] = {
 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-always",
+        [SERVICE_RESTART_NO] = "no",
+        [SERVICE_RESTART_ON_SUCCESS] = "on-success",
+        [SERVICE_RESTART_ON_FAILURE] = "on-failure",
+        [SERVICE_RESTART_ON_ABORT] = "on-abort",
+        [SERVICE_RESTART_ALWAYS] = "always"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);