Fix: Set CLOEXEC flag on every created sockets
authorDavid Goulet <dgoulet@efficios.com>
Wed, 31 Oct 2012 19:12:00 +0000 (15:12 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Wed, 7 Nov 2012 15:36:44 +0000 (10:36 -0500)
Every new socket created by an accept() in the session daemon is set
with the CLOEXEC flag so when the consumer is spawned we don't leak.

Fixes #367

Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng-sessiond/main.c
src/common/utils.c
src/common/utils.h

index ce2213c1bd2897e46df7b38f822e9e3cb061a20e..736ec83ff8712d891f92d3a2724f7085672b924f 100644 (file)
@@ -928,6 +928,12 @@ restart:
                goto error;
        }
 
+       /*
+        * Set the CLOEXEC flag. Return code is useless because either way, the
+        * show must go on.
+        */
+       (void) utils_set_fd_cloexec(sock);
+
        health_code_update(&consumer_data->health);
 
        DBG2("Receiving code from consumer err_sock");
@@ -1402,6 +1408,12 @@ static void *thread_registration_apps(void *data)
                                                goto error;
                                        }
 
+                                       /*
+                                        * Set the CLOEXEC flag. Return code is useless because
+                                        * either way, the show must go on.
+                                        */
+                                       (void) utils_set_fd_cloexec(sock);
+
                                        /* Create UST registration command for enqueuing */
                                        ust_cmd = zmalloc(sizeof(struct ust_command));
                                        if (ust_cmd == NULL) {
@@ -2882,6 +2894,12 @@ static void *thread_manage_health(void *data)
                goto error;
        }
 
+       /*
+        * Set the CLOEXEC flag. Return code is useless because either way, the
+        * show must go on.
+        */
+       (void) utils_set_fd_cloexec(sock);
+
        ret = lttcomm_listen_unix_sock(sock);
        if (ret < 0) {
                goto error;
@@ -2946,6 +2964,12 @@ restart:
                        goto error;
                }
 
+               /*
+                * Set the CLOEXEC flag. Return code is useless because either way, the
+                * show must go on.
+                */
+               (void) utils_set_fd_cloexec(new_sock);
+
                DBG("Receiving data from client for health...");
                ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
                if (ret <= 0) {
@@ -3143,6 +3167,12 @@ static void *thread_manage_clients(void *data)
                        goto error;
                }
 
+               /*
+                * Set the CLOEXEC flag. Return code is useless because either way, the
+                * show must go on.
+                */
+               (void) utils_set_fd_cloexec(sock);
+
                /* Set socket option for credentials retrieval */
                ret = lttcomm_setsockopt_creds_unix_sock(sock);
                if (ret < 0) {
@@ -3443,6 +3473,14 @@ static int init_daemon_socket(void)
                goto end;
        }
 
+       /* Set the cloexec flag */
+       ret = utils_set_fd_cloexec(client_sock);
+       if (ret < 0) {
+               ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
+                               "Continuing but note that the consumer daemon will have a "
+                               "reference to this socket on exec()", client_sock);
+       }
+
        /* File permission MUST be 660 */
        ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
        if (ret < 0) {
@@ -3459,6 +3497,14 @@ static int init_daemon_socket(void)
                goto end;
        }
 
+       /* Set the cloexec flag */
+       ret = utils_set_fd_cloexec(apps_sock);
+       if (ret < 0) {
+               ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
+                               "Continuing but note that the consumer daemon will have a "
+                               "reference to this socket on exec()", apps_sock);
+       }
+
        /* File permission MUST be 666 */
        ret = chmod(apps_unix_sock_path,
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
@@ -3468,6 +3514,9 @@ static int init_daemon_socket(void)
                goto end;
        }
 
+       DBG3("Session daemon client socket %d and application socket %d created",
+                       client_sock, apps_sock);
+
 end:
        umask(old_umask);
        return ret;
index 781888eb351d1ff720b13826706c3cd41812a51c..1a0e47ec4ed97b1601cbc3498126a760b5e02895 100644 (file)
@@ -179,3 +179,26 @@ char *utils_strdupdelim(const char *begin, const char *end)
 error:
        return str;
 }
+
+/*
+ * Set CLOEXEC flag to the give file descriptor.
+ */
+__attribute__((visibility("hidden")))
+int utils_set_fd_cloexec(int fd)
+{
+       int ret;
+
+       if (fd < 0) {
+               ret = -EINVAL;
+               goto end;
+       }
+
+       ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+       if (ret < 0) {
+               PERROR("fcntl cloexec");
+               ret = -errno;
+       }
+
+end:
+       return ret;
+}
index d47d448895952b609a529cdb15709279db830c73..c5723aa1d364b66e55fe7e5d606f52688a57c2f8 100644 (file)
@@ -23,5 +23,6 @@ int utils_create_pipe(int *dst);
 int utils_create_pipe_cloexec(int *dst);
 void utils_close_pipe(int *src);
 char *utils_strdupdelim(const char *begin, const char *end);
+int utils_set_fd_cloexec(int fd);
 
 #endif /* _COMMON_UTILS_H */
This page took 0.029536 seconds and 4 git commands to generate.