From: David Goulet Date: Mon, 27 Jun 2011 20:36:27 +0000 (-0400) Subject: Add FD_CLOEXEC option to all anonymous FDs X-Git-Tag: v2.0-pre1~75 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=7b3958902a08e7d96d388614f2726ad8a6fc9df2;p=lttng-tools.git Add FD_CLOEXEC option to all anonymous FDs Upon execlp() some anonymous file descriptors from the kernel tracer could be dup and thus making the refcount always > 1 on the lttng kernel tracer side. Acked-by: Mathieu Desnoyers Signed-off-by: David Goulet --- diff --git a/ltt-sessiond/kernel-ctl.c b/ltt-sessiond/kernel-ctl.c index db97dee20..3925c6ba5 100644 --- a/ltt-sessiond/kernel-ctl.c +++ b/ltt-sessiond/kernel-ctl.c @@ -18,6 +18,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -54,6 +55,12 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd) } lks->fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } + lks->kconsumer_fds_sent = 0; session->kernel_session = lks; session->kern_session_count++; @@ -92,6 +99,12 @@ int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_chann /* Setup the channel fd */ lkc->fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } + /* Add channel to session */ cds_list_add(&lkc->list, &session->channel_list.head); session->channel_count++; @@ -128,6 +141,12 @@ int kernel_create_event(struct ltt_kernel_channel *channel, struct lttng_event * } event->fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } + /* Add event to event list */ cds_list_add(&event->list, &channel->events_list.head); DBG("Event %s enabled (fd: %d)", ev->name, event->fd); @@ -162,6 +181,12 @@ int kernel_open_metadata(struct ltt_kernel_session *session) } lkm->fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } + session->metadata = lkm; DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); @@ -298,6 +323,12 @@ int kernel_open_channel_stream(struct ltt_kernel_channel *channel) } lks->fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } + ret = asprintf(&lks->pathname, "%s/trace_%d", channel->pathname, channel->stream_count); if (ret < 0) { @@ -336,6 +367,11 @@ int kernel_open_metadata_stream(struct ltt_kernel_session *session) DBG("Kernel metadata stream created (fd: %d)", ret); session->metadata_stream_fd = ret; + /* Prevent fd duplication after execlp() */ + ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); + if (ret < 0) { + perror("fcntl session fd"); + } return 0; diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index 46e6fff45..05114e1f2 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -826,12 +826,14 @@ static int process_client_msg(struct command_ctx *cmd_ctx) /* Need a session for kernel command */ if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS && cmd_ctx->session->kernel_session == NULL) { + ret = create_kernel_session(cmd_ctx->session); if (ret < 0) { ret = LTTCOMM_KERN_SESS_FAIL; goto error; } + /* Start the kernel consumer daemon */ if (kconsumerd_pid == 0) { ret = start_kconsumerd(); if (ret < 0) {