Change easter egg MSG to DBG
[lttng-tools.git] / lttng-sessiond / main.c
index ff096e1b90db47940a5c11092e785fd7b67724f9..dc43edb4d30a905e6c403c69b9fc3a35b1b7901c 100644 (file)
@@ -92,8 +92,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 = {
@@ -171,10 +173,10 @@ static struct ltt_session_list *session_list_ptr;
 int ust_consumerd64_fd = -1;
 int ust_consumerd32_fd = -1;
 
-static const char *consumerd32_path =
-       __stringify(CONFIG_CONSUMERD32_PATH);
-static const char *consumerd64_path =
-       __stringify(CONFIG_CONSUMERD64_PATH);
+static const char *consumerd32_bin =
+       __stringify(CONFIG_CONSUMERD32_BIN);
+static const char *consumerd64_bin =
+       __stringify(CONFIG_CONSUMERD64_BIN);
 static const char *consumerd32_libdir =
        __stringify(CONFIG_CONSUMERD32_LIBDIR);
 static const char *consumerd64_libdir =
@@ -183,7 +185,7 @@ static const char *consumerd64_libdir =
 static
 void setup_consumerd_path(void)
 {
-       const char *path, *libdir;
+       const char *bin, *libdir;
 
        /*
         * Allow INSTALL_BIN_PATH to be used as a target path for the
@@ -191,15 +193,15 @@ void setup_consumerd_path(void)
         * has not been defined.
         */
 #if (CAA_BITS_PER_LONG == 32)
-       if (!consumerd32_path[0]) {
-               consumerd32_path = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+       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_path[0]) {
-               consumerd64_path = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+       if (!consumerd64_bin[0]) {
+               consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
        }
        if (!consumerd64_libdir[0]) {
                consumerd64_libdir = INSTALL_LIB_PATH;
@@ -211,13 +213,13 @@ void setup_consumerd_path(void)
        /*
         * runtime env. var. overrides the build default.
         */
-       path = getenv("LTTNG_CONSUMERD32_PATH");
-       if (path) {
-               consumerd32_path = path;
+       bin = getenv("LTTNG_CONSUMERD32_BIN");
+       if (bin) {
+               consumerd32_bin = bin;
        }
-       path = getenv("LTTNG_CONSUMERD64_PATH");
-       if (path) {
-               consumerd64_path = path;
+       bin = getenv("LTTNG_CONSUMERD64_BIN");
+       if (bin) {
+               consumerd64_bin = bin;
        }
        libdir = getenv("LTTNG_TOOLS_CONSUMERD32_LIBDIR");
        if (libdir) {
@@ -423,19 +425,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 +458,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 +469,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> */
@@ -1508,7 +1508,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");
 
@@ -1524,11 +1526,27 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                }
                switch (consumer_data->type) {
                case LTTNG_CONSUMER_KERNEL:
-                       execl(INSTALL_BIN_PATH "/lttng-consumerd",
-                                       "lttng-consumerd", verbosity, "-k",
-                                       "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
-                                       "--consumerd-err-sock", consumer_data->err_unix_sock_path,
-                                       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, 
+                        */
+                       if (stat(consumerd64_bin, &st) == 0) {
+                               consumer_to_use = consumerd64_bin;
+                       } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
+                               consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
+                       } else if (stat(consumerd32_bin, &st) == 0) {
+                               consumer_to_use = consumerd32_bin;
+                       } else {
+                               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_CONSUMER64_UST:
                {
@@ -1561,7 +1579,8 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        goto error;
                                }
                        }
-                       ret = execl(consumerd64_path, verbosity, "-u",
+                       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);
@@ -1604,7 +1623,8 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        goto error;
                                }
                        }
-                       ret = execl(consumerd32_path, verbosity, "-u",
+                       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);
@@ -1851,10 +1871,11 @@ error:
  * Create an UST session and add it to the session ust list.
  */
 static int create_ust_session(struct ltt_session *session,
-               struct lttng_domain *domain)
+               struct lttng_domain *domain, struct ucred *creds)
 {
        int ret;
-       unsigned int uid;
+       unsigned int sess_uid;
+       gid_t gid;
        struct ltt_ust_session *lus = NULL;
 
        switch (domain->type) {
@@ -1867,15 +1888,23 @@ 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);
+       sess_uid = session->uid;
+       lus = trace_ust_create_session(session->path, sess_uid, domain);
        if (lus == NULL) {
                ret = LTTCOMM_UST_SESS_FAIL;
                goto error;
        }
 
-       ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
-                       geteuid(), allowed_group());
+       /*
+        * Get the right group ID. To use the tracing group, the daemon must be
+        * running with root credentials or else it's the user GID used.
+        */
+       gid = allowed_group();
+       if (gid < 0 || !is_root) {
+               gid = creds->gid;
+       }
+
+       ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG, creds->uid, gid);
        if (ret < 0) {
                if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
@@ -1905,9 +1934,11 @@ error:
 /*
  * Create a kernel tracer session then create the default channel.
  */
-static int create_kernel_session(struct ltt_session *session)
+static int create_kernel_session(struct ltt_session *session,
+               struct ucred *creds)
 {
        int ret;
+       gid_t gid;
 
        DBG("Creating kernel session");
 
@@ -1922,8 +1953,17 @@ static int create_kernel_session(struct ltt_session *session)
                session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
        }
 
+       gid = allowed_group();
+       if (gid < 0) {
+               /*
+                * Use GID 0 has a fallback since kernel session is only allowed under
+                * root or the gid of the calling user
+                */
+               is_root ? (gid = 0) : (gid = creds->gid);
+       }
+
        ret = mkdir_recursive(session->kernel_session->trace_path,
-                       S_IRWXU | S_IRWXG, geteuid(), allowed_group());
+                       S_IRWXU | S_IRWXG, creds->uid, gid);
        if (ret < 0) {
                if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
@@ -2073,6 +2113,9 @@ static int list_lttng_ust_global_events(char *channel_name,
                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++;
        }
@@ -2182,29 +2225,20 @@ static int cmd_disable_channel(struct ltt_session *session,
        case LTTNG_DOMAIN_UST:
        {
                struct ltt_ust_channel *uchan;
+               struct cds_lfht *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:
@@ -2223,40 +2257,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.
  */
@@ -2265,6 +2265,7 @@ static int cmd_enable_channel(struct ltt_session *session,
 {
        int ret;
        struct ltt_ust_session *usess = session->ust_session;
+       struct cds_lfht *chan_ht;
 
        DBG("Enabling channel %s for session %s", attr->name, session->name);
 
@@ -2293,49 +2294,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:
@@ -2348,8 +2314,6 @@ static int cmd_enable_channel(struct ltt_session *session,
                goto error;
        }
 
-       ret = LTTCOMM_OK;
-
 error:
        return ret;
 }
@@ -2386,9 +2350,8 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
        }
        case LTTNG_DOMAIN_UST:
        {
-               struct ltt_ust_session *usess;
                struct ltt_ust_channel *uchan;
-               struct ltt_ust_event *uevent;
+               struct ltt_ust_session *usess;
 
                usess = session->ust_session;
 
@@ -2399,23 +2362,13 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
                        goto error;
                }
 
-               uevent = trace_ust_find_event_by_name(uchan->events, event_name);
-               if (uevent == NULL) {
-                       ret = LTTCOMM_UST_EVENT_NOT_FOUND;
-                       goto error;
-               }
-
-               ret = ust_app_disable_event_glb(usess, uchan, uevent);
-               if (ret < 0) {
-                       ret = LTTCOMM_UST_DISABLE_FAIL;
+               ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
+               if (ret != LTTCOMM_OK) {
                        goto error;
                }
 
-               uevent->enabled = 0;
-
-               DBG2("Disable UST event %s in channel %s completed", event_name,
+               DBG3("Disable UST event %s in channel %s completed", event_name,
                                channel_name);
-
                break;
        }
        case LTTNG_DOMAIN_UST_EXEC_NAME:
@@ -2476,13 +2429,12 @@ static int cmd_disable_event_all(struct ltt_session *session, int domain,
                        goto error;
                }
 
-               ret = ust_app_disable_all_event_glb(usess, uchan);
-               if (ret < 0) {
-                       ret = LTTCOMM_UST_DISABLE_FAIL;
+               ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
+               if (ret != 0) {
                        goto error;
                }
 
-               DBG2("Disable all UST event in channel %s completed", channel_name);
+               DBG3("Disable all UST events in channel %s completed", channel_name);
 
                break;
        }
@@ -2519,21 +2471,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;
        }
@@ -2546,6 +2495,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)
@@ -2615,8 +2570,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;
@@ -2675,23 +2629,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.
@@ -2699,7 +2654,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);
@@ -2708,14 +2663,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;
        }
@@ -2774,8 +2792,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 */
@@ -2863,8 +2884,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 */
@@ -2996,7 +3020,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,6 +3182,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
@@ -3225,7 +3254,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                /* Need a session for kernel command */
                if (need_tracing_session) {
                        if (cmd_ctx->session->kernel_session == NULL) {
-                               ret = create_kernel_session(cmd_ctx->session);
+                               ret = create_kernel_session(cmd_ctx->session, &cmd_ctx->creds);
                                if (ret < 0) {
                                        ret = LTTCOMM_KERN_SESS_FAIL;
                                        goto error;
@@ -3252,7 +3281,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                if (need_tracing_session) {
                        if (cmd_ctx->session->ust_session == NULL) {
                                ret = create_ust_session(cmd_ctx->session,
-                                               &cmd_ctx->lsm->domain);
+                                               &cmd_ctx->lsm->domain, &cmd_ctx->creds);
                                if (ret != LTTCOMM_OK) {
                                        goto error;
                                }
@@ -3260,7 +3289,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        /* Start the UST consumer daemons */
                        /* 64-bit */
                        pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
-                       if (consumerd64_path[0] != '\0' &&
+                       if (consumerd64_bin[0] != '\0' &&
                                        ustconsumer64_data.pid == 0 &&
                                        cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
                                pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
@@ -3276,7 +3305,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
                        }
                        /* 32-bit */
-                       if (consumerd32_path[0] != '\0' &&
+                       if (consumerd32_bin[0] != '\0' &&
                                        ustconsumer32_data.pid == 0 &&
                                        cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
                                pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
@@ -3619,6 +3648,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) {
@@ -3642,8 +3677,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);
@@ -3681,10 +3716,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:
@@ -3727,6 +3762,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");
 }
 
 /*
@@ -3757,12 +3793,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;
@@ -3814,6 +3851,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;
@@ -3825,13 +3865,13 @@ static int parse_args(int argc, char **argv)
                        opt_verbose_consumer += 1;
                        break;
                case 'u':
-                       consumerd32_path= optarg;
+                       consumerd32_bin= optarg;
                        break;
                case 'U':
                        consumerd32_libdir = optarg;
                        break;
                case 't':
-                       consumerd64_path = optarg;
+                       consumerd64_bin = optarg;
                        break;
                case 'T':
                        consumerd64_libdir = optarg;
@@ -3921,27 +3961,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");
        }
 
@@ -3998,14 +4033,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;
@@ -4020,20 +4057,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");
@@ -4041,6 +4079,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) {
@@ -4064,7 +4104,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;
        }
 
@@ -4191,7 +4231,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;
                }
@@ -4211,6 +4254,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) {
@@ -4220,6 +4274,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);
@@ -4240,6 +4309,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.
@@ -4262,28 +4354,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;
        }
@@ -4294,7 +4388,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;
        }
 
@@ -4417,8 +4511,9 @@ exit:
        cleanup();
        rcu_thread_offline();
        rcu_unregister_thread();
-       if (!ret)
+       if (!ret) {
                exit(EXIT_SUCCESS);
+       }
 error:
        exit(EXIT_FAILURE);
 }
This page took 0.037306 seconds and 4 git commands to generate.