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