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