Add FD_CLOEXEC option to all anonymous FDs
authorDavid Goulet <david.goulet@polymtl.ca>
Mon, 27 Jun 2011 20:36:27 +0000 (16:36 -0400)
committerDavid Goulet <david.goulet@polymtl.ca>
Mon, 27 Jun 2011 20:36:27 +0000 (16:36 -0400)
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 <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <david.goulet@polymtl.ca>
ltt-sessiond/kernel-ctl.c
ltt-sessiond/main.c

index db97dee205bc4f6f13b437b44b57644cf01704dd..3925c6ba5afc8e17f193dea1b497c7291a5ac52f 100644 (file)
@@ -18,6 +18,7 @@
 
 #define _GNU_SOURCE
 #include <errno.h>
+#include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -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;
 
index 46e6fff454e66aa7dd091fb8a49edadc1d008c53..05114e1f2cbed563814c4f4babd80d963eddd1f6 100644 (file)
@@ -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) {
This page took 0.036916 seconds and 4 git commands to generate.