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