Create kernel-consumer.c/.h and consumer.h
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index 7327c3cb2262ddf4337b46c08f2d02160f1136e7..a9d538c7cd4b5e201bfafc29981e49409cf5081c 100644 (file)
@@ -33,7 +33,6 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <urcu/futex.h>
 #include <urcu/uatomic.h>
 #include <unistd.h>
 #include <config.h>
 #include <common/defaults.h>
 #include <common/kernel-consumer/kernel-consumer.h>
 #include <common/ust-consumer/ust-consumer.h>
+#include <common/futex.h>
 
 #include "lttng-sessiond.h"
 #include "channel.h"
 #include "context.h"
 #include "event.h"
-#include "futex.h"
 #include "kernel.h"
+#include "kernel-consumer.h"
 #include "modprobe.h"
 #include "shm.h"
 #include "ust-ctl.h"
 
 #define CONSUMERD_FILE "lttng-consumerd"
 
-struct consumer_data {
-       enum lttng_consumer_type type;
-
-       pthread_t thread;       /* Worker thread interacting with the consumer */
-       sem_t sem;
-
-       /* Mutex to control consumerd pid assignation */
-       pthread_mutex_t pid_mutex;
-       pid_t pid;
-
-       int err_sock;
-       int cmd_sock;
-
-       /* consumer error and command Unix socket path */
-       char err_unix_sock_path[PATH_MAX];
-       char cmd_unix_sock_path[PATH_MAX];
-};
-
 /* Const values */
 const char default_home_dir[] = DEFAULT_HOME_DIR;
 const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
@@ -544,139 +526,6 @@ static void clean_command_ctx(struct command_ctx **cmd_ctx)
        }
 }
 
-/*
- * Send all stream fds of kernel channel to the consumer.
- */
-static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
-               int sock, struct ltt_kernel_channel *channel,
-               uid_t uid, gid_t gid)
-{
-       int ret;
-       struct ltt_kernel_stream *stream;
-       struct lttcomm_consumer_msg lkm;
-
-       DBG("Sending streams of channel %s to kernel consumer",
-                       channel->channel->name);
-
-       /* Send channel */
-       lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
-       lkm.u.channel.channel_key = channel->fd;
-       lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size;
-       lkm.u.channel.mmap_len = 0;     /* for kernel */
-       DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
-       ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
-       if (ret < 0) {
-               PERROR("send consumer channel");
-               goto error;
-       }
-
-       /* Send streams */
-       cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
-               if (!stream->fd) {
-                       continue;
-               }
-               lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
-               lkm.u.stream.channel_key = channel->fd;
-               lkm.u.stream.stream_key = stream->fd;
-               lkm.u.stream.state = stream->state;
-               lkm.u.stream.output = channel->channel->attr.output;
-               lkm.u.stream.mmap_len = 0;      /* for kernel */
-               lkm.u.stream.uid = uid;
-               lkm.u.stream.gid = gid;
-               strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1);
-               lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
-               DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
-               ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
-               if (ret < 0) {
-                       PERROR("send consumer stream");
-                       goto error;
-               }
-               ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1);
-               if (ret < 0) {
-                       PERROR("send consumer stream ancillary data");
-                       goto error;
-               }
-       }
-
-       DBG("consumer channel streams sent");
-
-       return 0;
-
-error:
-       return ret;
-}
-
-/*
- * Send all stream fds of the kernel session to the consumer.
- */
-static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
-               struct ltt_kernel_session *session)
-{
-       int ret;
-       struct ltt_kernel_channel *chan;
-       struct lttcomm_consumer_msg lkm;
-       int sock = session->consumer_fd;
-
-       DBG("Sending metadata stream fd");
-
-       /* Extra protection. It's NOT supposed to be set to -1 at this point */
-       if (session->consumer_fd < 0) {
-               session->consumer_fd = consumer_data->cmd_sock;
-       }
-
-       if (session->metadata_stream_fd >= 0) {
-               /* Send metadata channel fd */
-               lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
-               lkm.u.channel.channel_key = session->metadata->fd;
-               lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size;
-               lkm.u.channel.mmap_len = 0;     /* for kernel */
-               DBG("Sending metadata channel %d to consumer", lkm.u.channel.channel_key);
-               ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
-               if (ret < 0) {
-                       PERROR("send consumer channel");
-                       goto error;
-               }
-
-               /* Send metadata stream fd */
-               lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
-               lkm.u.stream.channel_key = session->metadata->fd;
-               lkm.u.stream.stream_key = session->metadata_stream_fd;
-               lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
-               lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
-               lkm.u.stream.mmap_len = 0;      /* for kernel */
-               lkm.u.stream.uid = session->uid;
-               lkm.u.stream.gid = session->gid;
-               strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1);
-               lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
-               DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
-               ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
-               if (ret < 0) {
-                       PERROR("send consumer stream");
-                       goto error;
-               }
-               ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1);
-               if (ret < 0) {
-                       PERROR("send consumer stream");
-                       goto error;
-               }
-       }
-
-       cds_list_for_each_entry(chan, &session->channel_list.head, list) {
-               ret = send_kconsumer_channel_streams(consumer_data, sock, chan,
-                               session->uid, session->gid);
-               if (ret < 0) {
-                       goto error;
-               }
-       }
-
-       DBG("consumer fds (metadata and channel streams) sent");
-
-       return 0;
-
-error:
-       return ret;
-}
-
 /*
  * Notify UST applications using the shm mmap futex.
  */
