lttng-sessiond: do not call ustctl_register_done()
[lttng-tools.git] / lttng-sessiond / main.c
index 822c1df7de1d86b5fefcc42d2f5a99e5564202d8..91db9677abdfd24a6d781db11bd47951f95fbed9 100644 (file)
 #include <config.h>
 
 #include <lttng-consumerd.h>
+#include <lttng-ht.h>
 #include <lttng-sessiond-comm.h>
 #include <lttng/lttng-consumer.h>
 
 #include <lttngerr.h>
+#include <runas.h>
 
 #include "channel.h"
 #include "compat/poll.h"
 #include "context.h"
 #include "event.h"
 #include "futex.h"
-#include "hashtable.h"
 #include "kernel.h"
 #include "lttng-sessiond.h"
 #include "shm.h"
@@ -92,8 +93,10 @@ const char *progname;
 const char *opt_tracing_group;
 static int opt_sig_parent;
 static int opt_daemon;
+static int opt_no_kernel;
 static int is_root;                    /* Set to 1 if the daemon is running as root */
 static pid_t ppid;          /* Parent PID for --sig-parent option */
+static char *rundir;
 
 /* Consumer daemon specific control data */
 static struct consumer_data kconsumer_data = {
@@ -423,19 +426,18 @@ static void cleanup(void)
 
        DBG("Cleaning up");
 
-       if (is_root) {
-               DBG("Removing %s directory", LTTNG_RUNDIR);
-               ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
-               if (ret < 0) {
-                       ERR("asprintf failed. Something is really wrong!");
-               }
+       DBG("Removing %s directory", rundir);
+       ret = asprintf(&cmd, "rm -rf %s", rundir);
+       if (ret < 0) {
+               ERR("asprintf failed. Something is really wrong!");
+       }
 
-               /* Remove lttng run directory */
-               ret = system(cmd);
-               if (ret < 0) {
-                       ERR("Unable to clean " LTTNG_RUNDIR);
-               }
+       /* Remove lttng run directory */
+       ret = system(cmd);
+       if (ret < 0) {
+               ERR("Unable to clean %s", rundir);
        }
+       free(cmd);
 
        DBG("Cleaning up all session");
 
@@ -457,10 +459,9 @@ static void cleanup(void)
 
        pthread_mutex_destroy(&kconsumer_data.pid_mutex);
 
-       DBG("Closing kernel fd");
-       close(kernel_tracer_fd);
-
-       if (is_root) {
+       if (is_root && !opt_no_kernel) {
+               DBG2("Closing kernel fd");
+               close(kernel_tracer_fd);
                DBG("Unloading kernel modules");
                modprobe_remove_kernel_modules();
        }
@@ -469,7 +470,7 @@ static void cleanup(void)
        close(thread_quit_pipe[1]);
 
        /* <fun> */
-       MSG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
+       DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
                        "Matthew, BEET driven development works!%c[%dm",
                        27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
        /* </fun> */
@@ -512,7 +513,8 @@ 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)
+               int sock, struct ltt_kernel_channel *channel,
+               uid_t uid, gid_t gid)
 {
        int ret;
        struct ltt_kernel_stream *stream;
@@ -544,6 +546,8 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
                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);
@@ -605,6 +609,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
                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);
