lttng-sessiond: do not call ustctl_register_done()
[lttng-tools.git] / lttng-sessiond / main.c
index 80505b3caf34fcc525a83b9fc2300638ef88f270..91db9677abdfd24a6d781db11bd47951f95fbed9 100644 (file)
 #include <config.h>
 
 #include <lttng-consumerd.h>
+#include <lttng-ht.h>
 #include <lttng-sessiond-comm.h>
 #include <lttng/lttng-consumer.h>
 
 #include <lttngerr.h>
+#include <runas.h>
 
 #include "channel.h"
 #include "compat/poll.h"
 #include "context.h"
 #include "event.h"
 #include "futex.h"
-#include "hashtable.h"
 #include "kernel.h"
 #include "lttng-sessiond.h"
 #include "shm.h"
@@ -1156,7 +1157,7 @@ static void *thread_manage_apps(void *data)
                                         */
                                        update_ust_app(ust_cmd.sock);
 
-                                       ret = ustctl_register_done(ust_cmd.sock);
+                                       ret = ust_app_register_done(ust_cmd.sock);
                                        if (ret < 0) {
                                                /*
                                                 * If the registration is not possible, we simply
@@ -1964,6 +1965,40 @@ 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.
@@ -1971,17 +2006,25 @@ error:
  * The session list lock MUST be acquired before calling this function. Use
  * session_lock_list() and session_unlock_list().
  */
-static void list_lttng_sessions(struct lttng_session *sessions)
+static void list_lttng_sessions(struct lttng_session *sessions,
+               uid_t uid, gid_t gid)
 {
-       int i = 0;
+       unsigned int i = 0;
        struct ltt_session *session;
 
-       DBG("Getting all available session");
+       DBG("Getting all available session for UID %d GID %d",
+               uid, gid);
        /*
         * Iterate over session list and append data after the control struct in
         * the buffer.
         */
        cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+               /*
+                * Only list the sessions the user can control.
+                */
+               if (!session_access_ok(session, uid, gid)) {
+                       continue;
+               }
                strncpy(sessions[i].path, session->path, PATH_MAX);
                sessions[i].path[PATH_MAX - 1] = '\0';
                strncpy(sessions[i].name, session->name, NAME_MAX);
@@ -2017,11 +2060,11 @@ static void list_lttng_channels(int domain, struct ltt_session *session,
                break;
        case LTTNG_DOMAIN_UST:
        {
-               struct cds_lfht_iter iter;
+               struct lttng_ht_iter iter;
                struct ltt_ust_channel *uchan;
 
-               cds_lfht_for_each_entry(session->ust_session->domain_global.channels,
-                               &iter, uchan, node) {
+               cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
+                               &iter.iter, uchan, node.node) {
                        strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
                        channels[i].attr.overwrite = uchan->attr.overwrite;
                        channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
@@ -2054,8 +2097,8 @@ static int list_lttng_ust_global_events(char *channel_name,
 {
        int i = 0, ret = 0;
        unsigned int nb_event = 0;
-       struct cds_lfht_iter iter;
-       struct cds_lfht_node *node;
+       struct lttng_ht_iter iter;
+       struct lttng_ht_node_str *node;
        struct ltt_ust_channel *uchan;
        struct ltt_ust_event *uevent;
        struct lttng_event *tmp;
@@ -2064,16 +2107,16 @@ static int list_lttng_ust_global_events(char *channel_name,
 
        rcu_read_lock();
 
-       node = hashtable_lookup(ust_global->channels, (void *) channel_name,
-                       strlen(channel_name), &iter);
+       lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
+       node = lttng_ht_iter_get_node_str(&iter);
        if (node == NULL) {
                ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
                goto error;
        }
 
-       uchan = caa_container_of(node, struct ltt_ust_channel, node);
+       uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
 
-       nb_event += hashtable_get_count(uchan->events);
+       nb_event += lttng_ht_get_count(uchan->events);
 
        if (nb_event == 0) {
                ret = nb_event;
@@ -2088,7 +2131,7 @@ static int list_lttng_ust_global_events(char *channel_name,
                goto error;
        }
 
-       cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
+       cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
                strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
                tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                tmp[i].enabled = uevent->enabled;
@@ -2214,7 +2257,7 @@ static int cmd_disable_channel(struct ltt_session *session,
        case LTTNG_DOMAIN_UST:
        {
                struct ltt_ust_channel *uchan;
-               struct cds_lfht *chan_ht;
+               struct lttng_ht *chan_ht;
 
                chan_ht = usess->domain_global.channels;
 
@@ -2254,7 +2297,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;
+       struct lttng_ht *chan_ht;
 
        DBG("Enabling channel %s for session %s", attr->name, session->name);
 
@@ -2577,7 +2620,6 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
                }
 
                /* At this point, the session and channel exist on the tracer */
-
                ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
                if (ret != LTTCOMM_OK) {
                        goto error;
@@ -3093,7 +3135,7 @@ static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                break;
        case LTTNG_DOMAIN_UST:
                if (session->ust_session != NULL) {
-                       nb_chan = hashtable_get_count(
+                       nb_chan = lttng_ht_get_count(
                                        session->ust_session->domain_global.channels);
                }
                DBG3("Number of UST global channels %zd", nb_chan);
@@ -3315,6 +3357,18 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                break;
        }
 
+       /*
+        * Check that the UID or GID match that of the tracing session.
+        * The root user can interact with all sessions.
+        */
+       if (need_tracing_session) {
+               if (!session_access_ok(cmd_ctx->session,
+                               cmd_ctx->creds.uid, cmd_ctx->creds.gid)) {
+                       ret = LTTCOMM_EPERM;
+                       goto error;
+               }
+       }
+
        /* Process by command type */
        switch (cmd_ctx->lsm->cmd_type) {
        case LTTNG_ADD_CONTEXT:
@@ -3500,23 +3554,24 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        }
        case LTTNG_LIST_SESSIONS:
        {
-               session_lock_list();
+               unsigned int nr_sessions;
 
-               if (session_list_ptr->count == 0) {
+               session_lock_list();
+               nr_sessions = lttng_sessions_count(cmd_ctx->creds.uid, cmd_ctx->creds.gid);
+               if (nr_sessions == 0) {
                        ret = LTTCOMM_NO_SESSION;
                        session_unlock_list();
                        goto error;
                }
-
-               ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
-                               session_list_ptr->count);
+               ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
                if (ret < 0) {
                        session_unlock_list();
                        goto setup_error;
                }
 
                /* Filled the session array */
-               list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
+               list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
+                       cmd_ctx->creds.uid, cmd_ctx->creds.gid);
 
                session_unlock_list();
 
This page took 0.026467 seconds and 4 git commands to generate.