+ return status;
+}
+
+/*
+ * Record a kernel snapshot.
+ *
+ * Return LTTNG_OK on success or a LTTNG_ERR code.
+ */
+static enum lttng_error_code record_kernel_snapshot(
+ struct ltt_kernel_session *ksess,
+ const struct consumer_output *output,
+ const struct ltt_session *session,
+ int wait, uint64_t nb_packets_per_stream)
+{
+ enum lttng_error_code status;
+
+ assert(ksess);
+ assert(output);
+ assert(session);
+
+ status = kernel_snapshot_record(
+ ksess, output, wait, nb_packets_per_stream);
+ return status;
+}
+
+/*
+ * Record a UST snapshot.
+ *
+ * Returns LTTNG_OK on success or a LTTNG_ERR error code.
+ */
+static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
+ const struct consumer_output *output,
+ const struct ltt_session *session,
+ int wait, uint64_t nb_packets_per_stream)
+{
+ enum lttng_error_code status;
+
+ assert(usess);
+ assert(output);
+ assert(session);
+
+ status = ust_app_snapshot_record(
+ usess, output, wait, nb_packets_per_stream);
+ return status;
+}
+
+static
+uint64_t get_session_size_one_more_packet_per_stream(
+ const struct ltt_session *session, uint64_t cur_nr_packets)
+{
+ uint64_t tot_size = 0;
+
+ if (session->kernel_session) {
+ struct ltt_kernel_channel *chan;
+ const struct ltt_kernel_session *ksess =
+ session->kernel_session;
+
+ cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
+ if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
+ /*
+ * Don't take channel into account if we
+ * already grab all its packets.
+ */
+ continue;
+ }
+ tot_size += chan->channel->attr.subbuf_size
+ * chan->stream_count;
+ }
+ }
+
+ if (session->ust_session) {
+ const struct ltt_ust_session *usess = session->ust_session;
+
+ tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
+ cur_nr_packets);
+ }
+
+ return tot_size;
+}
+
+/*
+ * Calculate the number of packets we can grab from each stream that
+ * fits within the overall snapshot max size.
+ *
+ * Returns -1 on error, 0 means infinite number of packets, else > 0 is
+ * the number of packets per stream.
+ *
+ * TODO: this approach is not perfect: we consider the worse case
+ * (packet filling the sub-buffers) as an upper bound, but we could do
+ * better if we do this calculation while we actually grab the packet
+ * content: we would know how much padding we don't actually store into
+ * the file.
+ *
+ * This algorithm is currently bounded by the number of packets per
+ * stream.
+ *
+ * Since we call this algorithm before actually grabbing the data, it's
+ * an approximation: for instance, applications could appear/disappear
+ * in between this call and actually grabbing data.
+ */
+static
+int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
+ uint64_t max_size)
+{
+ int64_t size_left;
+ uint64_t cur_nb_packets = 0;
+
+ if (!max_size) {
+ return 0; /* Infinite */
+ }
+
+ size_left = max_size;
+ for (;;) {
+ uint64_t one_more_packet_tot_size;
+
+ one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
+ session, cur_nb_packets);
+ if (!one_more_packet_tot_size) {
+ /* We are already grabbing all packets. */
+ break;
+ }
+ size_left -= one_more_packet_tot_size;
+ if (size_left < 0) {
+ break;
+ }
+ cur_nb_packets++;
+ }
+ if (!cur_nb_packets && size_left != max_size) {
+ /* Not enough room to grab one packet of each stream, error. */
+ return -1;
+ }
+ return cur_nb_packets;
+}
+
+static
+enum lttng_error_code snapshot_record(struct ltt_session *session,
+ const struct snapshot_output *snapshot_output, int wait)
+{
+ int64_t nb_packets_per_stream;
+ char snapshot_chunk_name[LTTNG_NAME_MAX];
+ int ret;
+ enum lttng_error_code ret_code = LTTNG_OK;
+ struct lttng_trace_chunk *snapshot_trace_chunk;
+ struct consumer_output *original_ust_consumer_output = NULL;
+ struct consumer_output *original_kernel_consumer_output = NULL;
+ struct consumer_output *snapshot_ust_consumer_output = NULL;
+ struct consumer_output *snapshot_kernel_consumer_output = NULL;
+
+ ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
+ "%s-%s-%" PRIu64,
+ snapshot_output->name,
+ snapshot_output->datetime,
+ snapshot_output->nb_snapshot);
+ if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
+ ERR("Failed to format snapshot name");
+ ret_code = LTTNG_ERR_INVALID;
+ goto error;
+ }
+ DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
+ snapshot_output->name, session->name,
+ snapshot_chunk_name);
+ if (!session->kernel_session && !session->ust_session) {
+ ERR("Failed to record snapshot as no channels exist");
+ ret_code = LTTNG_ERR_NO_CHANNEL;
+ goto error;
+ }
+
+ if (session->kernel_session) {
+ original_kernel_consumer_output =
+ session->kernel_session->consumer;
+ snapshot_kernel_consumer_output =
+ consumer_copy_output(snapshot_output->consumer);
+ strcpy(snapshot_kernel_consumer_output->chunk_path,
+ snapshot_chunk_name);
+ ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
+ original_kernel_consumer_output);
+ if (ret < 0) {
+ ERR("Failed to copy consumer sockets from snapshot output configuration");
+ ret_code = LTTNG_ERR_NOMEM;
+ goto error;
+ }
+ ret_code = set_relayd_for_snapshot(
+ snapshot_kernel_consumer_output, session);
+ if (ret_code != LTTNG_OK) {
+ ERR("Failed to setup relay daemon for kernel tracer snapshot");
+ goto error;
+ }
+ session->kernel_session->consumer =
+ snapshot_kernel_consumer_output;
+ }
+ if (session->ust_session) {
+ original_ust_consumer_output = session->ust_session->consumer;
+ snapshot_ust_consumer_output =
+ consumer_copy_output(snapshot_output->consumer);
+ strcpy(snapshot_ust_consumer_output->chunk_path,
+ snapshot_chunk_name);
+ ret = consumer_copy_sockets(snapshot_ust_consumer_output,
+ original_ust_consumer_output);
+ if (ret < 0) {
+ ERR("Failed to copy consumer sockets from snapshot output configuration");
+ ret_code = LTTNG_ERR_NOMEM;
+ goto error;
+ }
+ ret_code = set_relayd_for_snapshot(
+ snapshot_ust_consumer_output, session);
+ if (ret_code != LTTNG_OK) {
+ ERR("Failed to setup relay daemon for userspace tracer snapshot");
+ goto error;
+ }
+ session->ust_session->consumer =
+ snapshot_ust_consumer_output;
+ }
+
+ snapshot_trace_chunk = session_create_new_trace_chunk(session,
+ snapshot_kernel_consumer_output ?:
+ snapshot_ust_consumer_output,
+ consumer_output_get_base_path(
+ snapshot_output->consumer),
+ snapshot_chunk_name);
+ if (!snapshot_trace_chunk) {
+ ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
+ session->name);
+ ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
+ goto error;
+ }
+ assert(!session->current_trace_chunk);
+ ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
+ lttng_trace_chunk_put(snapshot_trace_chunk);
+ snapshot_trace_chunk = NULL;
+ if (ret) {
+ ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
+ session->name);
+ ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+
+ nb_packets_per_stream = get_session_nb_packets_per_stream(session,
+ snapshot_output->max_size);
+ if (nb_packets_per_stream < 0) {
+ ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
+ goto error_close_trace_chunk;
+ }
+
+ if (session->kernel_session) {
+ ret_code = record_kernel_snapshot(session->kernel_session,
+ snapshot_kernel_consumer_output, session,
+ wait, nb_packets_per_stream);
+ if (ret_code != LTTNG_OK) {
+ goto error_close_trace_chunk;
+ }
+ }
+
+ if (session->ust_session) {
+ ret_code = record_ust_snapshot(session->ust_session,
+ snapshot_ust_consumer_output, session,
+ wait, nb_packets_per_stream);
+ if (ret_code != LTTNG_OK) {
+ goto error_close_trace_chunk;
+ }
+ }
+
+error_close_trace_chunk:
+ if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
+ ERR("Failed to release the current trace chunk of session \"%s\"",
+ session->name);
+ ret_code = LTTNG_ERR_UNK;
+ }
+
+ if (session_close_trace_chunk(session, snapshot_trace_chunk,
+ LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
+ /*
+ * Don't goto end; make sure the chunk is closed for the session
+ * to allow future snapshots.
+ */
+ ERR("Failed to close snapshot trace chunk of session \"%s\"",
+ session->name);
+ ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
+ }
+error:
+ if (original_ust_consumer_output) {
+ session->ust_session->consumer = original_ust_consumer_output;
+ }
+ if (original_kernel_consumer_output) {
+ session->kernel_session->consumer =
+ original_kernel_consumer_output;
+ }
+ consumer_output_put(snapshot_ust_consumer_output);
+ consumer_output_put(snapshot_kernel_consumer_output);
+ return ret_code;
+}
+
+/*
+ * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
+ *
+ * The wait parameter is ignored so this call always wait for the snapshot to
+ * complete before returning.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR code.
+ */
+int cmd_snapshot_record(struct ltt_session *session,
+ const struct lttng_snapshot_output *output, int wait)
+{
+ enum lttng_error_code cmd_ret = LTTNG_OK;
+ int ret;
+ unsigned int snapshot_success = 0;
+ char datetime[16];
+ struct snapshot_output *tmp_output = NULL;
+
+ assert(session);
+ assert(output);
+
+ DBG("Cmd snapshot record for session %s", session->name);
+
+ /* Get the datetime for the snapshot output directory. */
+ ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
+ sizeof(datetime));
+ if (!ret) {
+ cmd_ret = LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ /*
+ * Permission denied to create an output if the session is not
+ * set in no output mode.
+ */
+ if (session->output_traces) {
+ cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
+ goto error;
+ }
+
+ /* The session needs to be started at least once. */
+ if (!session->has_been_started) {
+ cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
+ goto error;
+ }
+
+ /* Use temporary output for the session. */
+ if (*output->ctrl_url != '\0') {
+ tmp_output = snapshot_output_alloc();
+ if (!tmp_output) {
+ cmd_ret = LTTNG_ERR_NOMEM;
+ goto error;
+ }
+
+ ret = snapshot_output_init(session, output->max_size,
+ output->name,
+ output->ctrl_url, output->data_url,
+ session->consumer,
+ tmp_output, NULL);
+ if (ret < 0) {
+ if (ret == -ENOMEM) {
+ cmd_ret = LTTNG_ERR_NOMEM;
+ } else {
+ cmd_ret = LTTNG_ERR_INVALID;
+ }
+ goto error;
+ }
+ /* Use the global session count for the temporary snapshot. */
+ tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
+
+ /* Use the global datetime */
+ memcpy(tmp_output->datetime, datetime, sizeof(datetime));
+ cmd_ret = snapshot_record(session, tmp_output, wait);
+ if (cmd_ret != LTTNG_OK) {
+ goto error;
+ }
+ snapshot_success = 1;
+ } else {
+ struct snapshot_output *sout;
+ struct lttng_ht_iter iter;
+
+ rcu_read_lock();
+ cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
+ &iter.iter, sout, node.node) {
+ struct snapshot_output output_copy;
+
+ /*
+ * Make a local copy of the output and override output
+ * parameters with those provided as part of the
+ * command.
+ */
+ memcpy(&output_copy, sout, sizeof(output_copy));
+
+ if (output->max_size != (uint64_t) -1ULL) {
+ output_copy.max_size = output->max_size;
+ }
+
+ output_copy.nb_snapshot = session->snapshot.nb_snapshot;
+ memcpy(output_copy.datetime, datetime,
+ sizeof(datetime));
+
+ /* Use temporary name. */
+ if (*output->name != '\0') {
+ if (lttng_strncpy(output_copy.name,
+ output->name,
+ sizeof(output_copy.name))) {
+ cmd_ret = LTTNG_ERR_INVALID;
+ rcu_read_unlock();
+ goto error;
+ }
+ }
+
+ cmd_ret = snapshot_record(session, &output_copy, wait);
+ if (cmd_ret != LTTNG_OK) {
+ rcu_read_unlock();
+ goto error;
+ }
+ snapshot_success = 1;
+ }
+ rcu_read_unlock();
+ }
+
+ if (snapshot_success) {
+ session->snapshot.nb_snapshot++;
+ } else {
+ cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
+ }
+
+error:
+ if (tmp_output) {
+ snapshot_output_destroy(tmp_output);
+ }
+ return cmd_ret;
+}
+
+/*
+ * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
+ */
+int cmd_set_session_shm_path(struct ltt_session *session,
+ const char *shm_path)
+{
+ /* Safety net */
+ assert(session);
+
+ /*
+ * Can only set shm path before session is started.
+ */
+ if (session->has_been_started) {
+ return LTTNG_ERR_SESSION_STARTED;
+ }
+
+ strncpy(session->shm_path, shm_path,
+ sizeof(session->shm_path));
+ session->shm_path[sizeof(session->shm_path) - 1] = '\0';
+
+ return LTTNG_OK;
+}
+
+/*
+ * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
+ *
+ * Ask the consumer to rotate the session output directory.
+ * The session lock must be held.
+ *
+ * Returns LTTNG_OK on success or else a negative LTTng error code.
+ */
+int cmd_rotate_session(struct ltt_session *session,
+ struct lttng_rotate_session_return *rotate_return,
+ bool quiet_rotation,
+ enum lttng_trace_chunk_command_type command)
+{
+ int ret;
+ uint64_t ongoing_rotation_chunk_id;
+ enum lttng_error_code cmd_ret = LTTNG_OK;
+ struct lttng_trace_chunk *chunk_being_archived = NULL;
+ struct lttng_trace_chunk *new_trace_chunk = NULL;
+ enum lttng_trace_chunk_status chunk_status;
+ bool failed_to_rotate = false;
+ enum lttng_error_code rotation_fail_code = LTTNG_OK;
+
+ assert(session);
+
+ if (!session->has_been_started) {
+ cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
+ goto end;
+ }
+
+ /*
+ * Explicit rotation is not supported for live sessions.
+ * However, live sessions can perform a quiet rotation on
+ * destroy.
+ * Rotation is not supported for snapshot traces (no output).
+ */
+ if ((!quiet_rotation && session->live_timer) ||
+ !session->output_traces) {
+ cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
+ goto end;
+ }
+
+ /* Unsupported feature in lttng-relayd before 2.11. */
+ if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
+ (session->consumer->relay_major_version == 2 &&
+ session->consumer->relay_minor_version < 11)) {
+ cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
+ goto end;
+ }
+
+ /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
+ if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
+ cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
+ goto end;
+ }
+
+ if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
+ DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
+ session->name);
+ cmd_ret = LTTNG_ERR_ROTATION_PENDING;
+ goto end;
+ }
+
+ /*
+ * After a stop, we only allow one rotation to occur, the other ones are
+ * useless until a new start.
+ */
+ if (session->rotated_after_last_stop) {
+ DBG("Session \"%s\" was already rotated after stop, refusing rotation",
+ session->name);
+ cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
+ goto end;
+ }
+
+ /*
+ * After a stop followed by a clear, disallow following rotations a they would
+ * generate empty chunks.
+ */
+ if (session->cleared_after_last_stop) {
+ DBG("Session \"%s\" was already cleared after stop, refusing rotation",
+ session->name);
+ cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
+ goto end;
+ }
+
+ if (session->active) {
+ new_trace_chunk = session_create_new_trace_chunk(session, NULL,
+ NULL, NULL);
+ if (!new_trace_chunk) {
+ cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
+ goto error;
+ }
+ }
+
+ /*
+ * The current trace chunk becomes the chunk being archived.
+ *
+ * After this point, "chunk_being_archived" must absolutely
+ * be closed on the consumer(s), otherwise it will never be
+ * cleaned-up, which will result in a leak.
+ */
+ ret = session_set_trace_chunk(session, new_trace_chunk,
+ &chunk_being_archived);
+ if (ret) {
+ cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+
+ if (session->kernel_session) {
+ cmd_ret = kernel_rotate_session(session);
+ if (cmd_ret != LTTNG_OK) {
+ failed_to_rotate = true;
+ rotation_fail_code = cmd_ret;
+ }
+ }
+ if (session->ust_session) {
+ cmd_ret = ust_app_rotate_session(session);
+ if (cmd_ret != LTTNG_OK) {
+ failed_to_rotate = true;
+ rotation_fail_code = cmd_ret;
+ }
+ }
+
+ if (!session->active) {
+ session->rotated_after_last_stop = true;
+ }
+
+ if (!chunk_being_archived) {
+ DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
+ session->name);
+ if (failed_to_rotate) {
+ cmd_ret = rotation_fail_code;
+ goto error;
+ }
+ cmd_ret = LTTNG_OK;
+ goto end;
+ }
+
+ session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
+ chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
+ &ongoing_rotation_chunk_id);
+ assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
+
+ ret = session_close_trace_chunk(session, chunk_being_archived,
+ command, session->last_chunk_path);
+ if (ret) {
+ cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto error;
+ }
+
+ if (failed_to_rotate) {
+ cmd_ret = rotation_fail_code;
+ goto error;
+ }
+
+ session->quiet_rotation = quiet_rotation;
+ ret = timer_session_rotation_pending_check_start(session,
+ DEFAULT_ROTATE_PENDING_TIMER);
+ if (ret) {
+ cmd_ret = LTTNG_ERR_UNK;
+ goto error;
+ }
+
+ if (rotate_return) {
+ rotate_return->rotation_id = ongoing_rotation_chunk_id;
+ }
+
+ session->chunk_being_archived = chunk_being_archived;
+ chunk_being_archived = NULL;
+ if (!quiet_rotation) {
+ ret = notification_thread_command_session_rotation_ongoing(
+ notification_thread_handle,
+ session->name, session->uid, session->gid,
+ ongoing_rotation_chunk_id);
+ if (ret != LTTNG_OK) {
+ ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
+ session->name);
+ cmd_ret = ret;
+ }
+ }
+
+ DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
+ session->name, ongoing_rotation_chunk_id);
+end:
+ lttng_trace_chunk_put(new_trace_chunk);
+ lttng_trace_chunk_put(chunk_being_archived);
+ ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
+ return ret;
+error:
+ if (session_reset_rotation_state(session,
+ LTTNG_ROTATION_STATE_ERROR)) {
+ ERR("Failed to reset rotation state of session \"%s\"",
+ session->name);
+ }
+ goto end;
+}
+
+/*
+ * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
+ *
+ * Check if the session has finished its rotation.
+ *
+ * Return LTTNG_OK on success or else an LTTNG_ERR code.
+ */
+int cmd_rotate_get_info(struct ltt_session *session,
+ struct lttng_rotation_get_info_return *info_return,
+ uint64_t rotation_id)
+{
+ enum lttng_error_code cmd_ret = LTTNG_OK;
+ enum lttng_rotation_state rotation_state;
+
+ DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
+ session->most_recent_chunk_id.value);
+
+ if (session->chunk_being_archived) {
+ enum lttng_trace_chunk_status chunk_status;
+ uint64_t chunk_id;
+
+ chunk_status = lttng_trace_chunk_get_id(
+ session->chunk_being_archived,
+ &chunk_id);
+ assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
+
+ rotation_state = rotation_id == chunk_id ?
+ LTTNG_ROTATION_STATE_ONGOING :
+ LTTNG_ROTATION_STATE_EXPIRED;
+ } else {
+ if (session->last_archived_chunk_id.is_set &&
+ rotation_id != session->last_archived_chunk_id.value) {
+ rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
+ } else {
+ rotation_state = session->rotation_state;
+ }
+ }
+
+ switch (rotation_state) {
+ case LTTNG_ROTATION_STATE_NO_ROTATION:
+ DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
+ session->name);
+ goto end;
+ case LTTNG_ROTATION_STATE_EXPIRED:
+ DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
+ rotation_id, session->name);
+ break;
+ case LTTNG_ROTATION_STATE_ONGOING:
+ DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
+ rotation_id, session->name);
+ break;
+ case LTTNG_ROTATION_STATE_COMPLETED:
+ {
+ int fmt_ret;
+ char *chunk_path;
+ char *current_tracing_path_reply;
+ size_t current_tracing_path_reply_len;
+
+ DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
+ rotation_id, session->name);
+
+ switch (session_get_consumer_destination_type(session)) {
+ case CONSUMER_DST_LOCAL:
+ current_tracing_path_reply =
+ info_return->location.local.absolute_path;
+ current_tracing_path_reply_len =
+ sizeof(info_return->location.local.absolute_path);
+ info_return->location_type =
+ (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
+ fmt_ret = asprintf(&chunk_path,
+ "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
+ session_get_base_path(session),
+ session->last_archived_chunk_name);
+ if (fmt_ret == -1) {
+ PERROR("Failed to format the path of the last archived trace chunk");
+ info_return->status = LTTNG_ROTATION_STATUS_ERROR;
+ cmd_ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+ break;
+ case CONSUMER_DST_NET:
+ {
+ uint16_t ctrl_port, data_port;
+
+ current_tracing_path_reply =
+ info_return->location.relay.relative_path;
+ current_tracing_path_reply_len =
+ sizeof(info_return->location.relay.relative_path);
+ /* Currently the only supported relay protocol. */
+ info_return->location.relay.protocol =
+ (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
+
+ fmt_ret = lttng_strncpy(info_return->location.relay.host,
+ session_get_net_consumer_hostname(session),
+ sizeof(info_return->location.relay.host));
+ if (fmt_ret) {
+ ERR("Failed to copy host name to rotate_get_info reply");
+ info_return->status = LTTNG_ROTATION_STATUS_ERROR;
+ cmd_ret = LTTNG_ERR_SET_URL;
+ goto end;
+ }
+
+ session_get_net_consumer_ports(session, &ctrl_port, &data_port);
+ info_return->location.relay.ports.control = ctrl_port;
+ info_return->location.relay.ports.data = data_port;
+ info_return->location_type =
+ (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
+ chunk_path = strdup(session->last_chunk_path);
+ if (!chunk_path) {
+ ERR("Failed to allocate the path of the last archived trace chunk");
+ info_return->status = LTTNG_ROTATION_STATUS_ERROR;
+ cmd_ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+ break;
+ }
+ default:
+ abort();
+ }
+
+ fmt_ret = lttng_strncpy(current_tracing_path_reply,
+ chunk_path, current_tracing_path_reply_len);
+ free(chunk_path);
+ if (fmt_ret) {
+ ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
+ info_return->status = LTTNG_ROTATION_STATUS_ERROR;
+ cmd_ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ break;
+ }
+ case LTTNG_ROTATION_STATE_ERROR:
+ DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
+ rotation_id, session->name);
+ break;
+ default:
+ abort();
+ }
+
+ cmd_ret = LTTNG_OK;
+end:
+ info_return->status = (int32_t) rotation_state;
+ return cmd_ret;
+}
+
+/*
+ * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
+ *
+ * Configure the automatic rotation parameters.
+ * 'activate' to true means activate the rotation schedule type with 'new_value'.
+ * 'activate' to false means deactivate the rotation schedule and validate that
+ * 'new_value' has the same value as the currently active value.
+ *
+ * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
+ */
+int cmd_rotation_set_schedule(struct ltt_session *session,
+ bool activate, enum lttng_rotation_schedule_type schedule_type,
+ uint64_t new_value,
+ struct notification_thread_handle *notification_thread_handle)
+{
+ int ret;
+ uint64_t *parameter_value;
+
+ assert(session);
+
+ DBG("Cmd rotate set schedule session %s", session->name);
+
+ if (session->live_timer || !session->output_traces) {
+ DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
+ ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
+ goto end;
+ }
+
+ switch (schedule_type) {
+ case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
+ parameter_value = &session->rotate_size;
+ break;
+ case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
+ parameter_value = &session->rotate_timer_period;
+ if (new_value >= UINT_MAX) {
+ DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
+ new_value, UINT_MAX);
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+ break;
+ default:
+ WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ /* Improper use of the API. */
+ if (new_value == -1ULL) {
+ WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ /*
+ * As indicated in struct ltt_session's comments, a value of == 0 means
+ * this schedule rotation type is not in use.
+ *
+ * Reject the command if we were asked to activate a schedule that was
+ * already active.
+ */
+ if (activate && *parameter_value != 0) {
+ DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
+ ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
+ goto end;
+ }
+
+ /*
+ * Reject the command if we were asked to deactivate a schedule that was
+ * not active.
+ */
+ if (!activate && *parameter_value == 0) {
+ DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
+ ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
+ goto end;
+ }
+
+ /*
+ * Reject the command if we were asked to deactivate a schedule that
+ * doesn't exist.
+ */
+ if (!activate && *parameter_value != new_value) {
+ DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
+ ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
+ goto end;
+ }
+
+ *parameter_value = activate ? new_value : 0;
+
+ switch (schedule_type) {
+ case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
+ if (activate && session->active) {
+ /*
+ * Only start the timer if the session is active,
+ * otherwise it will be started when the session starts.
+ */
+ ret = timer_session_rotation_schedule_timer_start(
+ session, new_value);
+ if (ret) {
+ ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
+ ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+ } else {
+ ret = timer_session_rotation_schedule_timer_stop(
+ session);
+ if (ret) {
+ ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
+ ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+ }
+ break;
+ case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
+ if (activate) {
+ ret = subscribe_session_consumed_size_rotation(session,
+ new_value, notification_thread_handle);
+ if (ret) {
+ ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
+ ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+ } else {
+ ret = unsubscribe_session_consumed_size_rotation(session,
+ notification_thread_handle);
+ if (ret) {
+ ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
+ ret = LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ }
+ break;
+ default:
+ /* Would have been caught before. */
+ abort();
+ }
+
+ ret = LTTNG_OK;
+
+ goto end;
+
+end:
+ return ret;
+}
+
+/* Wait for a given path to be removed before continuing. */
+static enum lttng_error_code wait_on_path(void *path_data)
+{
+ const char *shm_path = path_data;
+
+ DBG("Waiting for the shm path at %s to be removed before completing session destruction",
+ shm_path);
+ while (true) {
+ int ret;
+ struct stat st;
+
+ ret = stat(shm_path, &st);
+ if (ret) {
+ if (errno != ENOENT) {
+ PERROR("stat() returned an error while checking for the existence of the shm path");
+ } else {
+ DBG("shm path no longer exists, completing the destruction of session");
+ }
+ break;
+ } else {
+ if (!S_ISDIR(st.st_mode)) {
+ ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
+ shm_path);
+ break;
+ }
+ }
+ usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
+ }
+ return LTTNG_OK;
+}
+
+/*
+ * Returns a pointer to a handler to run on completion of a command.
+ * Returns NULL if no handler has to be run for the last command executed.
+ */
+const struct cmd_completion_handler *cmd_pop_completion_handler(void)
+{
+ struct cmd_completion_handler *handler = current_completion_handler;
+
+ current_completion_handler = NULL;
+ return handler;