@@ -621,7 +627,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
        }
 
        cds_list_for_each_entry(chan, &session->channel_list.head, list) {
-               ret = send_kconsumer_channel_streams(consumer_data, sock, chan);
+               ret = send_kconsumer_channel_streams(consumer_data, sock, chan,
+                               session->uid, session->gid);
                if (ret < 0) {
                        goto error;
                }
@@ -777,7 +784,8 @@ static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
                                 */
                                if (session->kernel_session->consumer_fds_sent == 1) {
                                        ret = send_kconsumer_channel_streams(consumer_data,
-                                                       session->kernel_session->consumer_fd, channel);
+                                                       session->kernel_session->consumer_fd, channel,
+                                                       session->uid, session->gid);
                                        if (ret < 0) {
                                                goto error;
                                        }
@@ -1149,7 +1157,7 @@ static void *thread_manage_apps(void *data)
                                         */
                                        update_ust_app(ust_cmd.sock);
 
-                                       ret = ustctl_register_done(ust_cmd.sock);
+                                       ret = ust_app_register_done(ust_cmd.sock);
                                        if (ret < 0) {
                                                /*
                                                 * If the registration is not possible, we simply
@@ -1580,7 +1588,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                }
                        }
                        DBG("Using 64-bit UST consumer at: %s",  consumerd64_bin);
-                       ret = execl(consumerd64_bin, verbosity, "-u",
+                       ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
                                        "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
                                        "--consumerd-err-sock", consumer_data->err_unix_sock_path,
                                        NULL);
@@ -1624,7 +1632,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                }
                        }
                        DBG("Using 32-bit UST consumer at: %s",  consumerd32_bin);
-                       ret = execl(consumerd32_bin, verbosity, "-u",
+                       ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
                                        "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
                                        "--consumerd-err-sock", consumer_data->err_unix_sock_path,
                                        NULL);
@@ -1737,7 +1745,7 @@ static int mount_debugfs(char *path)
        int ret;
        char *type = "debugfs";
 
-       ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
+       ret = mkdir_recursive_run_as(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
        if (ret < 0) {
                PERROR("Cannot create debugfs path");
                goto error;
@@ -1873,9 +1881,8 @@ error:
 static int create_ust_session(struct ltt_session *session,
                struct lttng_domain *domain)
 {
-       int ret;
-       unsigned int uid;
        struct ltt_ust_session *lus = NULL;
+       int ret;
 
        switch (domain->type) {
        case LTTNG_DOMAIN_UST:
@@ -1887,15 +1894,14 @@ static int create_ust_session(struct ltt_session *session,
 
        DBG("Creating UST session");
 
-       uid = session->uid;
-       lus = trace_ust_create_session(session->path, uid, domain);
+       lus = trace_ust_create_session(session->path, session->id, domain);
        if (lus == NULL) {
                ret = LTTCOMM_UST_SESS_FAIL;
                goto error;
        }
 
-       ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
-                       geteuid(), allowed_group());
+       ret = mkdir_recursive_run_as(lus->pathname, S_IRWXU | S_IRWXG,
+                       session->uid, session->gid);
        if (ret < 0) {
                if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
@@ -1913,6 +1919,8 @@ static int create_ust_session(struct ltt_session *session,
                ERR("Unknown UST domain on create session %d", domain->type);
                goto error;
        }
+       lus->uid = session->uid;
+       lus->gid = session->gid;
        session->ust_session = lus;
 
        return LTTCOMM_OK;
@@ -1942,19 +1950,55 @@ static int create_kernel_session(struct ltt_session *session)
                session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
        }
 
-       ret = mkdir_recursive(session->kernel_session->trace_path,
-                       S_IRWXU | S_IRWXG, geteuid(), allowed_group());
+       ret = mkdir_recursive_run_as(session->kernel_session->trace_path,
+                       S_IRWXU | S_IRWXG, session->uid, session->gid);
        if (ret < 0) {
                if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
                        goto error;
                }
        }
+       session->kernel_session->uid = session->uid;
+       session->kernel_session->gid = session->gid;
 
 error:
        return ret;
 }
 
+/*
+ * Check if the UID or GID match the session. Root user has access to
+ * all sessions.
+ */
+static int session_access_ok(struct ltt_session *session,
+        uid_t uid, gid_t gid)
+{
+       if (uid != session->uid && gid != session->gid
+                       && uid != 0) {
+               return 0;
+       } else {
+               return 1;
+       }
+}
+
+static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
+{
+       unsigned int i = 0;
+       struct ltt_session *session;
+
+       DBG("Counting number of available session for UID %d GID %d",
+               uid, gid);
+       cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+               /*
+                * Only list the sessions the user can control.
+                */
+               if (!session_access_ok(session, uid, gid)) {
+                       continue;
+               }
+               i++;
+       }
+       return i;
+}
+
 /*
  * Using the session list, filled a lttng_session array to send back to the
  * client for session listing.
@@ -1962,17 +2006,25 @@ error:
  * The session list lock MUST be acquired before calling this function. Use
  * session_lock_list() and session_unlock_list().
  */
-static void list_lttng_sessions(struct lttng_session *sessions)
+static void list_lttng_sessions(struct lttng_session *sessions,
+               uid_t uid, gid_t gid)
 {
-       int i = 0;
+       unsigned int i = 0;
        struct ltt_session *session;
 
-       DBG("Getting all available session");
+       DBG("Getting all available session for UID %d GID %d",
+               uid, gid);
        /*
         * Iterate over session list and append data after the control struct in
         * the buffer.
         */
        cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+               /*
+                * Only list the sessions the user can control.
+                */
+               if (!session_access_ok(session, uid, gid)) {
+                       continue;
+               }
                strncpy(sessions[i].path, session->path, PATH_MAX);
                sessions[i].path[PATH_MAX - 1] = '\0';
                strncpy(sessions[i].name, session->name, NAME_MAX);
@@ -2008,11 +2060,11 @@ static void list_lttng_channels(int domain, struct ltt_session *session,
                break;
        case LTTNG_DOMAIN_UST:
        {
-               struct cds_lfht_iter iter;
+               struct lttng_ht_iter iter;
                struct ltt_ust_channel *uchan;
 
-               cds_lfht_for_each_entry(session->ust_session->domain_global.channels,
-                               &iter, uchan, node) {
+               cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
+                               &iter.iter, uchan, node.node) {
                        strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
                        channels[i].attr.overwrite = uchan->attr.overwrite;
                        channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
@@ -2045,8 +2097,8 @@ static int list_lttng_ust_global_events(char *channel_name,
 {
        int i = 0, ret = 0;
        unsigned int nb_event = 0;
-       struct cds_lfht_iter iter;
-       struct cds_lfht_node *node;
+       struct lttng_ht_iter iter;
+       struct lttng_ht_node_str *node;
        struct ltt_ust_channel *uchan;
        struct ltt_ust_event *uevent;
        struct lttng_event *tmp;
@@ -2055,16 +2107,16 @@ static int list_lttng_ust_global_events(char *channel_name,
 
        rcu_read_lock();
 
-       node = hashtable_lookup(ust_global->channels, (void *) channel_name,
-                       strlen(channel_name), &iter);
+       lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
+       node = lttng_ht_iter_get_node_str(&iter);
        if (node == NULL) {
                ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
                goto error;
        }
 
-       uchan = caa_container_of(node, struct ltt_ust_channel, node);
+       uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
 
-       nb_event += hashtable_get_count(uchan->events);
+       nb_event += lttng_ht_get_count(uchan->events);
 
        if (nb_event == 0) {
                ret = nb_event;
@@ -2079,7 +2131,7 @@ static int list_lttng_ust_global_events(char *channel_name,
                goto error;
        }
 
-       cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
+       cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
                strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
                tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                tmp[i].enabled = uevent->enabled;
@@ -2205,29 +2257,20 @@ static int cmd_disable_channel(struct ltt_session *session,
        case LTTNG_DOMAIN_UST:
        {
                struct ltt_ust_channel *uchan;
+               struct lttng_ht *chan_ht;
 
-               /* Get channel in global UST domain HT */
-               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
-                               channel_name);
+               chan_ht = usess->domain_global.channels;
+
+               uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
                if (uchan == NULL) {
                        ret = LTTCOMM_UST_CHAN_NOT_FOUND;
                        goto error;
                }
 
-               /* Already disabled */
-               if (!uchan->enabled) {
-                       DBG2("UST channel %s already disabled", channel_name);
-                       break;
-               }
-
-               ret = ust_app_disable_channel_glb(usess, uchan);
-               if (ret < 0) {
-                       ret = LTTCOMM_UST_DISABLE_FAIL;
+               ret = channel_ust_disable(usess, domain, uchan);
+               if (ret != LTTCOMM_OK) {
                        goto error;
                }
-
-               uchan->enabled = 0;
-
                break;
        }
        case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
@@ -2246,40 +2289,6 @@ error:
        return ret;
 }
 
-/*
- * Copy channel from attributes and set it in the application channel list.
- */
-/*
-static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
-               struct lttng_channel *attr, struct ust_app *app)
-{
-       int ret;
-       struct ltt_ust_channel *uchan, *new_chan;
-
-       uchan = trace_ust_get_channel_by_key(usess->channels, attr->name);
-       if (uchan == NULL) {
-               ret = LTTCOMM_FATAL;
-               goto error;
-       }
-
-       new_chan = trace_ust_create_channel(attr, usess->path);
-       if (new_chan == NULL) {
-               PERROR("malloc ltt_ust_channel");
-               ret = LTTCOMM_FATAL;
-               goto error;
-       }
-
-       ret = channel_ust_copy(new_chan, uchan);
-       if (ret < 0) {
-               ret = LTTCOMM_FATAL;
-               goto error;
-       }
-
-error:
-       return ret;
-}
-*/
-
 /*
  * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
  */
@@ -2288,6 +2297,7 @@ static int cmd_enable_channel(struct ltt_session *session,
 {
        int ret;
        struct ltt_ust_session *usess = session->ust_session;
+       struct lttng_ht *chan_ht;
 
        DBG("Enabling channel %s for session %s", attr->name, session->name);
 
@@ -2316,49 +2326,14 @@ static int cmd_enable_channel(struct ltt_session *session,
        {
                struct ltt_ust_channel *uchan;
 
-               DBG2("Enabling channel for LTTNG_DOMAIN_UST");
+               chan_ht = usess->domain_global.channels;
 
-               /* Get channel in global UST domain HT */
-               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
-                               attr->name);
+               uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
                if (uchan == NULL) {
-                       uchan = trace_ust_create_channel(attr, usess->pathname);
-                       if (uchan == NULL) {
-                               ret = LTTCOMM_UST_CHAN_FAIL;
-                               goto error;
-                       }
-
-                       /* Add channel to all registered applications */
-                       ret = ust_app_create_channel_glb(usess, uchan);
-                       if (ret != 0) {
-                               ret = LTTCOMM_UST_CHAN_FAIL;
-                               goto error;
-                       }
-
-                       rcu_read_lock();
-                       hashtable_add_unique(usess->domain_global.channels, &uchan->node);
-                       rcu_read_unlock();
-
-                       DBG2("UST channel %s added to global domain HT", attr->name);
+                       ret = channel_ust_create(usess, domain, attr);
                } else {
-                       /* If already enabled, everything is OK */
-                       if (uchan->enabled) {
-                               break;
-                       }
-
-                       ret = ust_app_enable_channel_glb(usess, uchan);
-                       if (ret < 0) {
-                               if (ret != -EEXIST) {
-                                       ret = LTTCOMM_UST_CHAN_ENABLE_FAIL;
-                                       goto error;
-                               } else {
-                                       ret = LTTCOMM_OK;
-                               }
-                       }
+                       ret = channel_ust_enable(usess, domain, uchan);
                }
-
-               uchan->enabled = 1;
-
                break;
        }
        case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
@@ -2371,8 +2346,6 @@ static int cmd_enable_channel(struct ltt_session *session,
                goto error;
        }
 
-       ret = LTTCOMM_OK;
-
 error:
        return ret;
 }
@@ -2629,8 +2602,7 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
                        snprintf(attr->name, NAME_MAX, "%s", channel_name);
                        attr->name[NAME_MAX - 1] = '\0';
 
-                       /* Use the internal command enable channel */
-                       ret = cmd_enable_channel(session, domain, attr);
+                       ret = channel_ust_create(usess, domain, attr);
                        if (ret != LTTCOMM_OK) {
                                free(attr);
                                goto error;
@@ -2648,7 +2620,6 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
                }
 
                /* At this point, the session and channel exist on the tracer */
-
                ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
                if (ret != LTTCOMM_OK) {
                        goto error;
@@ -2751,7 +2722,7 @@ static int cmd_enable_event_all(struct ltt_session *session, int domain,
                        attr->name[NAME_MAX - 1] = '\0';
 
                        /* Use the internal command enable channel */
-                       ret = cmd_enable_channel(session, domain, attr);
+                       ret = channel_ust_create(usess, domain, attr);
                        if (ret != LTTCOMM_OK) {
                                free(attr);
                                goto error;
@@ -2852,8 +2823,11 @@ static int cmd_start_trace(struct ltt_session *session)
        ksession = session->kernel_session;
        usess = session->ust_session;
 
-       if (session->enabled)
-               return LTTCOMM_UST_START_FAIL;
+       if (session->enabled) {
+               ret = LTTCOMM_UST_START_FAIL;
+               goto error;
+       }
+
        session->enabled = 1;
 
        /* Kernel tracing */
@@ -2941,8 +2915,11 @@ static int cmd_stop_trace(struct ltt_session *session)
        ksession = session->kernel_session;
        usess = session->ust_session;
 
-       if (!session->enabled)
-               return LTTCOMM_UST_START_FAIL;
+       if (!session->enabled) {
+               ret = LTTCOMM_UST_START_FAIL;
+               goto error;
+       }
+
        session->enabled = 0;
 
        /* Kernel tracer */
@@ -2990,11 +2967,11 @@ error:
 /*
  * Command LTTNG_CREATE_SESSION processed by the client thread.
  */
-static int cmd_create_session(char *name, char *path)
+static int cmd_create_session(char *name, char *path, struct ucred *creds)
 {
        int ret;
 
-       ret = session_create(name, path);
+       ret = session_create(name, path, creds->uid, creds->gid);
        if (ret != LTTCOMM_OK) {
                goto error;
        }
@@ -3074,7 +3051,7 @@ static int cmd_register_consumer(struct ltt_session *session, int domain,
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
                /* Can't register a consumer if there is already one */
-               if (session->kernel_session->consumer_fd != 0) {
+               if (session->kernel_session->consumer_fds_sent != 0) {
                        ret = LTTCOMM_KERN_CONSUMER_FAIL;
                        goto error;
                }
@@ -3158,7 +3135,7 @@ static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                break;
        case LTTNG_DOMAIN_UST:
                if (session->ust_session != NULL) {
-                       nb_chan = hashtable_get_count(
+                       nb_chan = lttng_ht_get_count(
                                        session->ust_session->domain_global.channels);
                }
                DBG3("Number of UST global channels %zd", nb_chan);
@@ -3236,6 +3213,11 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
        DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
 
+       if (opt_no_kernel && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
+               ret = LTTCOMM_KERN_NA;
+               goto error;
+       }
+
        /*
         * Check for command that don't needs to allocate a returned payload. We do
         * this here so we don't have to make the call for no payload at each
@@ -3375,6 +3357,18 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                break;
        }
 
+       /*
+        * Check that the UID or GID match that of the tracing session.
+        * The root user can interact with all sessions.
+        */
+       if (need_tracing_session) {
+               if (!session_access_ok(cmd_ctx->session,
+                               cmd_ctx->creds.uid, cmd_ctx->creds.gid)) {
+                       ret = LTTCOMM_EPERM;
+                       goto error;
+               }
+       }
+
        /* Process by command type */
        switch (cmd_ctx->lsm->cmd_type) {
        case LTTNG_ADD_CONTEXT:
@@ -3472,7 +3466,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        case LTTNG_CREATE_SESSION:
        {
                ret = cmd_create_session(cmd_ctx->lsm->session.name,
-                               cmd_ctx->lsm->session.path);
+                               cmd_ctx->lsm->session.path, &cmd_ctx->creds);
                break;
        }
        case LTTNG_DESTROY_SESSION:
@@ -3560,23 +3554,24 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        }
        case LTTNG_LIST_SESSIONS:
        {
-               session_lock_list();
+               unsigned int nr_sessions;
 
-               if (session_list_ptr->count == 0) {
+               session_lock_list();
+               nr_sessions = lttng_sessions_count(cmd_ctx->creds.uid, cmd_ctx->creds.gid);
+               if (nr_sessions == 0) {
                        ret = LTTCOMM_NO_SESSION;
                        session_unlock_list();
                        goto error;
                }
-
-               ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
-                               session_list_ptr->count);
+               ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
                if (ret < 0) {
                        session_unlock_list();
                        goto setup_error;
                }
 
                /* Filled the session array */
-               list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
+               list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
+                       cmd_ctx->creds.uid, cmd_ctx->creds.gid);
 
                session_unlock_list();
 
@@ -3697,6 +3692,12 @@ static void *thread_manage_clients(void *data)
                        goto error;
                }
 
+               /* Set socket option for credentials retrieval */
+               ret = lttcomm_setsockopt_creds_unix_sock(sock);
+               if (ret < 0) {
+                       goto error;
+               }
+
                /* Allocate context command to process the client request */
                cmd_ctx = zmalloc(sizeof(struct command_ctx));
                if (cmd_ctx == NULL) {
@@ -3720,8 +3721,8 @@ static void *thread_manage_clients(void *data)
                 * the client.
                 */
                DBG("Receiving data from client ...");
-               ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm,
-                               sizeof(struct lttcomm_session_msg));
+               ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
+                               sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
                if (ret <= 0) {
                        DBG("Nothing recv() from client... continuing");
                        close(sock);
@@ -3759,10 +3760,10 @@ static void *thread_manage_clients(void *data)
                        ERR("Failed to send data back to client");
                }
 
-               clean_command_ctx(&cmd_ctx);
-
                /* End of transmission */
                close(sock);
+
+               clean_command_ctx(&cmd_ctx);
        }
 
 error:
@@ -3805,6 +3806,7 @@ static void usage(void)
        fprintf(stderr, "  -q, --quiet                        No output at all.\n");
        fprintf(stderr, "  -v, --verbose                      Verbose mode. Activate DBG() macro.\n");
        fprintf(stderr, "      --verbose-consumer             Verbose mode for consumer. Activate DBG() macro.\n");
+       fprintf(stderr, "      --no-kernel                    Disable kernel tracer\n");
 }
 
 /*
@@ -3835,12 +3837,13 @@ static int parse_args(int argc, char **argv)
                { "quiet", 0, 0, 'q' },
                { "verbose", 0, 0, 'v' },
                { "verbose-consumer", 0, 0, 'Z' },
+               { "no-kernel", 0, 0, 'N' },
                { NULL, 0, 0, 0 }
        };
 
        while (1) {
                int option_index = 0;
-               c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:C:E:D:F:Z:u:t",
+               c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
                                long_options, &option_index);
                if (c == -1) {
                        break;
@@ -3892,6 +3895,9 @@ static int parse_args(int argc, char **argv)
                case 'G':
                        snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
                        break;
+               case 'N':
+                       opt_no_kernel = 1;
+                       break;
                case 'q':
                        opt_quiet = 1;
                        break;
@@ -3999,27 +4005,22 @@ static int check_existing_daemon(void)
  * Race window between mkdir and chown is OK because we are going from more
  * permissive (root.root) to les permissive (root.tracing).
  */
-static int set_permissions(void)
+static int set_permissions(char *rundir)
 {
        int ret;
        gid_t gid;
 
        gid = allowed_group();
        if (gid < 0) {
-               if (is_root) {
-                       WARN("No tracing group detected");
-                       ret = 0;
-               } else {
-                       ERR("Missing tracing group. Aborting execution.");
-                       ret = -1;
-               }
+               WARN("No tracing group detected");
+               ret = 0;
                goto end;
        }
 
        /* Set lttng run dir */
-       ret = chown(LTTNG_RUNDIR, 0, gid);
+       ret = chown(rundir, 0, gid);
        if (ret < 0) {
-               ERR("Unable to set group on " LTTNG_RUNDIR);
+               ERR("Unable to set group on %s", rundir);
                perror("chown");
        }
 
@@ -4076,14 +4077,16 @@ static int create_apps_cmd_pipe(void)
 /*
  * Create the lttng run directory needed for all global sockets and pipe.
  */
-static int create_lttng_rundir(void)
+static int create_lttng_rundir(const char *rundir)
 {
        int ret;
 
-       ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
+       DBG3("Creating LTTng run directory: %s", rundir);
+
+       ret = mkdir(rundir, S_IRWXU | S_IRWXG );
        if (ret < 0) {
                if (errno != EEXIST) {
-                       ERR("Unable to create " LTTNG_RUNDIR);
+                       ERR("Unable to create %s", rundir);
                        goto error;
                } else {
                        ret = 0;
@@ -4098,20 +4101,21 @@ error:
  * Setup sockets and directory needed by the kconsumerd communication with the
  * session daemon.
  */
-static int set_consumer_sockets(struct consumer_data *consumer_data)
+static int set_consumer_sockets(struct consumer_data *consumer_data,
+               const char *rundir)
 {
        int ret;
-       const char *path;
+       char path[PATH_MAX];
 
-       switch (consumer_data->type) {
+    switch (consumer_data->type) {
        case LTTNG_CONSUMER_KERNEL:
-               path = KCONSUMERD_PATH;
+               snprintf(path, PATH_MAX, KCONSUMERD_PATH, rundir);
                break;
        case LTTNG_CONSUMER64_UST:
-               path = USTCONSUMERD64_PATH;
+               snprintf(path, PATH_MAX, USTCONSUMERD64_PATH, rundir);
                break;
        case LTTNG_CONSUMER32_UST:
-               path = USTCONSUMERD32_PATH;
+               snprintf(path, PATH_MAX, USTCONSUMERD32_PATH, rundir);
                break;
        default:
                ERR("Consumer type unknown");
@@ -4119,6 +4123,8 @@ static int set_consumer_sockets(struct consumer_data *consumer_data)
                goto error;
        }
 
+       DBG2("Creating consumer directory: %s", path);
+
        ret = mkdir(path, S_IRWXU | S_IRWXG);
        if (ret < 0) {
                if (errno != EEXIST) {
@@ -4142,7 +4148,7 @@ static int set_consumer_sockets(struct consumer_data *consumer_data)
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
        if (ret < 0) {
                ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
-               perror("chmod");
+               PERROR("chmod");
                goto error;
        }
 
@@ -4269,7 +4275,10 @@ int main(int argc, char **argv)
        is_root = !getuid();
 
        if (is_root) {
-               ret = create_lttng_rundir();
+               rundir = strdup(LTTNG_RUNDIR);
+
+               /* Create global run dir with root access */
+               ret = create_lttng_rundir(rundir);
                if (ret < 0) {
                        goto error;
                }
@@ -4289,6 +4298,17 @@ int main(int argc, char **argv)
                        snprintf(wait_shm_path, PATH_MAX,
                                        DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
                }
+
+               /* Setup kernel consumerd path */
+               snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
+                               KCONSUMERD_ERR_SOCK_PATH, rundir);
+               snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
+                               KCONSUMERD_CMD_SOCK_PATH, rundir);
+
+               DBG2("Kernel consumer err path: %s",
+                               kconsumer_data.err_unix_sock_path);
+               DBG2("Kernel consumer cmd path: %s",
+                               kconsumer_data.cmd_unix_sock_path);
        } else {
                home_path = get_home_dir();
                if (home_path == NULL) {
@@ -4298,6 +4318,21 @@ int main(int argc, char **argv)
                        goto error;
                }
 
+               /*
+                * Create rundir from home path. This will create something like
+                * $HOME/.lttng
+                */
+               ret = asprintf(&rundir, LTTNG_HOME_RUNDIR, home_path);
+               if (ret < 0) {
+                       ret = -ENOMEM;
+                       goto error;
+               }
+
+               ret = create_lttng_rundir(rundir);
+               if (ret < 0) {
+                       goto error;
+               }
+
                if (strlen(apps_unix_sock_path) == 0) {
                        snprintf(apps_unix_sock_path, PATH_MAX,
                                        DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
@@ -4318,6 +4353,29 @@ int main(int argc, char **argv)
 
        DBG("Client socket path %s", client_unix_sock_path);
        DBG("Application socket path %s", apps_unix_sock_path);
+       DBG("LTTng run directory path: %s", rundir);
+
+       /* 32 bits consumerd path setup */
+       snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
+                       USTCONSUMERD32_ERR_SOCK_PATH, rundir);
+       snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
+                       USTCONSUMERD32_CMD_SOCK_PATH, rundir);
+
+       DBG2("UST consumer 32 bits err path: %s",
+                       ustconsumer32_data.err_unix_sock_path);
+       DBG2("UST consumer 32 bits cmd path: %s",
+                       ustconsumer32_data.cmd_unix_sock_path);
+
+       /* 64 bits consumerd path setup */
+       snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
+                       USTCONSUMERD64_ERR_SOCK_PATH, rundir);
+       snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
+                       USTCONSUMERD64_CMD_SOCK_PATH, rundir);
+
+       DBG2("UST consumer 64 bits err path: %s",
+                       ustconsumer64_data.err_unix_sock_path);
+       DBG2("UST consumer 64 bits cmd path: %s",
+                       ustconsumer64_data.cmd_unix_sock_path);
 
        /*
         * See if daemon already exist.
@@ -4340,28 +4398,30 @@ int main(int argc, char **argv)
         * kernel tracer.
         */
        if (is_root) {
-               ret = set_consumer_sockets(&kconsumer_data);
-               if (ret < 0) {
-                       goto exit;
-               }
-
-               ret = set_consumer_sockets(&ustconsumer64_data);
-               if (ret < 0) {
-                       goto exit;
-               }
-
-               ret = set_consumer_sockets(&ustconsumer32_data);
+               ret = set_consumer_sockets(&kconsumer_data, rundir);
                if (ret < 0) {
                        goto exit;
                }
 
                /* Setup kernel tracer */
-               init_kernel_tracer();
+               if (!opt_no_kernel) {
+                       init_kernel_tracer();
+               }
 
                /* Set ulimit for open files */
                set_ulimit();
        }
 
+       ret = set_consumer_sockets(&ustconsumer64_data, rundir);
+       if (ret < 0) {
+               goto exit;
+       }
+
+       ret = set_consumer_sockets(&ustconsumer32_data, rundir);
+       if (ret < 0) {
+               goto exit;
+       }
+
        if ((ret = set_signal_handler()) < 0) {
                goto exit;
        }
@@ -4372,7 +4432,7 @@ int main(int argc, char **argv)
        }
 
        /* Set credentials to socket */
-       if (is_root && ((ret = set_permissions()) < 0)) {
+       if (is_root && ((ret = set_permissions(rundir)) < 0)) {
                goto exit;
        }
 
@@ -4495,8 +4555,9 @@ exit:
        cleanup();
        rcu_thread_offline();
        rcu_unregister_thread();
-       if (!ret)
+       if (!ret) {
                exit(EXIT_SUCCESS);
+       }
 error:
        exit(EXIT_FAILURE);
 }
This page took 0.035647 seconds and 4 git commands to generate.