Fix bad channel output when sending to consumer
[lttng-tools.git] / ltt-sessiond / main.c
index 1139df8b94fd26e256d10be10b6dda15965616b0..5db41869f5b1e8c5176cbe07ca42b79e302e52a8 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -40,9 +41,9 @@
 
 #include <urcu/list.h>         /* URCU list library (-lurcu) */
 #include <lttng/lttng.h>
+#include <lttng-sessiond-comm.h>
 
 #include "context.h"
-#include "liblttsessiondcomm.h"
 #include "ltt-sessiond.h"
 #include "lttngerr.h"
 #include "kernel-ctl.h"
@@ -114,6 +115,22 @@ static int modprobe_remove_kernel_modules(void);
  */
 static struct ltt_session_list *session_list_ptr;
 
+static gid_t allowed_group(void)
+{
+       struct group *grp;
+
+       if (opt_tracing_group) {
+               grp = getgrnam(opt_tracing_group);
+       } else {
+               grp = getgrnam(default_tracing_group);
+       }
+       if (!grp) {
+               return -1;
+       } else {
+               return grp->gr_gid;
+       }
+}
+
 /*
  * Init quit pipe.
  *
@@ -141,6 +158,15 @@ static void teardown_kernel_session(struct ltt_session *session)
 {
        if (session->kernel_session != NULL) {
                DBG("Tearing down kernel session");
+
+               /*
+                * If a custom kernel consumer was registered, close the socket before
+                * tearing down the complete kernel session structure
+                */
+               if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
+                       lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
+               }
+
                trace_destroy_kernel_session(session->kernel_session);
                /* Extra precaution */
                session->kernel_session = NULL;
@@ -271,7 +297,9 @@ static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *chan
                        lkm.fd = stream->fd;
                        lkm.state = stream->state;
                        lkm.max_sb_size = channel->channel->attr.subbuf_size;
+                       lkm.output = channel->channel->attr.output;
                        strncpy(lkm.path_name, stream->pathname, PATH_MAX);
+                       lkm.path_name[PATH_MAX - 1] = '\0';
 
                        DBG("Sending fd %d to kconsumerd", lkm.fd);
 
@@ -294,7 +322,7 @@ error:
 /*
  * Send all stream fds of the kernel session to the consumer.
  */
