Remove stringify and add quotes directly in the configure
[lttng-tools.git] / lttng-sessiond / main.c
index 54dcbdff06f68965b8f2923160825e180bfe65c9..f7cd81027a43d103bed2499bc4e2bf8b257061be 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-ctl.h"
+#include "kernel.h"
 #include "lttng-sessiond.h"
 #include "shm.h"
 #include "ust-app.h"
 #include "ust-ctl.h"
 #include "utils.h"
 
+#define CONSUMERD_FILE "lttng-consumerd"
+
 struct consumer_data {
        enum lttng_consumer_type type;
 
@@ -90,15 +93,26 @@ 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 = {
        .type = LTTNG_CONSUMER_KERNEL,
+       .err_unix_sock_path = KCONSUMERD_ERR_SOCK_PATH,
+       .cmd_unix_sock_path = KCONSUMERD_CMD_SOCK_PATH,
+};
+static struct consumer_data ustconsumer64_data = {
+       .type = LTTNG_CONSUMER64_UST,
+       .err_unix_sock_path = USTCONSUMERD64_ERR_SOCK_PATH,
+       .cmd_unix_sock_path = USTCONSUMERD64_CMD_SOCK_PATH,
 };
-static struct consumer_data ustconsumer_data = {
-       .type = LTTNG_CONSUMER_UST,
+static struct consumer_data ustconsumer32_data = {
+       .type = LTTNG_CONSUMER32_UST,
+       .err_unix_sock_path = USTCONSUMERD32_ERR_SOCK_PATH,
+       .cmd_unix_sock_path = USTCONSUMERD32_CMD_SOCK_PATH,
 };
 
 static int dispatch_thread_exit;
@@ -157,7 +171,62 @@ static struct ust_cmd_queue ust_cmd_queue;
  */
 static struct ltt_session_list *session_list_ptr;
 
-int ust_consumer_fd;
+int ust_consumerd64_fd = -1;
+int ust_consumerd32_fd = -1;
+
+static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
+static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
+static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
+static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
+
+static
+void setup_consumerd_path(void)
+{
+       const char *bin, *libdir;
+
+       /*
+        * Allow INSTALL_BIN_PATH to be used as a target path for the
+        * native architecture size consumer if CONFIG_CONSUMER*_PATH
+        * has not been defined.
+        */
+#if (CAA_BITS_PER_LONG == 32)
+       if (!consumerd32_bin[0]) {
+               consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+       }
+       if (!consumerd32_libdir[0]) {
+               consumerd32_libdir = INSTALL_LIB_PATH;
+       }
+#elif (CAA_BITS_PER_LONG == 64)
+       if (!consumerd64_bin[0]) {
+               consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+       }
+       if (!consumerd64_libdir[0]) {
+               consumerd64_libdir = INSTALL_LIB_PATH;
+       }
+#else
+#error "Unknown bitness"
+#endif
+
+       /*
+        * runtime env. var. overrides the build default.
+        */
+       bin = getenv("LTTNG_CONSUMERD32_BIN");
+       if (bin) {
+               consumerd32_bin = bin;
+       }
+       bin = getenv("LTTNG_CONSUMERD64_BIN");
+       if (bin) {
+               consumerd64_bin = bin;
+       }
+       libdir = getenv("LTTNG_TOOLS_CONSUMERD32_LIBDIR");
+       if (libdir) {
+               consumerd32_libdir = libdir;
+       }
+       libdir = getenv("LTTNG_TOOLS_CONSUMERD64_LIBDIR");
+       if (libdir) {
+               consumerd64_libdir = libdir;
+       }
+}
 
 /*
  * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
@@ -282,21 +351,22 @@ error:
  */
 static void teardown_kernel_session(struct ltt_session *session)
 {
-       if (session->kernel_session != NULL) {
-               DBG("Tearing down kernel session");
+       if (!session->kernel_session) {
+               DBG3("No kernel session when tearingdown session");
+               return;
+       }
 
-               /*
-                * If a custom kernel consumer was registered, close the socket before
-                * tearing down the complete kernel session structure
-                */
-               if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
-                       lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
-               }
+       DBG("Tearing down kernel session");
 
-               trace_kernel_destroy_session(session->kernel_session);
-               /* Extra precaution */
-               session->kernel_session = NULL;
+       /*
+        * If a custom kernel consumer was registered, close the socket before
+        * tearing down the complete kernel session structure
+        */
+       if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
+               lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
        }
+
+       trace_kernel_destroy_session(session->kernel_session);
 }
 
 /*
@@ -307,12 +377,18 @@ static void teardown_ust_session(struct ltt_session *session)
 {
        int ret;
 
+       if (!session->ust_session) {
+               DBG3("No UST session when tearingdown session");
+               return;
+       }
+
        DBG("Tearing down UST session(s)");
 
        ret = ust_app_destroy_trace_all(session->ust_session);
        if (ret) {
                ERR("Error in ust_app_destroy_trace_all");
        }
+
        trace_ust_destroy_session(session->ust_session);
 }
 
@@ -346,19 +422,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");
 
@@ -380,10 +455,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();
        }
@@ -392,7 +466,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> */
@@ -435,7 +509,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;
@@ -467,6 +542,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);
@@ -528,6 +605,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);
@@ -544,7 +623,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;
                }
