X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=lttng-sessiond%2Fmain.c;h=10139448aee3dcbe811c75adb3ca55a99c9b5d4d;hp=27d4c04de9314d08dbdd530752b7dd457c1c684b;hb=6c9bfa35ac65810ce106433fb5721af842b75c78;hpb=b9ef1c833d0a0dbb457ca920a15c0f54f3585374 diff --git a/lttng-sessiond/main.c b/lttng-sessiond/main.c index 27d4c04de..10139448a 100644 --- a/lttng-sessiond/main.c +++ b/lttng-sessiond/main.c @@ -43,6 +43,7 @@ #include #include +#include #include "channel.h" #include "compat/poll.h" @@ -92,6 +93,7 @@ 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; @@ -457,10 +459,9 @@ static void cleanup(void) pthread_mutex_destroy(&kconsumer_data.pid_mutex); - DBG("Closing kernel fd"); - close(kernel_tracer_fd); - - if (is_root) { + if (is_root && !opt_no_kernel) { + DBG2("Closing kernel fd"); + close(kernel_tracer_fd); DBG("Unloading kernel modules"); modprobe_remove_kernel_modules(); } @@ -469,7 +470,7 @@ static void cleanup(void) close(thread_quit_pipe[1]); /* */ - 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); /* */ @@ -512,7 +513,8 @@ static void clean_command_ctx(struct command_ctx **cmd_ctx) * Send all stream fds of kernel channel to the consumer. */ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, - int sock, struct ltt_kernel_channel *channel) + int sock, struct ltt_kernel_channel *channel, + uid_t uid, gid_t gid) { int ret; struct ltt_kernel_stream *stream; @@ -544,6 +546,8 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, lkm.u.stream.state = stream->state; lkm.u.stream.output = channel->channel->attr.output; lkm.u.stream.mmap_len = 0; /* for kernel */ + lkm.u.stream.uid = uid; + lkm.u.stream.gid = gid; strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1); lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; DBG("Sending stream %d to consumer", lkm.u.stream.stream_key); @@ -605,6 +609,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data, lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; lkm.u.stream.mmap_len = 0; /* for kernel */ + lkm.u.stream.uid = session->uid; + lkm.u.stream.gid = session->gid; strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1); lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key); @@ -621,7 +627,8 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data, } cds_list_for_each_entry(chan, &session->channel_list.head, list) { - ret = send_kconsumer_channel_streams(consumer_data, sock, chan); + ret = send_kconsumer_channel_streams(consumer_data, sock, chan, + session->uid, session->gid); if (ret < 0) { goto error; } @@ -777,7 +784,8 @@ static int update_kernel_stream(struct consumer_data *consumer_data, int fd) */ if (session->kernel_session->consumer_fds_sent == 1) { ret = send_kconsumer_channel_streams(consumer_data, - session->kernel_session->consumer_fd, channel); + session->kernel_session->consumer_fd, channel, + session->uid, session->gid); if (ret < 0) { goto error; } @@ -1737,7 +1745,7 @@ static int mount_debugfs(char *path) int ret; char *type = "debugfs"; - ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); + ret = mkdir_recursive_run_as(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); if (ret < 0) { PERROR("Cannot create debugfs path"); goto error; @@ -1873,9 +1881,8 @@ error: static int create_ust_session(struct ltt_session *session, struct lttng_domain *domain) { - int ret; - unsigned int uid; struct ltt_ust_session *lus = NULL; + int ret; switch (domain->type) { case LTTNG_DOMAIN_UST: @@ -1887,15 +1894,14 @@ static int create_ust_session(struct ltt_session *session, DBG("Creating UST session"); - uid = session->uid; - lus = trace_ust_create_session(session->path, uid, domain); + lus = trace_ust_create_session(session->path, session->id, domain); if (lus == NULL) { ret = LTTCOMM_UST_SESS_FAIL; goto error; } - ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG, - geteuid(), allowed_group()); + ret = mkdir_recursive_run_as(lus->pathname, S_IRWXU | S_IRWXG, + session->uid, session->gid); if (ret < 0) { if (ret != -EEXIST) { ERR("Trace directory creation error"); @@ -1913,6 +1919,8 @@ static int create_ust_session(struct ltt_session *session, ERR("Unknown UST domain on create session %d", domain->type); goto error; } + lus->uid = session->uid; + lus->gid = session->gid; session->ust_session = lus; return LTTCOMM_OK; @@ -1942,14 +1950,16 @@ 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; @@ -2916,11 +2926,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; } @@ -3000,7 +3010,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; } @@ -3162,6 +3172,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 @@ -3398,7 +3413,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: @@ -3623,6 +3638,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) { @@ -3646,8 +3667,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); @@ -3685,10 +3706,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: @@ -3731,6 +3752,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"); } /* @@ -3761,12 +3783,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; @@ -3818,6 +3841,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; @@ -3925,27 +3951,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"); } @@ -4329,7 +4350,9 @@ int main(int argc, char **argv) } /* Setup kernel tracer */ - init_kernel_tracer(); + if (!opt_no_kernel) { + init_kernel_tracer(); + } /* Set ulimit for open files */ set_ulimit(); @@ -4355,7 +4378,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; }