@@ -818,7 +667,7 @@ static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
                                 * stream fds.
                                 */
                                if (session->kernel_session->consumer_fds_sent == 1) {
-                                       ret = send_kconsumer_channel_streams(consumer_data,
+                                       ret = kernel_consumer_send_channel_stream(consumer_data,
                                                        session->kernel_session->consumer_fd, channel,
                                                        session->uid, session->gid);
                                        if (ret < 0) {
@@ -1932,7 +1781,7 @@ static int init_kernel_tracing(struct ltt_kernel_session *session)
                        session->consumer_fd = kconsumer_data.cmd_sock;
                }
 
-               ret = send_kconsumer_session_streams(&kconsumer_data, session);
+               ret = kernel_consumer_send_session(&kconsumer_data, session);
                if (ret < 0) {
                        ret = LTTCOMM_KERN_CONSUMER_FAIL;
                        goto error;
@@ -2891,6 +2740,36 @@ error:
        return -ret;
 }
 
+/*
+ * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
+ */
+static ssize_t cmd_list_tracepoint_fields(int domain,
+                       struct lttng_event_field **fields)
+{
+       int ret;
+       ssize_t nb_fields = 0;
+
+       switch (domain) {
+       case LTTNG_DOMAIN_UST:
+               nb_fields = ust_app_list_event_fields(fields);
+               if (nb_fields < 0) {
+                       ret = LTTCOMM_UST_LIST_FAIL;
+                       goto error;
+               }
+               break;
+       case LTTNG_DOMAIN_KERNEL:
+       default:        /* fall-through */
+               ret = LTTCOMM_UND;
+               goto error;
+       }
+
+       return nb_fields;
+
+error:
+       /* Return negative value to differentiate return code */
+       return -ret;
+}
+
 /*
  * Command LTTNG_START_TRACE processed by the client thread.
  */
@@ -3339,6 +3218,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        switch(cmd_ctx->lsm->cmd_type) {
        case LTTNG_LIST_SESSIONS:
        case LTTNG_LIST_TRACEPOINTS:
+       case LTTNG_LIST_TRACEPOINT_FIELDS:
        case LTTNG_LIST_DOMAINS:
        case LTTNG_LIST_CHANNELS:
        case LTTNG_LIST_EVENTS:
@@ -3358,6 +3238,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        case LTTNG_CALIBRATE:
        case LTTNG_LIST_SESSIONS:
        case LTTNG_LIST_TRACEPOINTS:
+       case LTTNG_LIST_TRACEPOINT_FIELDS:
                need_tracing_session = 0;
                break;
        default:
@@ -3614,6 +3495,37 @@ skip_domain:
                ret = LTTCOMM_OK;
                break;
        }
+       case LTTNG_LIST_TRACEPOINT_FIELDS:
+       {
+               struct lttng_event_field *fields;
+               ssize_t nb_fields;
+
+               nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, &fields);
+               if (nb_fields < 0) {
+                       ret = -nb_fields;
+                       goto error;
+               }
+
+               /*
+                * Setup lttng message with payload size set to the event list size in
+                * bytes and then copy list into the llm payload.
+                */
+               ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event_field) * nb_fields);
+               if (ret < 0) {
+                       free(fields);
+                       goto setup_error;
+               }
+
+               /* Copy event list into message payload */
+               memcpy(cmd_ctx->llm->payload, fields,
+                               sizeof(struct lttng_event_field) * nb_fields);
+
+               free(fields);
+
+               ret = LTTCOMM_OK;
+               break;
+       }
+
        case LTTNG_START_TRACE:
        {
                ret = cmd_start_trace(cmd_ctx->session);
@@ -3668,7 +3580,7 @@ skip_domain:
        }
        case LTTNG_LIST_CHANNELS:
        {
-               size_t nb_chan;
+               int nb_chan;
                struct lttng_channel *channels;
 
                nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
@@ -4198,13 +4110,15 @@ static int set_permissions(char *rundir)
        int ret;
        gid_t gid;
 
-       gid = allowed_group();
-       if (gid < 0) {
+       ret = allowed_group();
+       if (ret < 0) {
                WARN("No tracing group detected");
                ret = 0;
                goto end;
        }
 
+       gid = ret;
+
        /* Set lttng run dir */
        ret = chown(rundir, 0, gid);
        if (ret < 0) {
@@ -4483,11 +4397,6 @@ int main(int argc, char **argv)
 
        rcu_register_thread();
 
-       /* Create thread quit pipe */
-       if ((ret = init_thread_quit_pipe()) < 0) {
-               goto error;
-       }
-
        setup_consumerd_path();
 
        /* Parse arguments */
@@ -4498,11 +4407,31 @@ int main(int argc, char **argv)
 
        /* Daemonize */
        if (opt_daemon) {
+               int i;
+
+               /*
+                * fork
+                * child: setsid, close FD 0, 1, 2, chdir /
+                * parent: exit (if fork is successful)
+                */
                ret = daemon(0, 0);
                if (ret < 0) {
                        PERROR("daemon");
                        goto error;
                }
+               /*
+                * We are in the child. Make sure all other file
+                * descriptors are closed, in case we are called with
+                * more opened file descriptors than the standard ones.
+                */
+               for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
+                       (void) close(i);
+               }
+       }
+
+       /* Create thread quit pipe */
+       if ((ret = init_thread_quit_pipe()) < 0) {
+               goto error;
        }
 
        /* Check if daemon is UID = 0 */
This page took 0.027058 seconds and 4 git commands to generate.