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