docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-events.cpp
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 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.hpp"
10 #define _LGPL_SOURCE
11 #include "condition-internal.hpp"
12 #include "event-notifier-error-accounting.hpp"
13 #include "kernel.hpp"
14 #include "lttng-sessiond.hpp"
15 #include "notification-thread-commands.hpp"
16 #include "notification-thread-events.hpp"
17 #include "notification-thread.hpp"
18
19 #include <common/defaults.hpp>
20 #include <common/dynamic-buffer.hpp>
21 #include <common/error.hpp>
22 #include <common/futex.hpp>
23 #include <common/hashtable/utils.hpp>
24 #include <common/macros.hpp>
25 #include <common/sessiond-comm/sessiond-comm.hpp>
26 #include <common/unix.hpp>
27 #include <common/urcu.hpp>
28
29 #include <lttng/action/action-internal.hpp>
30 #include <lttng/action/list-internal.hpp>
31 #include <lttng/condition/buffer-usage-internal.hpp>
32 #include <lttng/condition/condition-internal.hpp>
33 #include <lttng/condition/condition.h>
34 #include <lttng/condition/event-rule-matches-internal.hpp>
35 #include <lttng/condition/session-consumed-size-internal.hpp>
36 #include <lttng/condition/session-rotation-internal.hpp>
37 #include <lttng/domain-internal.hpp>
38 #include <lttng/event-rule/event-rule-internal.hpp>
39 #include <lttng/location-internal.hpp>
40 #include <lttng/notification/channel-internal.hpp>
41 #include <lttng/notification/notification-internal.hpp>
42 #include <lttng/trigger/trigger-internal.hpp>
43
44 #include <fcntl.h>
45 #include <inttypes.h>
46 #include <time.h>
47 #include <unistd.h>
48 #include <urcu.h>
49 #include <urcu/rculfhash.h>
50
51 #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLRDHUP)
52 #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT)
53
54 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56
57 enum lttng_object_type {
58 LTTNG_OBJECT_TYPE_UNKNOWN,
59 LTTNG_OBJECT_TYPE_NONE,
60 LTTNG_OBJECT_TYPE_CHANNEL,
61 LTTNG_OBJECT_TYPE_SESSION,
62 };
63
64 struct lttng_channel_trigger_list {
65 struct channel_key channel_key;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node;
72 };
73
74 /*
75 * List of triggers applying to a given session.
76 *
77 * See:
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
82 */
83 struct lttng_session_trigger_list {
84 char *session_name;
85 /* List of struct lttng_trigger_list_element. */
86 struct cds_list_head list;
87 /* Node in the session_triggers_ht */
88 struct cds_lfht_node session_triggers_ht_node;
89 /*
90 * Weak reference to the notification system's session triggers
91 * hashtable.
92 *
93 * The session trigger list structure structure is owned by
94 * the session's session_info.
95 *
96 * The session_info is kept alive the the channel_infos holding a
97 * reference to it (reference counting). When those channels are
98 * destroyed (at runtime or on teardown), the reference they hold
99 * to the session_info are released. On destruction of session_info,
100 * session_info_destroy() will remove the list of triggers applying
101 * to this session from the notification system's state.
102 *
103 * This implies that the session_triggers_ht must be destroyed
104 * after the channels.
105 */
106 struct cds_lfht *session_triggers_ht;
107 /* Used for delayed RCU reclaim. */
108 struct rcu_head rcu_node;
109 };
110
111 namespace {
112 struct lttng_trigger_list_element {
113 /* No ownership of the trigger object is assumed. */
114 struct lttng_trigger *trigger;
115 struct cds_list_head node;
116 };
117
118 struct lttng_trigger_ht_element {
119 struct lttng_trigger *trigger;
120 struct cds_lfht_node node;
121 struct cds_lfht_node node_by_name_uid;
122 struct cds_list_head client_list_trigger_node;
123 /* call_rcu delayed reclaim. */
124 struct rcu_head rcu_node;
125 };
126
127 struct lttng_condition_list_element {
128 struct lttng_condition *condition;
129 struct cds_list_head node;
130 };
131
132 struct channel_state_sample {
133 struct channel_key key;
134 struct cds_lfht_node channel_state_ht_node;
135 uint64_t highest_usage;
136 uint64_t lowest_usage;
137 /* call_rcu delayed reclaim. */
138 struct rcu_head rcu_node;
139 };
140 } /* namespace */
141
142 static unsigned long hash_channel_key(struct channel_key *key);
143 static int evaluate_buffer_condition(const struct lttng_condition *condition,
144 struct lttng_evaluation **evaluation,
145 const struct notification_thread_state *state,
146 const struct channel_state_sample *previous_sample,
147 const struct channel_state_sample *latest_sample,
148 struct channel_info *channel_info);
149 static int send_evaluation_to_clients(const struct lttng_trigger *trigger,
150 const struct lttng_evaluation *evaluation,
151 struct notification_client_list *client_list,
152 struct notification_thread_state *state,
153 uid_t channel_uid,
154 gid_t channel_gid);
155
156 /* session_info API */
157 static void session_info_destroy(void *_data);
158 static void session_info_get(struct session_info *session_info);
159 static void session_info_put(struct session_info *session_info);
160 static struct session_info *session_info_create(uint64_t id,
161 const char *name,
162 uid_t uid,
163 gid_t gid,
164 struct lttng_session_trigger_list *trigger_list,
165 struct cds_lfht *sessions_ht);
166 static void session_info_add_channel(struct session_info *session_info,
167 struct channel_info *channel_info);
168 static void session_info_remove_channel(struct session_info *session_info,
169 struct channel_info *channel_info);
170
171 /* lttng_session_trigger_list API */
172 static struct lttng_session_trigger_list *
173 lttng_session_trigger_list_create(const char *session_name, struct cds_lfht *session_triggers_ht);
174 static struct lttng_session_trigger_list *
175 lttng_session_trigger_list_build(const struct notification_thread_state *state,
176 const char *session_name);
177 static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list);
178 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
179 struct lttng_trigger *trigger);
180
181 static int client_handle_transmission_status(struct notification_client *client,
182 enum client_transmission_status transmission_status,
183 struct notification_thread_state *state);
184
185 static int handle_one_event_notifier_notification(struct notification_thread_state *state,
186 int pipe,
187 enum lttng_domain_type domain);
188
189 static void free_lttng_trigger_ht_element_rcu(struct rcu_head *node);
190
191 static int match_client_socket(struct cds_lfht_node *node, const void *key)
192 {
193 /* This double-cast is intended to supress pointer-to-cast warning. */
194 const int socket = (int) (intptr_t) key;
195 const struct notification_client *client =
196 caa_container_of(node, struct notification_client, client_socket_ht_node);
197
198 return client->socket == socket;
199 }
200
201 static int match_client_id(struct cds_lfht_node *node, const void *key)
202 {
203 /* This double-cast is intended to supress pointer-to-cast warning. */
204 const notification_client_id id = *((notification_client_id *) key);
205 const struct notification_client *client =
206 lttng::utils::container_of(node, &notification_client::client_id_ht_node);
207
208 return client->id == id;
209 }
210
211 static int match_channel_trigger_list(struct cds_lfht_node *node, const void *key)
212 {
213 struct channel_key *channel_key = (struct channel_key *) key;
214 struct lttng_channel_trigger_list *trigger_list;
215
216 trigger_list =
217 caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node);
218
219 return !!((channel_key->key == trigger_list->channel_key.key) &&
220 (channel_key->domain == trigger_list->channel_key.domain));
221 }
222
223 static int match_session_trigger_list(struct cds_lfht_node *node, const void *key)
224 {
225 const char *session_name = (const char *) key;
226 struct lttng_session_trigger_list *trigger_list;
227
228 trigger_list =
229 caa_container_of(node, struct lttng_session_trigger_list, session_triggers_ht_node);
230
231 return !!(strcmp(trigger_list->session_name, session_name) == 0);
232 }
233
234 static int match_channel_state_sample(struct cds_lfht_node *node, const void *key)
235 {
236 struct channel_key *channel_key = (struct channel_key *) key;
237 struct channel_state_sample *sample;
238
239 sample = caa_container_of(node, struct channel_state_sample, channel_state_ht_node);
240
241 return !!((channel_key->key == sample->key.key) &&
242 (channel_key->domain == sample->key.domain));
243 }
244
245 static int match_channel_info(struct cds_lfht_node *node, const void *key)
246 {
247 struct channel_key *channel_key = (struct channel_key *) key;
248 struct channel_info *channel_info;
249
250 channel_info = caa_container_of(node, struct channel_info, channels_ht_node);
251
252 return !!((channel_key->key == channel_info->key.key) &&
253 (channel_key->domain == channel_info->key.domain));
254 }
255
256 static int match_trigger(struct cds_lfht_node *node, const void *key)
257 {
258 struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
259 struct lttng_trigger_ht_element *trigger_ht_element;
260
261 trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element, node);
262
263 return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger);
264 }
265
266 static int match_trigger_token(struct cds_lfht_node *node, const void *key)
267 {
268 const uint64_t *_key = (uint64_t *) key;
269 struct notification_trigger_tokens_ht_element *element;
270
271 element = caa_container_of(node, struct notification_trigger_tokens_ht_element, node);
272 return *_key == element->token;
273 }
274
275 static int match_client_list_condition(struct cds_lfht_node *node, const void *key)
276 {
277 struct lttng_condition *condition_key = (struct lttng_condition *) key;
278 struct notification_client_list *client_list;
279 const struct lttng_condition *condition;
280
281 LTTNG_ASSERT(condition_key);
282
283 client_list = caa_container_of(
284 node, struct notification_client_list, notification_trigger_clients_ht_node);
285 condition = client_list->condition;
286
287 return !!lttng_condition_is_equal(condition_key, condition);
288 }
289
290 static int match_session_info(struct cds_lfht_node *node, const void *key)
291 {
292 const auto session_id = *((uint64_t *) key);
293 const auto *session_info =
294 lttng::utils::container_of(node, &session_info::sessions_ht_node);
295
296 return session_id == session_info->id;
297 }
298
299 static unsigned long hash_session_info_id(uint64_t id)
300 {
301 return hash_key_u64(&id, lttng_ht_seed);
302 }
303
304 static unsigned long hash_session_info(const struct session_info *session_info)
305 {
306 return hash_session_info_id(session_info->id);
307 }
308
309 static struct session_info *get_session_info_by_id(const struct notification_thread_state *state,
310 uint64_t id)
311 {
312 struct cds_lfht_iter iter;
313 struct cds_lfht_node *node;
314 lttng::urcu::read_lock_guard read_lock_guard;
315
316 cds_lfht_lookup(
317 state->sessions_ht, hash_session_info_id(id), match_session_info, &id, &iter);
318 node = cds_lfht_iter_get_node(&iter);
319
320 if (node) {
321 auto session_info =
322 lttng::utils::container_of(node, &session_info::sessions_ht_node);
323
324 session_info_get(session_info);
325 return session_info;
326 }
327
328 return nullptr;
329 }
330
331 static struct session_info *get_session_info_by_name(const struct notification_thread_state *state,
332 const char *name)
333 {
334 uint64_t session_id;
335 const auto found = sample_session_id_by_name(name, &session_id);
336
337 return found ? get_session_info_by_id(state, session_id) : nullptr;
338 }
339
340 static const char *notification_command_type_str(enum notification_thread_command_type type)
341 {
342 switch (type) {
343 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
344 return "REGISTER_TRIGGER";
345 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
346 return "UNREGISTER_TRIGGER";
347 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
348 return "ADD_CHANNEL";
349 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
350 return "REMOVE_CHANNEL";
351 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION:
352 return "ADD_SESSION";
353 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION:
354 return "REMOVE_SESSION";
355 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
356 return "SESSION_ROTATION_ONGOING";
357 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
358 return "SESSION_ROTATION_COMPLETED";
359 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
360 return "ADD_TRACER_EVENT_SOURCE";
361 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
362 return "REMOVE_TRACER_EVENT_SOURCE";
363 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
364 return "LIST_TRIGGERS";
365 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER:
366 return "GET_TRIGGER";
367 case NOTIFICATION_COMMAND_TYPE_QUIT:
368 return "QUIT";
369 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
370 return "CLIENT_COMMUNICATION_UPDATE";
371 default:
372 abort();
373 }
374 }
375
376 /*
377 * Match trigger based on name and credentials only.
378 * Name duplication is NOT allowed for the same uid.
379 */
380 static int match_trigger_by_name_uid(struct cds_lfht_node *node, const void *key)
381 {
382 bool match = false;
383 const char *element_trigger_name;
384 const char *key_name;
385 enum lttng_trigger_status status;
386 const struct lttng_credentials *key_creds;
387 const struct lttng_credentials *node_creds;
388 const struct lttng_trigger *trigger_key = (const struct lttng_trigger *) key;
389 const struct lttng_trigger_ht_element *trigger_ht_element =
390 caa_container_of(node, struct lttng_trigger_ht_element, node_by_name_uid);
391
392 status = lttng_trigger_get_name(trigger_ht_element->trigger, &element_trigger_name);
393 element_trigger_name = status == LTTNG_TRIGGER_STATUS_OK ? element_trigger_name : nullptr;
394
395 status = lttng_trigger_get_name(trigger_key, &key_name);
396 key_name = status == LTTNG_TRIGGER_STATUS_OK ? key_name : nullptr;
397
398 /*
399 * Compare the names.
400 * Consider null names as not equal. This is to maintain backwards
401 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
402 * triggers are allowed for a given user.
403 */
404 if (!element_trigger_name || !key_name) {
405 goto end;
406 }
407
408 if (strcmp(element_trigger_name, key_name) != 0) {
409 goto end;
410 }
411
412 /* Compare the owners' UIDs. */
413 key_creds = lttng_trigger_get_credentials(trigger_key);
414 node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
415
416 match = lttng_credentials_is_equal_uid(key_creds, node_creds);
417
418 end:
419 return match;
420 }
421
422 /*
423 * Hash trigger based on name and credentials only.
424 */
425 static unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger)
426 {
427 unsigned long hash = 0;
428 const struct lttng_credentials *trigger_creds;
429 const char *trigger_name;
430 enum lttng_trigger_status status;
431
432 status = lttng_trigger_get_name(trigger, &trigger_name);
433 if (status == LTTNG_TRIGGER_STATUS_OK) {
434 hash = hash_key_str(trigger_name, lttng_ht_seed);
435 }
436
437 trigger_creds = lttng_trigger_get_credentials(trigger);
438 hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid),
439 lttng_ht_seed);
440
441 return hash;
442 }
443
444 static unsigned long hash_channel_key(struct channel_key *key)
445 {
446 unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed);
447 unsigned long domain_hash =
448 hash_key_ulong((void *) (unsigned long) key->domain, lttng_ht_seed);
449
450 return key_hash ^ domain_hash;
451 }
452
453 static unsigned long hash_client_socket(int socket)
454 {
455 return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed);
456 }
457
458 static unsigned long hash_client_id(notification_client_id id)
459 {
460 return hash_key_u64(&id, lttng_ht_seed);
461 }
462
463 /*
464 * Get the type of object to which a given condition applies. Bindings let
465 * the notification system evaluate a trigger's condition when a given
466 * object's state is updated.
467 *
468 * For instance, a condition bound to a channel will be evaluated everytime
469 * the channel's state is changed by a channel monitoring sample.
470 */
471 static enum lttng_object_type get_condition_binding_object(const struct lttng_condition *condition)
472 {
473 switch (lttng_condition_get_type(condition)) {
474 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
475 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
476 return LTTNG_OBJECT_TYPE_CHANNEL;
477 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
478 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
480 return LTTNG_OBJECT_TYPE_SESSION;
481 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
482 return LTTNG_OBJECT_TYPE_NONE;
483 default:
484 return LTTNG_OBJECT_TYPE_UNKNOWN;
485 }
486 }
487
488 static void free_channel_info_rcu(struct rcu_head *node)
489 {
490 free(lttng::utils::container_of(node, &channel_info::rcu_node));
491 }
492
493 static void channel_info_destroy(struct channel_info *channel_info)
494 {
495 if (!channel_info) {
496 return;
497 }
498
499 if (channel_info->session_info) {
500 session_info_remove_channel(channel_info->session_info, channel_info);
501 session_info_put(channel_info->session_info);
502 }
503 if (channel_info->name) {
504 free(channel_info->name);
505 }
506 call_rcu(&channel_info->rcu_node, free_channel_info_rcu);
507 }
508
509 static void free_session_info_rcu(struct rcu_head *node)
510 {
511 free(lttng::utils::container_of(node, &session_info::rcu_node));
512 }
513
514 /* Don't call directly, use the ref-counting mechanism. */
515 static void session_info_destroy(void *_data)
516 {
517 struct session_info *session_info = (struct session_info *) _data;
518 int ret;
519
520 LTTNG_ASSERT(session_info);
521 if (session_info->channel_infos_ht) {
522 ret = cds_lfht_destroy(session_info->channel_infos_ht, nullptr);
523 if (ret) {
524 ERR("Failed to destroy channel information hash table");
525 }
526 }
527 lttng_session_trigger_list_destroy(session_info->trigger_list);
528
529 lttng::urcu::read_lock_guard read_lock;
530 cds_lfht_del(session_info->sessions_ht, &session_info->sessions_ht_node);
531 free(session_info->name);
532 lttng_trace_archive_location_put(session_info->last_state_sample.rotation.location);
533 call_rcu(&session_info->rcu_node, free_session_info_rcu);
534 }
535
536 static void session_info_get(struct session_info *session_info)
537 {
538 if (!session_info) {
539 return;
540 }
541 lttng_ref_get(&session_info->ref);
542 }
543
544 static void session_info_put(struct session_info *session_info)
545 {
546 if (!session_info) {
547 return;
548 }
549 lttng_ref_put(&session_info->ref);
550 }
551
552 static struct session_info *session_info_create(uint64_t id,
553 const char *name,
554 uid_t uid,
555 gid_t gid,
556 struct lttng_session_trigger_list *trigger_list,
557 struct cds_lfht *sessions_ht)
558 {
559 struct session_info *session_info;
560
561 LTTNG_ASSERT(name);
562
563 session_info = zmalloc<struct session_info>();
564 if (!session_info) {
565 goto end;
566 }
567
568 lttng_ref_init(&session_info->ref, session_info_destroy);
569
570 session_info->channel_infos_ht = cds_lfht_new(
571 DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
572 if (!session_info->channel_infos_ht) {
573 goto error;
574 }
575
576 cds_lfht_node_init(&session_info->sessions_ht_node);
577 session_info->id = id;
578 session_info->name = strdup(name);
579 if (!session_info->name) {
580 goto error;
581 }
582
583 session_info->uid = uid;
584 session_info->gid = gid;
585 session_info->trigger_list = trigger_list;
586 session_info->sessions_ht = sessions_ht;
587 end:
588 return session_info;
589 error:
590 session_info_put(session_info);
591 return nullptr;
592 }
593
594 static void session_info_add_channel(struct session_info *session_info,
595 struct channel_info *channel_info)
596 {
597 lttng::urcu::read_lock_guard read_lock;
598 cds_lfht_add(session_info->channel_infos_ht,
599 hash_channel_key(&channel_info->key),
600 &channel_info->session_info_channels_ht_node);
601 }
602
603 static void session_info_remove_channel(struct session_info *session_info,
604 struct channel_info *channel_info)
605 {
606 lttng::urcu::read_lock_guard read_lock;
607 cds_lfht_del(session_info->channel_infos_ht, &channel_info->session_info_channels_ht_node);
608 }
609
610 static struct channel_info *channel_info_create(const char *channel_name,
611 struct channel_key *channel_key,
612 uint64_t channel_capacity,
613 struct session_info *session_info)
614 {
615 struct channel_info *channel_info = zmalloc<struct channel_info>();
616
617 if (!channel_info) {
618 goto end;
619 }
620
621 cds_lfht_node_init(&channel_info->channels_ht_node);
622 cds_lfht_node_init(&channel_info->session_info_channels_ht_node);
623 memcpy(&channel_info->key, channel_key, sizeof(*channel_key));
624 channel_info->capacity = channel_capacity;
625
626 channel_info->name = strdup(channel_name);
627 if (!channel_info->name) {
628 goto error;
629 }
630
631 /*
632 * Set the references between session and channel infos:
633 * - channel_info holds a strong reference to session_info
634 * - session_info holds a weak reference to channel_info
635 */
636 session_info_get(session_info);
637 session_info_add_channel(session_info, channel_info);
638 channel_info->session_info = session_info;
639 end:
640 return channel_info;
641 error:
642 channel_info_destroy(channel_info);
643 return nullptr;
644 }
645
646 bool notification_client_list_get(struct notification_client_list *list)
647 {
648 return urcu_ref_get_unless_zero(&list->ref);
649 }
650
651 static void free_notification_client_list_rcu(struct rcu_head *node)
652 {
653 free(caa_container_of(node, struct notification_client_list, rcu_node));
654 }
655
656 static void notification_client_list_release(struct urcu_ref *list_ref)
657 {
658 struct notification_client_list *list =
659 lttng::utils::container_of(list_ref, &notification_client_list::ref);
660 struct notification_client_list_element *client_list_element, *tmp;
661
662 lttng_condition_put(list->condition);
663
664 if (list->notification_trigger_clients_ht) {
665 lttng::urcu::read_lock_guard read_lock;
666
667 cds_lfht_del(list->notification_trigger_clients_ht,
668 &list->notification_trigger_clients_ht_node);
669 list->notification_trigger_clients_ht = nullptr;
670 }
671 cds_list_for_each_entry_safe (client_list_element, tmp, &list->clients_list, node) {
672 free(client_list_element);
673 }
674
675 LTTNG_ASSERT(cds_list_empty(&list->triggers_list));
676
677 pthread_mutex_destroy(&list->lock);
678 call_rcu(&list->rcu_node, free_notification_client_list_rcu);
679 }
680
681 static bool condition_applies_to_client(const struct lttng_condition *condition,
682 struct notification_client *client)
683 {
684 bool applies = false;
685 struct lttng_condition_list_element *condition_list_element;
686
687 cds_list_for_each_entry (condition_list_element, &client->condition_list, node) {
688 applies = lttng_condition_is_equal(condition_list_element->condition, condition);
689 if (applies) {
690 break;
691 }
692 }
693
694 return applies;
695 }
696
697 static struct notification_client_list *
698 notification_client_list_create(struct notification_thread_state *state,
699 const struct lttng_condition *condition)
700 {
701 struct notification_client *client;
702 struct cds_lfht_iter iter;
703 struct notification_client_list *client_list;
704
705 client_list = zmalloc<notification_client_list>();
706 if (!client_list) {
707 PERROR("Failed to allocate notification client list");
708 goto end;
709 }
710
711 pthread_mutex_init(&client_list->lock, nullptr);
712 /*
713 * The trigger that owns the condition has the first reference to this
714 * client list.
715 */
716 urcu_ref_init(&client_list->ref);
717 cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node);
718 CDS_INIT_LIST_HEAD(&client_list->clients_list);
719 CDS_INIT_LIST_HEAD(&client_list->triggers_list);
720
721 /*
722 * Create a copy of the condition so that it's independent of any
723 * trigger. The client list may outlive the trigger object (which owns
724 * the condition) that is used to create it.
725 */
726 client_list->condition = lttng_condition_copy(condition);
727
728 {
729 /* Build a list of clients to which this new condition applies. */
730 lttng::urcu::read_lock_guard read_lock;
731
732 cds_lfht_for_each_entry (
733 state->client_socket_ht, &iter, client, client_socket_ht_node) {
734 struct notification_client_list_element *client_list_element;
735
736 if (!condition_applies_to_client(condition, client)) {
737 continue;
738 }
739
740 client_list_element = zmalloc<notification_client_list_element>();
741 if (!client_list_element) {
742 goto error_put_client_list;
743 }
744
745 CDS_INIT_LIST_HEAD(&client_list_element->node);
746 client_list_element->client = client;
747 cds_list_add(&client_list_element->node, &client_list->clients_list);
748 }
749 }
750
751 client_list->notification_trigger_clients_ht = state->notification_trigger_clients_ht;
752
753 /*
754 * Add the client list to the global list of client list.
755 */
756 {
757 lttng::urcu::read_lock_guard read_lock;
758
759 cds_lfht_add_unique(state->notification_trigger_clients_ht,
760 lttng_condition_hash(client_list->condition),
761 match_client_list_condition,
762 client_list->condition,
763 &client_list->notification_trigger_clients_ht_node);
764 }
765 goto end;
766
767 error_put_client_list:
768 notification_client_list_put(client_list);
769 client_list = nullptr;
770
771 end:
772 return client_list;
773 }
774
775 void notification_client_list_put(struct notification_client_list *list)
776 {
777 if (!list) {
778 return;
779 }
780 return urcu_ref_put(&list->ref, notification_client_list_release);
781 }
782
783 /* Provides a reference to the returned list. */
784 static struct notification_client_list *
785 get_client_list_from_condition(struct notification_thread_state *state,
786 const struct lttng_condition *condition)
787 {
788 struct cds_lfht_node *node;
789 struct cds_lfht_iter iter;
790 struct notification_client_list *list = nullptr;
791
792 lttng::urcu::read_lock_guard read_lock;
793 cds_lfht_lookup(state->notification_trigger_clients_ht,
794 lttng_condition_hash(condition),
795 match_client_list_condition,
796 condition,
797 &iter);
798 node = cds_lfht_iter_get_node(&iter);
799 if (node) {
800 list = lttng::utils::container_of(
801 node, &notification_client_list::notification_trigger_clients_ht_node);
802 list = notification_client_list_get(list) ? list : nullptr;
803 }
804
805 return list;
806 }
807
808 static int evaluate_channel_condition_for_client(const struct lttng_condition *condition,
809 struct notification_thread_state *state,
810 struct lttng_evaluation **evaluation,
811 uid_t *session_uid,
812 gid_t *session_gid)
813 {
814 int ret;
815 struct cds_lfht_iter iter;
816 struct cds_lfht_node *node;
817 struct channel_info *channel_info = nullptr;
818 struct channel_key *channel_key = nullptr;
819 struct channel_state_sample *last_sample = nullptr;
820 struct lttng_channel_trigger_list *channel_trigger_list = nullptr;
821
822 lttng::urcu::read_lock_guard read_lock;
823
824 /* Find the channel associated with the condition. */
825 cds_lfht_for_each_entry (
826 state->channel_triggers_ht, &iter, channel_trigger_list, channel_triggers_ht_node) {
827 struct lttng_trigger_list_element *element;
828
829 cds_list_for_each_entry (element, &channel_trigger_list->list, node) {
830 const struct lttng_condition *current_condition =
831 lttng_trigger_get_const_condition(element->trigger);
832
833 LTTNG_ASSERT(current_condition);
834 if (!lttng_condition_is_equal(condition, current_condition)) {
835 continue;
836 }
837
838 /* Found the trigger, save the channel key. */
839 channel_key = &channel_trigger_list->channel_key;
840 break;
841 }
842 if (channel_key) {
843 /* The channel key was found stop iteration. */
844 break;
845 }
846 }
847
848 if (!channel_key) {
849 /* No channel found; normal exit. */
850 DBG("No known channel associated with newly subscribed-to condition");
851 ret = 0;
852 goto end;
853 }
854
855 /* Fetch channel info for the matching channel. */
856 cds_lfht_lookup(state->channels_ht,
857 hash_channel_key(channel_key),
858 match_channel_info,
859 channel_key,
860 &iter);
861 node = cds_lfht_iter_get_node(&iter);
862 LTTNG_ASSERT(node);
863 channel_info = caa_container_of(node, struct channel_info, channels_ht_node);
864
865 /* Retrieve the channel's last sample, if it exists. */
866 cds_lfht_lookup(state->channel_state_ht,
867 hash_channel_key(channel_key),
868 match_channel_state_sample,
869 channel_key,
870 &iter);
871 node = cds_lfht_iter_get_node(&iter);
872 if (node) {
873 last_sample =
874 caa_container_of(node, struct channel_state_sample, channel_state_ht_node);
875 } else {
876 /* Nothing to evaluate, no sample was ever taken. Normal exit */
877 DBG("No channel sample associated with newly subscribed-to condition");
878 ret = 0;
879 goto end;
880 }
881
882 ret = evaluate_buffer_condition(
883 condition, evaluation, state, nullptr, last_sample, channel_info);
884 if (ret) {
885 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
886 goto end;
887 }
888
889 *session_uid = channel_info->session_info->uid;
890 *session_gid = channel_info->session_info->gid;
891 end:
892 return ret;
893 }
894
895 static const char *get_condition_session_name(const struct lttng_condition *condition)
896 {
897 const char *session_name = nullptr;
898 enum lttng_condition_status status;
899
900 switch (lttng_condition_get_type(condition)) {
901 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
902 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
903 status = lttng_condition_buffer_usage_get_session_name(condition, &session_name);
904 break;
905 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
906 status = lttng_condition_session_consumed_size_get_session_name(condition,
907 &session_name);
908 break;
909 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
910 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
911 status =
912 lttng_condition_session_rotation_get_session_name(condition, &session_name);
913 break;
914 default:
915 abort();
916 }
917 if (status != LTTNG_CONDITION_STATUS_OK) {
918 ERR("Failed to retrieve session rotation condition's session name");
919 goto end;
920 }
921 end:
922 return session_name;
923 }
924
925 static bool evaluate_session_rotation_ongoing_condition(const struct lttng_condition *condition
926 __attribute__((unused)),
927 const struct session_state_sample *sample)
928 {
929 return sample->rotation.ongoing;
930 }
931
932 static bool evaluate_session_consumed_size_condition(const struct lttng_condition *condition,
933 const struct session_state_sample *sample)
934 {
935 uint64_t threshold;
936 const struct lttng_condition_session_consumed_size *size_condition =
937 lttng::utils::container_of(condition,
938 &lttng_condition_session_consumed_size::parent);
939
940 threshold = size_condition->consumed_threshold_bytes.value;
941 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
942 ", current size = %" PRIu64,
943 threshold,
944 sample->consumed_data_size);
945 return sample->consumed_data_size >= threshold;
946 }
947
948 /*
949 * `new_state` can be NULL to indicate that we are not evaluating a
950 * state transition. A client subscribed or a trigger was registered and
951 * we wish to perform an initial evaluation.
952 */
953 static int evaluate_session_condition(const struct lttng_condition *condition,
954 const struct session_info *session_info,
955 const struct session_state_sample *new_state,
956 struct lttng_evaluation **evaluation)
957 {
958 int ret;
959 bool previous_result, newest_result;
960
961 switch (lttng_condition_get_type(condition)) {
962 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
963 if (new_state) {
964 previous_result = evaluate_session_rotation_ongoing_condition(
965 condition, &session_info->last_state_sample);
966 newest_result =
967 evaluate_session_rotation_ongoing_condition(condition, new_state);
968 } else {
969 previous_result = false;
970 newest_result = evaluate_session_rotation_ongoing_condition(
971 condition, &session_info->last_state_sample);
972 }
973 break;
974 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
975 if (new_state) {
976 previous_result = evaluate_session_consumed_size_condition(
977 condition, &session_info->last_state_sample);
978 newest_result =
979 evaluate_session_consumed_size_condition(condition, new_state);
980 } else {
981 previous_result = false;
982 newest_result = evaluate_session_consumed_size_condition(
983 condition, &session_info->last_state_sample);
984 }
985 break;
986 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
987 /*
988 * Note that LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED is
989 * evaluated differently to only consider state transitions without regard for the
990 * initial state. This is a deliberate choice as it is unlikely that a user would
991 * expect an action to occur for a rotation that occurred long before the trigger or
992 * subscription occurred.
993 */
994 if (!new_state) {
995 ret = 0;
996 goto end;
997 }
998
999 previous_result = !session_info->last_state_sample.rotation.ongoing;
1000 newest_result = !new_state->rotation.ongoing;
1001 break;
1002 default:
1003 ret = 0;
1004 goto end;
1005 }
1006
1007 if (!newest_result || (previous_result == newest_result)) {
1008 /* Not a state transition, evaluate to false. */
1009 ret = 0;
1010 goto end;
1011 }
1012
1013 switch (lttng_condition_get_type(condition)) {
1014 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1015 {
1016 const auto rotation_id = new_state ? new_state->rotation.id :
1017 session_info->last_state_sample.rotation.id;
1018
1019 *evaluation = lttng_evaluation_session_rotation_ongoing_create(rotation_id);
1020 break;
1021 }
1022 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1023 {
1024 const auto& sample = new_state ? *new_state : session_info->last_state_sample;
1025 const auto rotation_id = sample.rotation.id;
1026
1027 /* Callee acquires a reference to location. */
1028 *evaluation = lttng_evaluation_session_rotation_completed_create(
1029 rotation_id, sample.rotation.location);
1030 break;
1031 }
1032 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1033 {
1034 const auto latest_session_consumed_total = new_state ?
1035 new_state->consumed_data_size :
1036 session_info->last_state_sample.consumed_data_size;
1037
1038 *evaluation = lttng_evaluation_session_consumed_size_create(
1039 latest_session_consumed_total);
1040 break;
1041 }
1042 default:
1043 abort();
1044 }
1045
1046 if (!*evaluation) {
1047 /* Fatal error. */
1048 ERR("Failed to create session condition evaluation: session name = `%s`",
1049 session_info->name);
1050 ret = -1;
1051 goto end;
1052 }
1053
1054 ret = 0;
1055 end:
1056 return ret;
1057 }
1058
1059 static int evaluate_condition_for_client(const struct lttng_trigger *trigger,
1060 const struct lttng_condition *condition,
1061 struct notification_client *client,
1062 struct notification_thread_state *state)
1063 {
1064 int ret;
1065 struct lttng_evaluation *evaluation = nullptr;
1066 struct notification_client_list client_list = {
1067 .lock = PTHREAD_MUTEX_INITIALIZER,
1068 .ref = {},
1069 .condition = nullptr,
1070 .triggers_list = {},
1071 .clients_list = {},
1072 .notification_trigger_clients_ht = nullptr,
1073 .notification_trigger_clients_ht_node = {},
1074 .rcu_node = {},
1075 };
1076 struct notification_client_list_element client_list_element = {};
1077 uid_t object_uid = 0;
1078 gid_t object_gid = 0;
1079
1080 LTTNG_ASSERT(trigger);
1081 LTTNG_ASSERT(condition);
1082 LTTNG_ASSERT(client);
1083 LTTNG_ASSERT(state);
1084
1085 switch (get_condition_binding_object(condition)) {
1086 case LTTNG_OBJECT_TYPE_SESSION:
1087 {
1088 /* Find the session associated with the condition. */
1089 const auto *session_name = get_condition_session_name(condition);
1090 auto session_info = get_session_info_by_name(state, session_name);
1091 if (!session_info) {
1092 /* Not an error, the session doesn't exist yet. */
1093 DBG("Session not found while evaluating session condition for client: session name = `%s`",
1094 session_name);
1095 ret = 0;
1096 goto end;
1097 }
1098
1099 object_uid = session_info->uid;
1100 object_gid = session_info->gid;
1101
1102 ret = evaluate_session_condition(condition, session_info, nullptr, &evaluation);
1103 session_info_put(session_info);
1104 break;
1105 }
1106 case LTTNG_OBJECT_TYPE_CHANNEL:
1107 ret = evaluate_channel_condition_for_client(
1108 condition, state, &evaluation, &object_uid, &object_gid);
1109 break;
1110 case LTTNG_OBJECT_TYPE_NONE:
1111 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1112 ret = 0;
1113 goto end;
1114 case LTTNG_OBJECT_TYPE_UNKNOWN:
1115 default:
1116 ret = -1;
1117 goto end;
1118 }
1119 if (ret) {
1120 /* Fatal error. */
1121 goto end;
1122 }
1123 if (!evaluation) {
1124 /* Evaluation yielded nothing. Normal exit. */
1125 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1126 ret = 0;
1127 goto end;
1128 }
1129
1130 /*
1131 * Create a temporary client list with the client currently
1132 * subscribing.
1133 */
1134 cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node);
1135 CDS_INIT_LIST_HEAD(&client_list.clients_list);
1136
1137 CDS_INIT_LIST_HEAD(&client_list_element.node);
1138 client_list_element.client = client;
1139 cds_list_add(&client_list_element.node, &client_list.clients_list);
1140
1141 /* Send evaluation result to the newly-subscribed client. */
1142 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1143 ret = send_evaluation_to_clients(
1144 trigger, evaluation, &client_list, state, object_uid, object_gid);
1145
1146 end:
1147 return ret;
1148 }
1149
1150 static int notification_thread_client_subscribe(struct notification_client *client,
1151 struct lttng_condition *condition,
1152 struct notification_thread_state *state,
1153 enum lttng_notification_channel_status *_status)
1154 {
1155 int ret = 0;
1156 struct notification_client_list *client_list = nullptr;
1157 struct lttng_condition_list_element *condition_list_element = nullptr;
1158 struct notification_client_list_element *client_list_element = nullptr;
1159 struct lttng_trigger_ht_element *trigger_ht_element;
1160 enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1161
1162 /*
1163 * Ensure that the client has not already subscribed to this condition
1164 * before.
1165 */
1166 cds_list_for_each_entry (condition_list_element, &client->condition_list, node) {
1167 if (lttng_condition_is_equal(condition_list_element->condition, condition)) {
1168 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED;
1169 goto end;
1170 }
1171 }
1172
1173 condition_list_element = zmalloc<lttng_condition_list_element>();
1174 if (!condition_list_element) {
1175 ret = -1;
1176 goto error;
1177 }
1178 client_list_element = zmalloc<notification_client_list_element>();
1179 if (!client_list_element) {
1180 ret = -1;
1181 goto error;
1182 }
1183
1184 /*
1185 * Add the newly-subscribed condition to the client's subscription list.
1186 */
1187 CDS_INIT_LIST_HEAD(&condition_list_element->node);
1188 condition_list_element->condition = condition;
1189 condition = nullptr;
1190 cds_list_add(&condition_list_element->node, &client->condition_list);
1191
1192 client_list = get_client_list_from_condition(state, condition_list_element->condition);
1193 if (!client_list) {
1194 /*
1195 * No notification-emiting trigger registered with this
1196 * condition. We don't evaluate the condition right away
1197 * since this trigger is not registered yet.
1198 */
1199 free(client_list_element);
1200 goto end;
1201 }
1202
1203 /*
1204 * The condition to which the client just subscribed is evaluated
1205 * at this point so that conditions that are already TRUE result
1206 * in a notification being sent out.
1207 *
1208 * Note the iteration on all triggers which share an identical
1209 * `condition` than the one to which the client is registering. This is
1210 * done to ensure that the client receives a distinct notification for
1211 * all triggers that have a `notify` action that have this condition.
1212 */
1213 pthread_mutex_lock(&client_list->lock);
1214 cds_list_for_each_entry (
1215 trigger_ht_element, &client_list->triggers_list, client_list_trigger_node) {
1216 if (evaluate_condition_for_client(trigger_ht_element->trigger,
1217 condition_list_element->condition,
1218 client,
1219 state)) {
1220 WARN("Evaluation of a condition on client subscription failed, aborting.");
1221 ret = -1;
1222 free(client_list_element);
1223 pthread_mutex_unlock(&client_list->lock);
1224 goto end;
1225 }
1226 }
1227 pthread_mutex_unlock(&client_list->lock);
1228
1229 /*
1230 * Add the client to the list of clients interested in a given trigger
1231 * if a "notification" trigger with a corresponding condition was
1232 * added prior.
1233 */
1234 client_list_element->client = client;
1235 CDS_INIT_LIST_HEAD(&client_list_element->node);
1236
1237 pthread_mutex_lock(&client_list->lock);
1238 cds_list_add(&client_list_element->node, &client_list->clients_list);
1239 pthread_mutex_unlock(&client_list->lock);
1240 end:
1241 if (_status) {
1242 *_status = status;
1243 }
1244 if (client_list) {
1245 notification_client_list_put(client_list);
1246 }
1247 lttng_condition_destroy(condition);
1248 return ret;
1249 error:
1250 free(condition_list_element);
1251 free(client_list_element);
1252 lttng_condition_destroy(condition);
1253 return ret;
1254 }
1255
1256 static int notification_thread_client_unsubscribe(struct notification_client *client,
1257 struct lttng_condition *condition,
1258 struct notification_thread_state *state,
1259 enum lttng_notification_channel_status *_status)
1260 {
1261 struct notification_client_list *client_list;
1262 struct lttng_condition_list_element *condition_list_element, *condition_tmp;
1263 struct notification_client_list_element *client_list_element, *client_tmp;
1264 bool condition_found = false;
1265 enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1266
1267 /* Remove the condition from the client's condition list. */
1268 cds_list_for_each_entry_safe (
1269 condition_list_element, condition_tmp, &client->condition_list, node) {
1270 if (!lttng_condition_is_equal(condition_list_element->condition, condition)) {
1271 continue;
1272 }
1273
1274 cds_list_del(&condition_list_element->node);
1275 /*
1276 * The caller may be iterating on the client's conditions to
1277 * tear down a client's connection. In this case, the condition
1278 * will be destroyed at the end.
1279 */
1280 if (condition != condition_list_element->condition) {
1281 lttng_condition_destroy(condition_list_element->condition);
1282 }
1283 free(condition_list_element);
1284 condition_found = true;
1285 break;
1286 }
1287
1288 if (!condition_found) {
1289 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
1290 goto end;
1291 }
1292
1293 /*
1294 * Remove the client from the list of clients interested the trigger
1295 * matching the condition.
1296 */
1297 client_list = get_client_list_from_condition(state, condition);
1298 if (!client_list) {
1299 goto end;
1300 }
1301
1302 pthread_mutex_lock(&client_list->lock);
1303 cds_list_for_each_entry_safe (
1304 client_list_element, client_tmp, &client_list->clients_list, node) {
1305 if (client_list_element->client->id != client->id) {
1306 continue;
1307 }
1308 cds_list_del(&client_list_element->node);
1309 free(client_list_element);
1310 break;
1311 }
1312 pthread_mutex_unlock(&client_list->lock);
1313 notification_client_list_put(client_list);
1314 client_list = nullptr;
1315 end:
1316 lttng_condition_destroy(condition);
1317 if (_status) {
1318 *_status = status;
1319 }
1320 return 0;
1321 }
1322
1323 static void free_notification_client_rcu(struct rcu_head *node)
1324 {
1325 free(lttng::utils::container_of(node, &notification_client::rcu_node));
1326 }
1327
1328 static void notification_client_destroy(struct notification_client *client)
1329 {
1330 if (!client) {
1331 return;
1332 }
1333
1334 /*
1335 * The client object is not reachable by other threads, no need to lock
1336 * the client here.
1337 */
1338 if (client->socket >= 0) {
1339 (void) lttcomm_close_unix_sock(client->socket);
1340 client->socket = -1;
1341 }
1342 client->communication.active = false;
1343 lttng_payload_reset(&client->communication.inbound.payload);
1344 lttng_payload_reset(&client->communication.outbound.payload);
1345 pthread_mutex_destroy(&client->lock);
1346 call_rcu(&client->rcu_node, free_notification_client_rcu);
1347 }
1348
1349 /*
1350 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1351 * client pointer).
1352 */
1353 static struct notification_client *get_client_from_socket(int socket,
1354 struct notification_thread_state *state)
1355 {
1356 struct cds_lfht_iter iter;
1357 struct cds_lfht_node *node;
1358 struct notification_client *client = nullptr;
1359
1360 ASSERT_RCU_READ_LOCKED();
1361
1362 cds_lfht_lookup(state->client_socket_ht,
1363 hash_client_socket(socket),
1364 match_client_socket,
1365 (void *) (unsigned long) socket,
1366 &iter);
1367 node = cds_lfht_iter_get_node(&iter);
1368 if (!node) {
1369 goto end;
1370 }
1371
1372 client = caa_container_of(node, struct notification_client, client_socket_ht_node);
1373 end:
1374 return client;
1375 }
1376
1377 /*
1378 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1379 * client pointer).
1380 */
1381 static struct notification_client *get_client_from_id(notification_client_id id,
1382 struct notification_thread_state *state)
1383 {
1384 struct cds_lfht_iter iter;
1385 struct cds_lfht_node *node;
1386 struct notification_client *client = nullptr;
1387
1388 ASSERT_RCU_READ_LOCKED();
1389
1390 cds_lfht_lookup(state->client_id_ht, hash_client_id(id), match_client_id, &id, &iter);
1391 node = cds_lfht_iter_get_node(&iter);
1392 if (!node) {
1393 goto end;
1394 }
1395
1396 client = caa_container_of(node, struct notification_client, client_id_ht_node);
1397 end:
1398 return client;
1399 }
1400
1401 static bool buffer_usage_condition_applies_to_channel(const struct lttng_condition *condition,
1402 const struct channel_info *channel_info)
1403 {
1404 enum lttng_condition_status status;
1405 enum lttng_domain_type condition_domain;
1406 const char *condition_session_name = nullptr;
1407 const char *condition_channel_name = nullptr;
1408
1409 status = lttng_condition_buffer_usage_get_domain_type(condition, &condition_domain);
1410 LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK);
1411 if (channel_info->key.domain != condition_domain) {
1412 goto fail;
1413 }
1414
1415 status = lttng_condition_buffer_usage_get_session_name(condition, &condition_session_name);
1416 LTTNG_ASSERT((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1417
1418 status = lttng_condition_buffer_usage_get_channel_name(condition, &condition_channel_name);
1419 LTTNG_ASSERT((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name);
1420
1421 if (strcmp(channel_info->session_info->name, condition_session_name) != 0) {
1422 goto fail;
1423 }
1424 if (strcmp(channel_info->name, condition_channel_name) != 0) {
1425 goto fail;
1426 }
1427
1428 return true;
1429 fail:
1430 return false;
1431 }
1432
1433 static bool trigger_applies_to_channel(const struct lttng_trigger *trigger,
1434 const struct channel_info *channel_info)
1435 {
1436 const struct lttng_condition *condition;
1437 bool trigger_applies;
1438
1439 condition = lttng_trigger_get_const_condition(trigger);
1440 if (!condition) {
1441 goto fail;
1442 }
1443
1444 switch (lttng_condition_get_type(condition)) {
1445 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1446 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1447 trigger_applies =
1448 buffer_usage_condition_applies_to_channel(condition, channel_info);
1449 break;
1450 default:
1451 goto fail;
1452 }
1453
1454 return trigger_applies;
1455 fail:
1456 return false;
1457 }
1458
1459 /* Must be called with RCU read lock held. */
1460 static struct lttng_session_trigger_list *
1461 get_session_trigger_list(struct notification_thread_state *state, const char *session_name)
1462 {
1463 struct lttng_session_trigger_list *list = nullptr;
1464 struct cds_lfht_node *node;
1465 struct cds_lfht_iter iter;
1466
1467 ASSERT_RCU_READ_LOCKED();
1468
1469 cds_lfht_lookup(state->session_triggers_ht,
1470 hash_key_str(session_name, lttng_ht_seed),
1471 match_session_trigger_list,
1472 session_name,
1473 &iter);
1474 node = cds_lfht_iter_get_node(&iter);
1475 if (!node) {
1476 /*
1477 * Not an error, the list of triggers applying to that session
1478 * will be initialized when the session is created.
1479 */
1480 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1481 session_name);
1482 goto end;
1483 }
1484
1485 list = caa_container_of(node, struct lttng_session_trigger_list, session_triggers_ht_node);
1486 end:
1487 return list;
1488 }
1489
1490 /*
1491 * Allocate an empty lttng_session_trigger_list for the session named
1492 * 'session_name'.
1493 */
1494 static struct lttng_session_trigger_list *
1495 lttng_session_trigger_list_create(const char *session_name, struct cds_lfht *session_triggers_ht)
1496 {
1497 struct lttng_session_trigger_list *list = nullptr;
1498 char *session_name_copy = strdup(session_name);
1499
1500 if (!session_name_copy) {
1501 PERROR("Failed to allocate session name while building trigger list");
1502 goto end;
1503 }
1504
1505 list = zmalloc<lttng_session_trigger_list>();
1506 if (!list) {
1507 PERROR("Failed to allocate session trigger list while building trigger list");
1508 goto end;
1509 }
1510
1511 list->session_name = session_name_copy;
1512 CDS_INIT_LIST_HEAD(&list->list);
1513 cds_lfht_node_init(&list->session_triggers_ht_node);
1514 list->session_triggers_ht = session_triggers_ht;
1515
1516 /* Publish the list through the session_triggers_ht. */
1517 {
1518 lttng::urcu::read_lock_guard read_lock;
1519 cds_lfht_add(session_triggers_ht,
1520 hash_key_str(session_name, lttng_ht_seed),
1521 &list->session_triggers_ht_node);
1522 }
1523 end:
1524 return list;
1525 }
1526
1527 static void free_session_trigger_list_rcu(struct rcu_head *node)
1528 {
1529 struct lttng_session_trigger_list *list =
1530 caa_container_of(node, struct lttng_session_trigger_list, rcu_node);
1531
1532 free(list->session_name);
1533 free(list);
1534 }
1535
1536 static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list)
1537 {
1538 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1539
1540 /* Empty the list element by element, and then free the list itself. */
1541 cds_list_for_each_entry_safe (trigger_list_element, tmp, &list->list, node) {
1542 cds_list_del(&trigger_list_element->node);
1543 free(trigger_list_element);
1544 }
1545 lttng::urcu::read_lock_guard read_lock;
1546 /* Unpublish the list from the session_triggers_ht. */
1547 cds_lfht_del(list->session_triggers_ht, &list->session_triggers_ht_node);
1548 call_rcu(&list->rcu_node, free_session_trigger_list_rcu);
1549 }
1550
1551 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
1552 struct lttng_trigger *trigger)
1553 {
1554 int ret = 0;
1555 struct lttng_trigger_list_element *new_element = zmalloc<lttng_trigger_list_element>();
1556
1557 if (!new_element) {
1558 ret = -1;
1559 goto end;
1560 }
1561 CDS_INIT_LIST_HEAD(&new_element->node);
1562 new_element->trigger = trigger;
1563 cds_list_add(&new_element->node, &list->list);
1564 end:
1565 return ret;
1566 }
1567
1568 static bool trigger_applies_to_session(const struct lttng_trigger *trigger,
1569 const char *session_name)
1570 {
1571 bool applies = false;
1572 const struct lttng_condition *condition;
1573
1574 condition = lttng_trigger_get_const_condition(trigger);
1575 switch (lttng_condition_get_type(condition)) {
1576 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1577 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1578 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1579 {
1580 const char *condition_session_name;
1581
1582 condition_session_name = get_condition_session_name(condition);
1583 LTTNG_ASSERT(condition_session_name);
1584 applies = !strcmp(condition_session_name, session_name);
1585 break;
1586 }
1587 default:
1588 goto end;
1589 }
1590 end:
1591 return applies;
1592 }
1593
1594 /*
1595 * Allocate and initialize an lttng_session_trigger_list which contains
1596 * all triggers that apply to the session named 'session_name'.
1597 */
1598 static struct lttng_session_trigger_list *
1599 lttng_session_trigger_list_build(const struct notification_thread_state *state,
1600 const char *session_name)
1601 {
1602 int trigger_count = 0;
1603 struct lttng_session_trigger_list *session_trigger_list = nullptr;
1604 struct lttng_trigger_ht_element *trigger_ht_element = nullptr;
1605 struct cds_lfht_iter iter;
1606
1607 session_trigger_list =
1608 lttng_session_trigger_list_create(session_name, state->session_triggers_ht);
1609
1610 {
1611 /* Add all triggers applying to the session named 'session_name'. */
1612 lttng::urcu::read_lock_guard read_lock;
1613
1614 cds_lfht_for_each_entry (state->triggers_ht, &iter, trigger_ht_element, node) {
1615 int ret;
1616
1617 if (!trigger_applies_to_session(trigger_ht_element->trigger,
1618 session_name)) {
1619 continue;
1620 }
1621
1622 ret = lttng_session_trigger_list_add(session_trigger_list,
1623 trigger_ht_element->trigger);
1624 if (ret) {
1625 goto error;
1626 }
1627
1628 trigger_count++;
1629 }
1630 }
1631
1632 DBG("Found %i triggers that apply to newly created session", trigger_count);
1633 return session_trigger_list;
1634 error:
1635 lttng_session_trigger_list_destroy(session_trigger_list);
1636 return nullptr;
1637 }
1638
1639 static struct session_info *create_and_publish_session_info(struct notification_thread_state *state,
1640 uint64_t id,
1641 const char *name,
1642 uid_t uid,
1643 gid_t gid)
1644 {
1645 struct session_info *session = nullptr;
1646 struct lttng_session_trigger_list *trigger_list;
1647
1648 lttng::urcu::read_lock_guard read_lock;
1649 trigger_list = lttng_session_trigger_list_build(state, name);
1650 if (!trigger_list) {
1651 goto error;
1652 }
1653
1654 session = session_info_create(id, name, uid, gid, trigger_list, state->sessions_ht);
1655 if (!session) {
1656 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1657 name,
1658 uid,
1659 gid);
1660 lttng_session_trigger_list_destroy(trigger_list);
1661 goto error;
1662 }
1663
1664 /* Transferred ownership to the new session. */
1665 trigger_list = nullptr;
1666
1667 if (cds_lfht_add_unique(state->sessions_ht,
1668 hash_session_info(session),
1669 match_session_info,
1670 &id,
1671 &session->sessions_ht_node) != &session->sessions_ht_node) {
1672 ERR("Duplicate session found: name = `%s`, id = %" PRIu64, name, id);
1673 goto error;
1674 }
1675
1676 return session;
1677 error:
1678 session_info_put(session);
1679 return nullptr;
1680 }
1681
1682 static int handle_notification_thread_command_add_channel(struct notification_thread_state *state,
1683 uint64_t session_id,
1684 const char *channel_name,
1685 enum lttng_domain_type channel_domain,
1686 uint64_t channel_key_int,
1687 uint64_t channel_capacity,
1688 enum lttng_error_code *cmd_result)
1689 {
1690 struct cds_list_head trigger_list;
1691 struct channel_info *new_channel_info = nullptr;
1692 struct channel_key channel_key = {
1693 .key = channel_key_int,
1694 .domain = channel_domain,
1695 };
1696 struct lttng_channel_trigger_list *channel_trigger_list = nullptr;
1697 struct lttng_trigger_ht_element *trigger_ht_element = nullptr;
1698 int trigger_count = 0;
1699 struct cds_lfht_iter iter;
1700 struct session_info *session_info = nullptr;
1701 lttng::urcu::read_lock_guard read_lock;
1702
1703 DBG("Adding channel: channel name = `%s`, session id = %" PRIu64 ", channel key = %" PRIu64
1704 ", domain = %s",
1705 channel_name,
1706 session_id,
1707 channel_key_int,
1708 lttng_domain_type_str(channel_domain));
1709
1710 CDS_INIT_LIST_HEAD(&trigger_list);
1711
1712 session_info = get_session_info_by_id(state, session_id);
1713 if (!session_info) {
1714 /* Fatal logic error. */
1715 ERR("Failed to find session while adding channel: session id = %" PRIu64,
1716 session_id);
1717 goto error;
1718 }
1719
1720 new_channel_info =
1721 channel_info_create(channel_name, &channel_key, channel_capacity, session_info);
1722 if (!new_channel_info) {
1723 goto error;
1724 }
1725
1726 /* Build a list of all triggers applying to the new channel. */
1727 cds_lfht_for_each_entry (state->triggers_ht, &iter, trigger_ht_element, node) {
1728 struct lttng_trigger_list_element *new_element;
1729
1730 if (!trigger_applies_to_channel(trigger_ht_element->trigger, new_channel_info)) {
1731 continue;
1732 }
1733
1734 new_element = zmalloc<lttng_trigger_list_element>();
1735 if (!new_element) {
1736 goto error;
1737 }
1738 CDS_INIT_LIST_HEAD(&new_element->node);
1739 new_element->trigger = trigger_ht_element->trigger;
1740 cds_list_add(&new_element->node, &trigger_list);
1741 trigger_count++;
1742 }
1743
1744 DBG("Found %i triggers that apply to newly added channel", trigger_count);
1745 channel_trigger_list = zmalloc<lttng_channel_trigger_list>();
1746 if (!channel_trigger_list) {
1747 goto error;
1748 }
1749 channel_trigger_list->channel_key = new_channel_info->key;
1750 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
1751 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
1752 cds_list_splice(&trigger_list, &channel_trigger_list->list);
1753
1754 /* Add channel to the channel_ht which owns the channel_infos. */
1755 cds_lfht_add(state->channels_ht,
1756 hash_channel_key(&new_channel_info->key),
1757 &new_channel_info->channels_ht_node);
1758 /*
1759 * Add the list of triggers associated with this channel to the
1760 * channel_triggers_ht.
1761 */
1762 cds_lfht_add(state->channel_triggers_ht,
1763 hash_channel_key(&new_channel_info->key),
1764 &channel_trigger_list->channel_triggers_ht_node);
1765 session_info_put(session_info);
1766 *cmd_result = LTTNG_OK;
1767 return 0;
1768 error:
1769 channel_info_destroy(new_channel_info);
1770 session_info_put(session_info);
1771 return 1;
1772 }
1773
1774 static int handle_notification_thread_command_add_session(struct notification_thread_state *state,
1775 uint64_t session_id,
1776 const char *session_name,
1777 uid_t session_uid,
1778 gid_t session_gid,
1779 enum lttng_error_code *cmd_result)
1780 {
1781 int ret;
1782
1783 DBG("Adding session: session name = `%s`, session id = %" PRIu64
1784 ", session uid = %d, session gid = %d",
1785 session_name,
1786 session_id,
1787 session_uid,
1788 session_gid);
1789
1790 auto session = create_and_publish_session_info(
1791 state, session_id, session_name, session_uid, session_gid);
1792 if (!session) {
1793 PERROR("Failed to add session: session name = `%s`, session id = %" PRIu64
1794 ", session uid = %d, session gid = %d",
1795 session_name,
1796 session_id,
1797 session_uid,
1798 session_gid);
1799 ret = -1;
1800 *cmd_result = LTTNG_ERR_NOMEM;
1801 goto end;
1802 }
1803
1804 /*
1805 * Note that the reference to `session` is not released; this reference is
1806 * the "global" reference that is used to allow look-ups. This reference will
1807 * only be released when the session is removed. See
1808 * handle_notification_thread_command_remove_session.
1809 */
1810 ret = 0;
1811 *cmd_result = LTTNG_OK;
1812 end:
1813 return ret;
1814 }
1815
1816 static int
1817 handle_notification_thread_command_remove_session(struct notification_thread_state *state,
1818 uint64_t session_id,
1819 enum lttng_error_code *cmd_result)
1820 {
1821 int ret;
1822
1823 DBG("Removing session: session id = %" PRIu64, session_id);
1824
1825 auto session = get_session_info_by_id(state, session_id);
1826 if (!session) {
1827 ERR("Failed to remove session: session id = %" PRIu64, session_id);
1828 ret = -1;
1829 *cmd_result = LTTNG_ERR_NO_SESSION;
1830 goto end;
1831 }
1832
1833 /* Release the reference returned by the look-up, and then release the global reference. */
1834 session_info_put(session);
1835 session_info_put(session);
1836 ret = 0;
1837 *cmd_result = LTTNG_OK;
1838 end:
1839 return ret;
1840 }
1841
1842 static void free_channel_trigger_list_rcu(struct rcu_head *node)
1843 {
1844 free(caa_container_of(node, struct lttng_channel_trigger_list, rcu_node));
1845 }
1846
1847 static void free_channel_state_sample_rcu(struct rcu_head *node)
1848 {
1849 free(caa_container_of(node, struct channel_state_sample, rcu_node));
1850 }
1851
1852 static int
1853 handle_notification_thread_command_remove_channel(struct notification_thread_state *state,
1854 uint64_t channel_key,
1855 enum lttng_domain_type domain,
1856 enum lttng_error_code *cmd_result)
1857 {
1858 struct cds_lfht_node *node;
1859 struct cds_lfht_iter iter;
1860 struct lttng_channel_trigger_list *trigger_list;
1861 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1862 struct channel_key key = { .key = channel_key, .domain = domain };
1863 struct channel_info *channel_info;
1864
1865 DBG("Removing channel key = %" PRIu64 " in %s domain",
1866 channel_key,
1867 lttng_domain_type_str(domain));
1868
1869 lttng::urcu::read_lock_guard read_lock;
1870
1871 cds_lfht_lookup(state->channel_triggers_ht,
1872 hash_channel_key(&key),
1873 match_channel_trigger_list,
1874 &key,
1875 &iter);
1876 node = cds_lfht_iter_get_node(&iter);
1877 /*
1878 * There is a severe internal error if we are being asked to remove a
1879 * channel that doesn't exist.
1880 */
1881 if (!node) {
1882 ERR("Channel being removed is unknown to the notification thread");
1883 goto end;
1884 }
1885
1886 /* Free the list of triggers associated with this channel. */
1887 trigger_list =
1888 caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node);
1889 cds_list_for_each_entry_safe (trigger_list_element, tmp, &trigger_list->list, node) {
1890 cds_list_del(&trigger_list_element->node);
1891 free(trigger_list_element);
1892 }
1893 cds_lfht_del(state->channel_triggers_ht, node);
1894 call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu);
1895
1896 /* Free sampled channel state. */
1897 cds_lfht_lookup(state->channel_state_ht,
1898 hash_channel_key(&key),
1899 match_channel_state_sample,
1900 &key,
1901 &iter);
1902 node = cds_lfht_iter_get_node(&iter);
1903 /*
1904 * This is expected to be NULL if the channel is destroyed before we
1905 * received a sample.
1906 */
1907 if (node) {
1908 struct channel_state_sample *sample =
1909 caa_container_of(node, struct channel_state_sample, channel_state_ht_node);
1910
1911 cds_lfht_del(state->channel_state_ht, node);
1912 call_rcu(&sample->rcu_node, free_channel_state_sample_rcu);
1913 }
1914
1915 /* Remove the channel from the channels_ht and free it. */
1916 cds_lfht_lookup(
1917 state->channels_ht, hash_channel_key(&key), match_channel_info, &key, &iter);
1918 node = cds_lfht_iter_get_node(&iter);
1919 LTTNG_ASSERT(node);
1920 channel_info = caa_container_of(node, struct channel_info, channels_ht_node);
1921 cds_lfht_del(state->channels_ht, node);
1922 channel_info_destroy(channel_info);
1923 end:
1924 *cmd_result = LTTNG_OK;
1925 return 0;
1926 }
1927
1928 static int
1929 handle_notification_thread_command_session_rotation(struct notification_thread_state *state,
1930 enum notification_thread_command_type cmd_type,
1931 uint64_t session_id,
1932 uint64_t trace_archive_chunk_id,
1933 struct lttng_trace_archive_location *location,
1934 enum lttng_error_code *_cmd_result)
1935 {
1936 int ret = 0;
1937 enum lttng_error_code cmd_result = LTTNG_OK;
1938 struct lttng_session_trigger_list *trigger_list;
1939 struct lttng_trigger_list_element *trigger_list_element;
1940 struct session_info *session_info;
1941 struct lttng_credentials session_creds;
1942 struct session_state_sample new_session_state;
1943
1944 lttng::urcu::read_lock_guard read_lock;
1945
1946 session_info = get_session_info_by_id(state, session_id);
1947 if (!session_info) {
1948 /* Fatal logic error. */
1949 ERR("Failed to find session while handling rotation state change: session id = %" PRIu64,
1950 session_id);
1951 ret = -1;
1952 cmd_result = LTTNG_ERR_FATAL;
1953 goto end;
1954 }
1955
1956 new_session_state = session_info->last_state_sample;
1957 if (location) {
1958 lttng_trace_archive_location_get(location);
1959 new_session_state.rotation.location = location;
1960 } else {
1961 new_session_state.rotation.location = nullptr;
1962 }
1963
1964 session_creds = {
1965 .uid = LTTNG_OPTIONAL_INIT_VALUE(session_info->uid),
1966 .gid = LTTNG_OPTIONAL_INIT_VALUE(session_info->gid),
1967 };
1968
1969 new_session_state.rotation.ongoing = cmd_type ==
1970 NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
1971 new_session_state.rotation.id = trace_archive_chunk_id;
1972
1973 trigger_list = get_session_trigger_list(state, session_info->name);
1974 LTTNG_ASSERT(trigger_list);
1975
1976 cds_list_for_each_entry (trigger_list_element, &trigger_list->list, node) {
1977 const struct lttng_condition *condition;
1978 struct lttng_trigger *trigger;
1979 struct notification_client_list *client_list;
1980 struct lttng_evaluation *evaluation = nullptr;
1981 enum action_executor_status executor_status;
1982
1983 trigger = trigger_list_element->trigger;
1984 condition = lttng_trigger_get_const_condition(trigger);
1985 LTTNG_ASSERT(condition);
1986
1987 ret = evaluate_session_condition(
1988 condition, session_info, &new_session_state, &evaluation);
1989 if (ret) {
1990 ret = -1;
1991 cmd_result = LTTNG_ERR_NOMEM;
1992 goto end;
1993 }
1994
1995 if (!evaluation) {
1996 continue;
1997 }
1998
1999 /*
2000 * Ownership of `evaluation` transferred to the action executor
2001 * no matter the result. The callee acquires a reference to the
2002 * client list: we can release our own.
2003 */
2004 client_list = get_client_list_from_condition(state, condition);
2005 executor_status = action_executor_enqueue_trigger(
2006 state->executor, trigger, evaluation, &session_creds, client_list);
2007 notification_client_list_put(client_list);
2008 evaluation = nullptr;
2009 switch (executor_status) {
2010 case ACTION_EXECUTOR_STATUS_OK:
2011 break;
2012 case ACTION_EXECUTOR_STATUS_ERROR:
2013 case ACTION_EXECUTOR_STATUS_INVALID:
2014 /*
2015 * TODO Add trigger identification (name/id) when
2016 * it is added to the API.
2017 */
2018 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
2019 ret = -1;
2020 goto end;
2021 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2022 /*
2023 * TODO Add trigger identification (name/id) when
2024 * it is added to the API.
2025 *
2026 * Not a fatal error.
2027 */
2028 WARN("No space left when enqueuing action associated with session rotation trigger");
2029 ret = 0;
2030 goto end;
2031 default:
2032 abort();
2033 }
2034 }
2035
2036 end:
2037 if (session_info) {
2038 /* Ownership of new_session_state::location is transferred. */
2039 lttng_trace_archive_location_put(session_info->last_state_sample.rotation.location);
2040 session_info->last_state_sample = new_session_state;
2041 }
2042
2043 session_info_put(session_info);
2044 *_cmd_result = cmd_result;
2045 return ret;
2046 }
2047
2048 static int
2049 handle_notification_thread_command_add_tracer_event_source(struct notification_thread_state *state,
2050 int tracer_event_source_fd,
2051 enum lttng_domain_type domain_type,
2052 enum lttng_error_code *_cmd_result)
2053 {
2054 int ret = 0;
2055 enum lttng_error_code cmd_result = LTTNG_OK;
2056 struct notification_event_tracer_event_source_element *element = nullptr;
2057
2058 element = zmalloc<notification_event_tracer_event_source_element>();
2059 if (!element) {
2060 cmd_result = LTTNG_ERR_NOMEM;
2061 ret = -1;
2062 goto end;
2063 }
2064
2065 element->fd = tracer_event_source_fd;
2066 element->domain = domain_type;
2067
2068 cds_list_add(&element->node, &state->tracer_event_sources_list);
2069
2070 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2071 tracer_event_source_fd,
2072 lttng_domain_type_str(domain_type));
2073
2074 /* Adding the read side pipe to the event poll. */
2075 ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLPRI | LPOLLIN);
2076 if (ret < 0) {
2077 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2078 tracer_event_source_fd,
2079 lttng_domain_type_str(element->domain));
2080 cds_list_del(&element->node);
2081 free(element);
2082 goto end;
2083 }
2084
2085 element->is_fd_in_poll_set = true;
2086
2087 end:
2088 *_cmd_result = cmd_result;
2089 return ret;
2090 }
2091
2092 static int drain_event_notifier_notification_pipe(struct notification_thread_state *state,
2093 int pipe,
2094 enum lttng_domain_type domain)
2095 {
2096 struct lttng_poll_event events = {};
2097 int ret;
2098
2099 ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
2100 if (ret < 0) {
2101 ERR("Error creating lttng_poll_event");
2102 goto end;
2103 }
2104
2105 ret = lttng_poll_add(&events, pipe, LPOLLIN);
2106 if (ret < 0) {
2107 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2108 pipe);
2109 goto end;
2110 }
2111
2112 while (true) {
2113 /*
2114 * Continue to consume notifications as long as there are new
2115 * ones coming in. The tracer has been asked to stop producing
2116 * them.
2117 *
2118 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2119 * monitored (on Linux, at least) and will be returned when
2120 * the pipe is closed but empty.
2121 */
2122 ret = lttng_poll_wait_interruptible(&events, 0);
2123 if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) {
2124 /* No more notification to be read on this pipe. */
2125 ret = 0;
2126 goto end;
2127 } else if (ret < 0) {
2128 PERROR("Failed on lttng_poll_wait_interruptible() call");
2129 ret = -1;
2130 goto end;
2131 }
2132
2133 ret = handle_one_event_notifier_notification(state, pipe, domain);
2134 if (ret) {
2135 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2136 pipe);
2137 }
2138 }
2139 end:
2140 lttng_poll_clean(&events);
2141 return ret;
2142 }
2143
2144 static struct notification_event_tracer_event_source_element *
2145 find_tracer_event_source_element(struct notification_thread_state *state,
2146 int tracer_event_source_fd)
2147 {
2148 struct notification_event_tracer_event_source_element *source_element;
2149
2150 cds_list_for_each_entry (source_element, &state->tracer_event_sources_list, node) {
2151 if (source_element->fd == tracer_event_source_fd) {
2152 goto end;
2153 }
2154 }
2155
2156 source_element = nullptr;
2157 end:
2158 return source_element;
2159 }
2160
2161 static int remove_tracer_event_source_from_pollset(
2162 struct notification_thread_state *state,
2163 struct notification_event_tracer_event_source_element *source_element)
2164 {
2165 int ret = 0;
2166
2167 LTTNG_ASSERT(source_element->is_fd_in_poll_set);
2168
2169 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2170 source_element->fd,
2171 lttng_domain_type_str(source_element->domain));
2172
2173 /* Removing the fd from the event poll set. */
2174 ret = lttng_poll_del(&state->events, source_element->fd);
2175 if (ret < 0) {
2176 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2177 source_element->fd,
2178 lttng_domain_type_str(source_element->domain));
2179 ret = -1;
2180 goto end;
2181 }
2182
2183 source_element->is_fd_in_poll_set = false;
2184
2185 /*
2186 * Force the notification thread to restart the poll() loop to ensure
2187 * that any events from the removed fd are removed.
2188 */
2189 state->restart_poll = true;
2190
2191 ret = drain_event_notifier_notification_pipe(
2192 state, source_element->fd, source_element->domain);
2193 if (ret) {
2194 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2195 source_element->fd,
2196 lttng_domain_type_str(source_element->domain));
2197 ret = -1;
2198 goto end;
2199 }
2200
2201 end:
2202 return ret;
2203 }
2204
2205 int handle_notification_thread_tracer_event_source_died(struct notification_thread_state *state,
2206 int tracer_event_source_fd)
2207 {
2208 int ret = 0;
2209 struct notification_event_tracer_event_source_element *source_element;
2210
2211 source_element = find_tracer_event_source_element(state, tracer_event_source_fd);
2212
2213 LTTNG_ASSERT(source_element);
2214
2215 ret = remove_tracer_event_source_from_pollset(state, source_element);
2216 if (ret) {
2217 ERR("Failed to remove dead tracer event source from poll set");
2218 }
2219
2220 return ret;
2221 }
2222
2223 static int handle_notification_thread_command_remove_tracer_event_source(
2224 struct notification_thread_state *state,
2225 int tracer_event_source_fd,
2226 enum lttng_error_code *_cmd_result)
2227 {
2228 int ret = 0;
2229 enum lttng_error_code cmd_result = LTTNG_OK;
2230 struct notification_event_tracer_event_source_element *source_element = nullptr;
2231
2232 source_element = find_tracer_event_source_element(state, tracer_event_source_fd);
2233
2234 LTTNG_ASSERT(source_element);
2235
2236 /* Remove the tracer source from the list. */
2237 cds_list_del(&source_element->node);
2238
2239 if (!source_element->is_fd_in_poll_set) {
2240 /* Skip the poll set removal. */
2241 goto end;
2242 }
2243
2244 ret = remove_tracer_event_source_from_pollset(state, source_element);
2245 if (ret) {
2246 ERR("Failed to remove tracer event source from poll set");
2247 cmd_result = LTTNG_ERR_FATAL;
2248 }
2249
2250 end:
2251 free(source_element);
2252 *_cmd_result = cmd_result;
2253 return ret;
2254 }
2255
2256 static int
2257 handle_notification_thread_command_list_triggers(struct notification_thread_handle *handle
2258 __attribute__((unused)),
2259 struct notification_thread_state *state,
2260 uid_t client_uid,
2261 struct lttng_triggers **triggers,
2262 enum lttng_error_code *_cmd_result)
2263 {
2264 int ret = 0;
2265 enum lttng_error_code cmd_result = LTTNG_OK;
2266 struct cds_lfht_iter iter;
2267 struct lttng_trigger_ht_element *trigger_ht_element;
2268 struct lttng_triggers *local_triggers = nullptr;
2269 const struct lttng_credentials *creds;
2270
2271 lttng::urcu::read_lock_guard read_lock;
2272
2273 local_triggers = lttng_triggers_create();
2274 if (!local_triggers) {
2275 /* Not a fatal error. */
2276 cmd_result = LTTNG_ERR_NOMEM;
2277 goto end;
2278 }
2279
2280 cds_lfht_for_each_entry (state->triggers_ht, &iter, trigger_ht_element, node) {
2281 /*
2282 * Only return the triggers to which the client has access.
2283 * The root user has visibility over all triggers.
2284 */
2285 creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
2286 if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
2287 continue;
2288 }
2289
2290 ret = lttng_triggers_add(local_triggers, trigger_ht_element->trigger);
2291 if (ret < 0) {
2292 /* Not a fatal error. */
2293 ret = 0;
2294 cmd_result = LTTNG_ERR_NOMEM;
2295 goto end;
2296 }
2297 }
2298
2299 /* Transferring ownership to the caller. */
2300 *triggers = local_triggers;
2301 local_triggers = nullptr;
2302
2303 end:
2304 lttng_triggers_destroy(local_triggers);
2305 *_cmd_result = cmd_result;
2306 return ret;
2307 }
2308
2309 static inline void get_trigger_info_for_log(const struct lttng_trigger *trigger,
2310 const char **trigger_name,
2311 uid_t *trigger_owner_uid)
2312 {
2313 enum lttng_trigger_status trigger_status;
2314
2315 trigger_status = lttng_trigger_get_name(trigger, trigger_name);
2316 switch (trigger_status) {
2317 case LTTNG_TRIGGER_STATUS_OK:
2318 break;
2319 case LTTNG_TRIGGER_STATUS_UNSET:
2320 *trigger_name = "(anonymous)";
2321 break;
2322 default:
2323 abort();
2324 }
2325
2326 trigger_status = lttng_trigger_get_owner_uid(trigger, trigger_owner_uid);
2327 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
2328 }
2329
2330 static int handle_notification_thread_command_get_trigger(struct notification_thread_state *state,
2331 const struct lttng_trigger *trigger,
2332 struct lttng_trigger **registered_trigger,
2333 enum lttng_error_code *_cmd_result)
2334 {
2335 int ret = -1;
2336 struct cds_lfht_iter iter;
2337 struct lttng_trigger_ht_element *trigger_ht_element;
2338 enum lttng_error_code cmd_result = LTTNG_ERR_TRIGGER_NOT_FOUND;
2339 const char *trigger_name;
2340 uid_t trigger_owner_uid;
2341
2342 {
2343 lttng::urcu::read_lock_guard read_lock;
2344
2345 cds_lfht_for_each_entry (state->triggers_ht, &iter, trigger_ht_element, node) {
2346 if (lttng_trigger_is_equal(trigger, trigger_ht_element->trigger)) {
2347 /* Take one reference on the return trigger. */
2348 *registered_trigger = trigger_ht_element->trigger;
2349 lttng_trigger_get(*registered_trigger);
2350 ret = 0;
2351 cmd_result = LTTNG_OK;
2352 goto end;
2353 }
2354 }
2355 }
2356
2357 /* Not a fatal error if the trigger is not found. */
2358 get_trigger_info_for_log(trigger, &trigger_name, &trigger_owner_uid);
2359 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2360 trigger_name,
2361 (int) trigger_owner_uid);
2362
2363 ret = 0;
2364
2365 end:
2366 *_cmd_result = cmd_result;
2367 return ret;
2368 }
2369
2370 static bool condition_is_supported(struct lttng_condition *condition)
2371 {
2372 bool is_supported;
2373
2374 switch (lttng_condition_get_type(condition)) {
2375 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2376 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2377 {
2378 int ret;
2379 enum lttng_domain_type domain;
2380
2381 ret = lttng_condition_buffer_usage_get_domain_type(condition, &domain);
2382 LTTNG_ASSERT(ret == 0);
2383
2384 if (domain != LTTNG_DOMAIN_KERNEL) {
2385 is_supported = true;
2386 goto end;
2387 }
2388
2389 /*
2390 * Older kernel tracers don't expose the API to monitor their
2391 * buffers. Therefore, we reject triggers that require that
2392 * mechanism to be available to be evaluated.
2393 *
2394 * Assume unsupported on error.
2395 */
2396 is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2397 break;
2398 }
2399 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
2400 {
2401 const struct lttng_event_rule *event_rule;
2402 enum lttng_domain_type domain;
2403 const enum lttng_condition_status status =
2404 lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
2405
2406 LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK);
2407
2408 domain = lttng_event_rule_get_domain_type(event_rule);
2409 if (domain != LTTNG_DOMAIN_KERNEL) {
2410 is_supported = true;
2411 goto end;
2412 }
2413
2414 /*
2415 * Older kernel tracers can't emit notification. Therefore, we
2416 * reject triggers that require that mechanism to be available
2417 * to be evaluated.
2418 *
2419 * Assume unsupported on error.
2420 */
2421 is_supported = kernel_supports_event_notifiers() == 1;
2422 break;
2423 }
2424 default:
2425 is_supported = true;
2426 }
2427 end:
2428 return is_supported;
2429 }
2430
2431 /* Must be called with RCU read lock held. */
2432 static int bind_trigger_to_matching_session(struct lttng_trigger *trigger,
2433 struct notification_thread_state *state)
2434 {
2435 int ret = 0;
2436 const struct lttng_condition *condition;
2437 const char *session_name;
2438 struct lttng_session_trigger_list *trigger_list;
2439
2440 ASSERT_RCU_READ_LOCKED();
2441
2442 condition = lttng_trigger_get_const_condition(trigger);
2443 session_name = get_condition_session_name(condition);
2444
2445 trigger_list = get_session_trigger_list(state, session_name);
2446 if (!trigger_list) {
2447 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2448 session_name);
2449 goto end;
2450 }
2451
2452 DBG("Newly registered trigger bound to session \"%s\"", session_name);
2453 ret = lttng_session_trigger_list_add(trigger_list, trigger);
2454 end:
2455 return ret;
2456 }
2457
2458 /* Must be called with RCU read lock held. */
2459 static int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
2460 struct notification_thread_state *state)
2461 {
2462 int ret = 0;
2463 struct cds_lfht_node *node;
2464 struct cds_lfht_iter iter;
2465 struct channel_info *channel;
2466
2467 ASSERT_RCU_READ_LOCKED();
2468
2469 cds_lfht_for_each_entry (state->channels_ht, &iter, channel, channels_ht_node) {
2470 struct lttng_trigger_list_element *trigger_list_element;
2471 struct lttng_channel_trigger_list *trigger_list;
2472 struct cds_lfht_iter lookup_iter;
2473
2474 if (!trigger_applies_to_channel(trigger, channel)) {
2475 continue;
2476 }
2477
2478 cds_lfht_lookup(state->channel_triggers_ht,
2479 hash_channel_key(&channel->key),
2480 match_channel_trigger_list,
2481 &channel->key,
2482 &lookup_iter);
2483 node = cds_lfht_iter_get_node(&lookup_iter);
2484 LTTNG_ASSERT(node);
2485 trigger_list = caa_container_of(
2486 node, struct lttng_channel_trigger_list, channel_triggers_ht_node);
2487
2488 trigger_list_element = zmalloc<lttng_trigger_list_element>();
2489 if (!trigger_list_element) {
2490 ret = -1;
2491 goto end;
2492 }
2493 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
2494 trigger_list_element->trigger = trigger;
2495 cds_list_add(&trigger_list_element->node, &trigger_list->list);
2496 DBG("Newly registered trigger bound to channel \"%s\"", channel->name);
2497 }
2498 end:
2499 return ret;
2500 }
2501
2502 static bool is_trigger_action_notify(const struct lttng_trigger *trigger)
2503 {
2504 bool is_notify = false;
2505 unsigned int i, count;
2506 enum lttng_action_status action_status;
2507 const struct lttng_action *action = lttng_trigger_get_const_action(trigger);
2508 enum lttng_action_type action_type;
2509
2510 LTTNG_ASSERT(action);
2511 action_type = lttng_action_get_type(action);
2512 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2513 is_notify = true;
2514 goto end;
2515 } else if (action_type != LTTNG_ACTION_TYPE_LIST) {
2516 goto end;
2517 }
2518
2519 action_status = lttng_action_list_get_count(action, &count);
2520 LTTNG_ASSERT(action_status == LTTNG_ACTION_STATUS_OK);
2521
2522 for (i = 0; i < count; i++) {
2523 const struct lttng_action *inner_action = lttng_action_list_get_at_index(action, i);
2524
2525 action_type = lttng_action_get_type(inner_action);
2526 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2527 is_notify = true;
2528 goto end;
2529 }
2530 }
2531
2532 end:
2533 return is_notify;
2534 }
2535
2536 static bool trigger_name_taken(struct notification_thread_state *state,
2537 const struct lttng_trigger *trigger)
2538 {
2539 struct cds_lfht_iter iter;
2540
2541 /*
2542 * No duplicata is allowed in the triggers_by_name_uid_ht.
2543 * The match is done against the trigger name and uid.
2544 */
2545 cds_lfht_lookup(state->triggers_by_name_uid_ht,
2546 hash_trigger_by_name_uid(trigger),
2547 match_trigger_by_name_uid,
2548 trigger,
2549 &iter);
2550 return !!cds_lfht_iter_get_node(&iter);
2551 }
2552
2553 static enum lttng_error_code generate_trigger_name(struct notification_thread_state *state,
2554 struct lttng_trigger *trigger,
2555 const char **name)
2556 {
2557 enum lttng_error_code ret_code = LTTNG_OK;
2558 bool taken = false;
2559 enum lttng_trigger_status status;
2560
2561 do {
2562 const int ret =
2563 lttng_trigger_generate_name(trigger, state->trigger_id.name_offset++);
2564 if (ret) {
2565 /* The only reason this can fail right now. */
2566 ret_code = LTTNG_ERR_NOMEM;
2567 break;
2568 }
2569
2570 status = lttng_trigger_get_name(trigger, name);
2571 LTTNG_ASSERT(status == LTTNG_TRIGGER_STATUS_OK);
2572
2573 taken = trigger_name_taken(state, trigger);
2574 } while (taken || state->trigger_id.name_offset == UINT64_MAX);
2575
2576 return ret_code;
2577 }
2578
2579 static inline void
2580 notif_thread_state_remove_trigger_ht_elem(struct notification_thread_state *state,
2581 struct lttng_trigger_ht_element *trigger_ht_element)
2582 {
2583 LTTNG_ASSERT(state);
2584 LTTNG_ASSERT(trigger_ht_element);
2585
2586 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2587 cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
2588 }
2589
2590 static enum lttng_error_code setup_tracer_notifier(struct notification_thread_state *state,
2591 struct lttng_trigger *trigger)
2592 {
2593 enum lttng_error_code ret;
2594 enum event_notifier_error_accounting_status error_accounting_status;
2595 struct cds_lfht_node *node;
2596 uint64_t error_counter_index = 0;
2597 struct lttng_condition *condition = lttng_trigger_get_condition(trigger);
2598 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = nullptr;
2599
2600 trigger_tokens_ht_element = zmalloc<notification_trigger_tokens_ht_element>();
2601 if (!trigger_tokens_ht_element) {
2602 ret = LTTNG_ERR_NOMEM;
2603 goto end;
2604 }
2605
2606 /* Add trigger token to the trigger_tokens_ht. */
2607 cds_lfht_node_init(&trigger_tokens_ht_element->node);
2608 trigger_tokens_ht_element->token = LTTNG_OPTIONAL_GET(trigger->tracer_token);
2609 trigger_tokens_ht_element->trigger = trigger;
2610
2611 node = cds_lfht_add_unique(state->trigger_tokens_ht,
2612 hash_key_u64(&trigger_tokens_ht_element->token, lttng_ht_seed),
2613 match_trigger_token,
2614 &trigger_tokens_ht_element->token,
2615 &trigger_tokens_ht_element->node);
2616 if (node != &trigger_tokens_ht_element->node) {
2617 ret = LTTNG_ERR_TRIGGER_EXISTS;
2618 goto error_free_ht_element;
2619 }
2620
2621 error_accounting_status = event_notifier_error_accounting_register_event_notifier(
2622 trigger, &error_counter_index);
2623 if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
2624 if (error_accounting_status ==
2625 EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) {
2626 DBG("Trigger list error accounting counter full.");
2627 ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL;
2628 } else {
2629 ERR("Error registering trigger for error accounting");
2630 ret = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION;
2631 }
2632
2633 goto error_remove_ht_element;
2634 }
2635
2636 lttng_condition_event_rule_matches_set_error_counter_index(condition, error_counter_index);
2637
2638 ret = LTTNG_OK;
2639 goto end;
2640
2641 error_remove_ht_element:
2642 cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node);
2643 error_free_ht_element:
2644 free(trigger_tokens_ht_element);
2645 end:
2646 return ret;
2647 }
2648
2649 /*
2650 * FIXME A client's credentials are not checked when registering a trigger.
2651 *
2652 * The effects of this are benign since:
2653 * - The client will succeed in registering the trigger, as it is valid,
2654 * - The trigger will, internally, be bound to the channel/session,
2655 * - The notifications will not be sent since the client's credentials
2656 * are checked against the channel at that moment.
2657 *
2658 * If this function returns a non-zero value, it means something is
2659 * fundamentally broken and the whole subsystem/thread will be torn down.
2660 *
2661 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2662 * error code.
2663 */
2664 static int
2665 handle_notification_thread_command_register_trigger(struct notification_thread_state *state,
2666 struct lttng_trigger *trigger,
2667 bool is_trigger_anonymous,
2668 enum lttng_error_code *cmd_result)
2669 {
2670 int ret = 0;
2671 struct lttng_condition *condition;
2672 struct notification_client_list *client_list = nullptr;
2673 struct lttng_trigger_ht_element *trigger_ht_element = nullptr;
2674 struct cds_lfht_node *node;
2675 const char *trigger_name;
2676 bool free_trigger = true;
2677 struct lttng_evaluation *evaluation = nullptr;
2678 struct lttng_credentials object_creds;
2679 uid_t object_uid;
2680 gid_t object_gid;
2681 enum action_executor_status executor_status;
2682 const uint64_t trigger_tracer_token = state->trigger_id.next_tracer_token++;
2683
2684 lttng::urcu::read_lock_guard read_lock;
2685
2686 /* Set the trigger's tracer token. */
2687 lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
2688
2689 if (!is_trigger_anonymous) {
2690 if (lttng_trigger_get_name(trigger, &trigger_name) == LTTNG_TRIGGER_STATUS_UNSET) {
2691 const enum lttng_error_code ret_code =
2692 generate_trigger_name(state, trigger, &trigger_name);
2693
2694 if (ret_code != LTTNG_OK) {
2695 /* Fatal error. */
2696 ret = -1;
2697 *cmd_result = ret_code;
2698 goto error;
2699 }
2700 } else if (trigger_name_taken(state, trigger)) {
2701 /* Not a fatal error. */
2702 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2703 ret = 0;
2704 goto error;
2705 }
2706 } else {
2707 trigger_name = "(anonymous)";
2708 }
2709
2710 condition = lttng_trigger_get_condition(trigger);
2711 LTTNG_ASSERT(condition);
2712
2713 /* Some conditions require tracers to implement a minimal ABI version. */
2714 if (!condition_is_supported(condition)) {
2715 *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
2716 goto error;
2717 }
2718
2719 trigger_ht_element = zmalloc<lttng_trigger_ht_element>();
2720 if (!trigger_ht_element) {
2721 ret = -1;
2722 goto error;
2723 }
2724
2725 /* Add trigger to the trigger_ht. */
2726 cds_lfht_node_init(&trigger_ht_element->node);
2727 cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
2728 trigger_ht_element->trigger = trigger;
2729
2730 node = cds_lfht_add_unique(state->triggers_ht,
2731 lttng_condition_hash(condition),
2732 match_trigger,
2733 trigger,
2734 &trigger_ht_element->node);
2735 if (node != &trigger_ht_element->node) {
2736 /* Not a fatal error, simply report it to the client. */
2737 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2738 goto error_free_ht_element;
2739 }
2740
2741 node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
2742 hash_trigger_by_name_uid(trigger),
2743 match_trigger_by_name_uid,
2744 trigger,
2745 &trigger_ht_element->node_by_name_uid);
2746 if (node != &trigger_ht_element->node_by_name_uid) {
2747 /* Internal error: add to triggers_ht should have failed. */
2748 ret = -1;
2749 goto error_free_ht_element;
2750 }
2751
2752 /* From this point consider the trigger registered. */
2753 lttng_trigger_set_as_registered(trigger);
2754
2755 /*
2756 * Some triggers might need a tracer notifier depending on its
2757 * condition and actions.
2758 */
2759 if (lttng_trigger_needs_tracer_notifier(trigger)) {
2760 enum lttng_error_code error_code;
2761
2762 error_code = setup_tracer_notifier(state, trigger);
2763 if (error_code != LTTNG_OK) {
2764 notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element);
2765 if (error_code == LTTNG_ERR_NOMEM) {
2766 ret = -1;
2767 } else {
2768 *cmd_result = error_code;
2769 ret = 0;
2770 }
2771
2772 goto error_free_ht_element;
2773 }
2774 }
2775
2776 /*
2777 * The rest only applies to triggers that have a "notify" action.
2778 * It is not skipped as this is the only action type currently
2779 * supported.
2780 */
2781 if (is_trigger_action_notify(trigger)) {
2782 /*
2783 * Find or create the client list of this condition. It may
2784 * already be present if another trigger is already registered
2785 * with the same condition.
2786 */
2787 client_list = get_client_list_from_condition(state, condition);
2788 if (!client_list) {
2789 /*
2790 * No client list for this condition yet. We create new
2791 * one and build it up.
2792 */
2793 client_list = notification_client_list_create(state, condition);
2794 if (!client_list) {
2795 ERR("Error creating notification client list for trigger %s",
2796 trigger->name);
2797 ret = -1;
2798 goto error_free_ht_element;
2799 }
2800 }
2801
2802 CDS_INIT_LIST_HEAD(&trigger_ht_element->client_list_trigger_node);
2803
2804 pthread_mutex_lock(&client_list->lock);
2805 cds_list_add(&trigger_ht_element->client_list_trigger_node,
2806 &client_list->triggers_list);
2807 pthread_mutex_unlock(&client_list->lock);
2808 }
2809
2810 /*
2811 * Ownership of the trigger and of its wrapper was transfered to
2812 * the triggers_ht. Same for token ht element if necessary.
2813 */
2814 trigger_ht_element = nullptr;
2815 free_trigger = false;
2816
2817 switch (get_condition_binding_object(condition)) {
2818 case LTTNG_OBJECT_TYPE_SESSION:
2819 /* Add the trigger to the list if it matches a known session. */
2820 ret = bind_trigger_to_matching_session(trigger, state);
2821 if (ret) {
2822 goto error_free_ht_element;
2823 }
2824 break;
2825 case LTTNG_OBJECT_TYPE_CHANNEL:
2826 /*
2827 * Add the trigger to list of triggers bound to the channels
2828 * currently known.
2829 */
2830 ret = bind_trigger_to_matching_channels(trigger, state);
2831 if (ret) {
2832 goto error_free_ht_element;
2833 }
2834 break;
2835 case LTTNG_OBJECT_TYPE_NONE:
2836 break;
2837 default:
2838 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2839 ret = -1;
2840 goto error_free_ht_element;
2841 }
2842
2843 /*
2844 * The new trigger's condition must be evaluated against the current
2845 * state.
2846 *
2847 * In the case of `notify` action, nothing preventing clients from
2848 * subscribing to a condition before the corresponding trigger is
2849 * registered, we have to evaluate this new condition right away.
2850 *
2851 * At some point, we were waiting for the next "evaluation" (e.g. on
2852 * reception of a channel sample) to evaluate this new condition, but
2853 * that was broken.
2854 *
2855 * The reason it was broken is that waiting for the next sample
2856 * does not allow us to properly handle transitions for edge-triggered
2857 * conditions.
2858 *
2859 * Consider this example: when we handle a new channel sample, we
2860 * evaluate each conditions twice: once with the previous state, and
2861 * again with the newest state. We then use those two results to
2862 * determine whether a state change happened: a condition was false and
2863 * became true. If a state change happened, we have to notify clients.
2864 *
2865 * Now, if a client subscribes to a given notification and registers
2866 * a trigger *after* that subscription, we have to make sure the
2867 * condition is evaluated at this point while considering only the
2868 * current state. Otherwise, the next evaluation cycle may only see
2869 * that the evaluations remain the same (true for samples n-1 and n) and
2870 * the client will never know that the condition has been met.
2871 */
2872 switch (get_condition_binding_object(condition)) {
2873 case LTTNG_OBJECT_TYPE_SESSION:
2874 {
2875 /* Find the session associated with the condition. */
2876 const auto *session_name = get_condition_session_name(condition);
2877 auto session_info = get_session_info_by_name(state, session_name);
2878 if (!session_info) {
2879 /* Not an error, the session doesn't exist yet. */
2880 DBG("Session not found while evaluating session condition during registration of trigger: session name = `%s`",
2881 session_name);
2882 ret = 0;
2883 goto success;
2884 }
2885
2886 LTTNG_OPTIONAL_SET(&object_creds.uid, session_info->uid);
2887 LTTNG_OPTIONAL_SET(&object_creds.gid, session_info->gid);
2888
2889 ret = evaluate_session_condition(condition, session_info, nullptr, &evaluation);
2890 session_info_put(session_info);
2891 break;
2892 }
2893 case LTTNG_OBJECT_TYPE_CHANNEL:
2894 ret = evaluate_channel_condition_for_client(
2895 condition, state, &evaluation, &object_uid, &object_gid);
2896 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2897 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
2898 break;
2899 case LTTNG_OBJECT_TYPE_NONE:
2900 ret = 0;
2901 break;
2902 case LTTNG_OBJECT_TYPE_UNKNOWN:
2903 default:
2904 ret = -1;
2905 break;
2906 }
2907
2908 if (ret) {
2909 /* Fatal error. */
2910 goto error_free_ht_element;
2911 }
2912
2913 DBG("Newly registered trigger's condition evaluated to %s", evaluation ? "true" : "false");
2914 if (!evaluation) {
2915 /* Evaluation yielded nothing. Normal exit. */
2916 ret = 0;
2917 goto success;
2918 }
2919
2920 /*
2921 * Ownership of `evaluation` transferred to the action executor
2922 * no matter the result.
2923 */
2924 executor_status = action_executor_enqueue_trigger(
2925 state->executor, trigger, evaluation, &object_creds, client_list);
2926 evaluation = nullptr;
2927 switch (executor_status) {
2928 case ACTION_EXECUTOR_STATUS_OK:
2929 break;
2930 case ACTION_EXECUTOR_STATUS_ERROR:
2931 case ACTION_EXECUTOR_STATUS_INVALID:
2932 /*
2933 * TODO Add trigger identification (name/id) when
2934 * it is added to the API.
2935 */
2936 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2937 ret = -1;
2938 goto error_free_ht_element;
2939 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2940 /*
2941 * TODO Add trigger identification (name/id) when
2942 * it is added to the API.
2943 *
2944 * Not a fatal error.
2945 */
2946 WARN("No space left when enqueuing action associated to newly registered trigger");
2947 ret = 0;
2948 goto success;
2949 default:
2950 abort();
2951 }
2952
2953 success:
2954 *cmd_result = LTTNG_OK;
2955 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64,
2956 trigger_name,
2957 trigger_tracer_token);
2958 goto end;
2959
2960 error_free_ht_element:
2961 if (trigger_ht_element) {
2962 /* Delayed removal due to RCU constraint on delete. */
2963 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
2964 }
2965 error:
2966 if (free_trigger) {
2967 /*
2968 * Other objects might have a reference to the trigger, mark it
2969 * as unregistered.
2970 */
2971 lttng_trigger_set_as_unregistered(trigger);
2972 lttng_trigger_destroy(trigger);
2973 }
2974 end:
2975 return ret;
2976 }
2977
2978 static void free_lttng_trigger_ht_element_rcu(struct rcu_head *node)
2979 {
2980 free(caa_container_of(node, struct lttng_trigger_ht_element, rcu_node));
2981 }
2982
2983 static void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node)
2984 {
2985 free(caa_container_of(node, struct notification_trigger_tokens_ht_element, rcu_node));
2986 }
2987
2988 static void teardown_tracer_notifier(struct notification_thread_state *state,
2989 const struct lttng_trigger *trigger)
2990 {
2991 struct cds_lfht_iter iter;
2992 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element;
2993
2994 {
2995 lttng::urcu::read_lock_guard read_lock;
2996
2997 cds_lfht_for_each_entry (
2998 state->trigger_tokens_ht, &iter, trigger_tokens_ht_element, node) {
2999 if (!lttng_trigger_is_equal(trigger, trigger_tokens_ht_element->trigger)) {
3000 continue;
3001 }
3002
3003 event_notifier_error_accounting_unregister_event_notifier(
3004 trigger_tokens_ht_element->trigger);
3005
3006 /* TODO talk to all app and remove it */
3007 DBG("Removed trigger from tokens_ht");
3008 cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node);
3009
3010 call_rcu(&trigger_tokens_ht_element->rcu_node,
3011 free_notification_trigger_tokens_ht_element_rcu);
3012
3013 break;
3014 }
3015 }
3016 }
3017
3018 static void
3019 remove_trigger_from_session_trigger_list(struct lttng_session_trigger_list *trigger_list,
3020 const struct lttng_trigger *trigger)
3021 {
3022 bool found = false;
3023 struct lttng_trigger_list_element *trigger_element, *tmp;
3024
3025 cds_list_for_each_entry_safe (trigger_element, tmp, &trigger_list->list, node) {
3026 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
3027 continue;
3028 }
3029
3030 DBG("Removed trigger from session_triggers_ht");
3031 cds_list_del(&trigger_element->node);
3032 free(trigger_element);
3033 /* A trigger can only appear once per session. */
3034 found = true;
3035 break;
3036 }
3037
3038 if (!found) {
3039 ERR("Failed to find trigger associated with session: session name = `%s`",
3040 trigger_list->session_name);
3041 }
3042
3043 LTTNG_ASSERT(found);
3044 }
3045
3046 static int
3047 handle_notification_thread_command_unregister_trigger(struct notification_thread_state *state,
3048 const struct lttng_trigger *trigger,
3049 enum lttng_error_code *_cmd_reply)
3050 {
3051 struct cds_lfht_iter iter;
3052 struct cds_lfht_node *triggers_ht_node;
3053 struct notification_client_list *client_list;
3054 struct lttng_trigger_ht_element *trigger_ht_element = nullptr;
3055 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
3056 enum lttng_error_code cmd_reply;
3057
3058 lttng::urcu::read_lock_guard read_lock;
3059
3060 cds_lfht_lookup(
3061 state->triggers_ht, lttng_condition_hash(condition), match_trigger, trigger, &iter);
3062 triggers_ht_node = cds_lfht_iter_get_node(&iter);
3063 if (!triggers_ht_node) {
3064 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
3065 goto end;
3066 } else {
3067 cmd_reply = LTTNG_OK;
3068 }
3069
3070 trigger_ht_element =
3071 caa_container_of(triggers_ht_node, struct lttng_trigger_ht_element, node);
3072
3073 switch (get_condition_binding_object(condition)) {
3074 case LTTNG_OBJECT_TYPE_CHANNEL:
3075 {
3076 struct lttng_channel_trigger_list *trigger_list;
3077
3078 /*
3079 * Remove trigger from channel_triggers_ht.
3080 *
3081 * Note that multiple channels may have matched the trigger's
3082 * condition (e.g. all instances of a given channel in per-pid buffering
3083 * mode).
3084 *
3085 * Iterate on all lists since we don't know the target channels' keys.
3086 */
3087 cds_lfht_for_each_entry (
3088 state->channel_triggers_ht, &iter, trigger_list, channel_triggers_ht_node) {
3089 struct lttng_trigger_list_element *trigger_element, *tmp;
3090
3091 cds_list_for_each_entry_safe (
3092 trigger_element, tmp, &trigger_list->list, node) {
3093 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
3094 continue;
3095 }
3096
3097 DBG("Removed trigger from channel_triggers_ht");
3098 cds_list_del(&trigger_element->node);
3099 free(trigger_element);
3100 /* A trigger can only appear once per channel */
3101 break;
3102 }
3103 }
3104 break;
3105 }
3106 case LTTNG_OBJECT_TYPE_SESSION:
3107 {
3108 auto session =
3109 get_session_info_by_name(state, get_condition_session_name(condition));
3110
3111 /* Session doesn't exist, no trigger to remove. */
3112 if (!session) {
3113 break;
3114 }
3115
3116 auto session_trigger_list = get_session_trigger_list(state, session->name);
3117 remove_trigger_from_session_trigger_list(session_trigger_list, trigger);
3118 session_info_put(session);
3119 }
3120 case LTTNG_OBJECT_TYPE_NONE:
3121 break;
3122 default:
3123 abort();
3124 }
3125
3126 if (lttng_trigger_needs_tracer_notifier(trigger)) {
3127 teardown_tracer_notifier(state, trigger);
3128 }
3129
3130 if (is_trigger_action_notify(trigger)) {
3131 /*
3132 * Remove and release the client list from
3133 * notification_trigger_clients_ht.
3134 */
3135 client_list = get_client_list_from_condition(state, condition);
3136 LTTNG_ASSERT(client_list);
3137
3138 pthread_mutex_lock(&client_list->lock);
3139 cds_list_del(&trigger_ht_element->client_list_trigger_node);
3140 pthread_mutex_unlock(&client_list->lock);
3141
3142 /* Put new reference and the hashtable's reference. */
3143 notification_client_list_put(client_list);
3144 notification_client_list_put(client_list);
3145 client_list = nullptr;
3146 }
3147
3148 /* Remove trigger from triggers_ht. */
3149 notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element);
3150
3151 /* Release the ownership of the trigger. */
3152 lttng_trigger_destroy(trigger_ht_element->trigger);
3153 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
3154 end:
3155 if (_cmd_reply) {
3156 *_cmd_reply = cmd_reply;
3157 }
3158 return 0;
3159 }
3160
3161 static int pop_cmd_queue(struct notification_thread_handle *handle,
3162 struct notification_thread_command **cmd)
3163 {
3164 int ret;
3165 uint64_t counter;
3166
3167 pthread_mutex_lock(&handle->cmd_queue.lock);
3168 ret = lttng_read(handle->cmd_queue.event_fd, &counter, sizeof(counter));
3169 if (ret != sizeof(counter)) {
3170 ret = -1;
3171 goto error_unlock;
3172 }
3173
3174 *cmd = cds_list_first_entry(
3175 &handle->cmd_queue.list, struct notification_thread_command, cmd_list_node);
3176 cds_list_del(&((*cmd)->cmd_list_node));
3177 ret = 0;
3178
3179 error_unlock:
3180 pthread_mutex_unlock(&handle->cmd_queue.lock);
3181 return ret;
3182 }
3183
3184 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3185 int handle_notification_thread_command(struct notification_thread_handle *handle,
3186 struct notification_thread_state *state)
3187 {
3188 int ret;
3189 struct notification_thread_command *cmd;
3190
3191 ret = pop_cmd_queue(handle, &cmd);
3192 if (ret) {
3193 goto error;
3194 }
3195
3196 DBG("Received `%s` command", notification_command_type_str(cmd->type));
3197 switch (cmd->type) {
3198 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
3199 ret = handle_notification_thread_command_register_trigger(
3200 state,
3201 cmd->parameters.register_trigger.trigger,
3202 cmd->parameters.register_trigger.is_trigger_anonymous,
3203 &cmd->reply_code);
3204 break;
3205 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
3206 ret = handle_notification_thread_command_unregister_trigger(
3207 state, cmd->parameters.unregister_trigger.trigger, &cmd->reply_code);
3208 break;
3209 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
3210 ret = handle_notification_thread_command_add_channel(
3211 state,
3212 cmd->parameters.add_channel.session.id,
3213 cmd->parameters.add_channel.channel.name,
3214 cmd->parameters.add_channel.channel.domain,
3215 cmd->parameters.add_channel.channel.key,
3216 cmd->parameters.add_channel.channel.capacity,
3217 &cmd->reply_code);
3218 break;
3219 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
3220 ret = handle_notification_thread_command_remove_channel(
3221 state,
3222 cmd->parameters.remove_channel.key,
3223 cmd->parameters.remove_channel.domain,
3224 &cmd->reply_code);
3225 break;
3226 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION:
3227 ret = handle_notification_thread_command_add_session(
3228 state,
3229 cmd->parameters.add_session.session_id,
3230 cmd->parameters.add_session.session_name,
3231 cmd->parameters.add_session.session_uid,
3232 cmd->parameters.add_session.session_gid,
3233 &cmd->reply_code);
3234 break;
3235 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION:
3236 ret = handle_notification_thread_command_remove_session(
3237 state, cmd->parameters.remove_session.session_id, &cmd->reply_code);
3238 break;
3239 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
3240 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
3241 ret = handle_notification_thread_command_session_rotation(
3242 state,
3243 cmd->type,
3244 cmd->parameters.session_rotation.session_id,
3245 cmd->parameters.session_rotation.trace_archive_chunk_id,
3246 cmd->parameters.session_rotation.location,
3247 &cmd->reply_code);
3248 break;
3249 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
3250 ret = handle_notification_thread_command_add_tracer_event_source(
3251 state,
3252 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3253 cmd->parameters.tracer_event_source.domain,
3254 &cmd->reply_code);
3255 break;
3256 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
3257 ret = handle_notification_thread_command_remove_tracer_event_source(
3258 state,
3259 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3260 &cmd->reply_code);
3261 break;
3262 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
3263 {
3264 struct lttng_triggers *triggers = nullptr;
3265
3266 ret = handle_notification_thread_command_list_triggers(
3267 handle,
3268 state,
3269 cmd->parameters.list_triggers.uid,
3270 &triggers,
3271 &cmd->reply_code);
3272 cmd->reply.list_triggers.triggers = triggers;
3273 ret = 0;
3274 break;
3275 }
3276 case NOTIFICATION_COMMAND_TYPE_QUIT:
3277 cmd->reply_code = LTTNG_OK;
3278 ret = 1;
3279 goto end;
3280 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER:
3281 {
3282 struct lttng_trigger *trigger = nullptr;
3283
3284 ret = handle_notification_thread_command_get_trigger(
3285 state, cmd->parameters.get_trigger.trigger, &trigger, &cmd->reply_code);
3286 cmd->reply.get_trigger.trigger = trigger;
3287 break;
3288 }
3289 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
3290 {
3291 const enum client_transmission_status client_status =
3292 cmd->parameters.client_communication_update.status;
3293 const notification_client_id client_id =
3294 cmd->parameters.client_communication_update.id;
3295 struct notification_client *client;
3296
3297 lttng::urcu::read_lock_guard read_lock;
3298 client = get_client_from_id(client_id, state);
3299
3300 if (!client) {
3301 /*
3302 * Client error was probably already picked-up by the
3303 * notification thread or it has disconnected
3304 * gracefully while this command was queued.
3305 */
3306 DBG("Failed to find notification client to update communication status, client id = %" PRIu64,
3307 client_id);
3308 ret = 0;
3309 } else {
3310 ret = client_handle_transmission_status(client, client_status, state);
3311 }
3312 break;
3313 }
3314 default:
3315 ERR("Unknown internal command received");
3316 goto error_unlock;
3317 }
3318
3319 if (ret) {
3320 goto error_unlock;
3321 }
3322 end:
3323 if (cmd->is_async) {
3324 free(cmd);
3325 cmd = nullptr;
3326 } else {
3327 lttng_waiter_wake_up(&cmd->reply_waiter);
3328 }
3329 return ret;
3330 error_unlock:
3331 /* Wake-up and return a fatal error to the calling thread. */
3332 lttng_waiter_wake_up(&cmd->reply_waiter);
3333 cmd->reply_code = LTTNG_ERR_FATAL;
3334 error:
3335 /* Indicate a fatal error to the caller. */
3336 return -1;
3337 }
3338
3339 static int socket_set_non_blocking(int socket)
3340 {
3341 int ret, flags;
3342
3343 /* Set the pipe as non-blocking. */
3344 ret = fcntl(socket, F_GETFL, 0);
3345 if (ret == -1) {
3346 PERROR("fcntl get socket flags");
3347 goto end;
3348 }
3349 flags = ret;
3350
3351 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
3352 if (ret == -1) {
3353 PERROR("fcntl set O_NONBLOCK socket flag");
3354 goto end;
3355 }
3356 DBG("Client socket (fd = %i) set as non-blocking", socket);
3357 end:
3358 return ret;
3359 }
3360
3361 static int client_reset_inbound_state(struct notification_client *client)
3362 {
3363 int ret;
3364
3365 lttng_payload_clear(&client->communication.inbound.payload);
3366
3367 client->communication.inbound.bytes_to_receive =
3368 sizeof(struct lttng_notification_channel_message);
3369 client->communication.inbound.msg_type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
3370 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
3371 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
3372 ret = lttng_dynamic_buffer_set_size(&client->communication.inbound.payload.buffer,
3373 client->communication.inbound.bytes_to_receive);
3374
3375 return ret;
3376 }
3377
3378 int handle_notification_thread_client_connect(struct notification_thread_state *state)
3379 {
3380 int ret;
3381 struct notification_client *client;
3382
3383 DBG("Handling new notification channel client connection");
3384
3385 client = zmalloc<notification_client>();
3386 if (!client) {
3387 /* Fatal error. */
3388 ret = -1;
3389 goto error;
3390 }
3391
3392 pthread_mutex_init(&client->lock, nullptr);
3393 client->id = state->next_notification_client_id++;
3394 CDS_INIT_LIST_HEAD(&client->condition_list);
3395 lttng_payload_init(&client->communication.inbound.payload);
3396 lttng_payload_init(&client->communication.outbound.payload);
3397 client->communication.inbound.expect_creds = true;
3398
3399 ret = client_reset_inbound_state(client);
3400 if (ret) {
3401 ERR("Failed to reset client communication's inbound state");
3402 ret = 0;
3403 goto error;
3404 }
3405
3406 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
3407 if (ret < 0) {
3408 ERR("Failed to accept new notification channel client connection");
3409 ret = 0;
3410 goto error;
3411 }
3412
3413 client->socket = ret;
3414
3415 ret = socket_set_non_blocking(client->socket);
3416 if (ret) {
3417 ERR("Failed to set new notification channel client connection socket as non-blocking");
3418 goto error;
3419 }
3420
3421 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
3422 if (ret < 0) {
3423 ERR("Failed to set socket options on new notification channel client socket");
3424 ret = 0;
3425 goto error;
3426 }
3427
3428 client->communication.current_poll_events = CLIENT_POLL_EVENTS_IN;
3429 ret = lttng_poll_add(
3430 &state->events, client->socket, client->communication.current_poll_events);
3431 if (ret < 0) {
3432 ERR("Failed to add notification channel client socket to poll set");
3433 ret = 0;
3434 goto error;
3435 }
3436 DBG("Added new notification channel client socket (%i) to poll set", client->socket);
3437
3438 {
3439 lttng::urcu::read_lock_guard read_lock;
3440
3441 cds_lfht_add(state->client_socket_ht,
3442 hash_client_socket(client->socket),
3443 &client->client_socket_ht_node);
3444 cds_lfht_add(state->client_id_ht,
3445 hash_client_id(client->id),
3446 &client->client_id_ht_node);
3447 }
3448
3449 return ret;
3450
3451 error:
3452 notification_client_destroy(client);
3453 return ret;
3454 }
3455
3456 /*
3457 * RCU read-lock must be held by the caller.
3458 * Client lock must _not_ be held by the caller.
3459 */
3460 static int notification_thread_client_disconnect(struct notification_client *client,
3461 struct notification_thread_state *state)
3462 {
3463 int ret;
3464 struct lttng_condition_list_element *condition_list_element, *tmp;
3465
3466 ASSERT_RCU_READ_LOCKED();
3467
3468 /* Acquire the client lock to disable its communication atomically. */
3469 pthread_mutex_lock(&client->lock);
3470 client->communication.active = false;
3471 cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node);
3472 cds_lfht_del(state->client_id_ht, &client->client_id_ht_node);
3473 pthread_mutex_unlock(&client->lock);
3474
3475 ret = lttng_poll_del(&state->events, client->socket);
3476 if (ret) {
3477 ERR("Failed to remove client socket %d from poll set", client->socket);
3478 }
3479
3480 /* Release all conditions to which the client was subscribed. */
3481 cds_list_for_each_entry_safe (condition_list_element, tmp, &client->condition_list, node) {
3482 (void) notification_thread_client_unsubscribe(
3483 client, condition_list_element->condition, state, nullptr);
3484 }
3485
3486 /*
3487 * Client no longer accessible to other threads (through the
3488 * client lists).
3489 */
3490 notification_client_destroy(client);
3491 return ret;
3492 }
3493
3494 int handle_notification_thread_client_disconnect(int client_socket,
3495 struct notification_thread_state *state)
3496 {
3497 int ret = 0;
3498 struct notification_client *client;
3499
3500 lttng::urcu::read_lock_guard read_lock;
3501
3502 DBG("Closing client connection (socket fd = %i)", client_socket);
3503 client = get_client_from_socket(client_socket, state);
3504 if (!client) {
3505 /* Internal state corruption, fatal error. */
3506 ERR("Unable to find client (socket fd = %i)", client_socket);
3507 ret = -1;
3508 goto end;
3509 }
3510
3511 ret = notification_thread_client_disconnect(client, state);
3512 end:
3513 return ret;
3514 }
3515
3516 int handle_notification_thread_client_disconnect_all(struct notification_thread_state *state)
3517 {
3518 struct cds_lfht_iter iter;
3519 struct notification_client *client;
3520 bool error_encoutered = false;
3521
3522 DBG("Closing all client connections");
3523
3524 {
3525 lttng::urcu::read_lock_guard read_lock;
3526
3527 cds_lfht_for_each_entry (
3528 state->client_socket_ht, &iter, client, client_socket_ht_node) {
3529 int ret;
3530
3531 ret = notification_thread_client_disconnect(client, state);
3532 if (ret) {
3533 error_encoutered = true;
3534 }
3535 }
3536 }
3537
3538 return error_encoutered ? 1 : 0;
3539 }
3540
3541 int handle_notification_thread_trigger_unregister_all(struct notification_thread_state *state)
3542 {
3543 bool error_occurred = false;
3544 struct cds_lfht_iter iter;
3545 struct lttng_trigger_ht_element *trigger_ht_element;
3546
3547 lttng::urcu::read_lock_guard read_lock;
3548 cds_lfht_for_each_entry (state->triggers_ht, &iter, trigger_ht_element, node) {
3549 int ret = handle_notification_thread_command_unregister_trigger(
3550 state, trigger_ht_element->trigger, nullptr);
3551 if (ret) {
3552 error_occurred = true;
3553 }
3554 }
3555 return error_occurred ? -1 : 0;
3556 }
3557
3558 static bool client_has_outbound_data_left(const struct notification_client *client)
3559 {
3560 const struct lttng_payload_view pv =
3561 lttng_payload_view_from_payload(&client->communication.outbound.payload, 0, -1);
3562 const bool has_data = pv.buffer.size != 0;
3563 const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv);
3564
3565 return has_data || has_fds;
3566 }
3567
3568 static int client_handle_transmission_status(struct notification_client *client,
3569 enum client_transmission_status transmission_status,
3570 struct notification_thread_state *state)
3571 {
3572 int ret = 0;
3573
3574 switch (transmission_status) {
3575 case CLIENT_TRANSMISSION_STATUS_COMPLETE:
3576 case CLIENT_TRANSMISSION_STATUS_QUEUED:
3577 {
3578 int current_poll_events;
3579 int new_poll_events;
3580 /*
3581 * We want to be notified whenever there is buffer space
3582 * available to send the rest of the payload if we are
3583 * waiting to send data to the client.
3584 *
3585 * The state of the outbound queue being sampled here is
3586 * fine since:
3587 * - it is okay to wake-up "for nothing" in case we see
3588 * that data is left, but another thread succeeds in
3589 * flushing it before us when handling the client "out"
3590 * event. We will simply stop monitoring that event the next
3591 * time it wakes us up and we see no data left to be sent,
3592 * - if another thread fails to flush the entire client
3593 * outgoing queue, it will issue a "communication update"
3594 * command and cause the client's (e)poll mask to be
3595 * re-evaluated.
3596 *
3597 * The situation we seek to avoid would be to disable the
3598 * monitoring of "out" client events indefinitely when there is
3599 * data to be sent, which can't happen because of the
3600 * aforementioned "communication update" mechanism.
3601 */
3602 pthread_mutex_lock(&client->lock);
3603 current_poll_events = client->communication.current_poll_events;
3604 new_poll_events = client_has_outbound_data_left(client) ?
3605 CLIENT_POLL_EVENTS_IN_OUT :
3606 CLIENT_POLL_EVENTS_IN;
3607 client->communication.current_poll_events = new_poll_events;
3608 pthread_mutex_unlock(&client->lock);
3609
3610 /* Update the monitored event set only if it changed. */
3611 if (current_poll_events != new_poll_events) {
3612 ret = lttng_poll_mod(&state->events, client->socket, new_poll_events);
3613 if (ret) {
3614 goto end;
3615 }
3616 }
3617
3618 break;
3619 }
3620 case CLIENT_TRANSMISSION_STATUS_FAIL:
3621 ret = notification_thread_client_disconnect(client, state);
3622 if (ret) {
3623 goto end;
3624 }
3625 break;
3626 case CLIENT_TRANSMISSION_STATUS_ERROR:
3627 ret = -1;
3628 goto end;
3629 default:
3630 abort();
3631 }
3632 end:
3633 return ret;
3634 }
3635
3636 /* Client lock must be acquired by caller. */
3637 static enum client_transmission_status
3638 client_flush_outgoing_queue(struct notification_client *client)
3639 {
3640 ssize_t ret;
3641 size_t to_send_count;
3642 enum client_transmission_status status;
3643 struct lttng_payload_view pv =
3644 lttng_payload_view_from_payload(&client->communication.outbound.payload, 0, -1);
3645 const int fds_to_send_count = lttng_payload_view_get_fd_handle_count(&pv);
3646
3647 ASSERT_LOCKED(client->lock);
3648
3649 if (!client->communication.active) {
3650 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3651 goto end;
3652 }
3653
3654 if (pv.buffer.size == 0) {
3655 /*
3656 * If both data and fds are equal to zero, we are in an invalid
3657 * state.
3658 */
3659 LTTNG_ASSERT(fds_to_send_count != 0);
3660 goto send_fds;
3661 }
3662
3663 /* Send data. */
3664 to_send_count = pv.buffer.size;
3665 DBG("Flushing client (socket fd = %i) outgoing queue", client->socket);
3666
3667 ret = lttcomm_send_unix_sock_non_block(client->socket, pv.buffer.data, to_send_count);
3668 if ((ret >= 0 && ret < to_send_count)) {
3669 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3670 client->socket);
3671 to_send_count -= std::max(ret, (ssize_t) 0);
3672
3673 memmove(client->communication.outbound.payload.buffer.data,
3674 pv.buffer.data + pv.buffer.size - to_send_count,
3675 to_send_count);
3676 ret = lttng_dynamic_buffer_set_size(&client->communication.outbound.payload.buffer,
3677 to_send_count);
3678 if (ret) {
3679 goto error;
3680 }
3681
3682 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
3683 goto end;
3684 } else if (ret < 0) {
3685 /* Generic error, disable the client's communication. */
3686 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3687 client->socket);
3688 client->communication.active = false;
3689 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3690 goto end;
3691 } else {
3692 /*
3693 * No error and flushed the queue completely.
3694 *
3695 * The payload buffer size is used later to
3696 * check if there is notifications queued. So albeit that the
3697 * direct caller knows that the transmission is complete, we
3698 * need to set the buffer size to zero.
3699 */
3700 ret = lttng_dynamic_buffer_set_size(&client->communication.outbound.payload.buffer,
3701 0);
3702 if (ret) {
3703 goto error;
3704 }
3705 }
3706
3707 send_fds:
3708 /* No fds to send, transmission is complete. */
3709 if (fds_to_send_count == 0) {
3710 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
3711 goto end;
3712 }
3713
3714 ret = lttcomm_send_payload_view_fds_unix_sock_non_block(client->socket, &pv);
3715 if (ret < 0) {
3716 /* Generic error, disable the client's communication. */
3717 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3718 client->socket);
3719 client->communication.active = false;
3720 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3721 goto end;
3722 } else if (ret == 0) {
3723 /* Nothing could be sent. */
3724 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
3725 } else {
3726 /* Fd passing is an all or nothing kind of thing. */
3727 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
3728 /*
3729 * The payload _fd_array count is used later to
3730 * check if there is notifications queued. So although the
3731 * direct caller knows that the transmission is complete, we
3732 * need to clear the _fd_array for the queuing check.
3733 */
3734 lttng_dynamic_pointer_array_clear(
3735 &client->communication.outbound.payload._fd_handles);
3736 }
3737
3738 end:
3739 if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) {
3740 client->communication.outbound.queued_command_reply = false;
3741 client->communication.outbound.dropped_notification = false;
3742 lttng_payload_clear(&client->communication.outbound.payload);
3743 }
3744
3745 return status;
3746 error:
3747 return CLIENT_TRANSMISSION_STATUS_ERROR;
3748 }
3749
3750 /* Client lock must _not_ be held by the caller. */
3751 static int client_send_command_reply(struct notification_client *client,
3752 struct notification_thread_state *state,
3753 enum lttng_notification_channel_status status)
3754 {
3755 int ret;
3756 struct lttng_notification_channel_command_reply reply = {
3757 .status = (int8_t) status,
3758 };
3759 struct lttng_notification_channel_message msg;
3760 char buffer[sizeof(msg) + sizeof(reply)];
3761 enum client_transmission_status transmission_status;
3762
3763 msg.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY;
3764 msg.size = sizeof(reply);
3765 msg.fds = 0;
3766
3767 memcpy(buffer, &msg, sizeof(msg));
3768 memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
3769 DBG("Send command reply (%i)", (int) status);
3770
3771 pthread_mutex_lock(&client->lock);
3772 if (client->communication.outbound.queued_command_reply) {
3773 /* Protocol error. */
3774 goto error_unlock;
3775 }
3776
3777 /* Enqueue buffer to outgoing queue and flush it. */
3778 ret = lttng_dynamic_buffer_append(
3779 &client->communication.outbound.payload.buffer, buffer, sizeof(buffer));
3780 if (ret) {
3781 goto error_unlock;
3782 }
3783
3784 transmission_status = client_flush_outgoing_queue(client);
3785
3786 if (client_has_outbound_data_left(client)) {
3787 /* Queue could not be emptied. */
3788 client->communication.outbound.queued_command_reply = true;
3789 }
3790
3791 pthread_mutex_unlock(&client->lock);
3792 ret = client_handle_transmission_status(client, transmission_status, state);
3793 if (ret) {
3794 goto error;
3795 }
3796
3797 return 0;
3798 error_unlock:
3799 pthread_mutex_unlock(&client->lock);
3800 error:
3801 return -1;
3802 }
3803
3804 static int client_handle_message_unknown(struct notification_client *client,
3805 struct notification_thread_state *state
3806 __attribute__((unused)))
3807 {
3808 int ret;
3809 /*
3810 * Receiving message header. The function will be called again
3811 * once the rest of the message as been received and can be
3812 * interpreted.
3813 */
3814 const struct lttng_notification_channel_message *msg;
3815
3816 LTTNG_ASSERT(sizeof(*msg) == client->communication.inbound.payload.buffer.size);
3817 msg = (const struct lttng_notification_channel_message *)
3818 client->communication.inbound.payload.buffer.data;
3819
3820 if (msg->size == 0 || msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
3821 ERR("Invalid notification channel message: length = %u", msg->size);
3822 ret = -1;
3823 goto end;
3824 }
3825
3826 switch (msg->type) {
3827 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3828 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3829 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3830 break;
3831 default:
3832 ret = -1;
3833 ERR("Invalid notification channel message: unexpected message type");
3834 goto end;
3835 }
3836
3837 client->communication.inbound.bytes_to_receive = msg->size;
3838 client->communication.inbound.fds_to_receive = msg->fds;
3839 client->communication.inbound.msg_type =
3840 (enum lttng_notification_channel_message_type) msg->type;
3841 ret = lttng_dynamic_buffer_set_size(&client->communication.inbound.payload.buffer,
3842 msg->size);
3843
3844 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3845 msg = nullptr;
3846 end:
3847 return ret;
3848 }
3849
3850 static int client_handle_message_handshake(struct notification_client *client,
3851 struct notification_thread_state *state)
3852 {
3853 int ret;
3854 struct lttng_notification_channel_command_handshake *handshake_client;
3855 const struct lttng_notification_channel_command_handshake handshake_reply = {
3856 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
3857 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
3858 };
3859 struct lttng_notification_channel_message msg_header;
3860 enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3861 char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
3862
3863 msg_header.type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE;
3864 msg_header.size = sizeof(handshake_reply);
3865 msg_header.fds = 0;
3866
3867 memcpy(send_buffer, &msg_header, sizeof(msg_header));
3868 memcpy(send_buffer + sizeof(msg_header), &handshake_reply, sizeof(handshake_reply));
3869
3870 handshake_client = (struct lttng_notification_channel_command_handshake *)
3871 client->communication.inbound.payload.buffer.data;
3872 client->major = handshake_client->major;
3873 client->minor = handshake_client->minor;
3874 if (!client->communication.inbound.creds_received) {
3875 ERR("No credentials received from client");
3876 ret = -1;
3877 goto end;
3878 }
3879
3880 client->uid = LTTNG_SOCK_GET_UID_CRED(&client->communication.inbound.creds);
3881 client->gid = LTTNG_SOCK_GET_GID_CRED(&client->communication.inbound.creds);
3882 client->is_sessiond = LTTNG_SOCK_GET_PID_CRED(&client->communication.inbound.creds) ==
3883 getpid();
3884 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3885 client->uid,
3886 client->gid,
3887 (int) client->major,
3888 (int) client->minor,
3889 client->is_sessiond ? "true" : "false");
3890
3891 if (handshake_client->major != LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
3892 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
3893 }
3894
3895 pthread_mutex_lock(&client->lock);
3896 /* Outgoing queue will be flushed when the command reply is sent. */
3897 ret = lttng_dynamic_buffer_append(
3898 &client->communication.outbound.payload.buffer, send_buffer, sizeof(send_buffer));
3899 if (ret) {
3900 ERR("Failed to send protocol version to notification channel client");
3901 goto end_unlock;
3902 }
3903
3904 client->validated = true;
3905 client->communication.active = true;
3906 pthread_mutex_unlock(&client->lock);
3907
3908 /* Set reception state to receive the next message header. */
3909 ret = client_reset_inbound_state(client);
3910 if (ret) {
3911 ERR("Failed to reset client communication's inbound state");
3912 goto end;
3913 }
3914
3915 /* Flushes the outgoing queue. */
3916 ret = client_send_command_reply(client, state, status);
3917 if (ret) {
3918 ERR("Failed to send reply to notification channel client");
3919 goto end;
3920 }
3921
3922 goto end;
3923 end_unlock:
3924 pthread_mutex_unlock(&client->lock);
3925 end:
3926 return ret;
3927 }
3928
3929 static int client_handle_message_subscription(struct notification_client *client,
3930 enum lttng_notification_channel_message_type msg_type,
3931 struct notification_thread_state *state)
3932 {
3933 int ret;
3934 struct lttng_condition *condition;
3935 enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3936 struct lttng_payload_view condition_view =
3937 lttng_payload_view_from_payload(&client->communication.inbound.payload, 0, -1);
3938 size_t expected_condition_size;
3939
3940 /*
3941 * No need to lock client to sample the inbound state as the only
3942 * other thread accessing clients (action executor) only uses the
3943 * outbound state.
3944 */
3945 expected_condition_size = client->communication.inbound.payload.buffer.size;
3946 ret = lttng_condition_create_from_payload(&condition_view, &condition);
3947 if (ret != expected_condition_size) {
3948 ERR("Malformed condition received from client");
3949 goto end;
3950 }
3951
3952 /* Ownership of condition is always transferred. */
3953 if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
3954 ret = notification_thread_client_subscribe(client, condition, state, &status);
3955 } else {
3956 ret = notification_thread_client_unsubscribe(client, condition, state, &status);
3957 }
3958
3959 if (ret) {
3960 goto end;
3961 }
3962
3963 /* Set reception state to receive the next message header. */
3964 ret = client_reset_inbound_state(client);
3965 if (ret) {
3966 ERR("Failed to reset client communication's inbound state");
3967 goto end;
3968 }
3969
3970 ret = client_send_command_reply(client, state, status);
3971 if (ret) {
3972 ERR("Failed to send reply to notification channel client");
3973 goto end;
3974 }
3975
3976 end:
3977 return ret;
3978 }
3979
3980 static int client_dispatch_message(struct notification_client *client,
3981 struct notification_thread_state *state)
3982 {
3983 int ret = 0;
3984
3985 if (client->communication.inbound.msg_type !=
3986 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE &&
3987 client->communication.inbound.msg_type !=
3988 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN &&
3989 !client->validated) {
3990 WARN("client attempted a command before handshake");
3991 ret = -1;
3992 goto end;
3993 }
3994
3995 switch (client->communication.inbound.msg_type) {
3996 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN:
3997 {
3998 ret = client_handle_message_unknown(client, state);
3999 break;
4000 }
4001 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
4002 {
4003 ret = client_handle_message_handshake(client, state);
4004 break;
4005 }
4006 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
4007 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
4008 {
4009 ret = client_handle_message_subscription(
4010 client, client->communication.inbound.msg_type, state);
4011 break;
4012 }
4013 default:
4014 abort();
4015 }
4016 end:
4017 return ret;
4018 }
4019
4020 /* Incoming data from client. */
4021 int handle_notification_thread_client_in(struct notification_thread_state *state, int socket)
4022 {
4023 int ret = 0;
4024 struct notification_client *client;
4025 ssize_t recv_ret;
4026 size_t offset;
4027
4028 lttng::urcu::read_lock_guard read_lock;
4029 client = get_client_from_socket(socket, state);
4030 if (!client) {
4031 /* Internal error, abort. */
4032 ret = -1;
4033 goto end;
4034 }
4035
4036 if (client->communication.inbound.bytes_to_receive == 0 &&
4037 client->communication.inbound.fds_to_receive != 0) {
4038 /* Only FDs left to receive. */
4039 goto receive_fds;
4040 }
4041
4042 offset = client->communication.inbound.payload.buffer.size -
4043 client->communication.inbound.bytes_to_receive;
4044 if (client->communication.inbound.expect_creds) {
4045 recv_ret = lttcomm_recv_creds_unix_sock(
4046 socket,
4047 client->communication.inbound.payload.buffer.data + offset,
4048 client->communication.inbound.bytes_to_receive,
4049 &client->communication.inbound.creds);
4050 if (recv_ret > 0) {
4051 client->communication.inbound.expect_creds = false;
4052 client->communication.inbound.creds_received = true;
4053 }
4054 } else {
4055 recv_ret = lttcomm_recv_unix_sock_non_block(
4056 socket,
4057 client->communication.inbound.payload.buffer.data + offset,
4058 client->communication.inbound.bytes_to_receive);
4059 }
4060 if (recv_ret >= 0) {
4061 client->communication.inbound.bytes_to_receive -= recv_ret;
4062 } else {
4063 goto error_disconnect_client;
4064 }
4065
4066 if (client->communication.inbound.bytes_to_receive != 0) {
4067 /* Message incomplete wait for more data. */
4068 ret = 0;
4069 goto end;
4070 }
4071
4072 receive_fds:
4073 LTTNG_ASSERT(client->communication.inbound.bytes_to_receive == 0);
4074
4075 /* Receive fds. */
4076 if (client->communication.inbound.fds_to_receive != 0) {
4077 ret = lttcomm_recv_payload_fds_unix_sock_non_block(
4078 client->socket,
4079 client->communication.inbound.fds_to_receive,
4080 &client->communication.inbound.payload);
4081 if (ret > 0) {
4082 /*
4083 * Fds received. non blocking fds passing is all
4084 * or nothing.
4085 */
4086 ssize_t expected_size;
4087
4088 expected_size = sizeof(int) * client->communication.inbound.fds_to_receive;
4089 LTTNG_ASSERT(ret == expected_size);
4090 client->communication.inbound.fds_to_receive = 0;
4091 } else if (ret == 0) {
4092 /* Received nothing. */
4093 ret = 0;
4094 goto end;
4095 } else {
4096 goto error_disconnect_client;
4097 }
4098 }
4099
4100 /* At this point the message is complete.*/
4101 LTTNG_ASSERT(client->communication.inbound.bytes_to_receive == 0 &&
4102 client->communication.inbound.fds_to_receive == 0);
4103 ret = client_dispatch_message(client, state);
4104 if (ret) {
4105 /*
4106 * Only returns an error if this client must be
4107 * disconnected.
4108 */
4109 goto error_disconnect_client;
4110 }
4111
4112 end:
4113 return ret;
4114
4115 error_disconnect_client:
4116 ret = notification_thread_client_disconnect(client, state);
4117 goto end;
4118 }
4119
4120 /* Client ready to receive outgoing data. */
4121 int handle_notification_thread_client_out(struct notification_thread_state *state, int socket)
4122 {
4123 int ret;
4124 struct notification_client *client;
4125 enum client_transmission_status transmission_status;
4126
4127 lttng::urcu::read_lock_guard read_lock;
4128 client = get_client_from_socket(socket, state);
4129 if (!client) {
4130 /* Internal error, abort. */
4131 ret = -1;
4132 goto end;
4133 }
4134
4135 pthread_mutex_lock(&client->lock);
4136 if (!client_has_outbound_data_left(client)) {
4137 /*
4138 * A client "out" event can be received when no payload is left
4139 * to send under some circumstances.
4140 *
4141 * Many threads can flush a client's outgoing queue and, if they
4142 * had to queue their message (socket was full), will use the
4143 * "communication update" command to signal the (e)poll thread
4144 * to monitor for space being made available in the socket.
4145 *
4146 * Commands are sent over an internal pipe serviced by the same
4147 * thread as the client sockets.
4148 *
4149 * When space is made available in the socket, there is a race
4150 * between the (e)poll thread and the other threads that may
4151 * wish to use the client's socket to flush its outgoing queue.
4152 *
4153 * A non-(e)poll thread may attempt (and succeed) in flushing
4154 * the queue before the (e)poll thread gets a chance to service
4155 * the client's "out" event.
4156 *
4157 * In this situation, the (e)poll thread processing the client
4158 * out event will see an empty payload: there is nothing to do
4159 * except unsubscribing (e)poll "out" events.
4160 *
4161 * Note that this thread is the (e)poll thread so it can modify
4162 * the (e)poll mask directly without using a communication
4163 * update command. Other threads that flush the outgoing queue
4164 * will use the "communication update" command to wake up this
4165 * thread and force it to monitor "out" events.
4166 *
4167 * When other threads succeed in emptying the outgoing queue,
4168 * they don't need to update the (e)poll mask: if the "out"
4169 * event is monitored, it will fire once and the (e)poll
4170 * thread will reach this condition, causing the event to
4171 * stop being monitored.
4172 */
4173 transmission_status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
4174 } else {
4175 transmission_status = client_flush_outgoing_queue(client);
4176 }
4177 pthread_mutex_unlock(&client->lock);
4178
4179 ret = client_handle_transmission_status(client, transmission_status, state);
4180 if (ret) {
4181 goto end;
4182 }
4183 end:
4184 return ret;
4185 }
4186
4187 static bool evaluate_buffer_usage_condition(const struct lttng_condition *condition,
4188 const struct channel_state_sample *sample,
4189 uint64_t buffer_capacity)
4190 {
4191 bool result = false;
4192 uint64_t threshold;
4193 enum lttng_condition_type condition_type;
4194 const struct lttng_condition_buffer_usage *use_condition =
4195 lttng::utils::container_of(condition, &lttng_condition_buffer_usage::parent);
4196
4197 if (use_condition->threshold_bytes.set) {
4198 threshold = use_condition->threshold_bytes.value;
4199 } else {
4200 /*
4201 * Threshold was expressed as a ratio.
4202 *
4203 * TODO the threshold (in bytes) of conditions expressed
4204 * as a ratio of total buffer size could be cached to
4205 * forego this double-multiplication or it could be performed
4206 * as fixed-point math.
4207 *
4208 * Note that caching should accommodates the case where the
4209 * condition applies to multiple channels (i.e. don't assume
4210 * that all channels matching my_chann* have the same size...)
4211 */
4212 threshold = (uint64_t) (use_condition->threshold_ratio.value *
4213 (double) buffer_capacity);
4214 }
4215
4216 condition_type = lttng_condition_get_type(condition);
4217 if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) {
4218 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
4219 ", highest usage = %" PRIu64,
4220 threshold,
4221 sample->highest_usage);
4222
4223 /*
4224 * The low condition should only be triggered once _all_ of the
4225 * streams in a channel have gone below the "low" threshold.
4226 */
4227 if (sample->highest_usage <= threshold) {
4228 result = true;
4229 }
4230 } else {
4231 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
4232 ", highest usage = %" PRIu64,
4233 threshold,
4234 sample->highest_usage);
4235
4236 /*
4237 * For high buffer usage scenarios, we want to trigger whenever
4238 * _any_ of the streams has reached the "high" threshold.
4239 */
4240 if (sample->highest_usage >= threshold) {
4241 result = true;
4242 }
4243 }
4244
4245 return result;
4246 }
4247
4248 static int evaluate_buffer_condition(const struct lttng_condition *condition,
4249 struct lttng_evaluation **evaluation,
4250 const struct notification_thread_state *state
4251 __attribute__((unused)),
4252 const struct channel_state_sample *previous_sample,
4253 const struct channel_state_sample *latest_sample,
4254 struct channel_info *channel_info)
4255 {
4256 int ret = 0;
4257 enum lttng_condition_type condition_type;
4258 const bool previous_sample_available = !!previous_sample;
4259 bool previous_sample_result = false;
4260 bool latest_sample_result;
4261
4262 condition_type = lttng_condition_get_type(condition);
4263
4264 switch (condition_type) {
4265 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4266 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
4267 if (caa_likely(previous_sample_available)) {
4268 previous_sample_result = evaluate_buffer_usage_condition(
4269 condition, previous_sample, channel_info->capacity);
4270 }
4271 latest_sample_result = evaluate_buffer_usage_condition(
4272 condition, latest_sample, channel_info->capacity);
4273 break;
4274 default:
4275 /* Unknown condition type; internal error. */
4276 abort();
4277 }
4278
4279 if (!latest_sample_result || (previous_sample_result == latest_sample_result)) {
4280 /*
4281 * Only trigger on a condition evaluation transition.
4282 *
4283 * NOTE: This edge-triggered logic may not be appropriate for
4284 * future condition types.
4285 */
4286 goto end;
4287 }
4288
4289 if (!evaluation || !latest_sample_result) {
4290 goto end;
4291 }
4292
4293 switch (condition_type) {
4294 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4295 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
4296 *evaluation = lttng_evaluation_buffer_usage_create(
4297 condition_type, latest_sample->highest_usage, channel_info->capacity);
4298 break;
4299 default:
4300 abort();
4301 }
4302
4303 if (!*evaluation) {
4304 ret = -1;
4305 goto end;
4306 }
4307 end:
4308 return ret;
4309 }
4310
4311 static int client_notification_overflow(struct notification_client *client)
4312 {
4313 int ret = 0;
4314 struct lttng_notification_channel_message msg;
4315
4316 msg.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED;
4317 msg.size = 0;
4318 msg.fds = 0;
4319
4320 ASSERT_LOCKED(client->lock);
4321
4322 DBG("Dropping notification addressed to client (socket fd = %i)", client->socket);
4323 if (client->communication.outbound.dropped_notification) {
4324 /*
4325 * The client already has a "notification dropped" message
4326 * in its outgoing queue. Nothing to do since all
4327 * of those messages are coalesced.
4328 */
4329 goto end;
4330 }
4331
4332 client->communication.outbound.dropped_notification = true;
4333 ret = lttng_dynamic_buffer_append(
4334 &client->communication.outbound.payload.buffer, &msg, sizeof(msg));
4335 if (ret) {
4336 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4337 client->socket);
4338 }
4339 end:
4340 return ret;
4341 }
4342
4343 static int client_handle_transmission_status_wrapper(struct notification_client *client,
4344 enum client_transmission_status status,
4345 void *user_data)
4346 {
4347 return client_handle_transmission_status(
4348 client, status, (struct notification_thread_state *) user_data);
4349 }
4350
4351 static int send_evaluation_to_clients(const struct lttng_trigger *trigger,
4352 const struct lttng_evaluation *evaluation,
4353 struct notification_client_list *client_list,
4354 struct notification_thread_state *state,
4355 uid_t object_uid,
4356 gid_t object_gid)
4357 {
4358 const struct lttng_credentials creds = {
4359 .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid),
4360 .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid),
4361 };
4362
4363 return notification_client_list_send_evaluation(client_list,
4364 trigger,
4365 evaluation,
4366 &creds,
4367 client_handle_transmission_status_wrapper,
4368 state);
4369 }
4370
4371 /*
4372 * Permission checks relative to notification channel clients are performed
4373 * here. Notice how object, client, and trigger credentials are involved in
4374 * this check.
4375 *
4376 * The `object` credentials are the credentials associated with the "subject"
4377 * of a condition. For instance, a `rotation completed` condition applies
4378 * to a session. When that condition is met, it will produce an evaluation
4379 * against a session. Hence, in this case, the `object` credentials are the
4380 * credentials of the "subject" session.
4381 *
4382 * The `trigger` credentials are the credentials of the user that registered the
4383 * trigger.
4384 *
4385 * The `client` credentials are the credentials of the user that created a given
4386 * notification channel.
4387 *
4388 * In terms of visibility, it is expected that non-privilieged users can only
4389 * register triggers against "their" objects (their own sessions and
4390 * applications they are allowed to interact with). They can then open a
4391 * notification channel and subscribe to notifications associated with those
4392 * triggers.
4393 *
4394 * As for privilieged users, they can register triggers against the objects of
4395 * other users. They can then subscribe to the notifications associated to their
4396 * triggers. Privilieged users _can't_ subscribe to the notifications of
4397 * triggers owned by other users; they must create their own triggers.
4398 *
4399 * This is more a concern of usability than security. It would be difficult for
4400 * a root user reliably subscribe to a specific set of conditions without
4401 * interference from external users (those could, for instance, unregister
4402 * their triggers).
4403 */
4404 int notification_client_list_send_evaluation(struct notification_client_list *client_list,
4405 const struct lttng_trigger *trigger,
4406 const struct lttng_evaluation *evaluation,
4407 const struct lttng_credentials *source_object_creds,
4408 report_client_transmission_result_cb client_report,
4409 void *user_data)
4410 {
4411 int ret = 0;
4412 struct lttng_payload msg_payload;
4413 struct notification_client_list_element *client_list_element, *tmp;
4414 const struct lttng_notification notification = {
4415 .trigger = (struct lttng_trigger *) trigger,
4416 .evaluation = (struct lttng_evaluation *) evaluation,
4417 };
4418 struct lttng_notification_channel_message msg_header;
4419 const struct lttng_credentials *trigger_creds = lttng_trigger_get_credentials(trigger);
4420
4421 lttng_payload_init(&msg_payload);
4422
4423 msg_header.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION;
4424 msg_header.size = 0;
4425 msg_header.fds = 0;
4426
4427 ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header, sizeof(msg_header));
4428 if (ret) {
4429 goto end;
4430 }
4431
4432 ret = lttng_notification_serialize(&notification, &msg_payload);
4433 if (ret) {
4434 ERR("Failed to serialize notification");
4435 ret = -1;
4436 goto end;
4437 }
4438
4439 /* Update payload size. */
4440 ((struct lttng_notification_channel_message *) msg_payload.buffer.data)->size =
4441 (uint32_t) (msg_payload.buffer.size - sizeof(msg_header));
4442
4443 /* Update the payload number of fds. */
4444 {
4445 const struct lttng_payload_view pv =
4446 lttng_payload_view_from_payload(&msg_payload, 0, -1);
4447
4448 ((struct lttng_notification_channel_message *) msg_payload.buffer.data)->fds =
4449 (uint32_t) lttng_payload_view_get_fd_handle_count(&pv);
4450 }
4451
4452 pthread_mutex_lock(&client_list->lock);
4453 cds_list_for_each_entry_safe (client_list_element, tmp, &client_list->clients_list, node) {
4454 enum client_transmission_status transmission_status;
4455 struct notification_client *client = client_list_element->client;
4456
4457 ret = 0;
4458 pthread_mutex_lock(&client->lock);
4459 if (!client->communication.active) {
4460 /*
4461 * Skip inactive client (protocol error or
4462 * disconnecting).
4463 */
4464 DBG("Skipping client at it is marked as inactive");
4465 goto skip_client;
4466 }
4467
4468 if (lttng_trigger_is_hidden(trigger) && !client->is_sessiond) {
4469 /*
4470 * Notifications resulting from an hidden trigger are
4471 * only sent to the session daemon.
4472 */
4473 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4474 goto skip_client;
4475 }
4476
4477 if (source_object_creds) {
4478 if (client->uid != lttng_credentials_get_uid(source_object_creds) &&
4479 client->gid != lttng_credentials_get_gid(source_object_creds) &&
4480 client->uid != 0) {
4481 /*
4482 * Client is not allowed to monitor this
4483 * object.
4484 */
4485 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4486 goto skip_client;
4487 }
4488 }
4489
4490 if (client->uid != lttng_credentials_get_uid(trigger_creds)) {
4491 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4492 goto skip_client;
4493 }
4494
4495 DBG("Sending notification to client (fd = %i, %zu bytes)",
4496 client->socket,
4497 msg_payload.buffer.size);
4498
4499 if (client_has_outbound_data_left(client)) {
4500 /*
4501 * Outgoing data is already buffered for this client;
4502 * drop the notification and enqueue a "dropped
4503 * notification" message if this is the first dropped
4504 * notification since the socket spilled-over to the
4505 * queue.
4506 */
4507 ret = client_notification_overflow(client);
4508 if (ret) {
4509 /* Fatal error. */
4510 goto skip_client;
4511 }
4512 }
4513
4514 ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload);
4515 if (ret) {
4516 /* Fatal error. */
4517 goto skip_client;
4518 }
4519
4520 transmission_status = client_flush_outgoing_queue(client);
4521 pthread_mutex_unlock(&client->lock);
4522 ret = client_report(client, transmission_status, user_data);
4523 if (ret) {
4524 /* Fatal error. */
4525 goto end_unlock_list;
4526 }
4527
4528 continue;
4529
4530 skip_client:
4531 pthread_mutex_unlock(&client->lock);
4532 if (ret) {
4533 /* Fatal error. */
4534 goto end_unlock_list;
4535 }
4536 }
4537 ret = 0;
4538
4539 end_unlock_list:
4540 pthread_mutex_unlock(&client_list->lock);
4541 end:
4542 lttng_payload_reset(&msg_payload);
4543 return ret;
4544 }
4545
4546 static struct lttng_event_notifier_notification *
4547 recv_one_event_notifier_notification(int notification_pipe_read_fd, enum lttng_domain_type domain)
4548 {
4549 int ret;
4550 uint64_t token;
4551 struct lttng_event_notifier_notification *notification = nullptr;
4552 char *capture_buffer = nullptr;
4553 size_t capture_buffer_size;
4554 void *reception_buffer;
4555 size_t reception_size;
4556
4557 struct lttng_ust_abi_event_notifier_notification ust_notification;
4558 struct lttng_kernel_abi_event_notifier_notification kernel_notification;
4559
4560 /* Init lttng_event_notifier_notification */
4561 switch (domain) {
4562 case LTTNG_DOMAIN_UST:
4563 reception_buffer = (void *) &ust_notification;
4564 reception_size = sizeof(ust_notification);
4565 break;
4566 case LTTNG_DOMAIN_KERNEL:
4567 reception_buffer = (void *) &kernel_notification;
4568 reception_size = sizeof(kernel_notification);
4569 break;
4570 default:
4571 abort();
4572 }
4573
4574 /*
4575 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4576 * ensuring that read/write of tracer notifications are atomic.
4577 */
4578 ret = lttng_read(notification_pipe_read_fd, reception_buffer, reception_size);
4579 if (ret != reception_size) {
4580 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4581 notification_pipe_read_fd,
4582 reception_size,
4583 ret);
4584 ret = -1;
4585 goto end;
4586 }
4587
4588 switch (domain) {
4589 case LTTNG_DOMAIN_UST:
4590 token = ust_notification.token;
4591 capture_buffer_size = ust_notification.capture_buf_size;
4592 break;
4593 case LTTNG_DOMAIN_KERNEL:
4594 token = kernel_notification.token;
4595 capture_buffer_size = kernel_notification.capture_buf_size;
4596 break;
4597 default:
4598 abort();
4599 }
4600
4601 if (capture_buffer_size == 0) {
4602 capture_buffer = nullptr;
4603 goto skip_capture;
4604 }
4605
4606 if (capture_buffer_size > MAX_CAPTURE_SIZE) {
4607 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4608 capture_buffer_size,
4609 MAX_CAPTURE_SIZE);
4610 goto end;
4611 }
4612
4613 capture_buffer = calloc<char>(capture_buffer_size);
4614 if (!capture_buffer) {
4615 ERR("Failed to allocate capture buffer");
4616 goto end;
4617 }
4618
4619 /* Fetch additional payload (capture). */
4620 ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size);
4621 if (ret != capture_buffer_size) {
4622 ERR("Failed to read from event source pipe (fd = %i)", notification_pipe_read_fd);
4623 goto end;
4624 }
4625
4626 skip_capture:
4627 notification = lttng_event_notifier_notification_create(
4628 token, domain, capture_buffer, capture_buffer_size);
4629 if (notification == nullptr) {
4630 goto end;
4631 }
4632
4633 /*
4634 * Ownership transfered to the lttng_event_notifier_notification object.
4635 */
4636 capture_buffer = nullptr;
4637
4638 end:
4639 free(capture_buffer);
4640 return notification;
4641 }
4642
4643 static int
4644 dispatch_one_event_notifier_notification(struct notification_thread_state *state,
4645 struct lttng_event_notifier_notification *notification)
4646 {
4647 struct cds_lfht_node *node;
4648 struct cds_lfht_iter iter;
4649 struct notification_trigger_tokens_ht_element *element;
4650 struct lttng_evaluation *evaluation = nullptr;
4651 enum action_executor_status executor_status;
4652 struct notification_client_list *client_list = nullptr;
4653 int ret;
4654 unsigned int capture_count = 0;
4655
4656 /* Find triggers associated with this token. */
4657 lttng::urcu::read_lock_guard read_lock;
4658 cds_lfht_lookup(state->trigger_tokens_ht,
4659 hash_key_u64(&notification->tracer_token, lttng_ht_seed),
4660 match_trigger_token,
4661 &notification->tracer_token,
4662 &iter);
4663 node = cds_lfht_iter_get_node(&iter);
4664 if (caa_unlikely(!node)) {
4665 /*
4666 * This is not an error, slow consumption of the tracer
4667 * notifications can lead to situations where a trigger is
4668 * removed but we still get tracer notifications matching a
4669 * trigger that no longer exists.
4670 */
4671 ret = 0;
4672 goto end_unlock;
4673 }
4674
4675 element = caa_container_of(node, struct notification_trigger_tokens_ht_element, node);
4676
4677 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4678 lttng_trigger_get_const_condition(element->trigger), &capture_count) !=
4679 LTTNG_CONDITION_STATUS_OK) {
4680 ERR("Failed to get capture count");
4681 ret = -1;
4682 goto end;
4683 }
4684
4685 if (!notification->capture_buffer && capture_count != 0) {
4686 ERR("Expected capture but capture buffer is null");
4687 ret = -1;
4688 goto end;
4689 }
4690
4691 evaluation = lttng_evaluation_event_rule_matches_create(
4692 lttng::utils::container_of(lttng_trigger_get_const_condition(element->trigger),
4693 &lttng_condition_event_rule_matches::parent),
4694 notification->capture_buffer,
4695 notification->capture_buf_size,
4696 false);
4697
4698 if (evaluation == nullptr) {
4699 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4700 ret = -1;
4701 goto end_unlock;
4702 }
4703
4704 client_list = get_client_list_from_condition(
4705 state, lttng_trigger_get_const_condition(element->trigger));
4706 executor_status = action_executor_enqueue_trigger(
4707 state->executor, element->trigger, evaluation, nullptr, client_list);
4708 switch (executor_status) {
4709 case ACTION_EXECUTOR_STATUS_OK:
4710 ret = 0;
4711 break;
4712 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4713 {
4714 struct notification_client_list_element *client_list_element, *tmp;
4715
4716 /*
4717 * Not a fatal error; this is expected and simply means the
4718 * executor has too much work queued already.
4719 */
4720 ret = 0;
4721
4722 /* No clients subscribed to notifications for this trigger. */
4723 if (!client_list) {
4724 break;
4725 }
4726
4727 /* Warn clients that a notification (or more) was dropped. */
4728 pthread_mutex_lock(&client_list->lock);
4729 cds_list_for_each_entry_safe (
4730 client_list_element, tmp, &client_list->clients_list, node) {
4731 enum client_transmission_status transmission_status;
4732 struct notification_client *client = client_list_element->client;
4733
4734 pthread_mutex_lock(&client->lock);
4735 ret = client_notification_overflow(client);
4736 if (ret) {
4737 /* Fatal error. */
4738 goto next_client;
4739 }
4740
4741 transmission_status = client_flush_outgoing_queue(client);
4742 ret = client_handle_transmission_status(client, transmission_status, state);
4743 if (ret) {
4744 /* Fatal error. */
4745 goto next_client;
4746 }
4747 next_client:
4748 pthread_mutex_unlock(&client->lock);
4749 if (ret) {
4750 break;
4751 }
4752 }
4753
4754 pthread_mutex_unlock(&client_list->lock);
4755 break;
4756 }
4757 case ACTION_EXECUTOR_STATUS_INVALID:
4758 case ACTION_EXECUTOR_STATUS_ERROR:
4759 /* Fatal error, shut down everything. */
4760 ERR("Fatal error encoutered while enqueuing action to the action executor");
4761 ret = -1;
4762 goto end_unlock;
4763 default:
4764 /* Unhandled error. */
4765 abort();
4766 }
4767
4768 end_unlock:
4769 notification_client_list_put(client_list);
4770 end:
4771 return ret;
4772 }
4773
4774 static int handle_one_event_notifier_notification(struct notification_thread_state *state,
4775 int pipe,
4776 enum lttng_domain_type domain)
4777 {
4778 int ret = 0;
4779 struct lttng_event_notifier_notification *notification = nullptr;
4780
4781 notification = recv_one_event_notifier_notification(pipe, domain);
4782 if (notification == nullptr) {
4783 /* Reception failed, don't consider it fatal. */
4784 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4785 pipe,
4786 lttng_domain_type_str(domain));
4787 goto end;
4788 }
4789
4790 ret = dispatch_one_event_notifier_notification(state, notification);
4791 if (ret) {
4792 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4793 pipe,
4794 lttng_domain_type_str(domain));
4795 goto end;
4796 }
4797
4798 end:
4799 lttng_event_notifier_notification_destroy(notification);
4800 return ret;
4801 }
4802
4803 int handle_notification_thread_event_notification(struct notification_thread_state *state,
4804 int pipe,
4805 enum lttng_domain_type domain)
4806 {
4807 return handle_one_event_notifier_notification(state, pipe, domain);
4808 }
4809
4810 int handle_notification_thread_channel_sample(struct notification_thread_state *state,
4811 int pipe,
4812 enum lttng_domain_type domain)
4813 {
4814 int ret = 0;
4815 struct lttcomm_consumer_channel_monitor_msg sample_msg;
4816 struct channel_info *channel_info = nullptr;
4817 struct cds_lfht_node *node;
4818 struct cds_lfht_iter iter;
4819 struct lttng_channel_trigger_list *channel_trigger_list;
4820 struct lttng_session_trigger_list *session_trigger_list;
4821 struct lttng_trigger_list_element *trigger_list_element;
4822 bool previous_sample_available = false;
4823 struct channel_state_sample channel_previous_sample, channel_new_sample;
4824 struct session_state_sample session_new_sample;
4825 struct lttng_credentials channel_creds = {};
4826 struct lttng_credentials session_creds = {};
4827 struct session_info *session;
4828 lttng::urcu::read_lock_guard read_lock;
4829
4830 /*
4831 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4832 * ensuring that read/write of sampling messages are atomic.
4833 */
4834 ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg));
4835 if (ret != sizeof(sample_msg)) {
4836 ERR("Failed to read from monitoring pipe (fd = %i)", pipe);
4837 ret = -1;
4838 goto end;
4839 }
4840
4841 ret = 0;
4842 channel_new_sample.key.key = sample_msg.key;
4843 channel_new_sample.key.domain = domain;
4844 channel_new_sample.highest_usage = sample_msg.highest;
4845 channel_new_sample.lowest_usage = sample_msg.lowest;
4846
4847 session = get_session_info_by_id(state, sample_msg.session_id);
4848 if (!session) {
4849 DBG("Received a sample for an unknown session from consumerd: session id = %" PRIu64,
4850 sample_msg.session_id);
4851 goto end_unlock;
4852 }
4853
4854 session_new_sample = session->last_state_sample;
4855 session_new_sample.consumed_data_size += sample_msg.consumed_since_last_sample;
4856 session_creds = {
4857 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
4858 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
4859 };
4860
4861 session_trigger_list = get_session_trigger_list(state, session->name);
4862 LTTNG_ASSERT(session_trigger_list);
4863 cds_list_for_each_entry (trigger_list_element, &session_trigger_list->list, node) {
4864 const struct lttng_condition *condition;
4865 struct lttng_trigger *trigger;
4866 struct notification_client_list *client_list = nullptr;
4867 struct lttng_evaluation *evaluation = nullptr;
4868 enum action_executor_status executor_status;
4869
4870 ret = 0;
4871 trigger = trigger_list_element->trigger;
4872 condition = lttng_trigger_get_const_condition(trigger);
4873 LTTNG_ASSERT(condition);
4874
4875 ret = evaluate_session_condition(
4876 condition, session, &session_new_sample, &evaluation);
4877 if (caa_unlikely(ret)) {
4878 break;
4879 }
4880
4881 if (caa_likely(!evaluation)) {
4882 continue;
4883 }
4884
4885 /*
4886 * Ownership of `evaluation` transferred to the action executor
4887 * no matter the result. The callee acquires a reference to the
4888 * client list: we can release our own.
4889 */
4890 client_list = get_client_list_from_condition(state, condition);
4891 executor_status = action_executor_enqueue_trigger(
4892 state->executor, trigger, evaluation, &session_creds, client_list);
4893 notification_client_list_put(client_list);
4894 evaluation = nullptr;
4895 switch (executor_status) {
4896 case ACTION_EXECUTOR_STATUS_OK:
4897 break;
4898 case ACTION_EXECUTOR_STATUS_ERROR:
4899 case ACTION_EXECUTOR_STATUS_INVALID:
4900 /*
4901 * TODO Add trigger identification (name/id) when
4902 * it is added to the API.
4903 */
4904 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4905 ret = -1;
4906 goto end_unlock;
4907 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4908 /*
4909 * TODO Add trigger identification (name/id) when
4910 * it is added to the API.
4911 *
4912 * Not a fatal error.
4913 */
4914 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4915 ret = 0;
4916 goto end_unlock;
4917 default:
4918 abort();
4919 }
4920 }
4921
4922 /* Retrieve the channel's informations */
4923 cds_lfht_lookup(state->channels_ht,
4924 hash_channel_key(&channel_new_sample.key),
4925 match_channel_info,
4926 &channel_new_sample.key,
4927 &iter);
4928 node = cds_lfht_iter_get_node(&iter);
4929 if (caa_unlikely(!node)) {
4930 /*
4931 * Not an error since the consumer can push a sample to the pipe
4932 * and the rest of the session daemon could notify us of the
4933 * channel's destruction before we get a chance to process that
4934 * sample.
4935 */
4936 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
4937 " in %s domain",
4938 channel_new_sample.key.key,
4939 lttng_domain_type_str(domain));
4940 goto end_unlock;
4941 }
4942
4943 channel_info = caa_container_of(node, struct channel_info, channels_ht_node);
4944 DBG("Handling channel sample for channel %s (key = %" PRIu64
4945 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64
4946 ", consumed since last sample = %" PRIu64 ")",
4947 channel_info->name,
4948 channel_new_sample.key.key,
4949 channel_info->session_info->name,
4950 channel_new_sample.highest_usage,
4951 channel_new_sample.lowest_usage,
4952 sample_msg.consumed_since_last_sample);
4953
4954 /* Retrieve the channel's last sample, if it exists, and update it. */
4955 cds_lfht_lookup(state->channel_state_ht,
4956 hash_channel_key(&channel_new_sample.key),
4957 match_channel_state_sample,
4958 &channel_new_sample.key,
4959 &iter);
4960 node = cds_lfht_iter_get_node(&iter);
4961 if (caa_likely(node)) {
4962 struct channel_state_sample *stored_sample;
4963
4964 /* Update the sample stored. */
4965 stored_sample =
4966 caa_container_of(node, struct channel_state_sample, channel_state_ht_node);
4967
4968 memcpy(&channel_previous_sample, stored_sample, sizeof(channel_previous_sample));
4969 stored_sample->highest_usage = channel_new_sample.highest_usage;
4970 stored_sample->lowest_usage = channel_new_sample.lowest_usage;
4971 previous_sample_available = true;
4972 } else {
4973 /*
4974 * This is the channel's first sample, allocate space for and
4975 * store the new sample.
4976 */
4977 struct channel_state_sample *stored_sample;
4978
4979 stored_sample = zmalloc<channel_state_sample>();
4980 if (!stored_sample) {
4981 ret = -1;
4982 goto end_unlock;
4983 }
4984
4985 memcpy(stored_sample, &channel_new_sample, sizeof(*stored_sample));
4986 cds_lfht_node_init(&stored_sample->channel_state_ht_node);
4987 cds_lfht_add(state->channel_state_ht,
4988 hash_channel_key(&stored_sample->key),
4989 &stored_sample->channel_state_ht_node);
4990 }
4991
4992 /* Find triggers associated with this channel. */
4993 cds_lfht_lookup(state->channel_triggers_ht,
4994 hash_channel_key(&channel_new_sample.key),
4995 match_channel_trigger_list,
4996 &channel_new_sample.key,
4997 &iter);
4998 node = cds_lfht_iter_get_node(&iter);
4999 LTTNG_ASSERT(node);
5000
5001 channel_creds = (typeof(channel_creds)){
5002 .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid),
5003 .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid),
5004 };
5005
5006 channel_trigger_list =
5007 caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node);
5008 cds_list_for_each_entry (trigger_list_element, &channel_trigger_list->list, node) {
5009 const struct lttng_condition *condition;
5010 struct lttng_trigger *trigger;
5011 struct notification_client_list *client_list = nullptr;
5012 struct lttng_evaluation *evaluation = nullptr;
5013 enum action_executor_status executor_status;
5014
5015 ret = 0;
5016 trigger = trigger_list_element->trigger;
5017 condition = lttng_trigger_get_const_condition(trigger);
5018 LTTNG_ASSERT(condition);
5019
5020 ret = evaluate_buffer_condition(
5021 condition,
5022 &evaluation,
5023 state,
5024 previous_sample_available ? &channel_previous_sample : nullptr,
5025 &channel_new_sample,
5026 channel_info);
5027 if (caa_unlikely(ret)) {
5028 break;
5029 }
5030
5031 if (caa_likely(!evaluation)) {
5032 continue;
5033 }
5034
5035 /*
5036 * Ownership of `evaluation` transferred to the action executor
5037 * no matter the result. The callee acquires a reference to the
5038 * client list: we can release our own.
5039 */
5040 client_list = get_client_list_from_condition(state, condition);
5041 executor_status = action_executor_enqueue_trigger(
5042 state->executor, trigger, evaluation, &channel_creds, client_list);
5043 notification_client_list_put(client_list);
5044 evaluation = nullptr;
5045 switch (executor_status) {
5046 case ACTION_EXECUTOR_STATUS_OK:
5047 break;
5048 case ACTION_EXECUTOR_STATUS_ERROR:
5049 case ACTION_EXECUTOR_STATUS_INVALID:
5050 /*
5051 * TODO Add trigger identification (name/id) when
5052 * it is added to the API.
5053 */
5054 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5055 ret = -1;
5056 goto end_unlock;
5057 case ACTION_EXECUTOR_STATUS_OVERFLOW:
5058 /*
5059 * TODO Add trigger identification (name/id) when
5060 * it is added to the API.
5061 *
5062 * Not a fatal error.
5063 */
5064 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5065 ret = 0;
5066 goto end_unlock;
5067 default:
5068 abort();
5069 }
5070 }
5071 end_unlock:
5072 if (session) {
5073 session->last_state_sample = session_new_sample;
5074 }
5075 session_info_put(session);
5076 end:
5077 return ret;
5078 }
This page took 0.184161 seconds and 4 git commands to generate.