SoW-2020-0002: Trace Hit Counters
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread.c
CommitLineData
ab0ee2ca 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
ab0ee2ca 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
ab0ee2ca 5 *
ab0ee2ca
JG
6 */
7
8#define _LGPL_SOURCE
9#include <lttng/trigger/trigger.h>
10#include <lttng/notification/channel-internal.h>
11#include <lttng/notification/notification-internal.h>
12#include <lttng/condition/condition-internal.h>
13#include <lttng/condition/buffer-usage-internal.h>
14#include <common/error.h>
15#include <common/config/session-config.h>
16#include <common/defaults.h>
17#include <common/utils.h>
ab0ee2ca
JG
18#include <common/align.h>
19#include <common/time.h>
ab0ee2ca
JG
20#include <sys/stat.h>
21#include <time.h>
22#include <signal.h>
23
24#include "notification-thread.h"
25#include "notification-thread-events.h"
26#include "notification-thread-commands.h"
27#include "lttng-sessiond.h"
28#include "health-sessiond.h"
c8a9de5a 29#include "thread.h"
c3e68e71 30#include "testpoint.h"
ab0ee2ca 31
94078603
JR
32#include "kernel.h"
33#include <common/kernel-ctl/kernel-ctl.h>
34
ab0ee2ca
JG
35#include <urcu.h>
36#include <urcu/list.h>
37#include <urcu/rculfhash.h>
38
c3e68e71
JR
39
40int notifier_consumption_paused;
ab0ee2ca
JG
41/*
42 * Destroy the thread data previously created by the init function.
43 */
44void notification_thread_handle_destroy(
45 struct notification_thread_handle *handle)
46{
47 int ret;
ab0ee2ca
JG
48
49 if (!handle) {
50 goto end;
51 }
52
8ada111f 53 assert(cds_list_empty(&handle->cmd_queue.list));
ab0ee2ca 54 pthread_mutex_destroy(&handle->cmd_queue.lock);
c8a9de5a 55 sem_destroy(&handle->ready);
ab0ee2ca 56
814b4934
JR
57 if (handle->cmd_queue.event_pipe) {
58 lttng_pipe_destroy(handle->cmd_queue.event_pipe);
59 }
ab0ee2ca
JG
60 if (handle->channel_monitoring_pipes.ust32_consumer >= 0) {
61 ret = close(handle->channel_monitoring_pipes.ust32_consumer);
62 if (ret) {
63 PERROR("close 32-bit consumer channel monitoring pipe");
64 }
65 }
66 if (handle->channel_monitoring_pipes.ust64_consumer >= 0) {
67 ret = close(handle->channel_monitoring_pipes.ust64_consumer);
68 if (ret) {
69 PERROR("close 64-bit consumer channel monitoring pipe");
70 }
71 }
72 if (handle->channel_monitoring_pipes.kernel_consumer >= 0) {
73 ret = close(handle->channel_monitoring_pipes.kernel_consumer);
74 if (ret) {
75 PERROR("close kernel consumer channel monitoring pipe");
76 }
77 }
94078603 78
ab0ee2ca
JG
79end:
80 free(handle);
81}
82
83struct notification_thread_handle *notification_thread_handle_create(
84 struct lttng_pipe *ust32_channel_monitor_pipe,
85 struct lttng_pipe *ust64_channel_monitor_pipe,
c8a9de5a 86 struct lttng_pipe *kernel_channel_monitor_pipe)
ab0ee2ca
JG
87{
88 int ret;
89 struct notification_thread_handle *handle;
814b4934 90 struct lttng_pipe *event_pipe = NULL;
ab0ee2ca
JG
91
92 handle = zmalloc(sizeof(*handle));
93 if (!handle) {
94 goto end;
95 }
96
c8a9de5a
JG
97 sem_init(&handle->ready, 0, 0);
98
18d08850 99 event_pipe = lttng_pipe_open(FD_CLOEXEC);
814b4934
JR
100 if (!event_pipe) {
101 ERR("event_pipe creation");
ab0ee2ca
JG
102 goto error;
103 }
814b4934
JR
104
105 handle->cmd_queue.event_pipe = event_pipe;
106 event_pipe = NULL;
107
ab0ee2ca
JG
108 CDS_INIT_LIST_HEAD(&handle->cmd_queue.list);
109 ret = pthread_mutex_init(&handle->cmd_queue.lock, NULL);
110 if (ret) {
111 goto error;
112 }
113
114 if (ust32_channel_monitor_pipe) {
115 handle->channel_monitoring_pipes.ust32_consumer =
116 lttng_pipe_release_readfd(
117 ust32_channel_monitor_pipe);
118 if (handle->channel_monitoring_pipes.ust32_consumer < 0) {
119 goto error;
120 }
121 } else {
122 handle->channel_monitoring_pipes.ust32_consumer = -1;
123 }
124 if (ust64_channel_monitor_pipe) {
125 handle->channel_monitoring_pipes.ust64_consumer =
126 lttng_pipe_release_readfd(
127 ust64_channel_monitor_pipe);
128 if (handle->channel_monitoring_pipes.ust64_consumer < 0) {
129 goto error;
130 }
131 } else {
132 handle->channel_monitoring_pipes.ust64_consumer = -1;
133 }
134 if (kernel_channel_monitor_pipe) {
135 handle->channel_monitoring_pipes.kernel_consumer =
136 lttng_pipe_release_readfd(
137 kernel_channel_monitor_pipe);
138 if (handle->channel_monitoring_pipes.kernel_consumer < 0) {
139 goto error;
140 }
141 } else {
142 handle->channel_monitoring_pipes.kernel_consumer = -1;
143 }
d02d7404 144
ab0ee2ca
JG
145end:
146 return handle;
147error:
814b4934 148 lttng_pipe_destroy(event_pipe);
ab0ee2ca
JG
149 notification_thread_handle_destroy(handle);
150 return NULL;
151}
152
153static
154char *get_notification_channel_sock_path(void)
155{
156 int ret;
157 bool is_root = !getuid();
158 char *sock_path;
159
160 sock_path = zmalloc(LTTNG_PATH_MAX);
161 if (!sock_path) {
162 goto error;
163 }
164
165 if (is_root) {
166 ret = snprintf(sock_path, LTTNG_PATH_MAX,
167 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK);
168 if (ret < 0) {
169 goto error;
170 }
171 } else {
4f00620d 172 const char *home_path = utils_get_home_dir();
ab0ee2ca
JG
173
174 if (!home_path) {
175 ERR("Can't get HOME directory for socket creation");
176 goto error;
177 }
178
179 ret = snprintf(sock_path, LTTNG_PATH_MAX,
180 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK,
181 home_path);
182 if (ret < 0) {
183 goto error;
184 }
185 }
186
187 return sock_path;
188error:
189 free(sock_path);
190 return NULL;
191}
192
193static
194void notification_channel_socket_destroy(int fd)
195{
196 int ret;
197 char *sock_path = get_notification_channel_sock_path();
198
199 DBG("[notification-thread] Destroying notification channel socket");
200
201 if (sock_path) {
202 ret = unlink(sock_path);
203 free(sock_path);
204 if (ret < 0) {
205 PERROR("unlink notification channel socket");
206 }
207 }
208
209 ret = close(fd);
210 if (ret) {
211 PERROR("close notification channel socket");
212 }
213}
214
215static
216int notification_channel_socket_create(void)
217{
218 int fd = -1, ret;
219 char *sock_path = get_notification_channel_sock_path();
220
221 DBG("[notification-thread] Creating notification channel UNIX socket at %s",
222 sock_path);
223
224 ret = lttcomm_create_unix_sock(sock_path);
225 if (ret < 0) {
226 ERR("[notification-thread] Failed to create notification socket");
227 goto error;
228 }
229 fd = ret;
230
231 ret = chmod(sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
232 if (ret < 0) {
233 ERR("Set file permissions failed: %s", sock_path);
234 PERROR("chmod notification channel socket");
235 goto error;
236 }
237
238 if (getuid() == 0) {
28ab59d0
JR
239 gid_t gid;
240
241 ret = utils_get_group_id(config.tracing_group_name.value, true,
242 &gid);
243 if (ret) {
244 /* Default to root group. */
245 gid = 0;
246 }
247
248 ret = chown(sock_path, 0, gid);
ab0ee2ca
JG
249 if (ret) {
250 ERR("Failed to set the notification channel socket's group");
251 ret = -1;
252 goto error;
253 }
254 }
255
256 DBG("[notification-thread] Notification channel UNIX socket created (fd = %i)",
257 fd);
258 free(sock_path);
259 return fd;
260error:
261 if (fd >= 0 && close(fd) < 0) {
262 PERROR("close notification channel socket");
263 }
264 free(sock_path);
265 return ret;
266}
267
268static
269int init_poll_set(struct lttng_poll_event *poll_set,
270 struct notification_thread_handle *handle,
271 int notification_channel_socket)
272{
273 int ret;
274
275 /*
276 * Create pollset with size 5:
277 * - notification channel socket (listen for new connections),
278 * - command queue event fd (internal sessiond commands),
279 * - consumerd (32-bit user space) channel monitor pipe,
280 * - consumerd (64-bit user space) channel monitor pipe,
281 * - consumerd (kernel) channel monitor pipe.
282 */
283 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
284 if (ret < 0) {
285 goto end;
286 }
287
288 ret = lttng_poll_add(poll_set, notification_channel_socket,
289 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
290 if (ret < 0) {
291 ERR("[notification-thread] Failed to add notification channel socket to pollset");
292 goto error;
293 }
814b4934 294 ret = lttng_poll_add(poll_set, lttng_pipe_get_readfd(handle->cmd_queue.event_pipe),
ab0ee2ca
JG
295 LPOLLIN | LPOLLERR);
296 if (ret < 0) {
297 ERR("[notification-thread] Failed to add notification command queue event fd to pollset");
298 goto error;
299 }
300 ret = lttng_poll_add(poll_set,
301 handle->channel_monitoring_pipes.ust32_consumer,
302 LPOLLIN | LPOLLERR);
303 if (ret < 0) {
304 ERR("[notification-thread] Failed to add ust-32 channel monitoring pipe fd to pollset");
305 goto error;
306 }
307 ret = lttng_poll_add(poll_set,
308 handle->channel_monitoring_pipes.ust64_consumer,
309 LPOLLIN | LPOLLERR);
310 if (ret < 0) {
311 ERR("[notification-thread] Failed to add ust-64 channel monitoring pipe fd to pollset");
312 goto error;
313 }
314 if (handle->channel_monitoring_pipes.kernel_consumer < 0) {
315 goto end;
316 }
317 ret = lttng_poll_add(poll_set,
318 handle->channel_monitoring_pipes.kernel_consumer,
319 LPOLLIN | LPOLLERR);
320 if (ret < 0) {
321 ERR("[notification-thread] Failed to add kernel channel monitoring pipe fd to pollset");
322 goto error;
323 }
324end:
325 return ret;
326error:
327 lttng_poll_clean(poll_set);
328 return ret;
329}
330
331static
332void fini_thread_state(struct notification_thread_state *state)
333{
334 int ret;
335
336 if (state->client_socket_ht) {
337 ret = handle_notification_thread_client_disconnect_all(state);
338 assert(!ret);
339 ret = cds_lfht_destroy(state->client_socket_ht, NULL);
340 assert(!ret);
341 }
ac1889bf
JG
342 if (state->client_id_ht) {
343 ret = cds_lfht_destroy(state->client_id_ht, NULL);
344 assert(!ret);
345 }
ab0ee2ca
JG
346 if (state->triggers_ht) {
347 ret = handle_notification_thread_trigger_unregister_all(state);
348 assert(!ret);
349 ret = cds_lfht_destroy(state->triggers_ht, NULL);
350 assert(!ret);
351 }
352 if (state->channel_triggers_ht) {
353 ret = cds_lfht_destroy(state->channel_triggers_ht, NULL);
354 assert(!ret);
355 }
356 if (state->channel_state_ht) {
357 ret = cds_lfht_destroy(state->channel_state_ht, NULL);
358 assert(!ret);
359 }
360 if (state->notification_trigger_clients_ht) {
361 ret = cds_lfht_destroy(state->notification_trigger_clients_ht,
362 NULL);
363 assert(!ret);
364 }
365 if (state->channels_ht) {
8abe313a
JG
366 ret = cds_lfht_destroy(state->channels_ht, NULL);
367 assert(!ret);
368 }
369 if (state->sessions_ht) {
370 ret = cds_lfht_destroy(state->sessions_ht, NULL);
ab0ee2ca
JG
371 assert(!ret);
372 }
242388e4
JR
373 if (state->triggers_by_name_uid_ht) {
374 ret = cds_lfht_destroy(state->triggers_by_name_uid_ht, NULL);
375 assert(!ret);
376 }
e7c93cf9
JR
377 if (state->trigger_tokens_ht) {
378 ret = cds_lfht_destroy(state->trigger_tokens_ht, NULL);
379 assert(!ret);
380 }
ea9a44f0
JG
381 /*
382 * Must be destroyed after all channels have been destroyed.
383 * See comment in struct lttng_session_trigger_list.
384 */
385 if (state->session_triggers_ht) {
386 ret = cds_lfht_destroy(state->session_triggers_ht, NULL);
387 assert(!ret);
388 }
ab0ee2ca
JG
389 if (state->notification_channel_socket >= 0) {
390 notification_channel_socket_destroy(
391 state->notification_channel_socket);
392 }
d02d7404
JR
393
394 assert(cds_list_empty(&state->tracer_event_sources_list));
395
f2b3ef9f
JG
396 if (state->executor) {
397 action_executor_destroy(state->executor);
398 }
ab0ee2ca
JG
399 lttng_poll_clean(&state->events);
400}
401
c8a9de5a
JG
402static
403void mark_thread_as_ready(struct notification_thread_handle *handle)
404{
405 DBG("Marking notification thread as ready");
406 sem_post(&handle->ready);
407}
408
409static
410void wait_until_thread_is_ready(struct notification_thread_handle *handle)
411{
412 DBG("Waiting for notification thread to be ready");
413 sem_wait(&handle->ready);
414 DBG("Notification thread is ready");
415}
416
ab0ee2ca
JG
417static
418int init_thread_state(struct notification_thread_handle *handle,
419 struct notification_thread_state *state)
420{
421 int ret;
422
423 memset(state, 0, sizeof(*state));
424 state->notification_channel_socket = -1;
e6887944 425 state->trigger_id.next_tracer_token = 1;
ab0ee2ca
JG
426 lttng_poll_init(&state->events);
427
428 ret = notification_channel_socket_create();
429 if (ret < 0) {
430 goto end;
431 }
432 state->notification_channel_socket = ret;
433
434 ret = init_poll_set(&state->events, handle,
435 state->notification_channel_socket);
436 if (ret) {
437 goto end;
438 }
439
440 DBG("[notification-thread] Listening on notification channel socket");
441 ret = lttcomm_listen_unix_sock(state->notification_channel_socket);
442 if (ret < 0) {
443 ERR("[notification-thread] Listen failed on notification channel socket");
444 goto error;
445 }
446
447 state->client_socket_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
448 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
449 if (!state->client_socket_ht) {
450 goto error;
451 }
452
ac1889bf
JG
453 state->client_id_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
454 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
455 if (!state->client_id_ht) {
456 goto error;
457 }
458
ab0ee2ca
JG
459 state->channel_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
460 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
461 if (!state->channel_triggers_ht) {
462 goto error;
463 }
464
ea9a44f0
JG
465 state->session_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
466 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
467 if (!state->session_triggers_ht) {
468 goto error;
469 }
470
ab0ee2ca
JG
471 state->channel_state_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
472 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
473 if (!state->channel_state_ht) {
474 goto error;
475 }
476
477 state->notification_trigger_clients_ht = cds_lfht_new(DEFAULT_HT_SIZE,
478 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
479 if (!state->notification_trigger_clients_ht) {
480 goto error;
481 }
482
483 state->channels_ht = cds_lfht_new(DEFAULT_HT_SIZE,
484 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
485 if (!state->channels_ht) {
486 goto error;
487 }
c3e68e71 488
8abe313a
JG
489 state->sessions_ht = cds_lfht_new(DEFAULT_HT_SIZE,
490 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
491 if (!state->sessions_ht) {
492 goto error;
493 }
ab0ee2ca
JG
494 state->triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE,
495 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
496 if (!state->triggers_ht) {
497 goto error;
f2b3ef9f 498 }
242388e4
JR
499 state->triggers_by_name_uid_ht = cds_lfht_new(DEFAULT_HT_SIZE,
500 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
501 if (!state->triggers_by_name_uid_ht) {
502 goto error;
503 }
f2b3ef9f 504
e7c93cf9
JR
505 state->trigger_tokens_ht = cds_lfht_new(DEFAULT_HT_SIZE,
506 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
507 if (!state->trigger_tokens_ht) {
508 goto error;
509 }
510
d02d7404
JR
511 CDS_INIT_LIST_HEAD(&state->tracer_event_sources_list);
512
f2b3ef9f
JG
513 state->executor = action_executor_create(handle);
514 if (!state->executor) {
515 goto error;
ab0ee2ca 516 }
8b524060
FD
517
518 state->restart_poll = false;
519
c8a9de5a 520 mark_thread_as_ready(handle);
ab0ee2ca
JG
521end:
522 return 0;
523error:
524 fini_thread_state(state);
525 return -1;
526}
527
528static
529int handle_channel_monitoring_pipe(int fd, uint32_t revents,
530 struct notification_thread_handle *handle,
531 struct notification_thread_state *state)
532{
533 int ret = 0;
534 enum lttng_domain_type domain;
535
536 if (fd == handle->channel_monitoring_pipes.ust32_consumer ||
537 fd == handle->channel_monitoring_pipes.ust64_consumer) {
538 domain = LTTNG_DOMAIN_UST;
539 } else if (fd == handle->channel_monitoring_pipes.kernel_consumer) {
540 domain = LTTNG_DOMAIN_KERNEL;
541 } else {
542 abort();
543 }
544
545 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
546 ret = lttng_poll_del(&state->events, fd);
547 if (ret) {
548 ERR("[notification-thread] Failed to remove consumer monitoring pipe from poll set");
549 }
550 goto end;
551 }
552
553 ret = handle_notification_thread_channel_sample(
554 state, fd, domain);
555 if (ret) {
4149ace8 556 ERR("[notification-thread] Consumer sample handling error occurred");
ab0ee2ca
JG
557 ret = -1;
558 goto end;
559 }
560end:
561 return ret;
562}
563
94078603
JR
564static int handle_event_notification_pipe(int event_source_fd,
565 enum lttng_domain_type domain,
566 uint32_t revents,
567 struct notification_thread_state *state)
568{
569 int ret = 0;
570
571 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
572 ret = handle_notification_thread_remove_tracer_event_source_no_result(
573 state, event_source_fd);
574 if (ret) {
575 ERR("[notification-thread] Failed to remove event notification pipe from poll set: fd = %d",
576 event_source_fd);
577 }
578 goto end;
579 }
580
c3e68e71
JR
581 if (testpoint(sessiond_handle_notifier_event_pipe)) {
582 ret = 0;
583 goto end;
584 }
585
586 if (caa_unlikely(notifier_consumption_paused)) {
587 DBG("Event notifier notification consumption paused, sleeping...");
588 sleep(1);
589 goto end;
590 }
591
94078603
JR
592 ret = handle_notification_thread_event_notification(
593 state, event_source_fd, domain);
594 if (ret) {
595 ERR("[notification-thread] Event notification handling error occurred for fd: %d",
596 event_source_fd);
597 ret = -1;
598 goto end;
599 }
c3e68e71 600
94078603
JR
601end:
602 return ret;
603}
604
605/*
606 * Return the event source domain type via parameter.
607 */
608static bool fd_is_event_notification_source(const struct notification_thread_state *state,
609 int fd,
610 enum lttng_domain_type *domain)
611{
612 struct notification_event_tracer_event_source_element *source_element;
613
614 assert(domain);
615
616 cds_list_for_each_entry(source_element,
617 &state->tracer_event_sources_list, node) {
618 if (source_element->fd != fd) {
619 continue;
620 }
621
622 *domain = source_element->domain;
623 return true;
624 }
625
626 return false;
627}
628
ab0ee2ca
JG
629/*
630 * This thread services notification channel clients and commands received
631 * from various lttng-sessiond components over a command queue.
632 */
c8a9de5a 633static
ab0ee2ca
JG
634void *thread_notification(void *data)
635{
636 int ret;
637 struct notification_thread_handle *handle = data;
638 struct notification_thread_state state;
94078603 639 enum lttng_domain_type domain;
ab0ee2ca
JG
640
641 DBG("[notification-thread] Started notification thread");
642
f620cc28
JG
643 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_NOTIFICATION);
644 rcu_register_thread();
645 rcu_thread_online();
646
ab0ee2ca
JG
647 if (!handle) {
648 ERR("[notification-thread] Invalid thread context provided");
649 goto end;
650 }
651
ab0ee2ca
JG
652 health_code_update();
653
654 ret = init_thread_state(handle, &state);
655 if (ret) {
656 goto end;
657 }
658
c3e68e71
JR
659 if (testpoint(sessiond_thread_notification)) {
660 goto end;
661 }
662
ab0ee2ca
JG
663 while (true) {
664 int fd_count, i;
665
666 health_poll_entry();
667 DBG("[notification-thread] Entering poll wait");
668 ret = lttng_poll_wait(&state.events, -1);
669 DBG("[notification-thread] Poll wait returned (%i)", ret);
670 health_poll_exit();
671 if (ret < 0) {
672 /*
673 * Restart interrupted system call.
674 */
675 if (errno == EINTR) {
676 continue;
677 }
678 ERR("[notification-thread] Error encountered during lttng_poll_wait (%i)", ret);
679 goto error;
680 }
681
8b524060
FD
682 /*
683 * Reset restart_poll flag so that calls below might turn it
684 * on.
685 */
686 state.restart_poll = false;
687
ab0ee2ca
JG
688 fd_count = ret;
689 for (i = 0; i < fd_count; i++) {
690 int fd = LTTNG_POLL_GETFD(&state.events, i);
691 uint32_t revents = LTTNG_POLL_GETEV(&state.events, i);
692
693 DBG("[notification-thread] Handling fd (%i) activity (%u)", fd, revents);
694
695 if (fd == state.notification_channel_socket) {
696 if (revents & LPOLLIN) {
697 ret = handle_notification_thread_client_connect(
698 &state);
699 if (ret < 0) {
700 goto error;
701 }
702 } else if (revents &
703 (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
704 ERR("[notification-thread] Notification socket poll error");
705 goto error;
706 } else {
707 ERR("[notification-thread] Unexpected poll events %u for notification socket %i", revents, fd);
708 goto error;
709 }
814b4934 710 } else if (fd == lttng_pipe_get_readfd(handle->cmd_queue.event_pipe)) {
ab0ee2ca
JG
711 ret = handle_notification_thread_command(handle,
712 &state);
713 if (ret < 0) {
714 DBG("[notification-thread] Error encountered while servicing command queue");
715 goto error;
716 } else if (ret > 0) {
717 goto exit;
718 }
719 } else if (fd == handle->channel_monitoring_pipes.ust32_consumer ||
720 fd == handle->channel_monitoring_pipes.ust64_consumer ||
721 fd == handle->channel_monitoring_pipes.kernel_consumer) {
722 ret = handle_channel_monitoring_pipe(fd,
723 revents, handle, &state);
724 if (ret) {
725 goto error;
726 }
94078603
JR
727 } else if (fd_is_event_notification_source(&state, fd, &domain)) {
728 ret = handle_event_notification_pipe(fd, domain, revents, &state);
729 if (ret) {
730 goto error;
731 }
ab0ee2ca
JG
732 } else {
733 /* Activity on a client's socket. */
734 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
735 /*
736 * It doesn't matter if a command was
737 * pending on the client socket at this
738 * point since it now has no way to
739 * receive the notifications to which
740 * it was subscribing or unsubscribing.
741 */
742 ret = handle_notification_thread_client_disconnect(
743 fd, &state);
744 if (ret) {
745 goto error;
746 }
747 } else {
748 if (revents & LPOLLIN) {
749 ret = handle_notification_thread_client_in(
750 &state, fd);
751 if (ret) {
752 goto error;
753 }
754 }
755
756 if (revents & LPOLLOUT) {
757 ret = handle_notification_thread_client_out(
758 &state, fd);
759 if (ret) {
760 goto error;
761 }
762 }
763 }
764 }
8b524060
FD
765
766 /*
767 * Calls above might have changed the state of the
768 * FDs in `state.events`. Call _poll_wait() again to
769 * ensure we have a consistent state.
770 */
771 if (state.restart_poll) {
772 break;
773 }
ab0ee2ca
JG
774 }
775 }
776exit:
777error:
778 fini_thread_state(&state);
f620cc28 779end:
ab0ee2ca
JG
780 rcu_thread_offline();
781 rcu_unregister_thread();
f620cc28 782 health_unregister(health_sessiond);
ab0ee2ca
JG
783 return NULL;
784}
c8a9de5a
JG
785
786static
787bool shutdown_notification_thread(void *thread_data)
788{
789 struct notification_thread_handle *handle = thread_data;
790
791 notification_thread_command_quit(handle);
792 return true;
793}
794
4a91420c
JG
795struct lttng_thread *launch_notification_thread(
796 struct notification_thread_handle *handle)
c8a9de5a
JG
797{
798 struct lttng_thread *thread;
799
800 thread = lttng_thread_create("Notification",
801 thread_notification,
802 shutdown_notification_thread,
803 NULL,
804 handle);
805 if (!thread) {
806 goto error;
807 }
808
809 /*
810 * Wait for the thread to be marked as "ready" before returning
811 * as other subsystems depend on the notification subsystem
812 * (e.g. rotation thread).
813 */
814 wait_until_thread_is_ready(handle);
4a91420c 815 return thread;
c8a9de5a 816error:
4a91420c 817 return NULL;
c8a9de5a 818}
This page took 0.08295 seconds and 5 git commands to generate.