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