+ pthread_mutex_unlock(socket->lock);
+ health_code_update();
+ return ret;
+}
+
+/*
+ * Ask the consumer to snapshot a specific channel using the key.
+ *
+ * Returns LTTNG_OK on success or else an LTTng error code.
+ */
+enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
+ uint64_t key, const struct consumer_output *output, int metadata,
+ uid_t uid, gid_t gid, const char *channel_path, int wait,
+ uint64_t nb_packets_per_stream)
+{
+ int ret;
+ enum lttng_error_code status = LTTNG_OK;
+ struct lttcomm_consumer_msg msg;
+
+ assert(socket);
+ assert(output);
+
+ DBG("Consumer snapshot channel key %" PRIu64, key);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL;
+ msg.u.snapshot_channel.key = key;
+ msg.u.snapshot_channel.nb_packets_per_stream = nb_packets_per_stream;
+ msg.u.snapshot_channel.metadata = metadata;
+
+ if (output->type == CONSUMER_DST_NET) {
+ msg.u.snapshot_channel.relayd_id =
+ output->net_seq_index;
+ msg.u.snapshot_channel.use_relayd = 1;
+ } else {
+ msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL;
+ }
+ ret = lttng_strncpy(msg.u.snapshot_channel.pathname,
+ channel_path,
+ sizeof(msg.u.snapshot_channel.pathname));
+ if (ret < 0) {
+ ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
+ sizeof(msg.u.snapshot_channel.pathname),
+ strlen(channel_path),
+ channel_path);
+ status = LTTNG_ERR_SNAPSHOT_FAIL;
+ goto error;
+ }
+
+ health_code_update();
+ pthread_mutex_lock(socket->lock);
+ ret = consumer_send_msg(socket, &msg);
+ pthread_mutex_unlock(socket->lock);
+ if (ret < 0) {
+ switch (-ret) {
+ case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
+ status = LTTNG_ERR_CHAN_NOT_FOUND;
+ break;
+ default:
+ status = LTTNG_ERR_SNAPSHOT_FAIL;
+ break;
+ }
+ goto error;
+ }
+
+error:
+ health_code_update();
+ return status;
+}
+
+/*
+ * Ask the consumer the number of discarded events for a channel.
+ */
+int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
+ struct consumer_output *consumer, uint64_t *discarded)
+{
+ int ret;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+ struct lttcomm_consumer_msg msg;
+
+ assert(consumer);
+
+ DBG3("Consumer discarded events id %" PRIu64, session_id);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.cmd_type = LTTNG_CONSUMER_DISCARDED_EVENTS;
+ msg.u.discarded_events.session_id = session_id;
+ msg.u.discarded_events.channel_key = channel_key;
+
+ *discarded = 0;
+
+ /* Send command for each consumer */
+ rcu_read_lock();
+ cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
+ node.node) {
+ uint64_t consumer_discarded = 0;
+ pthread_mutex_lock(socket->lock);
+ ret = consumer_socket_send(socket, &msg, sizeof(msg));
+ if (ret < 0) {
+ pthread_mutex_unlock(socket->lock);
+ goto end;
+ }
+
+ /*
+ * No need for a recv reply status because the answer to the
+ * command is the reply status message.
+ */
+ ret = consumer_socket_recv(socket, &consumer_discarded,
+ sizeof(consumer_discarded));
+ if (ret < 0) {
+ ERR("get discarded events");
+ pthread_mutex_unlock(socket->lock);
+ goto end;
+ }
+ pthread_mutex_unlock(socket->lock);
+ *discarded += consumer_discarded;
+ }
+ ret = 0;
+ DBG("Consumer discarded %" PRIu64 " events in session id %" PRIu64,
+ *discarded, session_id);
+
+end:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Ask the consumer the number of lost packets for a channel.
+ */
+int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
+ struct consumer_output *consumer, uint64_t *lost)
+{
+ int ret;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+ struct lttcomm_consumer_msg msg;
+
+ assert(consumer);
+
+ DBG3("Consumer lost packets id %" PRIu64, session_id);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.cmd_type = LTTNG_CONSUMER_LOST_PACKETS;
+ msg.u.lost_packets.session_id = session_id;
+ msg.u.lost_packets.channel_key = channel_key;
+
+ *lost = 0;
+
+ /* Send command for each consumer */
+ rcu_read_lock();
+ cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
+ node.node) {
+ uint64_t consumer_lost = 0;
+ pthread_mutex_lock(socket->lock);
+ ret = consumer_socket_send(socket, &msg, sizeof(msg));
+ if (ret < 0) {
+ pthread_mutex_unlock(socket->lock);
+ goto end;
+ }
+
+ /*
+ * No need for a recv reply status because the answer to the
+ * command is the reply status message.
+ */
+ ret = consumer_socket_recv(socket, &consumer_lost,
+ sizeof(consumer_lost));
+ if (ret < 0) {
+ ERR("get lost packets");
+ pthread_mutex_unlock(socket->lock);
+ goto end;
+ }
+ pthread_mutex_unlock(socket->lock);
+ *lost += consumer_lost;
+ }
+ ret = 0;
+ DBG("Consumer lost %" PRIu64 " packets in session id %" PRIu64,
+ *lost, session_id);
+
+end:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Ask the consumer to rotate a channel.
+ *
+ * The new_chunk_id is the session->rotate_count that has been incremented
+ * when the rotation started. On the relay, this allows to keep track in which
+ * chunk each stream is currently writing to (for the rotate_pending operation).
+ */
+int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
+ uid_t uid, gid_t gid, struct consumer_output *output,
+ bool is_metadata_channel)
+{
+ int ret;
+ struct lttcomm_consumer_msg msg;
+
+ assert(socket);
+
+ DBG("Consumer rotate channel key %" PRIu64, key);
+
+ pthread_mutex_lock(socket->lock);
+ memset(&msg, 0, sizeof(msg));
+ msg.cmd_type = LTTNG_CONSUMER_ROTATE_CHANNEL;
+ msg.u.rotate_channel.key = key;
+ msg.u.rotate_channel.metadata = !!is_metadata_channel;
+
+ if (output->type == CONSUMER_DST_NET) {
+ msg.u.rotate_channel.relayd_id = output->net_seq_index;
+ } else {
+ msg.u.rotate_channel.relayd_id = (uint64_t) -1ULL;
+ }
+
+ health_code_update();
+ ret = consumer_send_msg(socket, &msg);
+ if (ret < 0) {
+ switch (-ret) {
+ case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
+ ret = -LTTNG_ERR_CHAN_NOT_FOUND;
+ break;
+ default:
+ ret = -LTTNG_ERR_ROTATION_FAIL_CONSUMER;
+ break;
+ }
+ goto error;
+ }
+error:
+ pthread_mutex_unlock(socket->lock);
+ health_code_update();
+ return ret;
+}
+
+int consumer_clear_channel(struct consumer_socket *socket, uint64_t key)
+{
+ int ret;
+ struct lttcomm_consumer_msg msg;
+
+ assert(socket);
+
+ DBG("Consumer clear channel %" PRIu64, key);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.cmd_type = LTTNG_CONSUMER_CLEAR_CHANNEL;
+ msg.u.clear_channel.key = key;
+
+ health_code_update();
+
+ pthread_mutex_lock(socket->lock);
+ ret = consumer_send_msg(socket, &msg);
+ if (ret < 0) {
+ goto error_socket;
+ }
+
+error_socket:
+ pthread_mutex_unlock(socket->lock);
+
+ health_code_update();
+ return ret;
+}
+
+int consumer_init(struct consumer_socket *socket,
+ const lttng_uuid sessiond_uuid)
+{
+ int ret;
+ struct lttcomm_consumer_msg msg = {
+ .cmd_type = LTTNG_CONSUMER_INIT,
+ };
+
+ assert(socket);
+
+ DBG("Sending consumer initialization command");
+ lttng_uuid_copy(msg.u.init.sessiond_uuid, sessiond_uuid);
+
+ health_code_update();
+ ret = consumer_send_msg(socket, &msg);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ health_code_update();
+ return ret;
+}
+
+/*
+ * Ask the consumer to create a new chunk for a given session.
+ *
+ * Called with the consumer socket lock held.
+ */
+int consumer_create_trace_chunk(struct consumer_socket *socket,
+ uint64_t relayd_id, uint64_t session_id,
+ struct lttng_trace_chunk *chunk,
+ const char *domain_subdir)
+{
+ int ret;
+ enum lttng_trace_chunk_status chunk_status;
+ struct lttng_credentials chunk_credentials;
+ const struct lttng_directory_handle *chunk_directory_handle = NULL;
+ struct lttng_directory_handle *domain_handle = NULL;
+ int domain_dirfd;
+ const char *chunk_name;
+ bool chunk_name_overridden;
+ uint64_t chunk_id;
+ time_t creation_timestamp;
+ char creation_timestamp_buffer[ISO8601_STR_LEN];
+ const char *creation_timestamp_str = "(none)";
+ const bool chunk_has_local_output = relayd_id == -1ULL;
+ struct lttcomm_consumer_msg msg = {
+ .cmd_type = LTTNG_CONSUMER_CREATE_TRACE_CHUNK,
+ .u.create_trace_chunk.session_id = session_id,
+ };
+
+ assert(socket);
+ assert(chunk);
+
+ if (relayd_id != -1ULL) {
+ LTTNG_OPTIONAL_SET(&msg.u.create_trace_chunk.relayd_id,
+ relayd_id);
+ }
+
+ chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name,
+ &chunk_name_overridden);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
+ chunk_status != LTTNG_TRACE_CHUNK_STATUS_NONE) {
+ ERR("Failed to get name of trace chunk");
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ if (chunk_name_overridden) {
+ ret = lttng_strncpy(msg.u.create_trace_chunk.override_name,
+ chunk_name,
+ sizeof(msg.u.create_trace_chunk.override_name));
+ if (ret) {
+ ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
+ chunk_name);
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ }
+
+ chunk_status = lttng_trace_chunk_get_creation_timestamp(chunk,
+ &creation_timestamp);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ msg.u.create_trace_chunk.creation_timestamp =
+ (uint64_t) creation_timestamp;
+ /* Only used for logging purposes. */
+ ret = time_to_iso8601_str(creation_timestamp,
+ creation_timestamp_buffer,
+ sizeof(creation_timestamp_buffer));
+ creation_timestamp_str = !ret ? creation_timestamp_buffer :
+ "(formatting error)";
+
+ chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ /*
+ * Anonymous trace chunks should never be transmitted
+ * to remote peers (consumerd and relayd). They are used
+ * internally for backward-compatibility purposes.
+ */
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ msg.u.create_trace_chunk.chunk_id = chunk_id;
+
+ if (chunk_has_local_output) {
+ chunk_status = lttng_trace_chunk_borrow_chunk_directory_handle(
+ chunk, &chunk_directory_handle);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ chunk_status = lttng_trace_chunk_get_credentials(
+ chunk, &chunk_credentials);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ /*
+ * Not associating credentials to a sessiond chunk is a
+ * fatal internal error.
+ */
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ ret = lttng_directory_handle_create_subdirectory_as_user(
+ chunk_directory_handle,
+ domain_subdir,
+ S_IRWXU | S_IRWXG,
+ &chunk_credentials);
+ if (ret) {
+ PERROR("Failed to create chunk domain output directory \"%s\"",
+ domain_subdir);
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ domain_handle = lttng_directory_handle_create_from_handle(
+ domain_subdir,
+ chunk_directory_handle);
+ if (!domain_handle) {
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+
+ /*
+ * This will only compile on platforms that support
+ * dirfd (POSIX.2008). This is fine as the session daemon
+ * is only built for such platforms.
+ *
+ * The ownership of the chunk directory handle's is maintained
+ * by the trace chunk.
+ */
+ domain_dirfd = lttng_directory_handle_get_dirfd(
+ domain_handle);
+ assert(domain_dirfd >= 0);
+
+ msg.u.create_trace_chunk.credentials.value.uid =
+ chunk_credentials.uid;
+ msg.u.create_trace_chunk.credentials.value.gid =
+ chunk_credentials.gid;
+ msg.u.create_trace_chunk.credentials.is_set = 1;
+ }
+
+ DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
+ ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
+ ", creation_timestamp = %s",
+ relayd_id, session_id, chunk_id,
+ creation_timestamp_str);
+ health_code_update();
+ ret = consumer_send_msg(socket, &msg);
+ health_code_update();
+ if (ret < 0) {
+ ERR("Trace chunk creation error on consumer");
+ ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+
+ if (chunk_has_local_output) {
+ DBG("Sending trace chunk domain directory fd to consumer");
+ health_code_update();
+ ret = consumer_send_fds(socket, &domain_dirfd, 1);
+ health_code_update();
+ if (ret < 0) {
+ ERR("Trace chunk creation error on consumer");
+ ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+ }
+error:
+ lttng_directory_handle_put(domain_handle);
+ return ret;
+}
+
+/*
+ * Ask the consumer to close a trace chunk for a given session.
+ *
+ * Called with the consumer socket lock held.
+ */
+int consumer_close_trace_chunk(struct consumer_socket *socket,
+ uint64_t relayd_id, uint64_t session_id,
+ struct lttng_trace_chunk *chunk,
+ char *closed_trace_chunk_path)
+{
+ int ret;
+ enum lttng_trace_chunk_status chunk_status;
+ struct lttcomm_consumer_msg msg = {
+ .cmd_type = LTTNG_CONSUMER_CLOSE_TRACE_CHUNK,
+ .u.close_trace_chunk.session_id = session_id,
+ };
+ struct lttcomm_consumer_close_trace_chunk_reply reply;
+ uint64_t chunk_id;
+ time_t close_timestamp;
+ enum lttng_trace_chunk_command_type close_command;
+ const char *close_command_name = "none";
+ struct lttng_dynamic_buffer path_reception_buffer;
+
+ assert(socket);
+ lttng_dynamic_buffer_init(&path_reception_buffer);
+
+ if (relayd_id != -1ULL) {
+ LTTNG_OPTIONAL_SET(
+ &msg.u.close_trace_chunk.relayd_id, relayd_id);
+ }
+
+ chunk_status = lttng_trace_chunk_get_close_command(
+ chunk, &close_command);
+ switch (chunk_status) {
+ case LTTNG_TRACE_CHUNK_STATUS_OK:
+ LTTNG_OPTIONAL_SET(&msg.u.close_trace_chunk.close_command,
+ (uint32_t) close_command);
+ break;
+ case LTTNG_TRACE_CHUNK_STATUS_NONE:
+ break;
+ default:
+ ERR("Failed to get trace chunk close command");
+ ret = -1;
+ goto error;
+ }
+
+ chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
+ /*
+ * Anonymous trace chunks should never be transmitted to remote peers
+ * (consumerd and relayd). They are used internally for
+ * backward-compatibility purposes.
+ */
+ assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
+ msg.u.close_trace_chunk.chunk_id = chunk_id;
+
+ chunk_status = lttng_trace_chunk_get_close_timestamp(chunk,
+ &close_timestamp);
+ /*
+ * A trace chunk should be closed locally before being closed remotely.
+ * Otherwise, the close timestamp would never be transmitted to the
+ * peers.
+ */
+ assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
+ msg.u.close_trace_chunk.close_timestamp = (uint64_t) close_timestamp;
+
+ if (msg.u.close_trace_chunk.close_command.is_set) {
+ close_command_name = lttng_trace_chunk_command_type_get_name(
+ close_command);
+ }
+ DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
+ ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
+ ", close command = \"%s\"",
+ relayd_id, session_id, chunk_id, close_command_name);
+
+ health_code_update();
+ ret = consumer_socket_send(socket, &msg, sizeof(struct lttcomm_consumer_msg));
+ if (ret < 0) {
+ ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+ ret = consumer_socket_recv(socket, &reply, sizeof(reply));
+ if (ret < 0) {
+ ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+ if (reply.path_length >= LTTNG_PATH_MAX) {
+ ERR("Invalid path returned by relay daemon: %" PRIu32 "bytes exceeds maximal allowed length of %d bytes",
+ reply.path_length, LTTNG_PATH_MAX);
+ ret = -LTTNG_ERR_INVALID_PROTOCOL;
+ goto error;
+ }
+ ret = lttng_dynamic_buffer_set_size(&path_reception_buffer,
+ reply.path_length);
+ if (ret) {
+ ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
+ ret = -LTTNG_ERR_NOMEM;
+ goto error;
+ }
+ ret = consumer_socket_recv(socket, path_reception_buffer.data,
+ path_reception_buffer.size);
+ if (ret < 0) {
+ ERR("Communication error while receiving path of closed trace chunk");
+ ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+ if (path_reception_buffer.data[path_reception_buffer.size - 1] != '\0') {
+ ERR("Invalid path returned by relay daemon: not null-terminated");
+ ret = -LTTNG_ERR_INVALID_PROTOCOL;
+ goto error;
+ }
+ if (closed_trace_chunk_path) {
+ /*
+ * closed_trace_chunk_path is assumed to have a length >=
+ * LTTNG_PATH_MAX
+ */
+ memcpy(closed_trace_chunk_path, path_reception_buffer.data,
+ path_reception_buffer.size);
+ }
+error:
+ lttng_dynamic_buffer_reset(&path_reception_buffer);
+ health_code_update();
+ return ret;
+}
+
+/*
+ * Ask the consumer if a trace chunk exists.
+ *
+ * Called with the consumer socket lock held.
+ * Returns 0 on success, or a negative value on error.
+ */
+int consumer_trace_chunk_exists(struct consumer_socket *socket,
+ uint64_t relayd_id, uint64_t session_id,
+ struct lttng_trace_chunk *chunk,
+ enum consumer_trace_chunk_exists_status *result)
+{
+ int ret;
+ enum lttng_trace_chunk_status chunk_status;
+ struct lttcomm_consumer_msg msg = {
+ .cmd_type = LTTNG_CONSUMER_TRACE_CHUNK_EXISTS,
+ .u.trace_chunk_exists.session_id = session_id,
+ };
+ uint64_t chunk_id;
+ const char *consumer_reply_str;
+
+ assert(socket);
+
+ if (relayd_id != -1ULL) {
+ LTTNG_OPTIONAL_SET(&msg.u.trace_chunk_exists.relayd_id,
+ relayd_id);
+ }
+
+ chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ /*
+ * Anonymous trace chunks should never be transmitted
+ * to remote peers (consumerd and relayd). They are used
+ * internally for backward-compatibility purposes.
+ */
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+ msg.u.trace_chunk_exists.chunk_id = chunk_id;
+
+ DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
+ ", session_id = %" PRIu64
+ ", chunk_id = %" PRIu64, relayd_id, session_id, chunk_id);
+
+ health_code_update();
+ ret = consumer_send_msg(socket, &msg);
+ switch (-ret) {
+ case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK:
+ consumer_reply_str = "unknown trace chunk";
+ *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK;
+ break;
+ case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL:
+ consumer_reply_str = "trace chunk exists locally";
+ *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL;
+ break;
+ case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE:
+ consumer_reply_str = "trace chunk exists on remote peer";
+ *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE;
+ break;
+ default:
+ ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
+ ret = -1;
+ goto error;
+ }
+ DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
+ consumer_reply_str);
+ ret = 0;
+error: