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