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