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