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