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