Create all trace directories and files with client user credentials
[lttng-tools.git] / lttng-sessiond / main.c
index 7d63656e4c401b9d66037d2c9d8126974cf6f4c5..fbf8d7975330414cf6589d2aa80f707f25dde88e 100644 (file)
@@ -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> */
@@ -512,7 +512,8 @@ static void clean_command_ctx(struct command_ctx **cmd_ctx)
  * Send all stream fds of kernel channel to the consumer.
  */
 static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
-               int sock, struct ltt_kernel_channel *channel)
+               int sock, struct ltt_kernel_channel *channel,
+               uid_t uid, gid_t gid)
 {
        int ret;
        struct ltt_kernel_stream *stream;
@@ -544,6 +545,8 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
                lkm.u.stream.state = stream->state;
                lkm.u.stream.output = channel->channel->attr.output;
                lkm.u.stream.mmap_len = 0;      /* for kernel */
+               lkm.u.stream.uid = uid;
+               lkm.u.stream.gid = gid;
                strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1);
                lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
                DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
@@ -605,6 +608,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
                lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
                lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
                lkm.u.stream.mmap_len = 0;      /* for kernel */
+               lkm.u.stream.uid = session->uid;
+               lkm.u.stream.gid = session->gid;
                strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1);
                lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
                DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
@@ -621,7 +626,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
        }
 
        cds_list_for_each_entry(chan, &session->channel_list.head, list) {
-               ret = send_kconsumer_channel_streams(consumer_data, sock, chan);
+               ret = send_kconsumer_channel_streams(consumer_data, sock, chan,
+                               session->uid, session->gid);
                if (ret < 0) {
                        goto error;
                }
@@ -777,7 +783,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;
                                        }
@@ -1873,9 +1880,8 @@ error:
 static int create_ust_session(struct ltt_session *session,
                struct lttng_domain *domain)
 {
-       int ret;
-       unsigned int uid;
        struct ltt_ust_session *lus = NULL;
+       int ret;
 
        switch (domain->type) {
        case LTTNG_DOMAIN_UST:
@@ -1887,15 +1893,14 @@ static int create_ust_session(struct ltt_session *session,
 
        DBG("Creating UST session");
 
-       uid = session->uid;
-       lus = trace_ust_create_session(session->path, uid, domain);
+       lus = trace_ust_create_session(session->path, session->id, domain);
        if (lus == NULL) {
                ret = LTTCOMM_UST_SESS_FAIL;
                goto error;
        }
 
        ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
-                       geteuid(), allowed_group());
+                       session->uid, session->gid);
        if (ret < 0) {
                if (ret != -EEXIST) {
                        ERR("Trace directory creation error");
@@ -1913,6 +1918,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;
@@ -1943,13 +1950,15 @@ static int create_kernel_session(struct ltt_session *session)
        }
 
        ret = mkdir_recursive(session->kernel_session->trace_path,
-                       S_IRWXU | S_IRWXG, geteuid(), allowed_group());
+                       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;
@@ -2916,11 +2925,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;
        }
@@ -3403,7 +3412,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:
@@ -3628,6 +3637,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) {
@@ -3651,8 +3666,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);
@@ -3690,10 +3705,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:
@@ -3935,27 +3950,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");
        }
 
@@ -4367,7 +4377,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;
        }
 
This page took 0.026496 seconds and 4 git commands to generate.