@@ -700,7 +780,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;
                                        }
@@ -1072,7 +1153,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
@@ -1431,7 +1512,9 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
 {
        int ret;
        pid_t pid;
+       const char *consumer_to_use;
        const char *verbosity;
+       struct stat st;
 
        DBG("Spawning consumerd");
 
@@ -1440,20 +1523,131 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                /*
                 * Exec consumerd.
                 */
-               if (opt_verbose > 1 || opt_verbose_consumer) {
+               if (opt_verbose_consumer) {
                        verbosity = "--verbose";
                } else {
                        verbosity = "--quiet";
                }
                switch (consumer_data->type) {
                case LTTNG_CONSUMER_KERNEL:
-                       execl(INSTALL_BIN_PATH "/lttng-consumerd",
-                                       "lttng-consumerd", verbosity, "-k", NULL);
+                       /*
+                        * Find out which consumerd to execute. We will
+                        * first try the 64-bit path, then the
+                        * sessiond's installation directory, and
+                        * fallback on the 32-bit one, 
+                        */
+                       DBG3("Looking for a kernel consumer at these locations:");
+                       DBG3("  1) %s", consumerd64_bin);
+                       DBG3("  2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
+                       DBG3("  3) %s", consumerd32_bin);
+                       if (stat(consumerd64_bin, &st) == 0) {
+                               DBG3("Found location #1");
+                               consumer_to_use = consumerd64_bin;
+                       } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
+                               DBG3("Found location #2");
+                               consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+                       } else if (stat(consumerd32_bin, &st) == 0) {
+                               DBG3("Found location #3");
+                               consumer_to_use = consumerd32_bin;
+                       } else {
+                               DBG("Could not find any valid consumerd executable");
+                               break;
+                       }
+                       DBG("Using kernel consumer at: %s",  consumer_to_use);
+                       execl(consumer_to_use,
+                               "lttng-consumerd", verbosity, "-k",
+                               "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
+                               "--consumerd-err-sock", consumer_data->err_unix_sock_path,
+                               NULL);
                        break;
-               case LTTNG_CONSUMER_UST:
-                       execl(INSTALL_BIN_PATH "/lttng-consumerd",
-                                       "lttng-consumerd", verbosity, "-u", NULL);
+               case LTTNG_CONSUMER64_UST:
+               {
+                       char *tmpnew = NULL;
+
+                       if (consumerd64_libdir[0] != '\0') {
+                               char *tmp;
+                               size_t tmplen;
+
+                               tmp = getenv("LD_LIBRARY_PATH");
+                               if (!tmp) {
+                                       tmp = "";
+                               }
+                               tmplen = strlen("LD_LIBRARY_PATH=")
+                                       + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
+                               tmpnew = zmalloc(tmplen + 1 /* \0 */);
+                               if (!tmpnew) {
+                                       ret = -ENOMEM;
+                                       goto error;
+                               }
+                               strcpy(tmpnew, "LD_LIBRARY_PATH=");
+                               strcat(tmpnew, consumerd64_libdir);
+                               if (tmp[0] != '\0') {
+                                       strcat(tmpnew, ":");
+                                       strcat(tmpnew, tmp);
+                               }
+                               ret = putenv(tmpnew);
+                               if (ret) {
+                                       ret = -errno;
+                                       goto error;
+                               }
+                       }
+                       DBG("Using 64-bit UST consumer at: %s",  consumerd64_bin);
+                       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);
+                       if (consumerd64_libdir[0] != '\0') {
+                               free(tmpnew);
+                       }
+                       if (ret) {
+                               goto error;
+                       }
                        break;
+               }
+               case LTTNG_CONSUMER32_UST:
+               {
+                       char *tmpnew = NULL;
+
+                       if (consumerd32_libdir[0] != '\0') {
+                               char *tmp;
+                               size_t tmplen;
+
+                               tmp = getenv("LD_LIBRARY_PATH");
+                               if (!tmp) {
+                                       tmp = "";
+                               }
+                               tmplen = strlen("LD_LIBRARY_PATH=")
+                                       + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
+                               tmpnew = zmalloc(tmplen + 1 /* \0 */);
+                               if (!tmpnew) {
+                                       ret = -ENOMEM;
+                                       goto error;
+                               }
+                               strcpy(tmpnew, "LD_LIBRARY_PATH=");
+                               strcat(tmpnew, consumerd32_libdir);
+                               if (tmp[0] != '\0') {
+                                       strcat(tmpnew, ":");
+                                       strcat(tmpnew, tmp);
+                               }
+                               ret = putenv(tmpnew);
+                               if (ret) {
+                                       ret = -errno;
+                                       goto error;
+                               }
+                       }
+                       DBG("Using 32-bit UST consumer at: %s",  consumerd32_bin);
+                       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);
+                       if (consumerd32_libdir[0] != '\0') {
+                               free(tmpnew);
+                       }
+                       if (ret) {
+                               goto error;
+                       }
+                       break;
+               }
                default:
                        perror("unknown consumer type");
                        exit(EXIT_FAILURE);
@@ -1468,6 +1662,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                perror("start consumer fork");
                ret = -errno;
        }
