+ session->rotation_state = LTTNG_ROTATION_STATE_ERROR;
+end:
+ return ret;
+}
+
+int rename_active_chunk(struct ltt_session *session)
+{
+ int ret;
+
+ session->current_archive_id++;
+
+ /*
+ * The currently active tracing path is now the folder we
+ * want to rename.
+ */
+ ret = lttng_strncpy(session->rotation_chunk.current_rotate_path,
+ session->rotation_chunk.active_tracing_path,
+ sizeof(session->rotation_chunk.current_rotate_path));
+ if (ret) {
+ ERR("Failed to copy active tracing path");
+ goto end;
+ }
+
+ ret = rename_completed_chunk(session, time(NULL));
+ if (ret < 0) {
+ ERR("Failed to rename current rotation's path");
+ goto end;
+ }
+
+ /*
+ * We just renamed, the folder, we didn't do an actual rotation, so
+ * the active tracing path is now the renamed folder and we have to
+ * restore the rotate count.
+ */
+ ret = lttng_strncpy(session->rotation_chunk.active_tracing_path,
+ session->rotation_chunk.current_rotate_path,
+ sizeof(session->rotation_chunk.active_tracing_path));
+ if (ret) {
+ ERR("Failed to rename active session chunk tracing path");
+ goto end;
+ }
+end:
+ session->current_archive_id--;
+ return ret;
+}
+
+int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size,
+ struct notification_thread_handle *notification_thread_handle)
+{
+ int ret;
+ enum lttng_condition_status condition_status;
+ enum lttng_notification_channel_status nc_status;
+ struct lttng_action *action;
+
+ session->rotate_condition = lttng_condition_session_consumed_size_create();
+ if (!session->rotate_condition) {
+ ERR("Failed to create session consumed size condition object");
+ ret = -1;
+ goto end;
+ }
+
+ condition_status = lttng_condition_session_consumed_size_set_threshold(
+ session->rotate_condition, size);
+ if (condition_status != LTTNG_CONDITION_STATUS_OK) {
+ ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
+ size);
+ ret = -1;
+ goto end;
+ }
+
+ condition_status =
+ lttng_condition_session_consumed_size_set_session_name(
+ session->rotate_condition, session->name);
+ if (condition_status != LTTNG_CONDITION_STATUS_OK) {
+ ERR("Could not set session consumed size condition session name (name = %s)",
+ session->name);
+ ret = -1;
+ goto end;
+ }
+
+ action = lttng_action_notify_create();
+ if (!action) {
+ ERR("Could not create notify action");
+ ret = -1;
+ goto end;
+ }
+
+ session->rotate_trigger = lttng_trigger_create(session->rotate_condition,
+ action);
+ if (!session->rotate_trigger) {
+ ERR("Could not create size-based rotation trigger");
+ ret = -1;
+ goto end;
+ }
+
+ nc_status = lttng_notification_channel_subscribe(
+ rotate_notification_channel, session->rotate_condition);
+ if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
+ ERR("Could not subscribe to session consumed size notification");
+ ret = -1;
+ goto end;
+ }
+
+ ret = notification_thread_command_register_trigger(
+ notification_thread_handle, session->rotate_trigger);
+ if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
+ ERR("Register trigger, %s", lttng_strerror(ret));
+ ret = -1;
+ goto end;
+ }
+
+ ret = 0;
+
+end:
+ return ret;
+}
+
+int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
+ struct notification_thread_handle *notification_thread_handle)
+{
+ int ret = 0;
+ enum lttng_notification_channel_status status;
+
+ status = lttng_notification_channel_unsubscribe(
+ rotate_notification_channel,
+ session->rotate_condition);
+ if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
+ ERR("Session unsubscribe error: %d", (int) status);
+ ret = -1;
+ goto end;
+ }
+
+ ret = notification_thread_command_unregister_trigger(
+ notification_thread_handle, session->rotate_trigger);
+ if (ret != LTTNG_OK) {
+ ERR("Session unregister trigger error: %d", ret);
+ goto end;
+ }
+
+ ret = 0;