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