0c2d0d294628e2aea442498592ec1f379b15727a
[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 if (state->triggers_by_name_uid_ht) {
366 ret = cds_lfht_destroy(state->triggers_by_name_uid_ht, NULL);
367 assert(!ret);
368 }
369 /*
370 * Must be destroyed after all channels have been destroyed.
371 * See comment in struct lttng_session_trigger_list.
372 */
373 if (state->session_triggers_ht) {
374 ret = cds_lfht_destroy(state->session_triggers_ht, NULL);
375 assert(!ret);
376 }
377 if (state->notification_channel_socket >= 0) {
378 notification_channel_socket_destroy(
379 state->notification_channel_socket);
380 }
381 if (state->executor) {
382 action_executor_destroy(state->executor);
383 }
384 lttng_poll_clean(&state->events);
385 }
386
387 static
388 void mark_thread_as_ready(struct notification_thread_handle *handle)
389 {
390 DBG("Marking notification thread as ready");
391 sem_post(&handle->ready);
392 }
393
394 static
395 void wait_until_thread_is_ready(struct notification_thread_handle *handle)
396 {
397 DBG("Waiting for notification thread to be ready");
398 sem_wait(&handle->ready);
399 DBG("Notification thread is ready");
400 }
401
402 static
403 int init_thread_state(struct notification_thread_handle *handle,
404 struct notification_thread_state *state)
405 {
406 int ret;
407
408 memset(state, 0, sizeof(*state));
409 state->notification_channel_socket = -1;
410 lttng_poll_init(&state->events);
411
412 ret = notification_channel_socket_create();
413 if (ret < 0) {
414 goto end;
415 }
416 state->notification_channel_socket = ret;
417
418 ret = init_poll_set(&state->events, handle,
419 state->notification_channel_socket);
420 if (ret) {
421 goto end;
422 }
423
424 DBG("[notification-thread] Listening on notification channel socket");
425 ret = lttcomm_listen_unix_sock(state->notification_channel_socket);
426 if (ret < 0) {
427 ERR("[notification-thread] Listen failed on notification channel socket");
428 goto error;
429 }
430
431 state->client_socket_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
432 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
433 if (!state->client_socket_ht) {
434 goto error;
435 }
436
437 state->client_id_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
438 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
439 if (!state->client_id_ht) {
440 goto error;
441 }
442
443 state->channel_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
444 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
445 if (!state->channel_triggers_ht) {
446 goto error;
447 }
448
449 state->session_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
450 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
451 if (!state->session_triggers_ht) {
452 goto error;
453 }
454
455 state->channel_state_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
456 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
457 if (!state->channel_state_ht) {
458 goto error;
459 }
460
461 state->notification_trigger_clients_ht = cds_lfht_new(DEFAULT_HT_SIZE,
462 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
463 if (!state->notification_trigger_clients_ht) {
464 goto error;
465 }
466
467 state->channels_ht = cds_lfht_new(DEFAULT_HT_SIZE,
468 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
469 if (!state->channels_ht) {
470 goto error;
471 }
472 state->sessions_ht = cds_lfht_new(DEFAULT_HT_SIZE,
473 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
474 if (!state->sessions_ht) {
475 goto error;
476 }
477 state->triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE,
478 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
479 if (!state->triggers_ht) {
480 goto error;
481 }
482 state->triggers_by_name_uid_ht = cds_lfht_new(DEFAULT_HT_SIZE,
483 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
484 if (!state->triggers_by_name_uid_ht) {
485 goto error;
486 }
487
488 state->executor = action_executor_create(handle);
489 if (!state->executor) {
490 goto error;
491 }
492 mark_thread_as_ready(handle);
493 end:
494 return 0;
495 error:
496 fini_thread_state(state);
497 return -1;
498 }
499
500 static
501 int handle_channel_monitoring_pipe(int fd, uint32_t revents,
502 struct notification_thread_handle *handle,
503 struct notification_thread_state *state)
504 {
505 int ret = 0;
506 enum lttng_domain_type domain;
507
508 if (fd == handle->channel_monitoring_pipes.ust32_consumer ||
509 fd == handle->channel_monitoring_pipes.ust64_consumer) {
510 domain = LTTNG_DOMAIN_UST;
511 } else if (fd == handle->channel_monitoring_pipes.kernel_consumer) {
512 domain = LTTNG_DOMAIN_KERNEL;
513 } else {
514 abort();
515 }
516
517 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
518 ret = lttng_poll_del(&state->events, fd);
519 if (ret) {
520 ERR("[notification-thread] Failed to remove consumer monitoring pipe from poll set");
521 }
522 goto end;
523 }
524
525 ret = handle_notification_thread_channel_sample(
526 state, fd, domain);
527 if (ret) {
528 ERR("[notification-thread] Consumer sample handling error occurred");
529 ret = -1;
530 goto end;
531 }
532 end:
533 return ret;
534 }
535
536 /*
537 * This thread services notification channel clients and commands received
538 * from various lttng-sessiond components over a command queue.
539 */
540 static
541 void *thread_notification(void *data)
542 {
543 int ret;
544 struct notification_thread_handle *handle = data;
545 struct notification_thread_state state;
546
547 DBG("[notification-thread] Started notification thread");
548
549 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_NOTIFICATION);
550 rcu_register_thread();
551 rcu_thread_online();
552
553 if (!handle) {
554 ERR("[notification-thread] Invalid thread context provided");
555 goto end;
556 }
557
558 health_code_update();
559
560 ret = init_thread_state(handle, &state);
561 if (ret) {
562 goto end;
563 }
564
565 while (true) {
566 int fd_count, i;
567
568 health_poll_entry();
569 DBG("[notification-thread] Entering poll wait");
570 ret = lttng_poll_wait(&state.events, -1);
571 DBG("[notification-thread] Poll wait returned (%i)", ret);
572 health_poll_exit();
573 if (ret < 0) {
574 /*
575 * Restart interrupted system call.
576 */
577 if (errno == EINTR) {
578 continue;
579 }
580 ERR("[notification-thread] Error encountered during lttng_poll_wait (%i)", ret);
581 goto error;
582 }
583
584 fd_count = ret;
585 for (i = 0; i < fd_count; i++) {
586 int fd = LTTNG_POLL_GETFD(&state.events, i);
587 uint32_t revents = LTTNG_POLL_GETEV(&state.events, i);
588
589 DBG("[notification-thread] Handling fd (%i) activity (%u)", fd, revents);
590
591 if (fd == state.notification_channel_socket) {
592 if (revents & LPOLLIN) {
593 ret = handle_notification_thread_client_connect(
594 &state);
595 if (ret < 0) {
596 goto error;
597 }
598 } else if (revents &
599 (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
600 ERR("[notification-thread] Notification socket poll error");
601 goto error;
602 } else {
603 ERR("[notification-thread] Unexpected poll events %u for notification socket %i", revents, fd);
604 goto error;
605 }
606 } else if (fd == lttng_pipe_get_readfd(handle->cmd_queue.event_pipe)) {
607 ret = handle_notification_thread_command(handle,
608 &state);
609 if (ret < 0) {
610 DBG("[notification-thread] Error encountered while servicing command queue");
611 goto error;
612 } else if (ret > 0) {
613 goto exit;
614 }
615 } else if (fd == handle->channel_monitoring_pipes.ust32_consumer ||
616 fd == handle->channel_monitoring_pipes.ust64_consumer ||
617 fd == handle->channel_monitoring_pipes.kernel_consumer) {
618 ret = handle_channel_monitoring_pipe(fd,
619 revents, handle, &state);
620 if (ret) {
621 goto error;
622 }
623 } else {
624 /* Activity on a client's socket. */
625 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
626 /*
627 * It doesn't matter if a command was
628 * pending on the client socket at this
629 * point since it now has no way to
630 * receive the notifications to which
631 * it was subscribing or unsubscribing.
632 */
633 ret = handle_notification_thread_client_disconnect(
634 fd, &state);
635 if (ret) {
636 goto error;
637 }
638 } else {
639 if (revents & LPOLLIN) {
640 ret = handle_notification_thread_client_in(
641 &state, fd);
642 if (ret) {
643 goto error;
644 }
645 }
646
647 if (revents & LPOLLOUT) {
648 ret = handle_notification_thread_client_out(
649 &state, fd);
650 if (ret) {
651 goto error;
652 }
653 }
654 }
655 }
656 }
657 }
658 exit:
659 error:
660 fini_thread_state(&state);
661 end:
662 rcu_thread_offline();
663 rcu_unregister_thread();
664 health_unregister(health_sessiond);
665 return NULL;
666 }
667
668 static
669 bool shutdown_notification_thread(void *thread_data)
670 {
671 struct notification_thread_handle *handle = thread_data;
672
673 notification_thread_command_quit(handle);
674 return true;
675 }
676
677 struct lttng_thread *launch_notification_thread(
678 struct notification_thread_handle *handle)
679 {
680 struct lttng_thread *thread;
681
682 thread = lttng_thread_create("Notification",
683 thread_notification,
684 shutdown_notification_thread,
685 NULL,
686 handle);
687 if (!thread) {
688 goto error;
689 }
690
691 /*
692 * Wait for the thread to be marked as "ready" before returning
693 * as other subsystems depend on the notification subsystem
694 * (e.g. rotation thread).
695 */
696 wait_until_thread_is_ready(handle);
697 return thread;
698 error:
699 return NULL;
700 }
This page took 0.065761 seconds and 3 git commands to generate.