-static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
+static int send_kconsumerd_fds(struct ltt_kernel_session *session)
 {
        int ret;
        struct ltt_kernel_channel *chan;
@@ -307,7 +335,7 @@ static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
 
        DBG("Sending kconsumerd header for metadata");
 
-       ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
+       ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
        if (ret < 0) {
                perror("send kconsumerd header");
                goto error;
@@ -315,14 +343,21 @@ static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
 
        DBG("Sending metadata stream fd");
 
+       /* Extra protection. It's NOT suppose to be set to 0 at this point */
+       if (session->consumer_fd == 0) {
+               session->consumer_fd = kconsumerd_cmd_sock;
+       }
+
        if (session->metadata_stream_fd != 0) {
                /* Send metadata stream fd first */
                lkm.fd = session->metadata_stream_fd;
                lkm.state = ACTIVE_FD;
                lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
+               lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
                strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
+               lkm.path_name[PATH_MAX - 1] = '\0';
 
-               ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
+               ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &lkm, &lkm.fd, 1, sizeof(lkm));
                if (ret < 0) {
                        perror("send kconsumerd fd");
                        goto error;
@@ -330,7 +365,7 @@ static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
        }
 
        cds_list_for_each_entry(chan, &session->channel_list.head, list) {
-               ret = send_kconsumerd_channel_fds(sock, chan);
+               ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
                if (ret < 0) {
                        goto error;
                }
@@ -529,6 +564,12 @@ static int update_kernel_stream(int fd)
                        unlock_session(session);
                        continue;
                }
+
+               /* This is not suppose to be 0 but this is an extra security check */
+               if (session->kernel_session->consumer_fd == 0) {
+                       session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
+               }
+
                cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
                        if (channel->fd == fd) {
                                DBG("Channel found, updating kernel streams");
@@ -536,12 +577,14 @@ static int update_kernel_stream(int fd)
                                if (ret < 0) {
                                        goto end;
                                }
+
                                /*
                                 * Have we already sent fds to the consumer? If yes, it means that
                                 * tracing is started so it is safe to send our updated stream fds.
                                 */
                                if (session->kernel_session->kconsumer_fds_sent == 1) {
-                                       ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel);
+                                       ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
+                                                       channel);
                                        if (ret < 0) {
                                                goto end;
                                        }
@@ -866,6 +909,9 @@ error:
        return ret;
 }
 
+/*
+ * Join kernel consumer thread
+ */
 static int join_kconsumerd_thread(void)
 {
        void *status;
@@ -988,7 +1034,7 @@ static int modprobe_kernel_modules(void)
                        ERR("Unable to launch modprobe for module %s",
                                kernel_modules_list[i].name);
                } else if (kernel_modules_list[i].required
-                          && WEXITSTATUS(ret) != 0) {
+                               && WEXITSTATUS(ret) != 0) {
                        ERR("Unable to load module %s",
                                kernel_modules_list[i].name);
                } else {
@@ -1024,7 +1070,7 @@ static int modprobe_remove_kernel_modules(void)
                        ERR("Unable to launch modprobe --remove for module %s",
                                kernel_modules_list[i].name);
                } else if (kernel_modules_list[i].required
-                          && WEXITSTATUS(ret) != 0) {
+                               && WEXITSTATUS(ret) != 0) {
                        ERR("Unable to remove module %s",
                                kernel_modules_list[i].name);
                } else {
@@ -1045,7 +1091,7 @@ static int mount_debugfs(char *path)
        int ret;
        char *type = "debugfs";
 
-       ret = mkdir_recursive(path, S_IRWXU | S_IRWXG);
+       ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
        if (ret < 0) {
                goto error;
        }
@@ -1152,7 +1198,16 @@ static int start_kernel_trace(struct ltt_kernel_session *session)
        int ret = 0;
 
        if (session->kconsumer_fds_sent == 0) {
-               ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
+               /*
+                * Assign default kernel consumer if no consumer assigned to the kernel
+                * session. At this point, it's NOT suppose to be 0 but this is an extra
+                * security check.
+                */
+               if (session->consumer_fd == 0) {
+                       session->consumer_fd = kconsumerd_cmd_sock;
+               }
+
+               ret = send_kconsumerd_fds(session);
                if (ret < 0) {
                        ERR("Send kconsumerd fds failed");
                        ret = LTTCOMM_KERN_CONSUMER_FAIL;
@@ -1226,9 +1281,22 @@ static int create_kernel_session(struct ltt_session *session)
                goto error;
        }
 
-       ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG );
+       /* Set kernel consumer socket fd */
+       if (kconsumerd_cmd_sock) {
+               session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
+       }
+
+       ret = asprintf(&session->kernel_session->trace_path, "%s/kernel",
+                       session->path);
+       if (ret < 0) {
+               perror("asprintf kernel traces path");
+               goto error;
+       }
+
+       ret = mkdir_recursive(session->kernel_session->trace_path,
+                       S_IRWXU | S_IRWXG, geteuid(), allowed_group());
        if (ret < 0) {
-               if (ret != EEXIST) {
+               if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
                        goto error;
                }
@@ -1257,7 +1325,9 @@ static void list_lttng_sessions(struct lttng_session *sessions)
         */
        cds_list_for_each_entry(session, &session_list_ptr->head, list) {
                strncpy(sessions[i].path, session->path, PATH_MAX);
+               sessions[i].path[PATH_MAX - 1] = '\0';
                strncpy(sessions[i].name, session->name, NAME_MAX);
+               sessions[i].name[NAME_MAX - 1] = '\0';
                i++;
        }
 }
@@ -1303,6 +1373,7 @@ static void list_lttng_events(struct ltt_kernel_channel *kchan,
        /* Kernel channels */
        cds_list_for_each_entry(event, &kchan->events_list.head , list) {
                strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
+               events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                events[i].enabled = event->enabled;
                switch (event->event->instrumentation) {
                        case LTTNG_KERNEL_TRACEPOINT:
@@ -1337,7 +1408,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
        DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
 
-       /* Listing commands don't need a session */
+       /*
+        * Commands that DO NOT need a session.
+        */
        switch (cmd_ctx->lsm->cmd_type) {
        case LTTNG_CREATE_SESSION:
        case LTTNG_LIST_SESSIONS:
@@ -1378,10 +1451,10 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                /* Need a session for kernel command */
                switch (cmd_ctx->lsm->cmd_type) {
+               case LTTNG_CALIBRATE:
                case LTTNG_CREATE_SESSION:
                case LTTNG_LIST_SESSIONS:
                case LTTNG_LIST_TRACEPOINTS:
-               case LTTNG_CALIBRATE:
                        break;
                default:
                        if (cmd_ctx->session->kernel_session == NULL) {
@@ -1392,7 +1465,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                }
 
                                /* Start the kernel consumer daemon */
-                               if (kconsumerd_pid == 0) {
+
+                               if (kconsumerd_pid == 0 &&
+                                               cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
                                        ret = start_kconsumerd();
                                        if (ret < 0) {
                                                goto error;
@@ -1426,6 +1501,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        strncpy(kctx.u.perf_counter.name,
                                        cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
                                        LTTNG_SYMBOL_NAME_LEN);
+                       kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
 
                        /* Add kernel context to kernel tracer. See context.c */
                        ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
@@ -1585,7 +1661,8 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                DBG("Creating kernel channel");
 
                                ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                               &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path);
+                                               &cmd_ctx->lsm->u.channel.chan,
+                                               cmd_ctx->session->kernel_session->trace_path);
                                if (ret < 0) {
                                        ret = LTTCOMM_KERN_CHAN_FAIL;
                                        goto error;
@@ -1648,7 +1725,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                        }
 
                                        ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->path);
+                                                       chan, cmd_ctx->session->kernel_session->trace_path);
                                        if (ret < 0) {
                                                ret = LTTCOMM_KERN_CHAN_FAIL;
                                                goto error;
@@ -1720,7 +1797,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                        }
 
                                        ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->path);
+                                                       chan, cmd_ctx->session->kernel_session->trace_path);
                                        if (ret < 0) {
                                                ret = LTTCOMM_KERN_CHAN_FAIL;
                                                goto error;
@@ -1825,7 +1902,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        if (cmd_ctx->session->kernel_session->metadata == NULL) {
                                DBG("Open kernel metadata");
                                ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
-                                               cmd_ctx->session->path);
+                                               cmd_ctx->session->kernel_session->trace_path);
                                if (ret < 0) {
                                        ret = LTTCOMM_KERN_META_FAIL;
                                        goto error;
@@ -2044,7 +2121,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        goto setup_error;
                }
 
-               DBG("Listing events (%ld events)", nb_event);
+               DBG("Listing events (%zu events)", nb_event);
 
                list_lttng_events(kchan,
                                (struct lttng_event *)(cmd_ctx->llm->payload));
@@ -2077,7 +2154,6 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                ret = LTTCOMM_OK;
                break;
        }
-
        case LTTNG_CALIBRATE:
        {
                /* Setup lttng message with no payload */
@@ -2107,6 +2183,43 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                ret = LTTCOMM_OK;
                break;
        }
+       case LTTNG_REGISTER_CONSUMER:
+       {
+               int sock;
+
+               /* Setup lttng message with no payload */
+               ret = setup_lttng_msg(cmd_ctx, 0);
+               if (ret < 0) {
+                       goto setup_error;
+               }
+
+               switch (cmd_ctx->lsm->domain.type) {
+               case LTTNG_DOMAIN_KERNEL:
+                       {
+                               /* Can't register a consumer if there is already one */
+                               if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
+                                       ret = LTTCOMM_CONNECT_FAIL;
+                                       goto error;
+                               }
+
+                               sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
+                               if (sock < 0) {
+                                       ret = LTTCOMM_CONNECT_FAIL;
+                                       goto error;
+                               }
+
+                               cmd_ctx->session->kernel_session->consumer_fd = sock;
+                               break;
+                       }
+               default:
+                       /* TODO: Userspace tracing */
+                       ret = LTTCOMM_NOT_IMPLEMENTED;
+                       goto error;
+               }
+
+               ret = LTTCOMM_OK;
+               break;
+       }
 
        default:
                /* Undefined command */
@@ -2418,18 +2531,19 @@ end:
 }
 
 /*
- * Check if the global socket is available.  If yes, error is returned.
+ * Check if the global socket is available, and if a daemon is answering
+ * at the other side. If yes, error is returned.
  */
 static int check_existing_daemon(void)
 {
-       int ret;
-
-       ret = access(client_unix_sock_path, F_OK);
-       if (ret == 0) {
-               ret = access(apps_unix_sock_path, F_OK);
-       }
-
-       return ret;
+       if (access(client_unix_sock_path, F_OK) < 0 &&
+           access(apps_unix_sock_path, F_OK) < 0)
+               return 0;
+       /* Is there anybody out there ? */
+       if (lttng_session_daemon_alive())
+               return -EEXIST;
+       else
+               return 0;
 }
 
 /*
@@ -2441,14 +2555,10 @@ static int check_existing_daemon(void)
 static int set_permissions(void)
 {
        int ret;
-       struct group *grp;
-
-       /* Decide which group name to use */
-       (opt_tracing_group != NULL) ?
-               (grp = getgrnam(opt_tracing_group)) :
-               (grp = getgrnam(default_tracing_group));
+       gid_t gid;
 
-       if (grp == NULL) {
+       gid = allowed_group();
+       if (gid < 0) {
                if (is_root) {
                        WARN("No tracing group detected");
                        ret = 0;
@@ -2460,21 +2570,21 @@ static int set_permissions(void)
        }
 
        /* Set lttng run dir */
-       ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
+       ret = chown(LTTNG_RUNDIR, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on " LTTNG_RUNDIR);
                perror("chown");
        }
 
        /* lttng client socket path */
-       ret = chown(client_unix_sock_path, 0, grp->gr_gid);
+       ret = chown(client_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", client_unix_sock_path);
                perror("chown");
        }
 
        /* kconsumerd error socket path */
-       ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
+       ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
                perror("chown");
@@ -2714,11 +2824,9 @@ int main(int argc, char **argv)
        DBG("Application socket path %s", apps_unix_sock_path);
 
        /*
-        * See if daemon already exist. If any of the two socket needed by the
-        * daemon are present, this test fails. However, if the daemon is killed
-        * with a SIGKILL, those unix socket must be unlinked by hand.
+        * See if daemon already exist.
         */
-       if ((ret = check_existing_daemon()) == 0) {
+       if ((ret = check_existing_daemon()) < 0) {
                ERR("Already running daemon.\n");
                /*
                 * We do not goto exit because we must not cleanup()
This page took 0.030014 seconds and 4 git commands to generate.