+error:
        return ret;
 }
 
@@ -1554,7 +1749,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;
@@ -1690,9 +1885,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:
@@ -1704,18 +1898,14 @@ static int create_ust_session(struct ltt_session *session,
 
        DBG("Creating UST session");
 
-       session_lock_list();
-       uid = session_list_ptr->count;
-       session_unlock_list();
-
-       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");
@@ -1733,6 +1923,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;
@@ -1762,19 +1954,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.
@@ -1782,17 +2010,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);
@@ -1828,11 +2064,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;
@@ -1841,7 +2077,14 @@ static void list_lttng_channels(int domain, struct ltt_session *session,
                                uchan->attr.switch_timer_interval;
                        channels[i].attr.read_timer_interval =
                                uchan->attr.read_timer_interval;
-                       channels[i].attr.output = uchan->attr.output;
+                       channels[i].enabled = uchan->enabled;
+                       switch (uchan->attr.output) {
+                       case LTTNG_UST_MMAP:
+                       default:
+                               channels[i].attr.output = LTTNG_EVENT_MMAP;
+                               break;
+                       }
+                       i++;
                }
                break;
        }
@@ -1858,7 +2101,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 lttng_ht_iter iter;
+       struct lttng_ht_node_str *node;
        struct ltt_ust_channel *uchan;
        struct ltt_ust_event *uevent;
        struct lttng_event *tmp;
