chiark / gitweb /
execute: use fd_nonblock()//fd_cloexec() where applicable
[elogind.git] / socket.c
index 1b23483881549caa3de4ce14882a8360e5e1bdfb..a1f3ef8af75eadb9a637638f88ce494c99a1b0b7 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -1,18 +1,39 @@
 /*-*- Mode: C; c-basic-offset: 8 -*-*/
 
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/poll.h>
+#include <sys/epoll.h>
 #include <signal.h>
 
 #include "unit.h"
 #include "socket.h"
 #include "log.h"
+#include "load-dropin.h"
+#include "load-fragment.h"
 
-static const UnitActiveState state_table[_SOCKET_STATE_MAX] = {
+static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
         [SOCKET_DEAD] = UNIT_INACTIVE,
         [SOCKET_START_PRE] = UNIT_ACTIVATING,
         [SOCKET_START_POST] = UNIT_ACTIVATING,
@@ -27,6 +48,21 @@ static const UnitActiveState state_table[_SOCKET_STATE_MAX] = {
         [SOCKET_MAINTAINANCE] = UNIT_INACTIVE,
 };
 
+static const char* const state_string_table[_SOCKET_STATE_MAX] = {
+        [SOCKET_DEAD] = "dead",
+        [SOCKET_START_PRE] = "start-pre",
+        [SOCKET_START_POST] = "start-post",
+        [SOCKET_LISTENING] = "listening",
+        [SOCKET_RUNNING] = "running",
+        [SOCKET_STOP_PRE] = "stop-pre",
+        [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
+        [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
+        [SOCKET_STOP_POST] = "stop-post",
+        [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm",
+        [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
+        [SOCKET_MAINTAINANCE] = "maintainance"
+};
+
 static void socket_done(Unit *u) {
         Socket *s = SOCKET(u);
         SocketPort *p;
@@ -53,10 +89,12 @@ static void socket_done(Unit *u) {
 
         s->service = NULL;
 
-        unit_unwatch_timer(u, &s->timer_id);
+        free(s->bind_to_device);
+
+        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;
@@ -65,34 +103,46 @@ static int socket_init(Unit *u) {
          * reload */
 
         s->state = 0;
-        s->timer_id = -1;
+        s->timer_watch.type = WATCH_INVALID;
         s->bind_ipv6_only = false;
         s->backlog = SOMAXCONN;
         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
+        s->directory_mode = 0755;
+        s->socket_mode = 0666;
         exec_context_init(&s->exec_context);
 
-        if ((r = unit_load_fragment_and_dropin(u)) < 0)
-                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) {
@@ -110,21 +160,6 @@ static const char* listen_lookup(int type) {
 
 static void socket_dump(Unit *u, FILE *f, const char *prefix) {
 
-        static const char* const state_table[_SOCKET_STATE_MAX] = {
-                [SOCKET_DEAD] = "dead",
-                [SOCKET_START_PRE] = "start-pre",
-                [SOCKET_START_POST] = "start-post",
-                [SOCKET_LISTENING] = "listening",
-                [SOCKET_RUNNING] = "running",
-                [SOCKET_STOP_PRE] = "stop-pre",
-                [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
-                [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
-                [SOCKET_STOP_POST] = "stop-post",
-                [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm",
-                [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
-                [SOCKET_MAINTAINANCE] = "maintainance"
-        };
-
         static const char* const command_table[_SOCKET_EXEC_MAX] = {
                 [SOCKET_EXEC_START_PRE] = "StartPre",
                 [SOCKET_EXEC_START_POST] = "StartPost",
@@ -135,21 +170,31 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
         SocketExecCommand c;
         Socket *s = SOCKET(u);
         SocketPort *p;
-        char *prefix2;
+        const char *prefix2;
+        char *p2;
 
         assert(s);
+        assert(f);
 
-        prefix2 = strappend(prefix, "\t");
-        if (!prefix2)
-                prefix2 = "";
+        p2 = strappend(prefix, "\t");
+        prefix2 = p2 ? p2 : prefix;
 
         fprintf(f,
                 "%sSocket State: %s\n"
                 "%sBindIPv6Only: %s\n"
-                "%sBacklog: %u\n",
-                prefix, state_table[s->state],
+                "%sBacklog: %u\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, s->socket_mode,
+                prefix, s->directory_mode);
+
+        if (s->bind_to_device)
+                fprintf(f,
+                        "%sBindToDevice: %s\n",
+                        prefix, s->bind_to_device);
 
         LIST_FOREACH(port, p, s->ports) {
 
@@ -181,7 +226,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
                 exec_command_dump_list(s->exec_command[c], f, prefix2);
         }
 
-        free(prefix2);
+        free(p2);
 }
 
 static void socket_close_fds(Socket *s) {
@@ -193,7 +238,7 @@ static void socket_close_fds(Socket *s) {
                 if (p->fd < 0)
                         continue;
 
-                unit_unwatch_fd(UNIT(s), p->fd);
+                unit_unwatch_fd(UNIT(s), &p->fd_watch);
                 assert_se(close_nointr(p->fd) >= 0);
 
                 p->fd = -1;
@@ -213,14 +258,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, &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;
                         }
@@ -260,7 +314,7 @@ static void socket_unwatch_fds(Socket *s) {
                 if (p->fd < 0)
                         continue;
 
-                unit_unwatch_fd(UNIT(s), p->fd);
+                unit_unwatch_fd(UNIT(s), &p->fd_watch);
         }
 }
 
@@ -274,7 +328,7 @@ static int socket_watch_fds(Socket *s) {
                 if (p->fd < 0)
                         continue;
 
-                if ((r = unit_watch_fd(UNIT(s), p->fd, POLLIN)) < 0)
+                if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
                         goto fail;
         }
 
@@ -300,7 +354,7 @@ static void socket_set_state(Socket *s, SocketState state) {
             state != SOCKET_STOP_POST &&
             state != SOCKET_STOP_POST_SIGTERM &&
             state != SOCKET_STOP_POST_SIGKILL)
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         if (state != SOCKET_START_PRE &&
             state != SOCKET_START_POST &&
@@ -310,7 +364,7 @@ static void socket_set_state(Socket *s, SocketState state) {
             state != SOCKET_STOP_POST &&
             state != SOCKET_STOP_POST_SIGTERM &&
             state != SOCKET_STOP_POST_SIGKILL)
-                if (s->control_pid >= 0) {
+                if (s->control_pid > 0) {
                         unit_unwatch_pid(UNIT(s), s->control_pid);
                         s->control_pid = 0;
                 }
@@ -332,7 +386,9 @@ static void socket_set_state(Socket *s, SocketState state) {
         if (state != SOCKET_LISTENING)
                 socket_unwatch_fds(s);
 
-        unit_notify(UNIT(s), state_table[old_state], state_table[s->state]);
+        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 socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
@@ -344,12 +400,18 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
         assert(_pid);
 
         if (timeout) {
-                if ((r = unit_watch_timer(UNIT(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
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
-
-        if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
+
+        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)
@@ -362,7 +424,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
 
 fail:
         if (timeout)
-                unit_unwatch_timer(UNIT(s), &s->timer_id);
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
 
         return r;
 }
@@ -383,13 +445,13 @@ static void socket_enter_stop_post(Socket *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_STOP_POST);
-        } else
+        socket_set_state(s, SOCKET_STOP_POST);
+
+        if (!s->control_command)
                 socket_enter_dead(s, true);
 
         return;
@@ -416,9 +478,11 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) {
                         r = -errno;
                         goto fail;
                 }
+        }
 
-                socket_set_state(s, state);
-        } else
+        socket_set_state(s, state);
+
+        if (s->control_pid <= 0)
                 socket_enter_dead(s, true);
 
         return;
@@ -439,13 +503,13 @@ static void socket_enter_stop_pre(Socket *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_STOP_PRE);
-        } else
+        socket_set_state(s, SOCKET_STOP_PRE);
+
+        if (!s->control_command)
                 socket_enter_stop_post(s, true);
 
         return;
@@ -455,26 +519,41 @@ fail:
         socket_enter_stop_post(s, false);
 }
 
+static void socket_enter_listening(Socket *s) {
+        int r;
+        assert(s);
+
+        if ((r = socket_watch_fds(s)) < 0) {
+                log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s)), strerror(-r));
+                goto fail;
+        }
+
+        socket_set_state(s, SOCKET_LISTENING);
+        return;
+
+fail:
+        socket_enter_stop_pre(s, false);
+}
+
 static void socket_enter_start_post(Socket *s) {
         int r;
         assert(s);
 
-        if ((r = socket_open_fds(s)) < 0 ||
-            (r = socket_watch_fds(s)) < 0) {
+        if ((r = socket_open_fds(s)) < 0) {
                 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r));
                 goto fail;
         }
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) {
                         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
                         goto fail;
                 }
 
-                socket_set_state(s, SOCKET_START_POST);
-        } else
-                socket_set_state(s, SOCKET_LISTENING);
+        socket_set_state(s, SOCKET_START_POST);
+
+        if (!s->control_command)
+                socket_enter_listening(s);
 
         return;
 
@@ -486,13 +565,13 @@ static void socket_enter_start_pre(Socket *s) {
         int r;
         assert(s);
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_START_PRE);
-        } else
+        socket_set_state(s, SOCKET_START_PRE);
+
+        if (!s->control_command)
                 socket_enter_start_post(s);
 
         return;
@@ -594,17 +673,17 @@ static int socket_stop(Unit *u) {
 static UnitActiveState socket_active_state(Unit *u) {
         assert(u);
 
-        return state_table[SOCKET(u)->state];
+        return state_translation_table[SOCKET(u)->state];
 }
 
-static void socket_fd_event(Unit *u, int fd, uint32_t events) {
+static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         Socket *s = SOCKET(u);
 
         assert(s);
 
-        log_info("Incoming traffic on %s", unit_id(u));
+        log_debug("Incoming traffic on %s", unit_id(u));
 
-        if (events != POLLIN)
+        if (events != EPOLLIN)
                 socket_enter_stop_pre(s, false);
 
         socket_enter_running(s);
@@ -617,7 +696,7 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         assert(s);
         assert(pid >= 0);
 
-        success = code == CLD_EXITED || status == 0;
+        success = code == CLD_EXITED && status == 0;
         s->failure = s->failure || !success;
 
         assert(s->control_pid == pid);
@@ -626,27 +705,30 @@ static void socket_sigchld_event(Unit *u, 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", unit_id(u), sigchld_code(code), 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 {
+        else {
                 /* 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 SOCKET_START_PRE:
                         if (success)
-                                socket_enter_start_pre(s);
+                                socket_enter_start_post(s);
                         else
                                 socket_enter_stop_pre(s, false);
                         break;
 
                 case SOCKET_START_POST:
                         if (success)
-                                socket_set_state(s, SOCKET_LISTENING);
+                                socket_enter_listening(s);
                         else
                                 socket_enter_stop_pre(s, false);
                         break;
@@ -669,13 +751,13 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         }
 }
 
-static void socket_timer_event(Unit *u, int id, uint64_t elapsed) {
+static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
         Socket *s = SOCKET(u);
 
         assert(s);
         assert(elapsed == 1);
 
-        assert(s->timer_id == id);
+        assert(w == &s->timer_watch);
 
         switch (s->state) {
 
@@ -752,6 +834,17 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
         return 0;
 }
 
+void socket_notify_service_dead(Socket *s) {
+        assert(s);
+
+        /* The service is dead. Dang. */
+
+        if (s->state == SOCKET_RUNNING) {
+                log_debug("%s got notified about service death.", unit_id(UNIT(s)));
+                socket_enter_listening(s);
+        }
+}
+
 const UnitVTable socket_vtable = {
         .suffix = ".socket",