daandemeyer / rpms / systemd

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