@@ -1867,11 +2111,17 @@ static int list_lttng_ust_global_events(char *channel_name,
 
        rcu_read_lock();
 
-       /* Count events in all channels */
-       cds_lfht_for_each_entry(ust_global->channels, &iter, uchan, node) {
-               nb_event += hashtable_get_count(uchan->events);
+       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->node, struct ltt_ust_channel, node.node);
+
+       nb_event += lttng_ht_get_count(uchan->events);
+
        if (nb_event == 0) {
                ret = nb_event;
                goto error;
@@ -1885,24 +2135,25 @@ static int list_lttng_ust_global_events(char *channel_name,
                goto error;
        }
 
-       cds_lfht_for_each_entry(ust_global->channels, &iter, uchan, node) {
-               cds_lfht_for_each_entry(uchan->events, &iter, uevent, 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;
-                       switch (uevent->attr.instrumentation) {
-                       case LTTNG_UST_TRACEPOINT:
-                               tmp[i].type = LTTNG_EVENT_TRACEPOINT;
-                               break;
-                       case LTTNG_UST_PROBE:
-                               tmp[i].type = LTTNG_EVENT_PROBE;
-                               break;
-                       case LTTNG_UST_FUNCTION:
-                               tmp[i].type = LTTNG_EVENT_FUNCTION;
-                               break;
-                       }
-                       i++;
+       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;
+               switch (uevent->attr.instrumentation) {
+               case LTTNG_UST_TRACEPOINT:
+                       tmp[i].type = LTTNG_EVENT_TRACEPOINT;
+                       break;
+               case LTTNG_UST_PROBE:
+                       tmp[i].type = LTTNG_EVENT_PROBE;
+                       break;
+               case LTTNG_UST_FUNCTION:
+                       tmp[i].type = LTTNG_EVENT_FUNCTION;
+                       break;
+               case LTTNG_UST_TRACEPOINT_LOGLEVEL:
+                       tmp[i].type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
+                       break;
                }
+               i++;
        }
 
        ret = nb_event;
@@ -1991,76 +2242,70 @@ static int cmd_disable_channel(struct ltt_session *session,
                int domain, char *channel_name)
 {
        int ret;
+       struct ltt_ust_session *usess;
 
-       switch (domain) {
-               case LTTNG_DOMAIN_KERNEL:
-                       ret = channel_kernel_disable(session->kernel_session,
-                                       channel_name);
-                       if (ret != LTTCOMM_OK) {
-                               goto error;
-                       }
+       usess = session->ust_session;
 
-                       kernel_wait_quiescent(kernel_tracer_fd);
-                       break;
-               case LTTNG_DOMAIN_UST_PID:
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
+       switch (domain) {
+       case LTTNG_DOMAIN_KERNEL:
+       {
+               ret = channel_kernel_disable(session->kernel_session,
+                               channel_name);
+               if (ret != LTTCOMM_OK) {
                        goto error;
-       }
+               }
 
-       ret = LTTCOMM_OK;
+               kernel_wait_quiescent(kernel_tracer_fd);
+               break;
+       }
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_channel *uchan;
+               struct lttng_ht *chan_ht;
 
-error:
-       return ret;
-}
+               chan_ht = usess->domain_global.channels;
 
-/*
- * 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_find_channel_by_name(chan_ht, channel_name);
+               if (uchan == NULL) {
+                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
+                       goto error;
+               }
 
-       uchan = trace_ust_get_channel_by_key(usess->channels, attr->name);
-       if (uchan == NULL) {
-               ret = LTTCOMM_FATAL;
-               goto error;
+               ret = channel_ust_disable(usess, domain, uchan);
+               if (ret != LTTCOMM_OK) {
+                       goto error;
+               }
+               break;
        }
-
-       new_chan = trace_ust_create_channel(attr, usess->path);
-       if (new_chan == NULL) {
-               PERROR("malloc ltt_ust_channel");
-               ret = LTTCOMM_FATAL;
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+               ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
-       }
-
-       ret = channel_ust_copy(new_chan, uchan);
-       if (ret < 0) {
-               ret = LTTCOMM_FATAL;
+       default:
+               ret = LTTCOMM_UNKNOWN_DOMAIN;
                goto error;
        }
 
+       ret = LTTCOMM_OK;
+
 error:
        return ret;
 }
-*/
 
 /*
  * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
  */
 static int cmd_enable_channel(struct ltt_session *session,
-               struct lttng_domain *domain, struct lttng_channel *attr)
+               int domain, struct lttng_channel *attr)
 {
        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);
 
-       switch (domain->type) {
+       switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
        {
                struct ltt_kernel_channel *kchan;
@@ -2085,88 +2330,26 @@ 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;
-                       }
-
-                       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 {
-                       ret = LTTCOMM_UST_CHAN_EXIST;
-                       goto error;
+                       ret = channel_ust_enable(usess, domain, uchan);
                }
-
-               /* Add channel to all registered applications */
-               ret = ust_app_create_channel_all(usess, uchan);
-               if (ret != 0) {
-                       goto error;
-               }
-
-               uchan->enabled = 1;
-
                break;
        }
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
        case LTTNG_DOMAIN_UST_PID:
-       {
-               /*
-               int sock;
-               struct ltt_ust_channel *uchan;
-               struct ltt_ust_session *usess;
-               struct ust_app *app;
-
-               usess = trace_ust_get_session_by_pid(&session->ust_session_list,
-                               domain->attr.pid);
-               if (usess == NULL) {
-                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
-                       goto error;
-               }
-
-               app = ust_app_get_by_pid(domain->attr.pid);
-               if (app == NULL) {
-                       ret = LTTCOMM_APP_NOT_FOUND;
-                       goto error;
-               }
-               sock = app->sock;
-
-               uchan = trace_ust_get_channel_by_name(attr->name, usess);
-               if (uchan == NULL) {
-                       ret = channel_ust_create(usess, attr, sock);
-               } else {
-                       ret = channel_ust_enable(usess, uchan, sock);
-               }
-
-               if (ret != LTTCOMM_OK) {
-                       goto error;
-               }
-
-               ret = copy_ust_channel_to_app(usess, attr, app);
-               if (ret != LTTCOMM_OK) {
-                       goto error;
-               }
-
-               DBG("UST channel %s created for app sock %d with pid %d",
-                               attr->name, app->sock, domain->attr.pid);
-               */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
-       }
        default:
                ret = LTTCOMM_UNKNOWN_DOMAIN;
                goto error;
        }
 
-       ret = LTTCOMM_OK;
-
 error:
        return ret;
 }
@@ -2183,15 +2366,17 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
        case LTTNG_DOMAIN_KERNEL:
        {
                struct ltt_kernel_channel *kchan;
+               struct ltt_kernel_session *ksess;
 
-               kchan = trace_kernel_get_channel_by_name(channel_name,
-                               session->kernel_session);
+               ksess = session->kernel_session;
+
+               kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
                if (kchan == NULL) {
                        ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
                        goto error;
                }
 
-               ret = event_kernel_disable_tracepoint(session->kernel_session, kchan, event_name);
+               ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
                if (ret != LTTCOMM_OK) {
                        goto error;
                }
@@ -2200,11 +2385,32 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
                break;
        }
        case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_channel *uchan;
+               struct ltt_ust_session *usess;
+
+               usess = session->ust_session;
+
+               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+                               channel_name);
+               if (uchan == NULL) {
+                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
+                       goto error;
+               }
+
+               ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
+               if (ret != LTTCOMM_OK) {
+                       goto error;
+               }
+
+               DBG3("Disable UST event %s in channel %s completed", event_name,
+                               channel_name);
+               break;
+       }
        case LTTNG_DOMAIN_UST_EXEC_NAME:
        case LTTNG_DOMAIN_UST_PID:
        case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: Other UST domains */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2222,26 +2428,56 @@ static int cmd_disable_event_all(struct ltt_session *session, int domain,
                char *channel_name)
 {
        int ret;
-       struct ltt_kernel_channel *kchan;
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
-               kchan = trace_kernel_get_channel_by_name(channel_name,
-                               session->kernel_session);
+       {
+               struct ltt_kernel_session *ksess;
+               struct ltt_kernel_channel *kchan;
+
+               ksess = session->kernel_session;
+
+               kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
                if (kchan == NULL) {
                        ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
                        goto error;
                }
 
-               ret = event_kernel_disable_all(session->kernel_session, kchan);
+               ret = event_kernel_disable_all(ksess, kchan);
                if (ret != LTTCOMM_OK) {
                        goto error;
                }
 
                kernel_wait_quiescent(kernel_tracer_fd);
                break;
+       }
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_session *usess;
+               struct ltt_ust_channel *uchan;
+
+               usess = session->ust_session;
+
+               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+                               channel_name);
+               if (uchan == NULL) {
+                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
+                       goto error;
+               }
+
+               ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
+               if (ret != 0) {
+                       goto error;
+               }
+
+               DBG3("Disable all UST events in channel %s completed", channel_name);
+
+               break;
+       }
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: Userspace tracing */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2271,21 +2507,18 @@ static int cmd_add_context(struct ltt_session *session, int domain,
                break;
        case LTTNG_DOMAIN_UST:
        {
-               /*
-               struct ltt_ust_session *usess;
+               struct ltt_ust_session *usess = session->ust_session;
 
-               cds_list_for_each_entry(usess, &session->ust_session_list.head, list) {
-                       ret = context_ust_add(usess, ctx,
-                                       event_name, channel_name, domain);
-                       if (ret != LTTCOMM_OK) {
-                               goto error;
-                       }
+               ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
+               if (ret != LTTCOMM_OK) {
+                       goto error;
                }
                break;
-               */
        }
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: UST other domains */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2298,6 +2531,12 @@ error:
 
 /*
  * Command LTTNG_ENABLE_EVENT processed by the client thread.
+ *
+ * TODO: currently, both events and loglevels are kept within the same
+ * namespace for UST global registry/app registery, so if an event
+ * happen to have the same name as the loglevel (very unlikely though),
+ * and an attempt is made to enable/disable both in the same session,
+ * the first to be created will be the only one allowed to exist.
  */
 static int cmd_enable_event(struct ltt_session *session, int domain,
                char *channel_name, struct lttng_event *event)
@@ -2325,8 +2564,10 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
                        ret = channel_kernel_create(session->kernel_session,
                                        attr, kernel_poll_pipe[1]);
                        if (ret != LTTCOMM_OK) {
+                               free(attr);
                                goto error;
                        }
+                       free(attr);
                }
 
                /* Get the newly created kernel channel pointer */
