X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fcmd.cpp;h=0b74920556d59230a5bf2a87adcef42590dd1627;hb=139a8d250fb18f8ffc95b0936f7285f7b484b72f;hp=84254f1a57c936d75457433e540db4b368c454fc;hpb=1c9a0b0e83c7e073c4e576c0bed95de335b0e502;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/cmd.cpp b/src/bin/lttng-sessiond/cmd.cpp index 84254f1a5..0b7492055 100644 --- a/src/bin/lttng-sessiond/cmd.cpp +++ b/src/bin/lttng-sessiond/cmd.cpp @@ -15,57 +15,70 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -#include -#include +#include #include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "agent-thread.h" -#include "agent.h" -#include "buffer-registry.h" -#include "channel.h" -#include "cmd.h" -#include "consumer.h" -#include "event-notifier-error-accounting.h" -#include "event.h" -#include "health-sessiond.h" -#include "kernel-consumer.h" -#include "kernel.h" -#include "lttng-sessiond.h" -#include "lttng-syscall.h" -#include "notification-thread-commands.h" -#include "notification-thread.h" -#include "rotate.h" -#include "rotation-thread.h" -#include "session.h" -#include "timer.h" -#include "tracker.h" -#include "utils.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "agent-thread.hpp" +#include "agent.hpp" +#include "buffer-registry.hpp" +#include "channel.hpp" +#include "cmd.hpp" +#include "consumer.hpp" +#include "event-notifier-error-accounting.hpp" +#include "event.hpp" +#include "health-sessiond.hpp" +#include "kernel-consumer.hpp" +#include "kernel.hpp" +#include "lttng-sessiond.hpp" +#include "lttng-syscall.hpp" +#include "notification-thread-commands.hpp" +#include "notification-thread.hpp" +#include "rotate.hpp" +#include "rotation-thread.hpp" +#include "session.hpp" +#include "timer.hpp" +#include "tracker.hpp" +#include "utils.hpp" /* Sleep for 100ms between each check for the shm path's deletion. */ #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 +namespace lsu = lttng::sessiond::ust; + +static enum lttng_error_code wait_on_path(void *path); + +namespace { struct cmd_destroy_session_reply_context { int reply_sock_fd; bool implicit_rotation_on_destroy; @@ -76,15 +89,13 @@ struct cmd_destroy_session_reply_context { enum lttng_error_code destruction_status; }; -static enum lttng_error_code wait_on_path(void *path); - /* * Command completion handler that is used by the destroy command * when a session that has a non-default shm_path is being destroyed. * * See comment in cmd_destroy_session() for the rationale. */ -static struct destroy_completion_handler { +struct destroy_completion_handler { struct cmd_completion_handler handler; char shm_path[member_sizeof(struct ltt_session, shm_path)]; } destroy_completion_handler = { @@ -95,17 +106,17 @@ static struct destroy_completion_handler { .shm_path = { 0 }, }; -static struct cmd_completion_handler *current_completion_handler; - /* * Used to keep a unique index for each relayd socket created where this value * is associated with streams on the consumer so it can match the right relayd * to send to. It must be accessed with the relayd_net_seq_idx_lock * held. */ -static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; -static uint64_t relayd_net_seq_idx; +pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; +uint64_t relayd_net_seq_idx; +} /* namespace */ +static struct cmd_completion_handler *current_completion_handler; static int validate_ust_event_name(const char *); static int cmd_enable_event_internal(struct ltt_session *session, const struct lttng_domain *domain, @@ -114,7 +125,8 @@ static int cmd_enable_event_internal(struct ltt_session *session, struct lttng_bytecode *filter, struct lttng_event_exclusion *exclusion, int wpipe); -static int cmd_enable_channel_internal(struct ltt_session *session, +static enum lttng_error_code cmd_enable_channel_internal( + struct ltt_session *session, const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe); @@ -1273,12 +1285,13 @@ end: return ret; } -static int cmd_enable_channel_internal(struct ltt_session *session, +static enum lttng_error_code cmd_enable_channel_internal( + struct ltt_session *session, const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe) { - int ret; + enum lttng_error_code ret_code; struct ltt_ust_session *usess = session->ust_session; struct lttng_ht *chan_ht; size_t len; @@ -1290,7 +1303,7 @@ static int cmd_enable_channel_internal(struct ltt_session *session, attr = lttng_channel_copy(_attr); if (!attr) { - ret = -LTTNG_ERR_NOMEM; + ret_code = LTTNG_ERR_NOMEM; goto end; } @@ -1299,7 +1312,7 @@ static int cmd_enable_channel_internal(struct ltt_session *session, /* Validate channel name */ if (attr->name[0] == '.' || memchr(attr->name, '/', len) != NULL) { - ret = LTTNG_ERR_INVALID_CHANNEL_NAME; + ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; goto end; } @@ -1338,12 +1351,12 @@ static int cmd_enable_channel_internal(struct ltt_session *session, case LTTNG_DOMAIN_PYTHON: if (!agent_tracing_is_enabled()) { DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); - ret = LTTNG_ERR_AGENT_TRACING_DISABLED; + ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED; goto error; } break; default: - ret = LTTNG_ERR_UNKNOWN_DOMAIN; + ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; goto error; } @@ -1360,7 +1373,7 @@ static int cmd_enable_channel_internal(struct ltt_session *session, * some point in time before. The tracer does not allow it. */ if (session->has_been_started) { - ret = LTTNG_ERR_TRACE_ALREADY_STARTED; + ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; goto error; } @@ -1369,16 +1382,16 @@ static int cmd_enable_channel_internal(struct ltt_session *session, /* Enforce mmap output for snapshot sessions. */ attr->attr.output = LTTNG_EVENT_MMAP; } - ret = channel_kernel_create( + ret_code = channel_kernel_create( session->kernel_session, attr, wpipe); if (attr->name[0] != '\0') { session->kernel_session->has_non_default_channel = 1; } } else { - ret = channel_kernel_enable(session->kernel_session, kchan); + ret_code = channel_kernel_enable(session->kernel_session, kchan); } - if (ret != LTTNG_OK) { + if (ret_code != LTTNG_OK) { goto error; } @@ -1403,19 +1416,19 @@ static int cmd_enable_channel_internal(struct ltt_session *session, if (domain->type == LTTNG_DOMAIN_JUL) { if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, LTTNG_SYMBOL_NAME_LEN)) { - ret = LTTNG_ERR_INVALID_CHANNEL_NAME; + ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; goto error; } } else if (domain->type == LTTNG_DOMAIN_LOG4J) { if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, LTTNG_SYMBOL_NAME_LEN)) { - ret = LTTNG_ERR_INVALID_CHANNEL_NAME; + ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; goto error; } } else if (domain->type == LTTNG_DOMAIN_PYTHON) { if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME, LTTNG_SYMBOL_NAME_LEN)) { - ret = LTTNG_ERR_INVALID_CHANNEL_NAME; + ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; goto error; } } @@ -1429,32 +1442,32 @@ static int cmd_enable_channel_internal(struct ltt_session *session, * some point in time before. The tracer does not allow it. */ if (session->has_been_started) { - ret = LTTNG_ERR_TRACE_ALREADY_STARTED; + ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; goto error; } - ret = channel_ust_create(usess, attr, domain->buf_type); + ret_code = channel_ust_create(usess, attr, domain->buf_type); if (attr->name[0] != '\0') { usess->has_non_default_channel = 1; } } else { - ret = channel_ust_enable(usess, uchan); + ret_code = channel_ust_enable(usess, uchan); } break; } default: - ret = LTTNG_ERR_UNKNOWN_DOMAIN; + ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; goto error; } - if (ret == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { + if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { session->has_non_mmap_channel = true; } error: rcu_read_unlock(); end: lttng_channel_destroy(attr); - return ret; + return ret_code; } enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy( @@ -1921,8 +1934,8 @@ int cmd_add_context(struct command_ctx *cmd_ctx, ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; goto error; } - /* break is _not_ missing here. */ } + /* fall through */ case LTTNG_DOMAIN_UST: { struct ltt_ust_session *usess = session->ust_session; @@ -2130,7 +2143,7 @@ static int _cmd_enable_event(struct ltt_session *session, } } if (filter) { - filter_a = (lttng_bytecode *) zmalloc(sizeof(*filter_a) + filter->len); + filter_a = zmalloc(sizeof(*filter_a) + filter->len); if (!filter_a) { free(filter_expression_a); ret = LTTNG_ERR_FATAL; @@ -2358,7 +2371,7 @@ static int _cmd_enable_event(struct ltt_session *session, struct lttng_bytecode) + filter->len; - filter_copy = (lttng_bytecode *) zmalloc(filter_size); + filter_copy = zmalloc(filter_size); if (!filter_copy) { ret = LTTNG_ERR_NOMEM; goto error; @@ -3123,6 +3136,28 @@ enum lttng_error_code cmd_create_session_from_descriptor( goto end; } + ret_code = notification_thread_command_add_session(the_notification_thread_handle, + new_session->id, new_session->name, new_session->uid, new_session->gid); + if (ret_code != LTTNG_OK) { + goto end; + } + + /* Announce the session's destruction to the notification thread when it is destroyed. */ + ret = session_add_destroy_notifier( + new_session, + [](const struct ltt_session *session, + void *user_data __attribute__((unused))) { + (void) notification_thread_command_remove_session( + the_notification_thread_handle, session->id); + }, + NULL); + if (ret) { + PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s", + new_session->name); + ret = LTTNG_ERR_NOMEM; + goto end; + } + if (!session_name) { ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name); @@ -3370,11 +3405,12 @@ int cmd_destroy_session(struct ltt_session *session, struct cmd_destroy_session_reply_context *reply_context = NULL; if (sock_fd) { - reply_context = (cmd_destroy_session_reply_context *) zmalloc(sizeof(*reply_context)); + reply_context = zmalloc(); if (!reply_context) { ret = LTTNG_ERR_NOMEM; goto end; } + reply_context->reply_sock_fd = *sock_fd; } @@ -3573,12 +3609,13 @@ int cmd_register_consumer(struct ltt_session *session, goto error; } - socket->lock = (pthread_mutex_t *) zmalloc(sizeof(pthread_mutex_t)); + socket->lock = zmalloc(); if (socket->lock == NULL) { PERROR("zmalloc pthread mutex"); ret = LTTNG_ERR_FATAL; goto error; } + pthread_mutex_init(socket->lock, NULL); socket->registered = 1; @@ -3641,7 +3678,7 @@ ssize_t cmd_list_domains(struct ltt_session *session, goto end; } - *domains = (lttng_domain *) zmalloc(nb_dom * sizeof(struct lttng_domain)); + *domains = calloc(nb_dom); if (*domains == NULL) { ret = LTTNG_ERR_FATAL; goto error; @@ -4183,7 +4220,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, goto end; } - list = (lttng_snapshot_output *) zmalloc(session->snapshot.nb_output * sizeof(*list)); + list = calloc(session->snapshot.nb_output); if (!list) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -4283,99 +4320,6 @@ end: return ret; } -static -int clear_metadata_file(int fd) -{ - int ret; - off_t lseek_ret; - - lseek_ret = lseek(fd, 0, SEEK_SET); - if (lseek_ret < 0) { - PERROR("lseek"); - ret = -1; - goto end; - } - - ret = ftruncate(fd, 0); - if (ret < 0) { - PERROR("ftruncate"); - goto end; - } - -end: - return ret; -} - -static -int ust_regenerate_metadata(struct ltt_ust_session *usess) -{ - int ret = 0; - struct buffer_reg_uid *uid_reg = NULL; - struct buffer_reg_session *session_reg = NULL; - - rcu_read_lock(); - cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { - struct ust_registry_session *registry; - struct ust_registry_channel *chan; - struct lttng_ht_iter iter_chan; - - session_reg = uid_reg->registry; - registry = session_reg->reg.ust; - - pthread_mutex_lock(®istry->lock); - registry->metadata_len_sent = 0; - memset(registry->metadata, 0, registry->metadata_alloc_len); - registry->metadata_len = 0; - registry->metadata_version++; - if (registry->metadata_fd > 0) { - /* Clear the metadata file's content. */ - ret = clear_metadata_file(registry->metadata_fd); - if (ret) { - pthread_mutex_unlock(®istry->lock); - goto end; - } - } - - ret = ust_metadata_session_statedump(registry, NULL, - registry->major, registry->minor); - if (ret) { - pthread_mutex_unlock(®istry->lock); - ERR("Failed to generate session metadata (err = %d)", - ret); - goto end; - } - cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, - chan, node.node) { - struct ust_registry_event *event; - struct lttng_ht_iter iter_event; - - ret = ust_metadata_channel_statedump(registry, chan); - if (ret) { - pthread_mutex_unlock(®istry->lock); - ERR("Failed to generate channel metadata " - "(err = %d)", ret); - goto end; - } - cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, - event, node.node) { - ret = ust_metadata_event_statedump(registry, - chan, event); - if (ret) { - pthread_mutex_unlock(®istry->lock); - ERR("Failed to generate event metadata " - "(err = %d)", ret); - goto end; - } - } - } - pthread_mutex_unlock(®istry->lock); - } - -end: - rcu_read_unlock(); - return ret; -} - /* * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. * @@ -4406,7 +4350,7 @@ int cmd_regenerate_metadata(struct ltt_session *session) } if (session->ust_session) { - ret = ust_regenerate_metadata(session->ust_session); + ret = trace_ust_regenerate_metadata(session->ust_session); if (ret < 0) { ERR("Failed to regenerate the UST metadata"); goto end; @@ -5687,8 +5631,7 @@ int cmd_rotate_session(struct ltt_session *session, chunk_being_archived = NULL; if (!quiet_rotation) { ret = notification_thread_command_session_rotation_ongoing( - the_notification_thread_handle, session->name, - session->uid, session->gid, + the_notification_thread_handle, session->id, ongoing_rotation_chunk_id); if (ret != LTTNG_OK) { ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",