richardphibel / rpms / systemd

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