a52f674
From 4aaa35d08f4942e28a1046b46a853b170c9d8858 Mon Sep 17 00:00:00 2001
b12ff75
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
b12ff75
Date: Tue, 10 Dec 2013 21:52:11 -0500
b12ff75
Subject: [PATCH] activate: clean up inherited descriptors
b12ff75
b12ff75
> [simon@troela server]$ /usr/lib/systemd/systemd-activate -l 9000 main.js
b12ff75
> Assertion 'fd == 3 + count' failed at src/activate/activate.c:115,
b12ff75
> function open_sockets(). Aborting.
b12ff75
> Aborted (core dumped)
b12ff75
b12ff75
> after a bit debuging i found the problem:
b12ff75
> slim appears to leak an fd into all of its children:
b12ff75
> stat /proc/14004/fd/3  (14004 is the pid a random process in my session)
b12ff75
>  File: '/proc/14004/fd/3' -> '/var/log/slim.log'
b12ff75
b12ff75
systemd-activate should be robust against the shell (or anything else) leaking
b12ff75
descriptors. Now everything except stdin/stdout/stderr and received sockets
b12ff75
will be closed.
a52f674
a52f674
(cherry picked from commit c099716487df4a4f5394e57e7ca14da1d358166a)
b12ff75
---
b12ff75
 src/activate/activate.c | 23 +++++++++++++++++++----
b12ff75
 1 file changed, 19 insertions(+), 4 deletions(-)
b12ff75
b12ff75
diff --git a/src/activate/activate.c b/src/activate/activate.c
b12ff75
index a9461bc..6aa8b9f 100644
b12ff75
--- a/src/activate/activate.c
b12ff75
+++ b/src/activate/activate.c
b12ff75
@@ -137,6 +137,17 @@ static int open_sockets(int *epoll_fd, bool accept) {
b12ff75
                 count ++;
b12ff75
         }
b12ff75
 
b12ff75
+        /* Close logging and all other descriptors */
b12ff75
+        if (arg_listen) {
b12ff75
+                int except[3 + n];
b12ff75
+
b12ff75
+                for (fd = 0; fd < SD_LISTEN_FDS_START + n; fd++)
b12ff75
+                        except[fd] = fd;
b12ff75
+
b12ff75
+                log_close();
b12ff75
+                close_all_fds(except, 3 + n);
b12ff75
+        }
b12ff75
+
b12ff75
         /** Note: we leak some fd's on error here. I doesn't matter
b12ff75
          *  much, since the program will exit immediately anyway, but
b12ff75
          *  would be a pain to fix.
b12ff75
@@ -147,6 +158,7 @@ static int open_sockets(int *epoll_fd, bool accept) {
b12ff75
 
b12ff75
                 fd = make_socket_fd(*address, SOCK_STREAM | (arg_accept*SOCK_CLOEXEC));
b12ff75
                 if (fd < 0) {
b12ff75
+                        log_open();
b12ff75
                         log_error("Failed to open '%s': %s", *address, strerror(-fd));
b12ff75
                         return fd;
b12ff75
                 }
b12ff75
@@ -154,6 +166,9 @@ static int open_sockets(int *epoll_fd, bool accept) {
b12ff75
                 count ++;
b12ff75
         }
b12ff75
 
b12ff75
+        if (arg_listen)
b12ff75
+                log_open();
b12ff75
+
b12ff75
         *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
b12ff75
         if (*epoll_fd < 0) {
b12ff75
                 log_error("Failed to create epoll object: %m");
b12ff75
@@ -298,10 +313,10 @@ static void sigchld_hdl(int sig, siginfo_t *t, void *data) {
b12ff75
 
b12ff75
 static int install_chld_handler(void) {
b12ff75
         int r;
b12ff75
-        struct sigaction act;
b12ff75
-        zero(act);
b12ff75
-        act.sa_flags = SA_SIGINFO;
b12ff75
-        act.sa_sigaction = sigchld_hdl;
b12ff75
+        struct sigaction act = {
b12ff75
+                .sa_flags = SA_SIGINFO,
b12ff75
+                .sa_sigaction = sigchld_hdl,
b12ff75
+        };
b12ff75
 
b12ff75
         r = sigaction(SIGCHLD, &act, 0);
b12ff75
         if (r < 0)