+end:
+ if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
+ DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s",
+ session->current_archive_id - 1, session->name);
+ ret = timer_session_rotation_pending_check_start(session,
+ DEFAULT_ROTATE_PENDING_TIMER);
+ if (ret) {
+ ERR("Re-enabling rotate pending timer");
+ ret = -1;
+ goto end;
+ }
+ }
+
+ return ret;
+}
+
+/* Call with the session and session_list locks held. */
+static
+int launch_session_rotation(struct ltt_session *session)
+{
+ int ret;
+ struct lttng_rotate_session_return rotation_return;
+
+ DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
+ session->name);
+
+ ret = cmd_rotate_session(session, &rotation_return);
+ if (ret == LTTNG_OK) {
+ DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
+ session->name);
+ } else {
+ /* Don't consider errors as fatal. */
+ DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
+ session->name, lttng_strerror(ret));
+ }
+ return 0;
+}
+
+static
+int run_job(struct rotation_thread_job *job, struct ltt_session *session,
+ struct notification_thread_handle *notification_thread_handle)
+{
+ int ret;
+
+ switch (job->type) {
+ case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
+ ret = launch_session_rotation(session);
+ break;
+ case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
+ ret = check_session_rotation_pending(session,
+ notification_thread_handle);
+ break;
+ default:
+ abort();
+ }
+ return ret;
+}
+
+static
+int handle_job_queue(struct rotation_thread_handle *handle,
+ struct rotation_thread *state,
+ struct rotation_thread_timer_queue *queue)
+{
+ int ret = 0;
+
+ for (;;) {
+ struct ltt_session *session;
+ struct rotation_thread_job *job;
+
+ /* Take the queue lock only to pop an element from the list. */
+ pthread_mutex_lock(&queue->lock);
+ if (cds_list_empty(&queue->list)) {
+ pthread_mutex_unlock(&queue->lock);
+ break;
+ }
+ job = cds_list_first_entry(&queue->list,
+ typeof(*job), head);
+ cds_list_del(&job->head);
+ pthread_mutex_unlock(&queue->lock);
+
+ session_lock_list();
+ session = job->session;
+ if (!session) {
+ DBG("[rotation-thread] Session \"%s\" not found",
+ session->name);
+ /*
+ * This is a non-fatal error, and we cannot report it to
+ * the user (timer), so just print the error and
+ * continue the processing.
+ *
+ * While the timer thread will purge pending signals for
+ * a session on the session's destruction, it is
+ * possible for a job targeting that session to have
+ * already been queued before it was destroyed.
+ */
+ free(job);
+ session_put(session);
+ session_unlock_list();
+ continue;
+ }
+
+ session_lock(session);
+ ret = run_job(job, session, handle->notification_thread_handle);
+ session_unlock(session);
+ /* Release reference held by the job. */
+ session_put(session);
+ session_unlock_list();
+ free(job);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ ret = 0;
+
+end:
+ return ret;
+}