+ 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;
+ enum lttng_trace_chunk_status tc_status;
+ 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;
+ }
+ tc_status = lttng_trace_chunk_create_subdirectory(
+ chunk, domain_subdir);
+ if (tc_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ 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;