@@ -2349,42 +2590,44 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
        }
        case LTTNG_DOMAIN_UST:
        {
+               struct lttng_channel *attr;
                struct ltt_ust_channel *uchan;
-               struct ltt_ust_event *uevent;
 
+               /* Get channel from global UST domain */
                uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
                                channel_name);
                if (uchan == NULL) {
-                       /* TODO: Create default channel */
-                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
-                       goto error;
-               }
-
-               uevent = trace_ust_find_event_by_name(uchan->events, event->name);
-               if (uevent == NULL) {
-                       uevent = trace_ust_create_event(event);
-                       if (uevent == NULL) {
+                       /* Create default channel */
+                       attr = channel_new_default_attr(domain);
+                       if (attr == NULL) {
                                ret = LTTCOMM_FATAL;
                                goto error;
                        }
+                       snprintf(attr->name, NAME_MAX, "%s", channel_name);
+                       attr->name[NAME_MAX - 1] = '\0';
 
+                       ret = channel_ust_create(usess, domain, attr);
+                       if (ret != LTTCOMM_OK) {
+                               free(attr);
+                               goto error;
+                       }
+                       free(attr);
+
+                       /* Get the newly created channel reference back */
+                       uchan = trace_ust_find_channel_by_name(
+                                       usess->domain_global.channels, channel_name);
+                       if (uchan == NULL) {
+                               /* Something is really wrong */
+                               ret = LTTCOMM_FATAL;
+                               goto error;
+                       }
                }
 
-               ret = ust_app_create_event_all(usess, uchan, uevent);
-               if (ret < 0) {
-                       ret = LTTCOMM_UST_ENABLE_FAIL;
+               /* 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;
                }
-
-               /* Add ltt ust event to channel */
-               rcu_read_lock();
-               hashtable_add_unique(uchan->events, &uevent->node);
-               rcu_read_unlock();
-
-               uevent->enabled = 1;
-
-               DBG3("UST ltt event %s added to channel %s", uevent->attr.name,
-                               uchan->name);
                break;
        }
        case LTTNG_DOMAIN_UST_EXEC_NAME:
@@ -2421,23 +2664,24 @@ static int cmd_enable_event_all(struct ltt_session *session, int domain,
                        if (ret != LTTCOMM_OK) {
                                goto error;
                        }
-               }
 
-               /* Get the newly created kernel channel pointer */
-               kchan = trace_kernel_get_channel_by_name(channel_name,
-                               session->kernel_session);
-               if (kchan == NULL) {
-                       /* This sould not happen... */
-                       ret = LTTCOMM_FATAL;
-                       goto error;
+                       /* Get the newly created kernel channel pointer */
+                       kchan = trace_kernel_get_channel_by_name(channel_name,
+                                       session->kernel_session);
+                       if (kchan == NULL) {
+                               /* This sould not happen... */
+                               ret = LTTCOMM_FATAL;
+                               goto error;
+                       }
+
                }
 
                switch (event_type) {
-               case LTTNG_KERNEL_SYSCALL:
+               case LTTNG_EVENT_SYSCALL:
                        ret = event_kernel_enable_all_syscalls(session->kernel_session,
                                        kchan, kernel_tracer_fd);
                        break;
-               case LTTNG_KERNEL_TRACEPOINT:
+               case LTTNG_EVENT_TRACEPOINT:
                        /*
                         * This call enables all LTTNG_KERNEL_TRACEPOINTS and
                         * events already registered to the channel.
@@ -2445,7 +2689,7 @@ static int cmd_enable_event_all(struct ltt_session *session, int domain,
                        ret = event_kernel_enable_all_tracepoints(session->kernel_session,
                                        kchan, kernel_tracer_fd);
                        break;
-               case LTTNG_KERNEL_ALL:
+               case LTTNG_EVENT_ALL:
                        /* Enable syscalls and tracepoints */
                        ret = event_kernel_enable_all(session->kernel_session,
                                        kchan, kernel_tracer_fd);
@@ -2454,14 +2698,77 @@ static int cmd_enable_event_all(struct ltt_session *session, int domain,
                        ret = LTTCOMM_KERN_ENABLE_FAIL;
                        goto error;
                }
+
+               /* Manage return value */
                if (ret != LTTCOMM_OK) {
                        goto error;
                }
 
                kernel_wait_quiescent(kernel_tracer_fd);
                break;
+       case LTTNG_DOMAIN_UST:
+       {
+               struct lttng_channel *attr;
+               struct ltt_ust_channel *uchan;
+               struct ltt_ust_session *usess = session->ust_session;
+
+               /* Get channel from global UST domain */
+               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+                               channel_name);
+               if (uchan == NULL) {
+                       /* Create default channel */
+                       attr = channel_new_default_attr(domain);
+                       if (attr == NULL) {
+                               ret = LTTCOMM_FATAL;
+                               goto error;
+                       }
+                       snprintf(attr->name, NAME_MAX, "%s", channel_name);
+                       attr->name[NAME_MAX - 1] = '\0';
+
+                       /* Use the internal command enable channel */
+                       ret = channel_ust_create(usess, domain, attr);
+                       if (ret != LTTCOMM_OK) {
+                               free(attr);
+                               goto error;
+                       }
+                       free(attr);
+
+                       /* Get the newly created channel reference back */
+                       uchan = trace_ust_find_channel_by_name(
+                                       usess->domain_global.channels, channel_name);
+                       if (uchan == NULL) {
+                               /* Something is really wrong */
+                               ret = LTTCOMM_FATAL;
+                               goto error;
+                       }
+               }
+
+               /* At this point, the session and channel exist on the tracer */
+
+               switch (event_type) {
+               case LTTNG_EVENT_ALL:
+               case LTTNG_EVENT_TRACEPOINT:
+                       ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
+                       if (ret != LTTCOMM_OK) {
+                               goto error;
+                       }
+                       break;
+               default:
+                       ret = LTTCOMM_UST_ENABLE_FAIL;
+                       goto error;
+               }
+
+               /* Manage return value */
+               if (ret != LTTCOMM_OK) {
+                       goto error;
+               }
+
+               break;
+       }
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: Userspace tracing */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2520,8 +2827,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 */
@@ -2609,8 +2919,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_STOP_FAIL;
+               goto error;
+       }
+
        session->enabled = 0;
 
        /* Kernel tracer */
@@ -2639,13 +2952,12 @@ static int cmd_stop_trace(struct ltt_session *session)
                kernel_wait_quiescent(kernel_tracer_fd);
        }
 
-       /* Flag session that trace should start automatically */
        if (usess) {
                usess->start_trace = 0;
 
                ret = ust_app_stop_trace_all(usess);
                if (ret < 0) {
-                       ret = LTTCOMM_UST_START_FAIL;
+                       ret = LTTCOMM_UST_STOP_FAIL;
                        goto error;
                }
        }
@@ -2659,11 +2971,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;
        }
@@ -2743,7 +3055,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;
                }
@@ -2827,7 +3139,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);
@@ -2905,6 +3217,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
@@ -2989,8 +3306,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                        ret = LTTCOMM_KERN_CONSUMER_FAIL;
                                        goto error;
                                }
+                       } else {
+                               pthread_mutex_unlock(&kconsumer_data.pid_mutex);
                        }
-                       pthread_mutex_unlock(&kconsumer_data.pid_mutex);
                }
                break;
        case LTTNG_DOMAIN_UST:
@@ -3003,20 +3321,39 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                        goto error;
                                }
                        }
-                       /* Start the kernel consumer daemon */
-                       pthread_mutex_lock(&ustconsumer_data.pid_mutex);
-                       if (ustconsumer_data.pid == 0 &&
+                       /* Start the UST consumer daemons */
+                       /* 64-bit */
+                       pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
+                       if (consumerd64_bin[0] != '\0' &&
+                                       ustconsumer64_data.pid == 0 &&
                                        cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
-                               pthread_mutex_unlock(&ustconsumer_data.pid_mutex);
-                               ret = start_consumerd(&ustconsumer_data);
+                               pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
+                               ret = start_consumerd(&ustconsumer64_data);
                                if (ret < 0) {
-                                       ret = LTTCOMM_KERN_CONSUMER_FAIL;
+                                       ret = LTTCOMM_UST_CONSUMER64_FAIL;
+                                       ust_consumerd64_fd = -EINVAL;
                                        goto error;
                                }
 
-                               ust_consumer_fd = ustconsumer_data.cmd_sock;
+                               ust_consumerd64_fd = ustconsumer64_data.cmd_sock;
+                       } else {
+                               pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
+                       }
+                       /* 32-bit */
+                       if (consumerd32_bin[0] != '\0' &&
+                                       ustconsumer32_data.pid == 0 &&
+                                       cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
+                               pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
+                               ret = start_consumerd(&ustconsumer32_data);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_UST_CONSUMER32_FAIL;
+                                       ust_consumerd32_fd = -EINVAL;
+                                       goto error;
+                               }
+                               ust_consumerd32_fd = ustconsumer32_data.cmd_sock;
+                       } else {
+                               pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
                        }
