Fix incorrect thread cleanup on blocking recv()
[lttng-tools.git] / ltt-sessiond / main.c
index d104830667dd6cf9efc040584a5c18c4293381c9..abd4e087fd313c11fea10376393b32e9f6c29e5c 100644 (file)
 
 #include <urcu/list.h>         /* URCU list library (-lurcu) */
 #include <lttng/lttng.h>
+#include <lttng/lttng-kconsumerd.h>
+#include <lttng-sessiond-comm.h>
 
 #include "context.h"
-#include "liblttsessiondcomm.h"
 #include "ltt-sessiond.h"
 #include "lttngerr.h"
 #include "kernel-ctl.h"
 #include "ust-ctl.h"
 #include "session.h"
 #include "traceable-app.h"
-#include "lttng-kconsumerd.h"
+#include "ltt-kconsumerd.h"
 #include "utils.h"
 
 /* Const values */
@@ -119,9 +120,11 @@ static gid_t allowed_group(void)
 {
        struct group *grp;
 
-       grp = (opt_tracing_group != NULL) ?
-               (grp = getgrnam(opt_tracing_group)) :
-               (grp = getgrnam(default_tracing_group));
+       if (opt_tracing_group) {
+               grp = getgrnam(opt_tracing_group);
+       } else {
+               grp = getgrnam(default_tracing_group);
+       }
        if (!grp) {
                return -1;
        } else {
@@ -156,6 +159,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;
@@ -286,7 +298,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);
 
@@ -309,7 +323,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;
@@ -322,7 +336,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;
@@ -330,14 +344,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;
@@ -345,7 +366,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;
                }
@@ -544,6 +565,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");
@@ -551,12 +578,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;
                                        }
@@ -724,6 +753,25 @@ static void *thread_manage_kconsumerd(void *data)
                goto error;
        }
 
+       /* Kconsumerd err socket */
+       pollfd[1].fd = sock;
+       pollfd[1].events = POLLIN;
+
+       /* Inifinite blocking call, waiting for transmission */
+       ret = poll(pollfd, 2, -1);
+       if (ret < 0) {
+               perror("poll kconsumerd thread");
+               goto error;
+       }
+
+       /* Thread quit pipe has been closed. Killing thread. */
+       if (pollfd[0].revents == POLLNVAL) {
+               goto error;
+       } else if (pollfd[1].revents == POLLERR) {
+               ERR("Kconsumerd err socket second poll error");
+               goto error;
+       }
+
        /* Wait for any kconsumerd error */
        ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
        if (ret <= 0) {
@@ -881,6 +929,9 @@ error:
        return ret;
 }
 
