From: David Goulet Date: Wed, 31 Oct 2012 19:12:00 +0000 (-0400) Subject: Fix: Set CLOEXEC flag on every created sockets X-Git-Tag: v2.1.0-rc7~14 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=b662582bf448d2fad2f5990580771733a3b33d16;p=lttng-tools.git Fix: Set CLOEXEC flag on every created sockets 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 --- diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index ce2213c1b..736ec83ff 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -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; diff --git a/src/common/utils.c b/src/common/utils.c index 781888eb3..1a0e47ec4 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -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; +} diff --git a/src/common/utils.h b/src/common/utils.h index d47d44889..c5723aa1d 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -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 */