X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fust-app.c;h=f4e4bedb594e89946907dc56b151f72fd4fa6665;hp=f3e0209014974f54cf547a91162dcb1c40a246e6;hb=ef67c0723053abd24b25186597388d84f76b6727;hpb=5da88b0f58d7f838068037ea449ddfb25d3e85ad diff --git a/src/bin/lttng-sessiond/ust-app.c b/src/bin/lttng-sessiond/ust-app.c index f3e020901..f4e4bedb5 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -1,23 +1,12 @@ /* - * Copyright (C) 2011 - David Goulet - * Copyright (C) 2016 - Jérémie Galarneau + * Copyright (C) 2011 David Goulet + * Copyright (C) 2016 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LGPL_SOURCE -#include #include #include #include @@ -29,6 +18,7 @@ #include #include +#include #include #include @@ -45,6 +35,10 @@ #include "notification-thread-commands.h" #include "rotate.h" +struct lttng_ht *ust_app_ht; +struct lttng_ht *ust_app_ht_by_sock; +struct lttng_ht *ust_app_ht_by_notify_sock; + static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); @@ -251,7 +245,7 @@ static struct ust_registry_session *get_session_registry( { struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( ua_sess->tracing_id, ua_sess->bits_per_long, - ua_sess->real_credentials.uid); + lttng_credentials_get_uid(&ua_sess->real_credentials)); if (!reg_uid) { goto error; } @@ -1025,7 +1019,7 @@ error_free: * Alloc new UST app channel. */ static -struct ust_app_channel *alloc_ust_app_channel(char *name, +struct ust_app_channel *alloc_ust_app_channel(const char *name, struct ust_app_session *ua_sess, struct lttng_ust_channel_attr *attr) { @@ -1149,7 +1143,7 @@ struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) if (uctx) { memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { - char *provider_name = NULL, *ctx_name = NULL; + char *provider_name = NULL, *ctx_name = NULL; provider_name = strdup(uctx->u.app_ctx.provider_name); ctx_name = strdup(uctx->u.app_ctx.ctx_name); @@ -1853,10 +1847,10 @@ static void shadow_copy_session(struct ust_app_session *ua_sess, ua_sess->tracing_id = usess->id; ua_sess->id = get_next_session_id(); - ua_sess->real_credentials.uid = app->uid; - ua_sess->real_credentials.gid = app->gid; - ua_sess->effective_credentials.uid = usess->uid; - ua_sess->effective_credentials.gid = usess->gid; + LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); + LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); + LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); + LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); ua_sess->buffer_type = usess->buffer_type; ua_sess->bits_per_long = app->bits_per_long; @@ -1878,7 +1872,7 @@ static void shadow_copy_session(struct ust_app_session *ua_sess, case LTTNG_BUFFER_PER_UID: ret = snprintf(ua_sess->path, sizeof(ua_sess->path), DEFAULT_UST_TRACE_UID_PATH, - ua_sess->real_credentials.uid, + lttng_credentials_get_uid(&ua_sess->real_credentials), app->bits_per_long); break; default: @@ -2001,8 +1995,9 @@ static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, app->uint64_t_alignment, app->long_alignment, app->byte_order, app->version.major, app->version.minor, reg_pid->root_shm_path, reg_pid->shm_path, - ua_sess->effective_credentials.uid, - ua_sess->effective_credentials.gid, ua_sess->tracing_id, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), + ua_sess->tracing_id, app->uid); if (ret < 0) { /* @@ -2302,7 +2297,7 @@ end: */ static int create_ust_app_channel_context(struct ust_app_channel *ua_chan, - struct lttng_ust_context_attr *uctx, + struct lttng_ust_context_attr *uctx, struct ust_app *app) { int ret = 0; @@ -2893,8 +2888,9 @@ static int create_channel_per_uid(struct ust_app *app, notification_ret = notification_thread_command_add_channel( notification_thread_handle, session->name, - ua_sess->effective_credentials.uid, - ua_sess->effective_credentials.gid, ua_chan->name, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), + ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); if (notification_ret != LTTNG_OK) { @@ -2993,8 +2989,9 @@ static int create_channel_per_pid(struct ust_app *app, cmd_ret = notification_thread_command_add_channel( notification_thread_handle, session->name, - ua_sess->effective_credentials.uid, - ua_sess->effective_credentials.gid, ua_chan->name, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), + ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); if (cmd_ret != LTTNG_OK) { @@ -4118,11 +4115,14 @@ int ust_app_channel_create(struct ltt_ust_session *usess, ret = ust_app_channel_allocate(ua_sess, uchan, LTTNG_UST_CHAN_PER_CPU, usess, &ua_chan); - if (ret == 0) { - ret = ust_app_channel_send(app, usess, - ua_sess, ua_chan); - } else { - goto end; + if (ret < 0) { + goto error; + } + + ret = ust_app_channel_send(app, usess, + ua_sess, ua_chan); + if (ret) { + goto error; } /* Add contexts. */ @@ -4130,10 +4130,12 @@ int ust_app_channel_create(struct ltt_ust_session *usess, ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); if (ret) { - goto end; + goto error; } } } + +error: if (ret < 0) { switch (ret) { case -ENOTCONN: @@ -4149,7 +4151,7 @@ int ust_app_channel_create(struct ltt_ust_session *usess, break; } } -end: + if (ret == 0 && _ua_chan) { /* * Only return the application's channel on success. Note @@ -4161,75 +4163,6 @@ end: return ret; } -/* - * For a specific UST session, create the channel for all registered apps. - */ -int ust_app_create_channel_glb(struct ltt_ust_session *usess, - struct ltt_ust_channel *uchan) -{ - int ret = 0; - struct cds_lfht_iter iter; - struct ust_app *app; - - assert(usess); - assert(usess->active); - assert(uchan); - - DBG2("UST app adding channel %s to UST domain for session id %" PRIu64, - uchan->name, usess->id); - - rcu_read_lock(); - /* For every registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter, app, pid_n.node) { - struct ust_app_session *ua_sess; - int session_was_created = 0; - - if (!app->compatible || - !trace_ust_pid_tracker_lookup(usess, app->pid)) { - goto error_rcu_unlock; - } - - /* - * Create session on the tracer side and add it to app session HT. Note - * that if session exist, it will simply return a pointer to the ust - * app session. - */ - ret = find_or_create_ust_app_session(usess, app, &ua_sess, - &session_was_created); - if (ret < 0) { - switch (ret) { - case -ENOTCONN: - /* - * The application's socket is not valid. Either a bad - * socket or a timeout on it. We can't inform the caller - * that for a specific app, the session failed so lets - * continue here; it is not an error. - */ - ret = 0; - goto error_rcu_unlock; - case -ENOMEM: - default: - goto error_rcu_unlock; - } - } - - if (ua_sess->deleted) { - continue; - } - ret = ust_app_channel_create(usess, ua_sess, uchan, app, NULL); - if (ret) { - if (session_was_created) { - destroy_app_session(app, ua_sess); - } - /* Continue to the next application. */ - } - } - -error_rcu_unlock: - rcu_read_unlock(); - return ret; -} - /* * Enable event for a specific session and channel on the tracer. */ @@ -4416,20 +4349,16 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) goto end; } + if (ua_sess->enabled) { + pthread_mutex_unlock(&ua_sess->lock); + goto end; + } + /* Upon restart, we skip the setup, already done */ if (ua_sess->started) { goto skip_setup; } - /* - * Create the metadata for the application. This returns gracefully if a - * metadata was already set for the session. - */ - ret = create_ust_app_metadata(ua_sess, app, usess->consumer); - if (ret < 0) { - goto error_unlock; - } - health_code_update(); skip_setup: @@ -4456,6 +4385,7 @@ skip_setup: /* Indicate that the session has been started once */ ua_sess->started = 1; + ua_sess->enabled = 1; pthread_mutex_unlock(&ua_sess->lock); @@ -4545,6 +4475,7 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) } health_code_update(); + ua_sess->enabled = 0; /* Quiescent wait after stopping trace */ pthread_mutex_lock(&app->sock_lock); @@ -5064,7 +4995,7 @@ end: /* * The caller must ensure that the application is compatible and is tracked - * by the PID tracker. + * by the process attribute trackers. */ static void ust_app_synchronize(struct ltt_ust_session *usess, @@ -5095,6 +5026,7 @@ void ust_app_synchronize(struct ltt_ust_session *usess, } rcu_read_lock(); + cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, uchan, node.node) { struct ust_app_channel *ua_chan; @@ -5108,7 +5040,7 @@ void ust_app_synchronize(struct ltt_ust_session *usess, * allocated (if necessary) and sent to the application, and * all enabled contexts will be added to the channel. */ - ret = find_or_create_ust_app_channel(usess, ua_sess, + ret = find_or_create_ust_app_channel(usess, ua_sess, app, uchan, &ua_chan); if (ret) { /* Tracer is probably gone or ENOMEM. */ @@ -5138,6 +5070,21 @@ void ust_app_synchronize(struct ltt_ust_session *usess, } } } + + /* + * Create the metadata for the application. This returns gracefully if a + * metadata was already set for the session. + * + * The metadata channel must be created after the data channels as the + * consumer daemon assumes this ordering. When interacting with a relay + * daemon, the consumer will use this assumption to send the + * "STREAMS_SENT" message to the relay daemon. + */ + ret = create_ust_app_metadata(ua_sess, app, usess->consumer); + if (ret < 0) { + goto error_unlock; + } + rcu_read_unlock(); end: @@ -5184,7 +5131,14 @@ void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) if (!app->compatible) { return; } - if (trace_ust_pid_tracker_lookup(usess, app->pid)) { + if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, + usess, app->pid) && + trace_ust_id_tracker_lookup( + LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, + usess, app->uid) && + trace_ust_id_tracker_lookup( + LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, + usess, app->gid)) { /* * Synchronize the application's internal tracing configuration * and start tracing. @@ -5953,7 +5907,7 @@ enum lttng_error_code ust_app_snapshot_record( status = LTTNG_ERR_INVALID; goto error; } - /* Add the UST default trace dir to path. */ + /* Add the UST default trace dir to path. */ cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, reg_chan, node.node) { status = consumer_snapshot_channel(socket, @@ -6017,14 +5971,12 @@ enum lttng_error_code ust_app_snapshot_record( status = LTTNG_ERR_INVALID; goto error; } - cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, + cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) { status = consumer_snapshot_channel(socket, ua_chan->key, output, 0, - ua_sess->effective_credentials - .uid, - ua_sess->effective_credentials - .gid, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), &trace_path[consumer_path_offset], wait, nb_packets_per_stream); switch (status) { @@ -6044,8 +5996,8 @@ enum lttng_error_code ust_app_snapshot_record( } status = consumer_snapshot_channel(socket, registry->metadata_key, output, 1, - ua_sess->effective_credentials.uid, - ua_sess->effective_credentials.gid, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), &trace_path[consumer_path_offset], wait, 0); switch (status) { case LTTNG_OK: @@ -6326,6 +6278,11 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) struct buffer_reg_channel *reg_chan; struct consumer_socket *socket; + if (!reg->registry->reg.ust->metadata_key) { + /* Skip since no metadata is present */ + continue; + } + /* Get consumer socket to use to push the metadata.*/ socket = consumer_find_socket_by_bitness(reg->bits_per_long, usess->consumer); @@ -6396,10 +6353,8 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) ua_chan, node.node) { ret = consumer_rotate_channel(socket, ua_chan->key, - ua_sess->effective_credentials - .uid, - ua_sess->effective_credentials - .gid, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), ua_sess->consumer, /* is_metadata_channel */ false); if (ret < 0) { @@ -6415,8 +6370,8 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) (void) push_metadata(registry, usess->consumer); ret = consumer_rotate_channel(socket, registry->metadata_key, - ua_sess->effective_credentials.uid, - ua_sess->effective_credentials.gid, + lttng_credentials_get_uid(&ua_sess->effective_credentials), + lttng_credentials_get_gid(&ua_sess->effective_credentials), ua_sess->consumer, /* is_metadata_channel */ true); if (ret < 0) { @@ -6547,3 +6502,274 @@ error: rcu_read_unlock(); return ret; } + +/* + * Clear all the channels of a session. + * + * Return LTTNG_OK on success or else an LTTng error code. + */ +enum lttng_error_code ust_app_clear_session(struct ltt_session *session) +{ + int ret; + enum lttng_error_code cmd_ret = LTTNG_OK; + struct lttng_ht_iter iter; + struct ust_app *app; + struct ltt_ust_session *usess = session->ust_session; + + assert(usess); + + rcu_read_lock(); + + if (usess->active) { + ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); + cmd_ret = LTTNG_ERR_FATAL; + goto end; + } + + switch (usess->buffer_type) { + case LTTNG_BUFFER_PER_UID: + { + struct buffer_reg_uid *reg; + + cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { + struct buffer_reg_channel *reg_chan; + struct consumer_socket *socket; + + /* Get consumer socket to use to push the metadata.*/ + socket = consumer_find_socket_by_bitness(reg->bits_per_long, + usess->consumer); + if (!socket) { + cmd_ret = LTTNG_ERR_INVALID; + goto error_socket; + } + + /* Clear the data channels. */ + cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, + reg_chan, node.node) { + ret = consumer_clear_channel(socket, + reg_chan->consumer_key); + if (ret < 0) { + goto error; + } + } + + (void) push_metadata(reg->registry->reg.ust, usess->consumer); + + /* + * Clear the metadata channel. + * Metadata channel is not cleared per se but we still need to + * perform a rotation operation on it behind the scene. + */ + ret = consumer_clear_channel(socket, + reg->registry->reg.ust->metadata_key); + if (ret < 0) { + goto error; + } + } + break; + } + case LTTNG_BUFFER_PER_PID: + { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { + struct consumer_socket *socket; + struct lttng_ht_iter chan_iter; + struct ust_app_channel *ua_chan; + struct ust_app_session *ua_sess; + struct ust_registry_session *registry; + + ua_sess = lookup_session_by_app(usess, app); + if (!ua_sess) { + /* Session not associated with this app. */ + continue; + } + + /* Get the right consumer socket for the application. */ + socket = consumer_find_socket_by_bitness(app->bits_per_long, + usess->consumer); + if (!socket) { + cmd_ret = LTTNG_ERR_INVALID; + goto error_socket; + } + + registry = get_session_registry(ua_sess); + if (!registry) { + DBG("Application session is being torn down. Skip application."); + continue; + } + + /* Clear the data channels. */ + cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, + ua_chan, node.node) { + ret = consumer_clear_channel(socket, ua_chan->key); + if (ret < 0) { + /* Per-PID buffer and application going away. */ + if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { + continue; + } + goto error; + } + } + + (void) push_metadata(registry, usess->consumer); + + /* + * Clear the metadata channel. + * Metadata channel is not cleared per se but we still need to + * perform rotation operation on it behind the scene. + */ + ret = consumer_clear_channel(socket, registry->metadata_key); + if (ret < 0) { + /* Per-PID buffer and application going away. */ + if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { + continue; + } + goto error; + } + } + break; + } + default: + assert(0); + break; + } + + cmd_ret = LTTNG_OK; + goto end; + +error: + switch (-ret) { + case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: + cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; + break; + default: + cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; + } + +error_socket: +end: + rcu_read_unlock(); + return cmd_ret; +} + +/* + * This function skips the metadata channel as the begin/end timestamps of a + * metadata packet are useless. + * + * Moreover, opening a packet after a "clear" will cause problems for live + * sessions as it will introduce padding that was not part of the first trace + * chunk. The relay daemon expects the content of the metadata stream of + * successive metadata trace chunks to be strict supersets of one another. + * + * For example, flushing a packet at the beginning of the metadata stream of + * a trace chunk resulting from a "clear" session command will cause the + * size of the metadata stream of the new trace chunk to not match the size of + * the metadata stream of the original chunk. This will confuse the relay + * daemon as the same "offset" in a metadata stream will no longer point + * to the same content. + */ +enum lttng_error_code ust_app_open_packets(struct ltt_session *session) +{ + enum lttng_error_code ret = LTTNG_OK; + struct lttng_ht_iter iter; + struct ltt_ust_session *usess = session->ust_session; + + assert(usess); + + rcu_read_lock(); + + switch (usess->buffer_type) { + case LTTNG_BUFFER_PER_UID: + { + struct buffer_reg_uid *reg; + + cds_list_for_each_entry ( + reg, &usess->buffer_reg_uid_list, lnode) { + struct buffer_reg_channel *reg_chan; + struct consumer_socket *socket; + + socket = consumer_find_socket_by_bitness( + reg->bits_per_long, usess->consumer); + if (!socket) { + ret = LTTNG_ERR_FATAL; + goto error; + } + + cds_lfht_for_each_entry(reg->registry->channels->ht, + &iter.iter, reg_chan, node.node) { + const int open_ret = + consumer_open_channel_packets( + socket, + reg_chan->consumer_key); + + if (open_ret < 0) { + ret = LTTNG_ERR_UNK; + goto error; + } + } + } + break; + } + case LTTNG_BUFFER_PER_PID: + { + struct ust_app *app; + + cds_lfht_for_each_entry ( + ust_app_ht->ht, &iter.iter, app, pid_n.node) { + struct consumer_socket *socket; + struct lttng_ht_iter chan_iter; + struct ust_app_channel *ua_chan; + struct ust_app_session *ua_sess; + struct ust_registry_session *registry; + + ua_sess = lookup_session_by_app(usess, app); + if (!ua_sess) { + /* Session not associated with this app. */ + continue; + } + + /* Get the right consumer socket for the application. */ + socket = consumer_find_socket_by_bitness( + app->bits_per_long, usess->consumer); + if (!socket) { + ret = LTTNG_ERR_FATAL; + goto error; + } + + registry = get_session_registry(ua_sess); + if (!registry) { + DBG("Application session is being torn down. Skip application."); + continue; + } + + cds_lfht_for_each_entry(ua_sess->channels->ht, + &chan_iter.iter, ua_chan, node.node) { + const int open_ret = + consumer_open_channel_packets( + socket, + ua_chan->key); + + if (open_ret < 0) { + /* + * Per-PID buffer and application going + * away. + */ + if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { + continue; + } + + ret = LTTNG_ERR_UNK; + goto error; + } + } + } + break; + } + default: + abort(); + break; + } + +error: + rcu_read_unlock(); + return ret; +}