+/*
+ * Join kernel consumer thread
+ */
 static int join_kconsumerd_thread(void)
 {
        void *status;
@@ -1003,7 +1054,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 {
@@ -1039,7 +1090,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 {
@@ -1167,7 +1218,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;
@@ -1200,7 +1260,8 @@ static int notify_kernel_pollfd(void)
 /*
  * Allocate a channel structure and fill it.
  */
-static struct lttng_channel *init_default_channel(char *name)
+static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
+                                               char *name)
 {
        struct lttng_channel *chan;
 
@@ -1212,18 +1273,29 @@ static struct lttng_channel *init_default_channel(char *name)
 
        if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
                perror("snprintf channel name");
-               return NULL;
+               goto error;
        }
 
        chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
-       chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
-       chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
        chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
        chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
-       chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
 
-error:
+       switch (domain_type) {
+       case LTTNG_DOMAIN_KERNEL:
+               chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
+               chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
+               chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+               break;
+               /* TODO: add UST */
+       default:
+               goto error;     /* Not implemented */
+       }
+
        return chan;
+
+error:
+       free(chan);
+       return NULL;
 }
 
 /*
@@ -1241,8 +1313,20 @@ static int create_kernel_session(struct ltt_session *session)
                goto error;
        }
 
-       ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG,
-                             geteuid(), allowed_group()); 
+       /* 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) {
                        ERR("Trace directory creation error");
@@ -1273,7 +1357,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++;
        }
 }
@@ -1319,6 +1405,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:
@@ -1353,7 +1440,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:
@@ -1394,10 +1483,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) {
@@ -1408,7 +1497,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;
@@ -1442,6 +1533,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,
@@ -1601,7 +1693,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;
@@ -1651,26 +1744,32 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                switch (cmd_ctx->lsm->domain.type) {
                case LTTNG_DOMAIN_KERNEL:
-                       do {
+                       kchan = get_kernel_channel_by_name(channel_name,
+                                       cmd_ctx->session->kernel_session);
+                       if (kchan == NULL) {
+                               DBG("Channel not found. Creating channel %s", channel_name);
+
+                               chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+                               if (chan == NULL) {
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
+                               }
+
+                               ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+                                               chan, cmd_ctx->session->kernel_session->trace_path);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_KERN_CHAN_FAIL;
+                                       goto error;
+                               }
                                kchan = get_kernel_channel_by_name(channel_name,
                                                cmd_ctx->session->kernel_session);
                                if (kchan == NULL) {
-                                       DBG("Channel not found. Creating channel %s", channel_name);
-
-                                       chan = init_default_channel(channel_name);
-                                       if (chan == NULL) {
-                                               ret = LTTCOMM_FATAL;
-                                               goto error;
-                                       }
-
-                                       ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->path);
-                                       if (ret < 0) {
-                                               ret = LTTCOMM_KERN_CHAN_FAIL;
-                                               goto error;
-                                       }
+                                       ERR("Channel %s not found after creation. Internal error, giving up.",
+                                               channel_name);
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
                                }
-                       } while (kchan == NULL);
+                       }
 
                        kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
                        if (kevent == NULL) {
@@ -1723,26 +1822,32 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                switch (cmd_ctx->lsm->domain.type) {
                case LTTNG_DOMAIN_KERNEL:
-                       do {
+                       kchan = get_kernel_channel_by_name(channel_name,
+                                       cmd_ctx->session->kernel_session);
+                       if (kchan == NULL) {
+                               DBG("Channel not found. Creating channel %s", channel_name);
+
+                               chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+                               if (chan == NULL) {
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
+                               }
+
+                               ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+                                               chan, cmd_ctx->session->kernel_session->trace_path);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_KERN_CHAN_FAIL;
+                                       goto error;
+                               }
                                kchan = get_kernel_channel_by_name(channel_name,
                                                cmd_ctx->session->kernel_session);
                                if (kchan == NULL) {
-                                       DBG("Channel not found. Creating channel %s", channel_name);
-
-                                       chan = init_default_channel(channel_name);
-                                       if (chan == NULL) {
-                                               ret = LTTCOMM_FATAL;
-                                               goto error;
-                                       }
-
-                                       ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->path);
-                                       if (ret < 0) {
-                                               ret = LTTCOMM_KERN_CHAN_FAIL;
-                                               goto error;
-                                       }
+                                       ERR("Channel %s not found after creation. Internal error, giving up.",
+                                               channel_name);
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
                                }
-                       } while (kchan == NULL);
+                       }
 
                        /* For each event in the kernel session */
                        cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
@@ -1841,7 +1946,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;
@@ -1875,16 +1980,16 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                }
                        }
 
-                       DBG("Start kernel tracing");
-                       ret = kernel_start_session(cmd_ctx->session->kernel_session);
+                       ret = start_kernel_trace(cmd_ctx->session->kernel_session);
                        if (ret < 0) {
-                               ERR("Kernel start session failed");
                                ret = LTTCOMM_KERN_START_FAIL;
                                goto error;
                        }
 
-                       ret = start_kernel_trace(cmd_ctx->session->kernel_session);
+                       DBG("Start kernel tracing");
+                       ret = kernel_start_session(cmd_ctx->session->kernel_session);
                        if (ret < 0) {
+                               ERR("Kernel start session failed");
                                ret = LTTCOMM_KERN_START_FAIL;
                                goto error;
                        }
@@ -2060,7 +2165,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));
@@ -2093,7 +2198,6 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                ret = LTTCOMM_OK;
                break;
        }
-
        case LTTNG_CALIBRATE:
        {
                /* Setup lttng message with no payload */
@@ -2123,6 +2227,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 */
@@ -2439,8 +2580,6 @@ end:
  */
 static int check_existing_daemon(void)
 {
-       int ret;
-
        if (access(client_unix_sock_path, F_OK) < 0 &&
            access(apps_unix_sock_path, F_OK) < 0)
                return 0;
@@ -2460,7 +2599,6 @@ static int check_existing_daemon(void)
 static int set_permissions(void)
 {
        int ret;
-       struct group *grp;
        gid_t gid;
 
        gid = allowed_group();
This page took 0.030195 seconds and 4 git commands to generate.