Fix: sessiond: rotation trigger leak
[lttng-tools.git] / src / bin / lttng-sessiond / rotation-thread.c
1 /*
2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <lttng/trigger/trigger.h>
11 #include <common/error.h>
12 #include <common/config/session-config.h>
13 #include <common/defaults.h>
14 #include <common/utils.h>
15 #include <common/futex.h>
16 #include <common/align.h>
17 #include <common/time.h>
18 #include <common/hashtable/utils.h>
19 #include <sys/eventfd.h>
20 #include <sys/stat.h>
21 #include <time.h>
22 #include <signal.h>
23 #include <inttypes.h>
24
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <lttng/notification/channel-internal.h>
27 #include <lttng/rotate-internal.h>
28 #include <lttng/location-internal.h>
29 #include <lttng/condition/condition-internal.h>
30 #include <lttng/notification/notification-internal.h>
31
32 #include "rotation-thread.h"
33 #include "lttng-sessiond.h"
34 #include "health-sessiond.h"
35 #include "rotate.h"
36 #include "cmd.h"
37 #include "session.h"
38 #include "timer.h"
39 #include "notification-thread-commands.h"
40 #include "utils.h"
41 #include "thread.h"
42
43 #include <urcu.h>
44 #include <urcu/list.h>
45
46 struct lttng_notification_channel *rotate_notification_channel = NULL;
47
48 struct rotation_thread {
49 struct lttng_poll_event events;
50 };
51
52 struct rotation_thread_job {
53 enum rotation_thread_job_type type;
54 struct ltt_session *session;
55 /* List member in struct rotation_thread_timer_queue. */
56 struct cds_list_head head;
57 };
58
59 /*
60 * The timer thread enqueues jobs and wakes up the rotation thread.
61 * When the rotation thread wakes up, it empties the queue.
62 */
63 struct rotation_thread_timer_queue {
64 struct lttng_pipe *event_pipe;
65 struct cds_list_head list;
66 pthread_mutex_t lock;
67 };
68
69 struct rotation_thread_handle {
70 struct rotation_thread_timer_queue *rotation_timer_queue;
71 /* Access to the notification thread cmd_queue */
72 struct notification_thread_handle *notification_thread_handle;
73 /* Thread-specific quit pipe. */
74 struct lttng_pipe *quit_pipe;
75 };
76
77 static
78 const char *get_job_type_str(enum rotation_thread_job_type job_type)
79 {
80 switch (job_type) {
81 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
82 return "CHECK_PENDING_ROTATION";
83 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
84 return "SCHEDULED_ROTATION";
85 default:
86 abort();
87 }
88 }
89
90 struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void)
91 {
92 struct rotation_thread_timer_queue *queue = NULL;
93
94 queue = zmalloc(sizeof(*queue));
95 if (!queue) {
96 PERROR("Failed to allocate timer rotate queue");
97 goto end;
98 }
99
100 queue->event_pipe = lttng_pipe_open(FD_CLOEXEC | O_NONBLOCK);
101 CDS_INIT_LIST_HEAD(&queue->list);
102 pthread_mutex_init(&queue->lock, NULL);
103 end:
104 return queue;
105 }
106
107 void rotation_thread_timer_queue_destroy(
108 struct rotation_thread_timer_queue *queue)
109 {
110 if (!queue) {
111 return;
112 }
113
114 lttng_pipe_destroy(queue->event_pipe);
115
116 pthread_mutex_lock(&queue->lock);
117 assert(cds_list_empty(&queue->list));
118 pthread_mutex_unlock(&queue->lock);
119 pthread_mutex_destroy(&queue->lock);
120 free(queue);
121 }
122
123 /*
124 * Destroy the thread data previously created by the init function.
125 */
126 void rotation_thread_handle_destroy(
127 struct rotation_thread_handle *handle)
128 {
129 lttng_pipe_destroy(handle->quit_pipe);
130 free(handle);
131 }
132
133 struct rotation_thread_handle *rotation_thread_handle_create(
134 struct rotation_thread_timer_queue *rotation_timer_queue,
135 struct notification_thread_handle *notification_thread_handle)
136 {
137 struct rotation_thread_handle *handle;
138
139 handle = zmalloc(sizeof(*handle));
140 if (!handle) {
141 goto end;
142 }
143
144 handle->rotation_timer_queue = rotation_timer_queue;
145 handle->notification_thread_handle = notification_thread_handle;
146 handle->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
147 if (!handle->quit_pipe) {
148 goto error;
149 }
150
151 end:
152 return handle;
153 error:
154 rotation_thread_handle_destroy(handle);
155 return NULL;
156 }
157
158 /*
159 * Called with the rotation_thread_timer_queue lock held.
160 * Return true if the same timer job already exists in the queue, false if not.
161 */
162 static
163 bool timer_job_exists(const struct rotation_thread_timer_queue *queue,
164 enum rotation_thread_job_type job_type,
165 struct ltt_session *session)
166 {
167 bool exists = false;
168 struct rotation_thread_job *job;
169
170 cds_list_for_each_entry(job, &queue->list, head) {
171 if (job->session == session && job->type == job_type) {
172 exists = true;
173 goto end;
174 }
175 }
176 end:
177 return exists;
178 }
179
180 void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue,
181 enum rotation_thread_job_type job_type,
182 struct ltt_session *session)
183 {
184 int ret;
185 const char dummy = '!';
186 struct rotation_thread_job *job = NULL;
187 const char *job_type_str = get_job_type_str(job_type);
188
189 pthread_mutex_lock(&queue->lock);
190 if (timer_job_exists(queue, job_type, session)) {
191 /*
192 * This timer job is already pending, we don't need to add
193 * it.
194 */
195 goto end;
196 }
197
198 job = zmalloc(sizeof(struct rotation_thread_job));
199 if (!job) {
200 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
201 job_type_str, session->name);
202 goto end;
203 }
204 /* No reason for this to fail as the caller must hold a reference. */
205 (void) session_get(session);
206
207 job->session = session;
208 job->type = job_type;
209 cds_list_add_tail(&job->head, &queue->list);
210
211 ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), &dummy,
212 sizeof(dummy));
213 if (ret < 0) {
214 /*
215 * We do not want to block in the timer handler, the job has
216 * been enqueued in the list, the wakeup pipe is probably full,
217 * the job will be processed when the rotation_thread catches
218 * up.
219 */
220 if (errno == EAGAIN || errno == EWOULDBLOCK) {
221 /*
222 * Not an error, but would be surprising and indicate
223 * that the rotation thread can't keep up with the
224 * current load.
225 */
226 DBG("Wake-up pipe of rotation thread job queue is full");
227 goto end;
228 }
229 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
230 job_type_str, session->name);
231 goto end;
232 }
233
234 end:
235 pthread_mutex_unlock(&queue->lock);
236 }
237
238 static
239 int init_poll_set(struct lttng_poll_event *poll_set,
240 struct rotation_thread_handle *handle)
241 {
242 int ret;
243
244 /*
245 * Create pollset with size 3:
246 * - rotation thread quit pipe,
247 * - rotation thread timer queue pipe,
248 * - notification channel sock,
249 */
250 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
251 if (ret < 0) {
252 goto error;
253 }
254
255 ret = lttng_poll_add(poll_set,
256 lttng_pipe_get_readfd(handle->quit_pipe),
257 LPOLLIN | LPOLLERR);
258 if (ret < 0) {
259 ERR("[rotation-thread] Failed to add quit pipe read fd to poll set");
260 goto error;
261 }
262
263 ret = lttng_poll_add(poll_set,
264 lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe),
265 LPOLLIN | LPOLLERR);
266 if (ret < 0) {
267 ERR("[rotation-thread] Failed to add rotate_pending fd to poll set");
268 goto error;
269 }
270
271 return ret;
272 error:
273 lttng_poll_clean(poll_set);
274 return ret;
275 }
276
277 static
278 void fini_thread_state(struct rotation_thread *state)
279 {
280 lttng_poll_clean(&state->events);
281 if (rotate_notification_channel) {
282 lttng_notification_channel_destroy(rotate_notification_channel);
283 }
284 }
285
286 static
287 int init_thread_state(struct rotation_thread_handle *handle,
288 struct rotation_thread *state)
289 {
290 int ret;
291
292 memset(state, 0, sizeof(*state));
293 lttng_poll_init(&state->events);
294
295 ret = init_poll_set(&state->events, handle);
296 if (ret) {
297 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
298 goto end;
299 }
300
301 rotate_notification_channel = lttng_notification_channel_create(
302 lttng_session_daemon_notification_endpoint);
303 if (!rotate_notification_channel) {
304 ERR("[rotation-thread] Could not create notification channel");
305 ret = -1;
306 goto end;
307 }
308 ret = lttng_poll_add(&state->events, rotate_notification_channel->socket,
309 LPOLLIN | LPOLLERR);
310 if (ret < 0) {
311 ERR("[rotation-thread] Failed to add notification fd to pollset");
312 goto end;
313 }
314
315 end:
316 return ret;
317 }
318
319 static
320 void check_session_rotation_pending_on_consumers(struct ltt_session *session,
321 bool *_rotation_completed)
322 {
323 int ret = 0;
324 struct consumer_socket *socket;
325 struct cds_lfht_iter iter;
326 enum consumer_trace_chunk_exists_status exists_status;
327 uint64_t relayd_id;
328 bool chunk_exists_on_peer = false;
329 enum lttng_trace_chunk_status chunk_status;
330
331 assert(session->chunk_being_archived);
332
333 /*
334 * Check for a local pending rotation on all consumers (32-bit
335 * user space, 64-bit user space, and kernel).
336 */
337 rcu_read_lock();
338 if (!session->ust_session) {
339 goto skip_ust;
340 }
341 cds_lfht_for_each_entry(session->ust_session->consumer->socks->ht,
342 &iter, socket, node.node) {
343 relayd_id = session->ust_session->consumer->type == CONSUMER_DST_LOCAL ?
344 -1ULL :
345 session->ust_session->consumer->net_seq_index;
346
347 pthread_mutex_lock(socket->lock);
348 ret = consumer_trace_chunk_exists(socket,
349 relayd_id,
350 session->id, session->chunk_being_archived,
351 &exists_status);
352 if (ret) {
353 pthread_mutex_unlock(socket->lock);
354 ERR("Error occurred while checking rotation status on consumer daemon");
355 goto end;
356 }
357
358 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
359 pthread_mutex_unlock(socket->lock);
360 chunk_exists_on_peer = true;
361 goto end;
362 }
363 pthread_mutex_unlock(socket->lock);
364 }
365
366 skip_ust:
367 if (!session->kernel_session) {
368 goto skip_kernel;
369 }
370 cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht,
371 &iter, socket, node.node) {
372 pthread_mutex_lock(socket->lock);
373 relayd_id = session->kernel_session->consumer->type == CONSUMER_DST_LOCAL ?
374 -1ULL :
375 session->kernel_session->consumer->net_seq_index;
376
377 ret = consumer_trace_chunk_exists(socket,
378 relayd_id,
379 session->id, session->chunk_being_archived,
380 &exists_status);
381 if (ret) {
382 pthread_mutex_unlock(socket->lock);
383 ERR("Error occurred while checking rotation status on consumer daemon");
384 goto end;
385 }
386
387 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
388 pthread_mutex_unlock(socket->lock);
389 chunk_exists_on_peer = true;
390 goto end;
391 }
392 pthread_mutex_unlock(socket->lock);
393 }
394 skip_kernel:
395 end:
396 rcu_read_unlock();
397
398 if (!chunk_exists_on_peer) {
399 uint64_t chunk_being_archived_id;
400
401 chunk_status = lttng_trace_chunk_get_id(
402 session->chunk_being_archived,
403 &chunk_being_archived_id);
404 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
405 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " of session \"%s\" is complete on all consumers",
406 chunk_being_archived_id,
407 session->name);
408 }
409 *_rotation_completed = !chunk_exists_on_peer;
410 if (ret) {
411 ret = session_reset_rotation_state(session,
412 LTTNG_ROTATION_STATE_ERROR);
413 if (ret) {
414 ERR("Failed to reset rotation state of session \"%s\"",
415 session->name);
416 }
417 }
418 }
419
420 /*
421 * Check if the last rotation was completed, called with session lock held.
422 * Should only return non-zero in the event of a fatal error. Doing so will
423 * shutdown the thread.
424 */
425 static
426 int check_session_rotation_pending(struct ltt_session *session,
427 struct notification_thread_handle *notification_thread_handle)
428 {
429 int ret;
430 struct lttng_trace_archive_location *location;
431 enum lttng_trace_chunk_status chunk_status;
432 bool rotation_completed = false;
433 const char *archived_chunk_name;
434 uint64_t chunk_being_archived_id;
435
436 if (!session->chunk_being_archived) {
437 ret = 0;
438 goto end;
439 }
440
441 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived,
442 &chunk_being_archived_id);
443 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
444
445 DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64,
446 session->name, chunk_being_archived_id);
447
448 /*
449 * The rotation-pending check timer of a session is launched in
450 * one-shot mode. If the rotation is incomplete, the rotation
451 * thread will re-enable the pending-check timer.
452 *
453 * The timer thread can't stop the timer itself since it is involved
454 * in the check for the timer's quiescence.
455 */
456 ret = timer_session_rotation_pending_check_stop(session);
457 if (ret) {
458 goto check_ongoing_rotation;
459 }
460
461 check_session_rotation_pending_on_consumers(session,
462 &rotation_completed);
463 if (!rotation_completed ||
464 session->rotation_state == LTTNG_ROTATION_STATE_ERROR) {
465 goto check_ongoing_rotation;
466 }
467
468 /*
469 * Now we can clear the "ONGOING" state in the session. New
470 * rotations can start now.
471 */
472 chunk_status = lttng_trace_chunk_get_name(session->chunk_being_archived,
473 &archived_chunk_name, NULL);
474 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
475 free(session->last_archived_chunk_name);
476 session->last_archived_chunk_name = strdup(archived_chunk_name);
477 if (!session->last_archived_chunk_name) {
478 PERROR("Failed to duplicate archived chunk name");
479 }
480 session_reset_rotation_state(session, LTTNG_ROTATION_STATE_COMPLETED);
481
482 if (!session->quiet_rotation) {
483 location = session_get_trace_archive_location(session);
484 ret = notification_thread_command_session_rotation_completed(
485 notification_thread_handle,
486 session->name,
487 session->uid,
488 session->gid,
489 session->last_archived_chunk_id.value,
490 location);
491 lttng_trace_archive_location_put(location);
492 if (ret != LTTNG_OK) {
493 ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s",
494 session->name);
495 }
496 }
497
498 ret = 0;
499 check_ongoing_rotation:
500 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
501 uint64_t chunk_being_archived_id;
502
503 chunk_status = lttng_trace_chunk_get_id(
504 session->chunk_being_archived,
505 &chunk_being_archived_id);
506 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
507
508 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s",
509 chunk_being_archived_id, session->name);
510 ret = timer_session_rotation_pending_check_start(session,
511 DEFAULT_ROTATE_PENDING_TIMER);
512 if (ret) {
513 ERR("Failed to re-enable rotation pending timer");
514 ret = -1;
515 goto end;
516 }
517 }
518
519 end:
520 return ret;
521 }
522
523 /* Call with the session and session_list locks held. */
524 static
525 int launch_session_rotation(struct ltt_session *session)
526 {
527 int ret;
528 struct lttng_rotate_session_return rotation_return;
529
530 DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
531 session->name);
532
533 ret = cmd_rotate_session(session, &rotation_return, false,
534 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
535 if (ret == LTTNG_OK) {
536 DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
537 session->name);
538 } else {
539 /* Don't consider errors as fatal. */
540 DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
541 session->name, lttng_strerror(ret));
542 }
543 return 0;
544 }
545
546 static
547 int run_job(struct rotation_thread_job *job, struct ltt_session *session,
548 struct notification_thread_handle *notification_thread_handle)
549 {
550 int ret;
551
552 switch (job->type) {
553 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
554 ret = launch_session_rotation(session);
555 break;
556 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
557 ret = check_session_rotation_pending(session,
558 notification_thread_handle);
559 break;
560 default:
561 abort();
562 }
563 return ret;
564 }
565
566 static
567 int handle_job_queue(struct rotation_thread_handle *handle,
568 struct rotation_thread *state,
569 struct rotation_thread_timer_queue *queue)
570 {
571 int ret = 0;
572
573 for (;;) {
574 struct ltt_session *session;
575 struct rotation_thread_job *job;
576
577 /* Take the queue lock only to pop an element from the list. */
578 pthread_mutex_lock(&queue->lock);
579 if (cds_list_empty(&queue->list)) {
580 pthread_mutex_unlock(&queue->lock);
581 break;
582 }
583 job = cds_list_first_entry(&queue->list,
584 typeof(*job), head);
585 cds_list_del(&job->head);
586 pthread_mutex_unlock(&queue->lock);
587
588 session_lock_list();
589 session = job->session;
590 if (!session) {
591 DBG("[rotation-thread] Session not found");
592 /*
593 * This is a non-fatal error, and we cannot report it to
594 * the user (timer), so just print the error and
595 * continue the processing.
596 *
597 * While the timer thread will purge pending signals for
598 * a session on the session's destruction, it is
599 * possible for a job targeting that session to have
600 * already been queued before it was destroyed.
601 */
602 free(job);
603 session_unlock_list();
604 continue;
605 }
606
607 session_lock(session);
608 ret = run_job(job, session, handle->notification_thread_handle);
609 session_unlock(session);
610 /* Release reference held by the job. */
611 session_put(session);
612 session_unlock_list();
613 free(job);
614 if (ret) {
615 goto end;
616 }
617 }
618
619 ret = 0;
620
621 end:
622 return ret;
623 }
624
625 static
626 int handle_condition(const struct lttng_notification *notification,
627 struct notification_thread_handle *notification_thread_handle)
628 {
629 int ret = 0;
630 const char *condition_session_name = NULL;
631 enum lttng_condition_type condition_type;
632 enum lttng_condition_status condition_status;
633 enum lttng_evaluation_status evaluation_status;
634 uint64_t consumed;
635 struct ltt_session *session;
636 const struct lttng_condition *condition =
637 lttng_notification_get_const_condition(notification);
638 const struct lttng_evaluation *evaluation =
639 lttng_notification_get_const_evaluation(notification);
640
641 condition_type = lttng_condition_get_type(condition);
642
643 if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) {
644 ret = -1;
645 ERR("[rotation-thread] Condition type and session usage type are not the same");
646 goto end;
647 }
648
649 /* Fetch info to test */
650 condition_status = lttng_condition_session_consumed_size_get_session_name(
651 condition, &condition_session_name);
652 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
653 ERR("[rotation-thread] Session name could not be fetched");
654 ret = -1;
655 goto end;
656 }
657 evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation,
658 &consumed);
659 if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) {
660 ERR("[rotation-thread] Failed to get evaluation");
661 ret = -1;
662 goto end;
663 }
664
665 session_lock_list();
666 session = session_find_by_name(condition_session_name);
667 if (!session) {
668 DBG("[rotation-thread] Failed to find session while handling notification: session name = `%s`",
669 condition_session_name);
670 /*
671 * Not a fatal error: a session can be destroyed before we get
672 * the chance to handle the notification.
673 */
674 ret = 0;
675 session_unlock_list();
676 goto end;
677 }
678 session_lock(session);
679
680 if (!lttng_trigger_is_equal(session->rotate_trigger,
681 lttng_notification_get_const_trigger(notification))) {
682 /* Notification does not originate from our rotation trigger. */
683 ret = 0;
684 goto end_unlock;
685 }
686
687 ret = unsubscribe_session_consumed_size_rotation(session,
688 notification_thread_handle);
689 if (ret) {
690 goto end_unlock;
691 }
692
693 ret = cmd_rotate_session(session, NULL, false,
694 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
695 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
696 DBG("Rotate already pending, subscribe to the next threshold value");
697 } else if (ret != LTTNG_OK) {
698 ERR("[rotation-thread] Failed to rotate on size notification with error: %s",
699 lttng_strerror(ret));
700 ret = -1;
701 goto end_unlock;
702 }
703 ret = subscribe_session_consumed_size_rotation(session,
704 consumed + session->rotate_size,
705 notification_thread_handle);
706 if (ret) {
707 ERR("[rotation-thread] Failed to subscribe to session consumed size condition");
708 goto end_unlock;
709 }
710 ret = 0;
711
712 end_unlock:
713 session_unlock(session);
714 session_put(session);
715 session_unlock_list();
716 end:
717 return ret;
718 }
719
720 static
721 int handle_notification_channel(int fd,
722 struct rotation_thread_handle *handle,
723 struct rotation_thread *state)
724 {
725 int ret;
726 bool notification_pending;
727 struct lttng_notification *notification = NULL;
728 enum lttng_notification_channel_status status;
729
730 status = lttng_notification_channel_has_pending_notification(
731 rotate_notification_channel, &notification_pending);
732 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
733 ERR("[rotation-thread ]Error occurred while checking for pending notification");
734 ret = -1;
735 goto end;
736 }
737
738 if (!notification_pending) {
739 ret = 0;
740 goto end;
741 }
742
743 /* Receive the next notification. */
744 status = lttng_notification_channel_get_next_notification(
745 rotate_notification_channel,
746 &notification);
747
748 switch (status) {
749 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
750 break;
751 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
752 /* Not an error, we will wait for the next one */
753 ret = 0;
754 goto end;;
755 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
756 ERR("Notification channel was closed");
757 ret = -1;
758 goto end;
759 default:
760 /* Unhandled conditions / errors. */
761 ERR("Unknown notification channel status");
762 ret = -1;
763 goto end;
764 }
765
766 ret = handle_condition(notification,
767 handle->notification_thread_handle);
768
769 end:
770 lttng_notification_destroy(notification);
771 return ret;
772 }
773
774 static
775 void *thread_rotation(void *data)
776 {
777 int ret;
778 struct rotation_thread_handle *handle = data;
779 struct rotation_thread thread;
780 int queue_pipe_fd;
781
782 DBG("[rotation-thread] Started rotation thread");
783 rcu_register_thread();
784 rcu_thread_online();
785 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
786 health_code_update();
787
788 if (!handle) {
789 ERR("[rotation-thread] Invalid thread context provided");
790 goto end;
791 }
792
793 queue_pipe_fd = lttng_pipe_get_readfd(
794 handle->rotation_timer_queue->event_pipe);
795
796
797 ret = init_thread_state(handle, &thread);
798 if (ret) {
799 goto error;
800 }
801
802 while (true) {
803 int fd_count, i;
804
805 health_poll_entry();
806 DBG("[rotation-thread] Entering poll wait");
807 ret = lttng_poll_wait(&thread.events, -1);
808 DBG("[rotation-thread] Poll wait returned (%i)", ret);
809 health_poll_exit();
810 if (ret < 0) {
811 /*
812 * Restart interrupted system call.
813 */
814 if (errno == EINTR) {
815 continue;
816 }
817 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret);
818 goto error;
819 }
820
821 fd_count = ret;
822 for (i = 0; i < fd_count; i++) {
823 int fd = LTTNG_POLL_GETFD(&thread.events, i);
824 uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i);
825
826 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
827 fd, revents);
828
829 if (revents & LPOLLERR) {
830 ERR("[rotation-thread] Polling returned an error on fd %i", fd);
831 goto error;
832 }
833
834 if (fd == rotate_notification_channel->socket) {
835 ret = handle_notification_channel(fd, handle,
836 &thread);
837 if (ret) {
838 ERR("[rotation-thread] Error occurred while handling activity on notification channel socket");
839 goto error;
840 }
841 } else {
842 /* Job queue or quit pipe activity. */
843
844 /*
845 * The job queue is serviced if there is
846 * activity on the quit pipe to ensure it is
847 * flushed and all references held in the queue
848 * are released.
849 */
850 ret = handle_job_queue(handle, &thread,
851 handle->rotation_timer_queue);
852 if (ret) {
853 ERR("[rotation-thread] Failed to handle rotation timer pipe event");
854 goto error;
855 }
856
857 if (fd == queue_pipe_fd) {
858 char buf;
859
860 ret = lttng_read(fd, &buf, 1);
861 if (ret != 1) {
862 ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd);
863 goto error;
864 }
865 } else {
866 DBG("[rotation-thread] Quit pipe activity");
867 goto exit;
868 }
869 }
870 }
871 }
872 exit:
873 error:
874 DBG("[rotation-thread] Exit");
875 fini_thread_state(&thread);
876 end:
877 health_unregister(health_sessiond);
878 rcu_thread_offline();
879 rcu_unregister_thread();
880 return NULL;
881 }
882
883 static
884 bool shutdown_rotation_thread(void *thread_data)
885 {
886 struct rotation_thread_handle *handle = thread_data;
887 const int write_fd = lttng_pipe_get_writefd(handle->quit_pipe);
888
889 return notify_thread_pipe(write_fd) == 1;
890 }
891
892 bool launch_rotation_thread(struct rotation_thread_handle *handle)
893 {
894 struct lttng_thread *thread;
895
896 thread = lttng_thread_create("Rotation",
897 thread_rotation,
898 shutdown_rotation_thread,
899 NULL,
900 handle);
901 if (!thread) {
902 goto error;
903 }
904 lttng_thread_put(thread);
905 return true;
906 error:
907 return false;
908 }
This page took 0.047866 seconds and 4 git commands to generate.