chiark / gitweb /
execute: improve exec_spawn() logging
[elogind.git] / socket.c
index 6b1da016dd4cdb13695f9a914a38ee8b6c04bcb4..51bfc9a88d4c6c50be0a2a1bbbd68333d3c2515e 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -30,6 +30,8 @@
 #include "unit.h"
 #include "socket.h"
 #include "log.h"
+#include "load-dropin.h"
+#include "load-fragment.h"
 
 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
         [SOCKET_DEAD] = UNIT_INACTIVE,
@@ -92,7 +94,7 @@ static void socket_done(Unit *u) {
         unit_unwatch_timer(u, &s->timer_watch);
 }
 
-static int socket_init(Unit *u) {
+static int socket_init(Unit *u, UnitLoadState *new_state) {
         Socket *s = SOCKET(u);
         char *t;
         int r;
@@ -105,33 +107,45 @@ static int socket_init(Unit *u) {
         s->bind_ipv6_only = false;
         s->backlog = SOMAXCONN;
         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
+        s->directory_mode = 0755;
+        s->socket_mode = 0666;
+        s->kill_mode = 0;
+        s->failure = false;
+        s->control_pid = 0;
         exec_context_init(&s->exec_context);
 
-        if ((r = unit_load_fragment_and_dropin(u)) <= 0) {
-                if (r == 0)
-                        r = -ENOENT;
-                goto fail;
-        }
+        if ((r = unit_load_fragment(u, new_state)) < 0)
+                return r;
 
-        if (!(t = unit_name_change_suffix(unit_id(u), ".service"))) {
-                r = -ENOMEM;
-                goto fail;
-        }
+        if (*new_state == UNIT_STUB)
+                return -ENOENT;
 
-        r = manager_load_unit(u->meta.manager, t, (Unit**) &s->service);
-        free(t);
+        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
+                return r;
 
-        if (r < 0)
-                goto fail;
+        /* This is a new unit? Then let's add in some extras */
+        if (*new_state == UNIT_LOADED) {
 
-        if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
-                goto fail;
+                if (!(t = unit_name_change_suffix(unit_id(u), ".service")))
+                        return -ENOMEM;
 
-        return 0;
+                r = manager_load_unit(u->meta.manager, t, (Unit**) &s->service);
+                free(t);
 
-fail:
-        socket_done(u);
-        return r;
+                if (r < 0)
+                        return r;
+
+                if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
+                        return r;
+
+                if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
+                        return r;
+
+                if ((r = unit_add_default_cgroup(u)) < 0)
+                        return r;
+        }
+
+        return 0;
 }
 
 static const char* listen_lookup(int type) {
@@ -171,10 +185,21 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
         fprintf(f,
                 "%sSocket State: %s\n"
                 "%sBindIPv6Only: %s\n"
-                "%sBacklog: %u\n",
+                "%sBacklog: %u\n"
+                "%sKillMode: %s\n"
+                "%sSocketMode: %04o\n"
+                "%sDirectoryMode: %04o\n",
                 prefix, state_string_table[s->state],
                 prefix, yes_no(s->bind_ipv6_only),
-                prefix, s->backlog);
+                prefix, s->backlog,
+                prefix, kill_mode_to_string(s->kill_mode),
+                prefix, s->socket_mode,
+                prefix, s->directory_mode);
+
+        if (s->control_pid > 0)
+                fprintf(f,
+                        "%sControl PID: %llu\n",
+                        prefix, (unsigned long long) s->control_pid);
 
         if (s->bind_to_device)
                 fprintf(f,
@@ -243,14 +268,23 @@ static int socket_open_fds(Socket *s) {
 
                 if (p->type == SOCKET_SOCKET) {
 
-                        if ((r = socket_address_listen(&p->address, s->backlog, s->bind_ipv6_only, s->bind_to_device, &p->fd)) < 0)
+                        if ((r = socket_address_listen(
+                                             &p->address,
+                                             s->backlog,
+                                             s->bind_ipv6_only,
+                                             s->bind_to_device,
+                                             s->directory_mode,
+                                             s->socket_mode,
+                                             &p->fd)) < 0)
                                 goto rollback;
 
                 } else {
                         struct stat st;
                         assert(p->type == SOCKET_FIFO);
 
-                        if (mkfifo(p->path, 0666 & ~s->exec_context.umask) < 0 && errno != EEXIST) {
+                        mkdir_parents(p->path, s->directory_mode);
+
+                        if (mkfifo(p->path, s->socket_mode) < 0 && errno != EEXIST) {
                                 r = -errno;
                                 goto rollback;
                         }
@@ -362,6 +396,9 @@ static void socket_set_state(Socket *s, SocketState state) {
         if (state != SOCKET_LISTENING)
                 socket_unwatch_fds(s);
 
+        if (state == old_state)
+                return;
+
         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]);
@@ -381,7 +418,13 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
         } else
                 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,
+                            NULL, 0,
+                            true,
+                            true,
+                            UNIT(s)->meta.cgroup_bondings,
+                            &pid)) < 0)
                 goto fail;
 
         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
@@ -441,13 +484,25 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) {
 
         if (s->control_pid > 0) {
                 int sig;
+                bool sent = false;
 
                 sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL;
 
-                if (kill(s->control_pid, sig) < 0 && errno != ESRCH) {
-                        r = -errno;
-                        goto fail;
+                if (s->kill_mode == KILL_CONTROL_GROUP) {
+
+                        if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
+                                if (r != -EAGAIN && r != -ESRCH)
+                                        goto fail;
+                        } else
+                                sent = true;
                 }
+
+                if (!sent)
+                        if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) {
+                                r = -errno;
+                                goto fail;
+                        }
+
         }
 
         socket_set_state(s, state);
@@ -678,7 +733,7 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
 
         if (s->control_command->command_next &&
-            (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST))) {
+            (success || (s->state == SOCKET_STOP_PRE || s->state == SOCKET_STOP_POST))) {
                 log_debug("%s running next command for the state %s", unit_id(u), state_string_table[s->state]);
                 socket_run_next(s, success);
         } else {