-                       pthread_mutex_unlock(&ustconsumer_data.pid_mutex);
                }
                break;
        }
@@ -3024,6 +3361,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:
@@ -3058,7 +3407,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        }
        case LTTNG_ENABLE_CHANNEL:
        {
-               ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
+               ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
                                &cmd_ctx->lsm->u.channel.chan);
                break;
        }
@@ -3121,7 +3470,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:
@@ -3209,23 +3558,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();
 
@@ -3346,6 +3696,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) {
@@ -3369,8 +3725,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);
@@ -3408,10 +3764,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:
@@ -3439,8 +3795,14 @@ static void usage(void)
        fprintf(stderr, "  -a, --apps-sock PATH               Specify path for apps unix socket\n");
        fprintf(stderr, "      --kconsumerd-err-sock PATH     Specify path for the kernel consumer error socket\n");
        fprintf(stderr, "      --kconsumerd-cmd-sock PATH     Specify path for the kernel consumer command socket\n");
-       fprintf(stderr, "      --ustconsumerd-err-sock PATH   Specify path for the UST consumer error socket\n");
-       fprintf(stderr, "      --ustconsumerd-cmd-sock PATH   Specify path for the UST consumer command socket\n");
+       fprintf(stderr, "      --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
+       fprintf(stderr, "      --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
+       fprintf(stderr, "      --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
+       fprintf(stderr, "      --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
+       fprintf(stderr, "      --consumerd32-path PATH     Specify path for the 32-bit UST consumer daemon binary\n");
+       fprintf(stderr, "      --consumerd32-libdir PATH   Specify path for the 32-bit UST consumer daemon libraries\n");
+       fprintf(stderr, "      --consumerd64-path PATH     Specify path for the 64-bit UST consumer daemon binary\n");
+       fprintf(stderr, "      --consumerd64-libdir PATH   Specify path for the 64-bit UST consumer daemon libraries\n");
        fprintf(stderr, "  -d, --daemonize                    Start as a daemon.\n");
        fprintf(stderr, "  -g, --group NAME                   Specify the tracing group name. (default: tracing)\n");
        fprintf(stderr, "  -V, --version                      Show version number.\n");
@@ -3448,6 +3810,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");
 }
 
 /*
@@ -3462,8 +3825,14 @@ static int parse_args(int argc, char **argv)
                { "apps-sock", 1, 0, 'a' },
                { "kconsumerd-cmd-sock", 1, 0, 'C' },
                { "kconsumerd-err-sock", 1, 0, 'E' },
-               { "ustconsumerd-cmd-sock", 1, 0, 'D' },
-               { "ustconsumerd-err-sock", 1, 0, 'F' },
+               { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
+               { "ustconsumerd32-err-sock", 1, 0, 'H' },
+               { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
+               { "ustconsumerd64-err-sock", 1, 0, 'F' },
+               { "consumerd32-path", 1, 0, 'u' },
+               { "consumerd32-libdir", 1, 0, 'U' },
+               { "consumerd64-path", 1, 0, 't' },
+               { "consumerd64-libdir", 1, 0, 'T' },
                { "daemonize", 0, 0, 'd' },
                { "sig-parent", 0, 0, 'S' },
                { "help", 0, 0, 'h' },
@@ -3472,12 +3841,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",
+               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;
@@ -3500,7 +3870,7 @@ static int parse_args(int argc, char **argv)
                        opt_daemon = 1;
                        break;
                case 'g':
-                       opt_tracing_group = strdup(optarg);
+                       opt_tracing_group = optarg;
                        break;
                case 'h':
                        usage();
@@ -3518,10 +3888,19 @@ static int parse_args(int argc, char **argv)
                        snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
                        break;
                case 'F':
-                       snprintf(ustconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
+                       snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
                        break;
                case 'D':
-                       snprintf(ustconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
+                       snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
+                       break;
+               case 'H':
+                       snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
+                       break;
+               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;
@@ -3533,6 +3912,18 @@ static int parse_args(int argc, char **argv)
                case 'Z':
                        opt_verbose_consumer += 1;
                        break;
+               case 'u':
+                       consumerd32_bin= optarg;
+                       break;
+               case 'U':
+                       consumerd32_libdir = optarg;
+                       break;
+               case 't':
+                       consumerd64_bin = optarg;
+                       break;
+               case 'T':
+                       consumerd64_libdir = optarg;
+                       break;
                default:
                        /* Unknown option or other error.
                         * Error is printed by getopt, just return */
@@ -3618,27 +4009,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");
        }
 
@@ -3656,10 +4042,17 @@ static int set_permissions(void)
                perror("chown");
        }
 
-       /* ustconsumer error socket path */
-       ret = chown(ustconsumer_data.err_unix_sock_path, 0, gid);
+       /* 64-bit ustconsumer error socket path */
+       ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
        if (ret < 0) {
-               ERR("Unable to set group on %s", ustconsumer_data.err_unix_sock_path);
+               ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
+               perror("chown");
+       }
+
+       /* 32-bit ustconsumer compat32 error socket path */
+       ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
+       if (ret < 0) {
+               ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
                perror("chown");
        }
 
@@ -3688,14 +4081,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;
@@ -3710,25 +4105,29 @@ 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 = consumer_data->type == LTTNG_CONSUMER_KERNEL ?
-                       KCONSUMERD_PATH : USTCONSUMERD_PATH;
+       char path[PATH_MAX];
 
-       if (strlen(consumer_data->err_unix_sock_path) == 0) {
-               snprintf(consumer_data->err_unix_sock_path, PATH_MAX,
-                       consumer_data->type == LTTNG_CONSUMER_KERNEL ?
-                               KCONSUMERD_ERR_SOCK_PATH :
-                               USTCONSUMERD_ERR_SOCK_PATH);
+    switch (consumer_data->type) {
+       case LTTNG_CONSUMER_KERNEL:
+               snprintf(path, PATH_MAX, KCONSUMERD_PATH, rundir);
+               break;
+       case LTTNG_CONSUMER64_UST:
+               snprintf(path, PATH_MAX, USTCONSUMERD64_PATH, rundir);
+               break;
+       case LTTNG_CONSUMER32_UST:
+               snprintf(path, PATH_MAX, USTCONSUMERD32_PATH, rundir);
+               break;
+       default:
+               ERR("Consumer type unknown");
+               ret = -EINVAL;
+               goto error;
        }
 
-       if (strlen(consumer_data->cmd_unix_sock_path) == 0) {
-               snprintf(consumer_data->cmd_unix_sock_path, PATH_MAX,
-                       consumer_data->type == LTTNG_CONSUMER_KERNEL ?
-                               KCONSUMERD_CMD_SOCK_PATH :
-                               USTCONSUMERD_CMD_SOCK_PATH);
-       }
+       DBG2("Creating consumer directory: %s", path);
 
        ret = mkdir(path, S_IRWXU | S_IRWXG);
        if (ret < 0) {
@@ -3753,7 +4152,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;
        }
 
@@ -3859,6 +4258,8 @@ int main(int argc, char **argv)
                goto error;
        }
 
+       setup_consumerd_path();
+
        /* Parse arguments */
        progname = argv[0];
        if ((ret = parse_args(argc, argv) < 0)) {
@@ -3878,7 +4279,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;
                }
@@ -3898,6 +4302,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) {
@@ -3907,6 +4322,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);
@@ -3927,6 +4357,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.
@@ -3949,22 +4402,30 @@ int main(int argc, char **argv)
         * kernel tracer.
         */
        if (is_root) {
-               ret = set_consumer_sockets(&kconsumer_data);
+               ret = set_consumer_sockets(&kconsumer_data, rundir);
                if (ret < 0) {
                        goto exit;
                }
 
-               ret = set_consumer_sockets(&ustconsumer_data);
-               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;
        }
@@ -3975,7 +4436,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;
        }
 
@@ -4098,8 +4559,9 @@ exit:
        cleanup();
        rcu_thread_offline();
        rcu_unregister_thread();
-       if (!ret)
+       if (!ret) {
                exit(EXIT_SUCCESS);
+       }
 error:
        exit(EXIT_FAILURE);
 }
This page took 0.071776 seconds and 4 git commands to generate.