sessiond: notification: receive incoming notifications from tracers
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-events.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
10 #define _LGPL_SOURCE
11 #include <urcu.h>
12 #include <urcu/rculfhash.h>
13
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/domain-internal.h>
31 #include <lttng/notification/channel-internal.h>
32 #include <lttng/trigger/trigger-internal.h>
33 #include <lttng/event-rule/event-rule-internal.h>
34
35 #include <time.h>
36 #include <unistd.h>
37 #include <assert.h>
38 #include <inttypes.h>
39 #include <fcntl.h>
40
41 #include "condition-internal.h"
42 #include "notification-thread.h"
43 #include "notification-thread-events.h"
44 #include "notification-thread-commands.h"
45 #include "lttng-sessiond.h"
46 #include "kernel.h"
47
48 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
49 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
50
51 enum lttng_object_type {
52 LTTNG_OBJECT_TYPE_UNKNOWN,
53 LTTNG_OBJECT_TYPE_NONE,
54 LTTNG_OBJECT_TYPE_CHANNEL,
55 LTTNG_OBJECT_TYPE_SESSION,
56 };
57
58 struct lttng_trigger_list_element {
59 /* No ownership of the trigger object is assumed. */
60 struct lttng_trigger *trigger;
61 struct cds_list_head node;
62 };
63
64 struct lttng_channel_trigger_list {
65 struct channel_key channel_key;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node;
72 };
73
74 /*
75 * List of triggers applying to a given session.
76 *
77 * See:
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
82 */
83 struct lttng_session_trigger_list {
84 /*
85 * Not owned by this; points to the session_info structure's
86 * session name.
87 */
88 const char *session_name;
89 /* List of struct lttng_trigger_list_element. */
90 struct cds_list_head list;
91 /* Node in the session_triggers_ht */
92 struct cds_lfht_node session_triggers_ht_node;
93 /*
94 * Weak reference to the notification system's session triggers
95 * hashtable.
96 *
97 * The session trigger list structure structure is owned by
98 * the session's session_info.
99 *
100 * The session_info is kept alive the the channel_infos holding a
101 * reference to it (reference counting). When those channels are
102 * destroyed (at runtime or on teardown), the reference they hold
103 * to the session_info are released. On destruction of session_info,
104 * session_info_destroy() will remove the list of triggers applying
105 * to this session from the notification system's state.
106 *
107 * This implies that the session_triggers_ht must be destroyed
108 * after the channels.
109 */
110 struct cds_lfht *session_triggers_ht;
111 /* Used for delayed RCU reclaim. */
112 struct rcu_head rcu_node;
113 };
114
115 struct lttng_trigger_ht_element {
116 struct lttng_trigger *trigger;
117 struct cds_lfht_node node;
118 struct cds_lfht_node node_by_name_uid;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node;
121 };
122
123 struct lttng_condition_list_element {
124 struct lttng_condition *condition;
125 struct cds_list_head node;
126 };
127
128 /*
129 * Facilities to carry the different notifications type in the action processing
130 * code path.
131 */
132 struct lttng_event_notifier_notification {
133 union {
134 struct lttng_ust_event_notifier_notification *ust;
135 struct lttng_kernel_event_notifier_notification *kernel;
136 } notification;
137 uint64_t token;
138 enum lttng_domain_type type;
139 };
140
141 struct channel_state_sample {
142 struct channel_key key;
143 struct cds_lfht_node channel_state_ht_node;
144 uint64_t highest_usage;
145 uint64_t lowest_usage;
146 uint64_t channel_total_consumed;
147 /* call_rcu delayed reclaim. */
148 struct rcu_head rcu_node;
149 };
150
151 static unsigned long hash_channel_key(struct channel_key *key);
152 static int evaluate_buffer_condition(const struct lttng_condition *condition,
153 struct lttng_evaluation **evaluation,
154 const struct notification_thread_state *state,
155 const struct channel_state_sample *previous_sample,
156 const struct channel_state_sample *latest_sample,
157 uint64_t previous_session_consumed_total,
158 uint64_t latest_session_consumed_total,
159 struct channel_info *channel_info);
160 static
161 int send_evaluation_to_clients(const struct lttng_trigger *trigger,
162 const struct lttng_evaluation *evaluation,
163 struct notification_client_list *client_list,
164 struct notification_thread_state *state,
165 uid_t channel_uid, gid_t channel_gid);
166
167
168 /* session_info API */
169 static
170 void session_info_destroy(void *_data);
171 static
172 void session_info_get(struct session_info *session_info);
173 static
174 void session_info_put(struct session_info *session_info);
175 static
176 struct session_info *session_info_create(const char *name,
177 uid_t uid, gid_t gid,
178 struct lttng_session_trigger_list *trigger_list,
179 struct cds_lfht *sessions_ht);
180 static
181 void session_info_add_channel(struct session_info *session_info,
182 struct channel_info *channel_info);
183 static
184 void session_info_remove_channel(struct session_info *session_info,
185 struct channel_info *channel_info);
186
187 /* lttng_session_trigger_list API */
188 static
189 struct lttng_session_trigger_list *lttng_session_trigger_list_create(
190 const char *session_name,
191 struct cds_lfht *session_triggers_ht);
192 static
193 struct lttng_session_trigger_list *lttng_session_trigger_list_build(
194 const struct notification_thread_state *state,
195 const char *session_name);
196 static
197 void lttng_session_trigger_list_destroy(
198 struct lttng_session_trigger_list *list);
199 static
200 int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
201 struct lttng_trigger *trigger);
202
203 static
204 int client_handle_transmission_status(
205 struct notification_client *client,
206 enum client_transmission_status transmission_status,
207 struct notification_thread_state *state);
208
209 static
210 void free_lttng_trigger_ht_element_rcu(struct rcu_head *node);
211
212 static
213 int match_client_socket(struct cds_lfht_node *node, const void *key)
214 {
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const int socket = (int) (intptr_t) key;
217 const struct notification_client *client = caa_container_of(node,
218 struct notification_client, client_socket_ht_node);
219
220 return client->socket == socket;
221 }
222
223 static
224 int match_client_id(struct cds_lfht_node *node, const void *key)
225 {
226 /* This double-cast is intended to supress pointer-to-cast warning. */
227 const notification_client_id id = *((notification_client_id *) key);
228 const struct notification_client *client = caa_container_of(
229 node, struct notification_client, client_id_ht_node);
230
231 return client->id == id;
232 }
233
234 static
235 int match_channel_trigger_list(struct cds_lfht_node *node, const void *key)
236 {
237 struct channel_key *channel_key = (struct channel_key *) key;
238 struct lttng_channel_trigger_list *trigger_list;
239
240 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
241 channel_triggers_ht_node);
242
243 return !!((channel_key->key == trigger_list->channel_key.key) &&
244 (channel_key->domain == trigger_list->channel_key.domain));
245 }
246
247 static
248 int match_session_trigger_list(struct cds_lfht_node *node, const void *key)
249 {
250 const char *session_name = (const char *) key;
251 struct lttng_session_trigger_list *trigger_list;
252
253 trigger_list = caa_container_of(node, struct lttng_session_trigger_list,
254 session_triggers_ht_node);
255
256 return !!(strcmp(trigger_list->session_name, session_name) == 0);
257 }
258
259 static
260 int match_channel_state_sample(struct cds_lfht_node *node, const void *key)
261 {
262 struct channel_key *channel_key = (struct channel_key *) key;
263 struct channel_state_sample *sample;
264
265 sample = caa_container_of(node, struct channel_state_sample,
266 channel_state_ht_node);
267
268 return !!((channel_key->key == sample->key.key) &&
269 (channel_key->domain == sample->key.domain));
270 }
271
272 static
273 int match_channel_info(struct cds_lfht_node *node, const void *key)
274 {
275 struct channel_key *channel_key = (struct channel_key *) key;
276 struct channel_info *channel_info;
277
278 channel_info = caa_container_of(node, struct channel_info,
279 channels_ht_node);
280
281 return !!((channel_key->key == channel_info->key.key) &&
282 (channel_key->domain == channel_info->key.domain));
283 }
284
285 static
286 int match_trigger(struct cds_lfht_node *node, const void *key)
287 {
288 struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
289 struct lttng_trigger_ht_element *trigger_ht_element;
290
291 trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element,
292 node);
293
294 return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger);
295 }
296
297 static
298 int match_trigger_token(struct cds_lfht_node *node, const void *key)
299 {
300 const uint64_t *_key = key;
301 struct notification_trigger_tokens_ht_element *element;
302
303 element = caa_container_of(node,
304 struct notification_trigger_tokens_ht_element, node);
305 return *_key == element->token;
306 }
307
308 static
309 int match_client_list_condition(struct cds_lfht_node *node, const void *key)
310 {
311 struct lttng_condition *condition_key = (struct lttng_condition *) key;
312 struct notification_client_list *client_list;
313 const struct lttng_condition *condition;
314
315 assert(condition_key);
316
317 client_list = caa_container_of(node, struct notification_client_list,
318 notification_trigger_clients_ht_node);
319 condition = lttng_trigger_get_const_condition(client_list->trigger);
320
321 return !!lttng_condition_is_equal(condition_key, condition);
322 }
323
324 static
325 int match_session(struct cds_lfht_node *node, const void *key)
326 {
327 const char *name = key;
328 struct session_info *session_info = caa_container_of(
329 node, struct session_info, sessions_ht_node);
330
331 return !strcmp(session_info->name, name);
332 }
333
334 /*
335 * Match trigger based on name and credentials only.
336 * Name duplication is NOT allowed for the same uid.
337 */
338 static
339 int match_trigger_by_name_uid(struct cds_lfht_node *node,
340 const void *key)
341 {
342 bool match = false;
343 const char *name;
344 const char *key_name;
345 enum lttng_trigger_status status;
346 const struct lttng_credentials *key_creds;
347 const struct lttng_credentials *node_creds;
348 const struct lttng_trigger *trigger_key =
349 (const struct lttng_trigger *) key;
350 const struct lttng_trigger_ht_element *trigger_ht_element =
351 caa_container_of(node,
352 struct lttng_trigger_ht_element,
353 node_by_name_uid);
354
355 status = lttng_trigger_get_name(trigger_ht_element->trigger, &name);
356 assert(status == LTTNG_TRIGGER_STATUS_OK);
357
358 status = lttng_trigger_get_name(trigger_key, &key_name);
359 assert(status == LTTNG_TRIGGER_STATUS_OK);
360
361 /* Compare the names. */
362 if (strcmp(name, key_name) != 0) {
363 goto end;
364 }
365
366 /* Compare the owners' UIDs. */
367 key_creds = lttng_trigger_get_credentials(trigger_key);
368 node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
369
370 match = lttng_credentials_is_equal_uid(key_creds, node_creds);
371
372 end:
373 return match;
374 }
375
376 /*
377 * Hash trigger based on name and credentials only.
378 */
379 static
380 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger)
381 {
382 unsigned long hash = 0;
383 const struct lttng_credentials *trigger_creds;
384 const char *trigger_name;
385 enum lttng_trigger_status status;
386
387 status = lttng_trigger_get_name(trigger, &trigger_name);
388 if (status == LTTNG_TRIGGER_STATUS_OK) {
389 hash = hash_key_str(trigger_name, lttng_ht_seed);
390 }
391
392 trigger_creds = lttng_trigger_get_credentials(trigger);
393 hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid),
394 lttng_ht_seed);
395
396 return hash;
397 }
398
399 static
400 unsigned long hash_channel_key(struct channel_key *key)
401 {
402 unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed);
403 unsigned long domain_hash = hash_key_ulong(
404 (void *) (unsigned long) key->domain, lttng_ht_seed);
405
406 return key_hash ^ domain_hash;
407 }
408
409 static
410 unsigned long hash_client_socket(int socket)
411 {
412 return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed);
413 }
414
415 static
416 unsigned long hash_client_id(notification_client_id id)
417 {
418 return hash_key_u64(&id, lttng_ht_seed);
419 }
420
421 /*
422 * Get the type of object to which a given condition applies. Bindings let
423 * the notification system evaluate a trigger's condition when a given
424 * object's state is updated.
425 *
426 * For instance, a condition bound to a channel will be evaluated everytime
427 * the channel's state is changed by a channel monitoring sample.
428 */
429 static
430 enum lttng_object_type get_condition_binding_object(
431 const struct lttng_condition *condition)
432 {
433 switch (lttng_condition_get_type(condition)) {
434 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
435 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
436 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
437 return LTTNG_OBJECT_TYPE_CHANNEL;
438 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
439 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
440 return LTTNG_OBJECT_TYPE_SESSION;
441 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
442 return LTTNG_OBJECT_TYPE_NONE;
443 default:
444 return LTTNG_OBJECT_TYPE_UNKNOWN;
445 }
446 }
447
448 static
449 void free_channel_info_rcu(struct rcu_head *node)
450 {
451 free(caa_container_of(node, struct channel_info, rcu_node));
452 }
453
454 static
455 void channel_info_destroy(struct channel_info *channel_info)
456 {
457 if (!channel_info) {
458 return;
459 }
460
461 if (channel_info->session_info) {
462 session_info_remove_channel(channel_info->session_info,
463 channel_info);
464 session_info_put(channel_info->session_info);
465 }
466 if (channel_info->name) {
467 free(channel_info->name);
468 }
469 call_rcu(&channel_info->rcu_node, free_channel_info_rcu);
470 }
471
472 static
473 void free_session_info_rcu(struct rcu_head *node)
474 {
475 free(caa_container_of(node, struct session_info, rcu_node));
476 }
477
478 /* Don't call directly, use the ref-counting mechanism. */
479 static
480 void session_info_destroy(void *_data)
481 {
482 struct session_info *session_info = _data;
483 int ret;
484
485 assert(session_info);
486 if (session_info->channel_infos_ht) {
487 ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL);
488 if (ret) {
489 ERR("[notification-thread] Failed to destroy channel information hash table");
490 }
491 }
492 lttng_session_trigger_list_destroy(session_info->trigger_list);
493
494 rcu_read_lock();
495 cds_lfht_del(session_info->sessions_ht,
496 &session_info->sessions_ht_node);
497 rcu_read_unlock();
498 free(session_info->name);
499 call_rcu(&session_info->rcu_node, free_session_info_rcu);
500 }
501
502 static
503 void session_info_get(struct session_info *session_info)
504 {
505 if (!session_info) {
506 return;
507 }
508 lttng_ref_get(&session_info->ref);
509 }
510
511 static
512 void session_info_put(struct session_info *session_info)
513 {
514 if (!session_info) {
515 return;
516 }
517 lttng_ref_put(&session_info->ref);
518 }
519
520 static
521 struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid,
522 struct lttng_session_trigger_list *trigger_list,
523 struct cds_lfht *sessions_ht)
524 {
525 struct session_info *session_info;
526
527 assert(name);
528
529 session_info = zmalloc(sizeof(*session_info));
530 if (!session_info) {
531 goto end;
532 }
533 lttng_ref_init(&session_info->ref, session_info_destroy);
534
535 session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE,
536 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
537 if (!session_info->channel_infos_ht) {
538 goto error;
539 }
540
541 cds_lfht_node_init(&session_info->sessions_ht_node);
542 session_info->name = strdup(name);
543 if (!session_info->name) {
544 goto error;
545 }
546 session_info->uid = uid;
547 session_info->gid = gid;
548 session_info->trigger_list = trigger_list;
549 session_info->sessions_ht = sessions_ht;
550 end:
551 return session_info;
552 error:
553 session_info_put(session_info);
554 return NULL;
555 }
556
557 static
558 void session_info_add_channel(struct session_info *session_info,
559 struct channel_info *channel_info)
560 {
561 rcu_read_lock();
562 cds_lfht_add(session_info->channel_infos_ht,
563 hash_channel_key(&channel_info->key),
564 &channel_info->session_info_channels_ht_node);
565 rcu_read_unlock();
566 }
567
568 static
569 void session_info_remove_channel(struct session_info *session_info,
570 struct channel_info *channel_info)
571 {
572 rcu_read_lock();
573 cds_lfht_del(session_info->channel_infos_ht,
574 &channel_info->session_info_channels_ht_node);
575 rcu_read_unlock();
576 }
577
578 static
579 struct channel_info *channel_info_create(const char *channel_name,
580 struct channel_key *channel_key, uint64_t channel_capacity,
581 struct session_info *session_info)
582 {
583 struct channel_info *channel_info = zmalloc(sizeof(*channel_info));
584
585 if (!channel_info) {
586 goto end;
587 }
588
589 cds_lfht_node_init(&channel_info->channels_ht_node);
590 cds_lfht_node_init(&channel_info->session_info_channels_ht_node);
591 memcpy(&channel_info->key, channel_key, sizeof(*channel_key));
592 channel_info->capacity = channel_capacity;
593
594 channel_info->name = strdup(channel_name);
595 if (!channel_info->name) {
596 goto error;
597 }
598
599 /*
600 * Set the references between session and channel infos:
601 * - channel_info holds a strong reference to session_info
602 * - session_info holds a weak reference to channel_info
603 */
604 session_info_get(session_info);
605 session_info_add_channel(session_info, channel_info);
606 channel_info->session_info = session_info;
607 end:
608 return channel_info;
609 error:
610 channel_info_destroy(channel_info);
611 return NULL;
612 }
613
614 LTTNG_HIDDEN
615 bool notification_client_list_get(struct notification_client_list *list)
616 {
617 return urcu_ref_get_unless_zero(&list->ref);
618 }
619
620 static
621 void free_notification_client_list_rcu(struct rcu_head *node)
622 {
623 free(caa_container_of(node, struct notification_client_list,
624 rcu_node));
625 }
626
627 static
628 void notification_client_list_release(struct urcu_ref *list_ref)
629 {
630 struct notification_client_list *list =
631 container_of(list_ref, typeof(*list), ref);
632 struct notification_client_list_element *client_list_element, *tmp;
633
634 if (list->notification_trigger_clients_ht) {
635 rcu_read_lock();
636 cds_lfht_del(list->notification_trigger_clients_ht,
637 &list->notification_trigger_clients_ht_node);
638 rcu_read_unlock();
639 list->notification_trigger_clients_ht = NULL;
640 }
641 cds_list_for_each_entry_safe(client_list_element, tmp,
642 &list->list, node) {
643 free(client_list_element);
644 }
645 pthread_mutex_destroy(&list->lock);
646 call_rcu(&list->rcu_node, free_notification_client_list_rcu);
647 }
648
649 static
650 struct notification_client_list *notification_client_list_create(
651 const struct lttng_trigger *trigger)
652 {
653 struct notification_client_list *client_list =
654 zmalloc(sizeof(*client_list));
655
656 if (!client_list) {
657 goto error;
658 }
659 pthread_mutex_init(&client_list->lock, NULL);
660 urcu_ref_init(&client_list->ref);
661 cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node);
662 CDS_INIT_LIST_HEAD(&client_list->list);
663 client_list->trigger = trigger;
664 error:
665 return client_list;
666 }
667
668 static
669 void publish_notification_client_list(
670 struct notification_thread_state *state,
671 struct notification_client_list *list)
672 {
673 const struct lttng_condition *condition =
674 lttng_trigger_get_const_condition(list->trigger);
675
676 assert(!list->notification_trigger_clients_ht);
677 notification_client_list_get(list);
678
679 list->notification_trigger_clients_ht =
680 state->notification_trigger_clients_ht;
681
682 rcu_read_lock();
683 cds_lfht_add(state->notification_trigger_clients_ht,
684 lttng_condition_hash(condition),
685 &list->notification_trigger_clients_ht_node);
686 rcu_read_unlock();
687 }
688
689 LTTNG_HIDDEN
690 void notification_client_list_put(struct notification_client_list *list)
691 {
692 if (!list) {
693 return;
694 }
695 return urcu_ref_put(&list->ref, notification_client_list_release);
696 }
697
698 /* Provides a reference to the returned list. */
699 static
700 struct notification_client_list *get_client_list_from_condition(
701 struct notification_thread_state *state,
702 const struct lttng_condition *condition)
703 {
704 struct cds_lfht_node *node;
705 struct cds_lfht_iter iter;
706 struct notification_client_list *list = NULL;
707
708 rcu_read_lock();
709 cds_lfht_lookup(state->notification_trigger_clients_ht,
710 lttng_condition_hash(condition),
711 match_client_list_condition,
712 condition,
713 &iter);
714 node = cds_lfht_iter_get_node(&iter);
715 if (node) {
716 list = container_of(node, struct notification_client_list,
717 notification_trigger_clients_ht_node);
718 list = notification_client_list_get(list) ? list : NULL;
719 }
720
721 rcu_read_unlock();
722 return list;
723 }
724
725 static
726 int evaluate_channel_condition_for_client(
727 const struct lttng_condition *condition,
728 struct notification_thread_state *state,
729 struct lttng_evaluation **evaluation,
730 uid_t *session_uid, gid_t *session_gid)
731 {
732 int ret;
733 struct cds_lfht_iter iter;
734 struct cds_lfht_node *node;
735 struct channel_info *channel_info = NULL;
736 struct channel_key *channel_key = NULL;
737 struct channel_state_sample *last_sample = NULL;
738 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
739
740 rcu_read_lock();
741
742 /* Find the channel associated with the condition. */
743 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter,
744 channel_trigger_list, channel_triggers_ht_node) {
745 struct lttng_trigger_list_element *element;
746
747 cds_list_for_each_entry(element, &channel_trigger_list->list, node) {
748 const struct lttng_condition *current_condition =
749 lttng_trigger_get_const_condition(
750 element->trigger);
751
752 assert(current_condition);
753 if (!lttng_condition_is_equal(condition,
754 current_condition)) {
755 continue;
756 }
757
758 /* Found the trigger, save the channel key. */
759 channel_key = &channel_trigger_list->channel_key;
760 break;
761 }
762 if (channel_key) {
763 /* The channel key was found stop iteration. */
764 break;
765 }
766 }
767
768 if (!channel_key){
769 /* No channel found; normal exit. */
770 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
771 ret = 0;
772 goto end;
773 }
774
775 /* Fetch channel info for the matching channel. */
776 cds_lfht_lookup(state->channels_ht,
777 hash_channel_key(channel_key),
778 match_channel_info,
779 channel_key,
780 &iter);
781 node = cds_lfht_iter_get_node(&iter);
782 assert(node);
783 channel_info = caa_container_of(node, struct channel_info,
784 channels_ht_node);
785
786 /* Retrieve the channel's last sample, if it exists. */
787 cds_lfht_lookup(state->channel_state_ht,
788 hash_channel_key(channel_key),
789 match_channel_state_sample,
790 channel_key,
791 &iter);
792 node = cds_lfht_iter_get_node(&iter);
793 if (node) {
794 last_sample = caa_container_of(node,
795 struct channel_state_sample,
796 channel_state_ht_node);
797 } else {
798 /* Nothing to evaluate, no sample was ever taken. Normal exit */
799 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
800 ret = 0;
801 goto end;
802 }
803
804 ret = evaluate_buffer_condition(condition, evaluation, state,
805 NULL, last_sample,
806 0, channel_info->session_info->consumed_data_size,
807 channel_info);
808 if (ret) {
809 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
810 goto end;
811 }
812
813 *session_uid = channel_info->session_info->uid;
814 *session_gid = channel_info->session_info->gid;
815 end:
816 rcu_read_unlock();
817 return ret;
818 }
819
820 static
821 const char *get_condition_session_name(const struct lttng_condition *condition)
822 {
823 const char *session_name = NULL;
824 enum lttng_condition_status status;
825
826 switch (lttng_condition_get_type(condition)) {
827 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
828 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
829 status = lttng_condition_buffer_usage_get_session_name(
830 condition, &session_name);
831 break;
832 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
833 status = lttng_condition_session_consumed_size_get_session_name(
834 condition, &session_name);
835 break;
836 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
837 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
838 status = lttng_condition_session_rotation_get_session_name(
839 condition, &session_name);
840 break;
841 default:
842 abort();
843 }
844 if (status != LTTNG_CONDITION_STATUS_OK) {
845 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
846 goto end;
847 }
848 end:
849 return session_name;
850 }
851
852 static
853 int evaluate_session_condition_for_client(
854 const struct lttng_condition *condition,
855 struct notification_thread_state *state,
856 struct lttng_evaluation **evaluation,
857 uid_t *session_uid, gid_t *session_gid)
858 {
859 int ret;
860 struct cds_lfht_iter iter;
861 struct cds_lfht_node *node;
862 const char *session_name;
863 struct session_info *session_info = NULL;
864
865 rcu_read_lock();
866 session_name = get_condition_session_name(condition);
867
868 /* Find the session associated with the trigger. */
869 cds_lfht_lookup(state->sessions_ht,
870 hash_key_str(session_name, lttng_ht_seed),
871 match_session,
872 session_name,
873 &iter);
874 node = cds_lfht_iter_get_node(&iter);
875 if (!node) {
876 DBG("[notification-thread] No known session matching name \"%s\"",
877 session_name);
878 ret = 0;
879 goto end;
880 }
881
882 session_info = caa_container_of(node, struct session_info,
883 sessions_ht_node);
884 session_info_get(session_info);
885
886 /*
887 * Evaluation is performed in-line here since only one type of
888 * session-bound condition is handled for the moment.
889 */
890 switch (lttng_condition_get_type(condition)) {
891 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
892 if (!session_info->rotation.ongoing) {
893 ret = 0;
894 goto end_session_put;
895 }
896
897 *evaluation = lttng_evaluation_session_rotation_ongoing_create(
898 session_info->rotation.id);
899 if (!*evaluation) {
900 /* Fatal error. */
901 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
902 session_info->name);
903 ret = -1;
904 goto end_session_put;
905 }
906 ret = 0;
907 break;
908 default:
909 ret = 0;
910 goto end_session_put;
911 }
912
913 *session_uid = session_info->uid;
914 *session_gid = session_info->gid;
915
916 end_session_put:
917 session_info_put(session_info);
918 end:
919 rcu_read_unlock();
920 return ret;
921 }
922
923 static
924 int evaluate_condition_for_client(const struct lttng_trigger *trigger,
925 const struct lttng_condition *condition,
926 struct notification_client *client,
927 struct notification_thread_state *state)
928 {
929 int ret;
930 struct lttng_evaluation *evaluation = NULL;
931 struct notification_client_list client_list = {
932 .lock = PTHREAD_MUTEX_INITIALIZER,
933 };
934 struct notification_client_list_element client_list_element = { 0 };
935 uid_t object_uid = 0;
936 gid_t object_gid = 0;
937
938 assert(trigger);
939 assert(condition);
940 assert(client);
941 assert(state);
942
943 switch (get_condition_binding_object(condition)) {
944 case LTTNG_OBJECT_TYPE_SESSION:
945 ret = evaluate_session_condition_for_client(condition, state,
946 &evaluation, &object_uid, &object_gid);
947 break;
948 case LTTNG_OBJECT_TYPE_CHANNEL:
949 ret = evaluate_channel_condition_for_client(condition, state,
950 &evaluation, &object_uid, &object_gid);
951 break;
952 case LTTNG_OBJECT_TYPE_NONE:
953 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
954 ret = 0;
955 goto end;
956 case LTTNG_OBJECT_TYPE_UNKNOWN:
957 default:
958 ret = -1;
959 goto end;
960 }
961 if (ret) {
962 /* Fatal error. */
963 goto end;
964 }
965 if (!evaluation) {
966 /* Evaluation yielded nothing. Normal exit. */
967 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
968 ret = 0;
969 goto end;
970 }
971
972 /*
973 * Create a temporary client list with the client currently
974 * subscribing.
975 */
976 cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node);
977 CDS_INIT_LIST_HEAD(&client_list.list);
978 client_list.trigger = trigger;
979
980 CDS_INIT_LIST_HEAD(&client_list_element.node);
981 client_list_element.client = client;
982 cds_list_add(&client_list_element.node, &client_list.list);
983
984 /* Send evaluation result to the newly-subscribed client. */
985 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
986 ret = send_evaluation_to_clients(trigger, evaluation, &client_list,
987 state, object_uid, object_gid);
988
989 end:
990 return ret;
991 }
992
993 static
994 int notification_thread_client_subscribe(struct notification_client *client,
995 struct lttng_condition *condition,
996 struct notification_thread_state *state,
997 enum lttng_notification_channel_status *_status)
998 {
999 int ret = 0;
1000 struct notification_client_list *client_list = NULL;
1001 struct lttng_condition_list_element *condition_list_element = NULL;
1002 struct notification_client_list_element *client_list_element = NULL;
1003 enum lttng_notification_channel_status status =
1004 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1005
1006 /*
1007 * Ensure that the client has not already subscribed to this condition
1008 * before.
1009 */
1010 cds_list_for_each_entry(condition_list_element, &client->condition_list, node) {
1011 if (lttng_condition_is_equal(condition_list_element->condition,
1012 condition)) {
1013 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED;
1014 goto end;
1015 }
1016 }
1017
1018 condition_list_element = zmalloc(sizeof(*condition_list_element));
1019 if (!condition_list_element) {
1020 ret = -1;
1021 goto error;
1022 }
1023 client_list_element = zmalloc(sizeof(*client_list_element));
1024 if (!client_list_element) {
1025 ret = -1;
1026 goto error;
1027 }
1028
1029 /*
1030 * Add the newly-subscribed condition to the client's subscription list.
1031 */
1032 CDS_INIT_LIST_HEAD(&condition_list_element->node);
1033 condition_list_element->condition = condition;
1034 cds_list_add(&condition_list_element->node, &client->condition_list);
1035
1036 client_list = get_client_list_from_condition(state, condition);
1037 if (!client_list) {
1038 /*
1039 * No notification-emiting trigger registered with this
1040 * condition. We don't evaluate the condition right away
1041 * since this trigger is not registered yet.
1042 */
1043 free(client_list_element);
1044 goto end;
1045 }
1046
1047 /*
1048 * The condition to which the client just subscribed is evaluated
1049 * at this point so that conditions that are already TRUE result
1050 * in a notification being sent out.
1051 *
1052 * The client_list's trigger is used without locking the list itself.
1053 * This is correct since the list doesn't own the trigger and the
1054 * object is immutable.
1055 */
1056 if (evaluate_condition_for_client(client_list->trigger, condition,
1057 client, state)) {
1058 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1059 ret = -1;
1060 free(client_list_element);
1061 goto end;
1062 }
1063
1064 /*
1065 * Add the client to the list of clients interested in a given trigger
1066 * if a "notification" trigger with a corresponding condition was
1067 * added prior.
1068 */
1069 client_list_element->client = client;
1070 CDS_INIT_LIST_HEAD(&client_list_element->node);
1071
1072 pthread_mutex_lock(&client_list->lock);
1073 cds_list_add(&client_list_element->node, &client_list->list);
1074 pthread_mutex_unlock(&client_list->lock);
1075 end:
1076 if (_status) {
1077 *_status = status;
1078 }
1079 if (client_list) {
1080 notification_client_list_put(client_list);
1081 }
1082 return ret;
1083 error:
1084 free(condition_list_element);
1085 free(client_list_element);
1086 return ret;
1087 }
1088
1089 static
1090 int notification_thread_client_unsubscribe(
1091 struct notification_client *client,
1092 struct lttng_condition *condition,
1093 struct notification_thread_state *state,
1094 enum lttng_notification_channel_status *_status)
1095 {
1096 struct notification_client_list *client_list;
1097 struct lttng_condition_list_element *condition_list_element,
1098 *condition_tmp;
1099 struct notification_client_list_element *client_list_element,
1100 *client_tmp;
1101 bool condition_found = false;
1102 enum lttng_notification_channel_status status =
1103 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1104
1105 /* Remove the condition from the client's condition list. */
1106 cds_list_for_each_entry_safe(condition_list_element, condition_tmp,
1107 &client->condition_list, node) {
1108 if (!lttng_condition_is_equal(condition_list_element->condition,
1109 condition)) {
1110 continue;
1111 }
1112
1113 cds_list_del(&condition_list_element->node);
1114 /*
1115 * The caller may be iterating on the client's conditions to
1116 * tear down a client's connection. In this case, the condition
1117 * will be destroyed at the end.
1118 */
1119 if (condition != condition_list_element->condition) {
1120 lttng_condition_destroy(
1121 condition_list_element->condition);
1122 }
1123 free(condition_list_element);
1124 condition_found = true;
1125 break;
1126 }
1127
1128 if (!condition_found) {
1129 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
1130 goto end;
1131 }
1132
1133 /*
1134 * Remove the client from the list of clients interested the trigger
1135 * matching the condition.
1136 */
1137 client_list = get_client_list_from_condition(state, condition);
1138 if (!client_list) {
1139 goto end;
1140 }
1141
1142 pthread_mutex_lock(&client_list->lock);
1143 cds_list_for_each_entry_safe(client_list_element, client_tmp,
1144 &client_list->list, node) {
1145 if (client_list_element->client->id != client->id) {
1146 continue;
1147 }
1148 cds_list_del(&client_list_element->node);
1149 free(client_list_element);
1150 break;
1151 }
1152 pthread_mutex_unlock(&client_list->lock);
1153 notification_client_list_put(client_list);
1154 client_list = NULL;
1155 end:
1156 lttng_condition_destroy(condition);
1157 if (_status) {
1158 *_status = status;
1159 }
1160 return 0;
1161 }
1162
1163 static
1164 void free_notification_client_rcu(struct rcu_head *node)
1165 {
1166 free(caa_container_of(node, struct notification_client, rcu_node));
1167 }
1168
1169 static
1170 void notification_client_destroy(struct notification_client *client,
1171 struct notification_thread_state *state)
1172 {
1173 if (!client) {
1174 return;
1175 }
1176
1177 /*
1178 * The client object is not reachable by other threads, no need to lock
1179 * the client here.
1180 */
1181 if (client->socket >= 0) {
1182 (void) lttcomm_close_unix_sock(client->socket);
1183 client->socket = -1;
1184 }
1185 client->communication.active = false;
1186 lttng_payload_reset(&client->communication.inbound.payload);
1187 lttng_payload_reset(&client->communication.outbound.payload);
1188 pthread_mutex_destroy(&client->lock);
1189 call_rcu(&client->rcu_node, free_notification_client_rcu);
1190 }
1191
1192 /*
1193 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1194 * client pointer).
1195 */
1196 static
1197 struct notification_client *get_client_from_socket(int socket,
1198 struct notification_thread_state *state)
1199 {
1200 struct cds_lfht_iter iter;
1201 struct cds_lfht_node *node;
1202 struct notification_client *client = NULL;
1203
1204 cds_lfht_lookup(state->client_socket_ht,
1205 hash_client_socket(socket),
1206 match_client_socket,
1207 (void *) (unsigned long) socket,
1208 &iter);
1209 node = cds_lfht_iter_get_node(&iter);
1210 if (!node) {
1211 goto end;
1212 }
1213
1214 client = caa_container_of(node, struct notification_client,
1215 client_socket_ht_node);
1216 end:
1217 return client;
1218 }
1219
1220 /*
1221 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1222 * client pointer).
1223 */
1224 static
1225 struct notification_client *get_client_from_id(notification_client_id id,
1226 struct notification_thread_state *state)
1227 {
1228 struct cds_lfht_iter iter;
1229 struct cds_lfht_node *node;
1230 struct notification_client *client = NULL;
1231
1232 cds_lfht_lookup(state->client_id_ht,
1233 hash_client_id(id),
1234 match_client_id,
1235 &id,
1236 &iter);
1237 node = cds_lfht_iter_get_node(&iter);
1238 if (!node) {
1239 goto end;
1240 }
1241
1242 client = caa_container_of(node, struct notification_client,
1243 client_id_ht_node);
1244 end:
1245 return client;
1246 }
1247
1248 static
1249 bool buffer_usage_condition_applies_to_channel(
1250 const struct lttng_condition *condition,
1251 const struct channel_info *channel_info)
1252 {
1253 enum lttng_condition_status status;
1254 enum lttng_domain_type condition_domain;
1255 const char *condition_session_name = NULL;
1256 const char *condition_channel_name = NULL;
1257
1258 status = lttng_condition_buffer_usage_get_domain_type(condition,
1259 &condition_domain);
1260 assert(status == LTTNG_CONDITION_STATUS_OK);
1261 if (channel_info->key.domain != condition_domain) {
1262 goto fail;
1263 }
1264
1265 status = lttng_condition_buffer_usage_get_session_name(
1266 condition, &condition_session_name);
1267 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1268
1269 status = lttng_condition_buffer_usage_get_channel_name(
1270 condition, &condition_channel_name);
1271 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name);
1272
1273 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1274 goto fail;
1275 }
1276 if (strcmp(channel_info->name, condition_channel_name)) {
1277 goto fail;
1278 }
1279
1280 return true;
1281 fail:
1282 return false;
1283 }
1284
1285 static
1286 bool session_consumed_size_condition_applies_to_channel(
1287 const struct lttng_condition *condition,
1288 const struct channel_info *channel_info)
1289 {
1290 enum lttng_condition_status status;
1291 const char *condition_session_name = NULL;
1292
1293 status = lttng_condition_session_consumed_size_get_session_name(
1294 condition, &condition_session_name);
1295 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1296
1297 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1298 goto fail;
1299 }
1300
1301 return true;
1302 fail:
1303 return false;
1304 }
1305
1306 static
1307 bool trigger_applies_to_channel(const struct lttng_trigger *trigger,
1308 const struct channel_info *channel_info)
1309 {
1310 const struct lttng_condition *condition;
1311 bool trigger_applies;
1312
1313 condition = lttng_trigger_get_const_condition(trigger);
1314 if (!condition) {
1315 goto fail;
1316 }
1317
1318 switch (lttng_condition_get_type(condition)) {
1319 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1320 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1321 trigger_applies = buffer_usage_condition_applies_to_channel(
1322 condition, channel_info);
1323 break;
1324 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1325 trigger_applies = session_consumed_size_condition_applies_to_channel(
1326 condition, channel_info);
1327 break;
1328 default:
1329 goto fail;
1330 }
1331
1332 return trigger_applies;
1333 fail:
1334 return false;
1335 }
1336
1337 static
1338 bool trigger_applies_to_client(struct lttng_trigger *trigger,
1339 struct notification_client *client)
1340 {
1341 bool applies = false;
1342 struct lttng_condition_list_element *condition_list_element;
1343
1344 cds_list_for_each_entry(condition_list_element, &client->condition_list,
1345 node) {
1346 applies = lttng_condition_is_equal(
1347 condition_list_element->condition,
1348 lttng_trigger_get_condition(trigger));
1349 if (applies) {
1350 break;
1351 }
1352 }
1353 return applies;
1354 }
1355
1356 /* Must be called with RCU read lock held. */
1357 static
1358 struct lttng_session_trigger_list *get_session_trigger_list(
1359 struct notification_thread_state *state,
1360 const char *session_name)
1361 {
1362 struct lttng_session_trigger_list *list = NULL;
1363 struct cds_lfht_node *node;
1364 struct cds_lfht_iter iter;
1365
1366 cds_lfht_lookup(state->session_triggers_ht,
1367 hash_key_str(session_name, lttng_ht_seed),
1368 match_session_trigger_list,
1369 session_name,
1370 &iter);
1371 node = cds_lfht_iter_get_node(&iter);
1372 if (!node) {
1373 /*
1374 * Not an error, the list of triggers applying to that session
1375 * will be initialized when the session is created.
1376 */
1377 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1378 session_name);
1379 goto end;
1380 }
1381
1382 list = caa_container_of(node,
1383 struct lttng_session_trigger_list,
1384 session_triggers_ht_node);
1385 end:
1386 return list;
1387 }
1388
1389 /*
1390 * Allocate an empty lttng_session_trigger_list for the session named
1391 * 'session_name'.
1392 *
1393 * No ownership of 'session_name' is assumed by the session trigger list.
1394 * It is the caller's responsability to ensure the session name is alive
1395 * for as long as this list is.
1396 */
1397 static
1398 struct lttng_session_trigger_list *lttng_session_trigger_list_create(
1399 const char *session_name,
1400 struct cds_lfht *session_triggers_ht)
1401 {
1402 struct lttng_session_trigger_list *list;
1403
1404 list = zmalloc(sizeof(*list));
1405 if (!list) {
1406 goto end;
1407 }
1408 list->session_name = session_name;
1409 CDS_INIT_LIST_HEAD(&list->list);
1410 cds_lfht_node_init(&list->session_triggers_ht_node);
1411 list->session_triggers_ht = session_triggers_ht;
1412
1413 rcu_read_lock();
1414 /* Publish the list through the session_triggers_ht. */
1415 cds_lfht_add(session_triggers_ht,
1416 hash_key_str(session_name, lttng_ht_seed),
1417 &list->session_triggers_ht_node);
1418 rcu_read_unlock();
1419 end:
1420 return list;
1421 }
1422
1423 static
1424 void free_session_trigger_list_rcu(struct rcu_head *node)
1425 {
1426 free(caa_container_of(node, struct lttng_session_trigger_list,
1427 rcu_node));
1428 }
1429
1430 static
1431 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list)
1432 {
1433 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1434
1435 /* Empty the list element by element, and then free the list itself. */
1436 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1437 &list->list, node) {
1438 cds_list_del(&trigger_list_element->node);
1439 free(trigger_list_element);
1440 }
1441 rcu_read_lock();
1442 /* Unpublish the list from the session_triggers_ht. */
1443 cds_lfht_del(list->session_triggers_ht,
1444 &list->session_triggers_ht_node);
1445 rcu_read_unlock();
1446 call_rcu(&list->rcu_node, free_session_trigger_list_rcu);
1447 }
1448
1449 static
1450 int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
1451 struct lttng_trigger *trigger)
1452 {
1453 int ret = 0;
1454 struct lttng_trigger_list_element *new_element =
1455 zmalloc(sizeof(*new_element));
1456
1457 if (!new_element) {
1458 ret = -1;
1459 goto end;
1460 }
1461 CDS_INIT_LIST_HEAD(&new_element->node);
1462 new_element->trigger = trigger;
1463 cds_list_add(&new_element->node, &list->list);
1464 end:
1465 return ret;
1466 }
1467
1468 static
1469 bool trigger_applies_to_session(const struct lttng_trigger *trigger,
1470 const char *session_name)
1471 {
1472 bool applies = false;
1473 const struct lttng_condition *condition;
1474
1475 condition = lttng_trigger_get_const_condition(trigger);
1476 switch (lttng_condition_get_type(condition)) {
1477 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1478 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1479 {
1480 enum lttng_condition_status condition_status;
1481 const char *condition_session_name;
1482
1483 condition_status = lttng_condition_session_rotation_get_session_name(
1484 condition, &condition_session_name);
1485 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
1486 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1487 goto end;
1488 }
1489
1490 assert(condition_session_name);
1491 applies = !strcmp(condition_session_name, session_name);
1492 break;
1493 }
1494 default:
1495 goto end;
1496 }
1497 end:
1498 return applies;
1499 }
1500
1501 /*
1502 * Allocate and initialize an lttng_session_trigger_list which contains
1503 * all triggers that apply to the session named 'session_name'.
1504 *
1505 * No ownership of 'session_name' is assumed by the session trigger list.
1506 * It is the caller's responsability to ensure the session name is alive
1507 * for as long as this list is.
1508 */
1509 static
1510 struct lttng_session_trigger_list *lttng_session_trigger_list_build(
1511 const struct notification_thread_state *state,
1512 const char *session_name)
1513 {
1514 int trigger_count = 0;
1515 struct lttng_session_trigger_list *session_trigger_list = NULL;
1516 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1517 struct cds_lfht_iter iter;
1518
1519 session_trigger_list = lttng_session_trigger_list_create(session_name,
1520 state->session_triggers_ht);
1521
1522 /* Add all triggers applying to the session named 'session_name'. */
1523 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1524 node) {
1525 int ret;
1526
1527 if (!trigger_applies_to_session(trigger_ht_element->trigger,
1528 session_name)) {
1529 continue;
1530 }
1531
1532 ret = lttng_session_trigger_list_add(session_trigger_list,
1533 trigger_ht_element->trigger);
1534 if (ret) {
1535 goto error;
1536 }
1537
1538 trigger_count++;
1539 }
1540
1541 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1542 trigger_count);
1543 return session_trigger_list;
1544 error:
1545 lttng_session_trigger_list_destroy(session_trigger_list);
1546 return NULL;
1547 }
1548
1549 static
1550 struct session_info *find_or_create_session_info(
1551 struct notification_thread_state *state,
1552 const char *name, uid_t uid, gid_t gid)
1553 {
1554 struct session_info *session = NULL;
1555 struct cds_lfht_node *node;
1556 struct cds_lfht_iter iter;
1557 struct lttng_session_trigger_list *trigger_list;
1558
1559 rcu_read_lock();
1560 cds_lfht_lookup(state->sessions_ht,
1561 hash_key_str(name, lttng_ht_seed),
1562 match_session,
1563 name,
1564 &iter);
1565 node = cds_lfht_iter_get_node(&iter);
1566 if (node) {
1567 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1568 name, uid, gid);
1569 session = caa_container_of(node, struct session_info,
1570 sessions_ht_node);
1571 assert(session->uid == uid);
1572 assert(session->gid == gid);
1573 session_info_get(session);
1574 goto end;
1575 }
1576
1577 trigger_list = lttng_session_trigger_list_build(state, name);
1578 if (!trigger_list) {
1579 goto error;
1580 }
1581
1582 session = session_info_create(name, uid, gid, trigger_list,
1583 state->sessions_ht);
1584 if (!session) {
1585 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1586 name, uid, gid);
1587 lttng_session_trigger_list_destroy(trigger_list);
1588 goto error;
1589 }
1590 trigger_list = NULL;
1591
1592 cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed),
1593 &session->sessions_ht_node);
1594 end:
1595 rcu_read_unlock();
1596 return session;
1597 error:
1598 rcu_read_unlock();
1599 session_info_put(session);
1600 return NULL;
1601 }
1602
1603 static
1604 int handle_notification_thread_command_add_channel(
1605 struct notification_thread_state *state,
1606 const char *session_name, uid_t session_uid, gid_t session_gid,
1607 const char *channel_name, enum lttng_domain_type channel_domain,
1608 uint64_t channel_key_int, uint64_t channel_capacity,
1609 enum lttng_error_code *cmd_result)
1610 {
1611 struct cds_list_head trigger_list;
1612 struct channel_info *new_channel_info = NULL;
1613 struct channel_key channel_key = {
1614 .key = channel_key_int,
1615 .domain = channel_domain,
1616 };
1617 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
1618 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1619 int trigger_count = 0;
1620 struct cds_lfht_iter iter;
1621 struct session_info *session_info = NULL;
1622
1623 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain",
1624 channel_name, session_name, channel_key_int,
1625 channel_domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
1626
1627 CDS_INIT_LIST_HEAD(&trigger_list);
1628
1629 session_info = find_or_create_session_info(state, session_name,
1630 session_uid, session_gid);
1631 if (!session_info) {
1632 /* Allocation error or an internal error occurred. */
1633 goto error;
1634 }
1635
1636 new_channel_info = channel_info_create(channel_name, &channel_key,
1637 channel_capacity, session_info);
1638 if (!new_channel_info) {
1639 goto error;
1640 }
1641
1642 rcu_read_lock();
1643 /* Build a list of all triggers applying to the new channel. */
1644 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1645 node) {
1646 struct lttng_trigger_list_element *new_element;
1647
1648 if (!trigger_applies_to_channel(trigger_ht_element->trigger,
1649 new_channel_info)) {
1650 continue;
1651 }
1652
1653 new_element = zmalloc(sizeof(*new_element));
1654 if (!new_element) {
1655 rcu_read_unlock();
1656 goto error;
1657 }
1658 CDS_INIT_LIST_HEAD(&new_element->node);
1659 new_element->trigger = trigger_ht_element->trigger;
1660 cds_list_add(&new_element->node, &trigger_list);
1661 trigger_count++;
1662 }
1663 rcu_read_unlock();
1664
1665 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1666 trigger_count);
1667 channel_trigger_list = zmalloc(sizeof(*channel_trigger_list));
1668 if (!channel_trigger_list) {
1669 goto error;
1670 }
1671 channel_trigger_list->channel_key = new_channel_info->key;
1672 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
1673 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
1674 cds_list_splice(&trigger_list, &channel_trigger_list->list);
1675
1676 rcu_read_lock();
1677 /* Add channel to the channel_ht which owns the channel_infos. */
1678 cds_lfht_add(state->channels_ht,
1679 hash_channel_key(&new_channel_info->key),
1680 &new_channel_info->channels_ht_node);
1681 /*
1682 * Add the list of triggers associated with this channel to the
1683 * channel_triggers_ht.
1684 */
1685 cds_lfht_add(state->channel_triggers_ht,
1686 hash_channel_key(&new_channel_info->key),
1687 &channel_trigger_list->channel_triggers_ht_node);
1688 rcu_read_unlock();
1689 session_info_put(session_info);
1690 *cmd_result = LTTNG_OK;
1691 return 0;
1692 error:
1693 channel_info_destroy(new_channel_info);
1694 session_info_put(session_info);
1695 return 1;
1696 }
1697
1698 static
1699 void free_channel_trigger_list_rcu(struct rcu_head *node)
1700 {
1701 free(caa_container_of(node, struct lttng_channel_trigger_list,
1702 rcu_node));
1703 }
1704
1705 static
1706 void free_channel_state_sample_rcu(struct rcu_head *node)
1707 {
1708 free(caa_container_of(node, struct channel_state_sample,
1709 rcu_node));
1710 }
1711
1712 static
1713 int handle_notification_thread_command_remove_channel(
1714 struct notification_thread_state *state,
1715 uint64_t channel_key, enum lttng_domain_type domain,
1716 enum lttng_error_code *cmd_result)
1717 {
1718 struct cds_lfht_node *node;
1719 struct cds_lfht_iter iter;
1720 struct lttng_channel_trigger_list *trigger_list;
1721 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1722 struct channel_key key = { .key = channel_key, .domain = domain };
1723 struct channel_info *channel_info;
1724
1725 DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain",
1726 channel_key, domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
1727
1728 rcu_read_lock();
1729
1730 cds_lfht_lookup(state->channel_triggers_ht,
1731 hash_channel_key(&key),
1732 match_channel_trigger_list,
1733 &key,
1734 &iter);
1735 node = cds_lfht_iter_get_node(&iter);
1736 /*
1737 * There is a severe internal error if we are being asked to remove a
1738 * channel that doesn't exist.
1739 */
1740 if (!node) {
1741 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1742 goto end;
1743 }
1744
1745 /* Free the list of triggers associated with this channel. */
1746 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
1747 channel_triggers_ht_node);
1748 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1749 &trigger_list->list, node) {
1750 cds_list_del(&trigger_list_element->node);
1751 free(trigger_list_element);
1752 }
1753 cds_lfht_del(state->channel_triggers_ht, node);
1754 call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu);
1755
1756 /* Free sampled channel state. */
1757 cds_lfht_lookup(state->channel_state_ht,
1758 hash_channel_key(&key),
1759 match_channel_state_sample,
1760 &key,
1761 &iter);
1762 node = cds_lfht_iter_get_node(&iter);
1763 /*
1764 * This is expected to be NULL if the channel is destroyed before we
1765 * received a sample.
1766 */
1767 if (node) {
1768 struct channel_state_sample *sample = caa_container_of(node,
1769 struct channel_state_sample,
1770 channel_state_ht_node);
1771
1772 cds_lfht_del(state->channel_state_ht, node);
1773 call_rcu(&sample->rcu_node, free_channel_state_sample_rcu);
1774 }
1775
1776 /* Remove the channel from the channels_ht and free it. */
1777 cds_lfht_lookup(state->channels_ht,
1778 hash_channel_key(&key),
1779 match_channel_info,
1780 &key,
1781 &iter);
1782 node = cds_lfht_iter_get_node(&iter);
1783 assert(node);
1784 channel_info = caa_container_of(node, struct channel_info,
1785 channels_ht_node);
1786 cds_lfht_del(state->channels_ht, node);
1787 channel_info_destroy(channel_info);
1788 end:
1789 rcu_read_unlock();
1790 *cmd_result = LTTNG_OK;
1791 return 0;
1792 }
1793
1794 static
1795 int handle_notification_thread_command_session_rotation(
1796 struct notification_thread_state *state,
1797 enum notification_thread_command_type cmd_type,
1798 const char *session_name, uid_t session_uid, gid_t session_gid,
1799 uint64_t trace_archive_chunk_id,
1800 struct lttng_trace_archive_location *location,
1801 enum lttng_error_code *_cmd_result)
1802 {
1803 int ret = 0;
1804 enum lttng_error_code cmd_result = LTTNG_OK;
1805 struct lttng_session_trigger_list *trigger_list;
1806 struct lttng_trigger_list_element *trigger_list_element;
1807 struct session_info *session_info;
1808 const struct lttng_credentials session_creds = {
1809 .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid),
1810 .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid),
1811 };
1812
1813 rcu_read_lock();
1814
1815 session_info = find_or_create_session_info(state, session_name,
1816 session_uid, session_gid);
1817 if (!session_info) {
1818 /* Allocation error or an internal error occurred. */
1819 ret = -1;
1820 cmd_result = LTTNG_ERR_NOMEM;
1821 goto end;
1822 }
1823
1824 session_info->rotation.ongoing =
1825 cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
1826 session_info->rotation.id = trace_archive_chunk_id;
1827 trigger_list = get_session_trigger_list(state, session_name);
1828 if (!trigger_list) {
1829 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1830 session_name);
1831 goto end;
1832 }
1833
1834 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
1835 node) {
1836 const struct lttng_condition *condition;
1837 struct lttng_trigger *trigger;
1838 struct notification_client_list *client_list;
1839 struct lttng_evaluation *evaluation = NULL;
1840 enum lttng_condition_type condition_type;
1841 enum action_executor_status executor_status;
1842
1843 trigger = trigger_list_element->trigger;
1844 condition = lttng_trigger_get_const_condition(trigger);
1845 assert(condition);
1846 condition_type = lttng_condition_get_type(condition);
1847
1848 if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING &&
1849 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1850 continue;
1851 } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED &&
1852 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) {
1853 continue;
1854 }
1855
1856 client_list = get_client_list_from_condition(state, condition);
1857 if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1858 evaluation = lttng_evaluation_session_rotation_ongoing_create(
1859 trace_archive_chunk_id);
1860 } else {
1861 evaluation = lttng_evaluation_session_rotation_completed_create(
1862 trace_archive_chunk_id, location);
1863 }
1864
1865 if (!evaluation) {
1866 /* Internal error */
1867 ret = -1;
1868 cmd_result = LTTNG_ERR_UNK;
1869 goto put_list;
1870 }
1871
1872 /*
1873 * Ownership of `evaluation` transferred to the action executor
1874 * no matter the result.
1875 */
1876 executor_status = action_executor_enqueue(state->executor,
1877 trigger, evaluation, &session_creds,
1878 client_list);
1879 evaluation = NULL;
1880 switch (executor_status) {
1881 case ACTION_EXECUTOR_STATUS_OK:
1882 break;
1883 case ACTION_EXECUTOR_STATUS_ERROR:
1884 case ACTION_EXECUTOR_STATUS_INVALID:
1885 /*
1886 * TODO Add trigger identification (name/id) when
1887 * it is added to the API.
1888 */
1889 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1890 ret = -1;
1891 goto put_list;
1892 case ACTION_EXECUTOR_STATUS_OVERFLOW:
1893 /*
1894 * TODO Add trigger identification (name/id) when
1895 * it is added to the API.
1896 *
1897 * Not a fatal error.
1898 */
1899 WARN("No space left when enqueuing action associated with session rotation trigger");
1900 ret = 0;
1901 goto put_list;
1902 default:
1903 abort();
1904 }
1905
1906 put_list:
1907 notification_client_list_put(client_list);
1908 if (caa_unlikely(ret)) {
1909 break;
1910 }
1911 }
1912 end:
1913 session_info_put(session_info);
1914 *_cmd_result = cmd_result;
1915 rcu_read_unlock();
1916 return ret;
1917 }
1918
1919 static
1920 int handle_notification_thread_command_add_tracer_event_source(
1921 struct notification_thread_state *state,
1922 int tracer_event_source_fd,
1923 enum lttng_domain_type domain_type,
1924 enum lttng_error_code *_cmd_result)
1925 {
1926 int ret = 0;
1927 enum lttng_error_code cmd_result = LTTNG_OK;
1928 struct notification_event_tracer_event_source_element *element = NULL;
1929
1930 element = zmalloc(sizeof(*element));
1931 if (!element) {
1932 cmd_result = LTTNG_ERR_NOMEM;
1933 ret = -1;
1934 goto end;
1935 }
1936
1937 element->fd = tracer_event_source_fd;
1938 element->domain = domain_type;
1939
1940 cds_list_add(&element->node, &state->tracer_event_sources_list);
1941
1942 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1943 tracer_event_source_fd,
1944 lttng_domain_type_str(domain_type));
1945
1946 /* Adding the read side pipe to the event poll. */
1947 ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR);
1948 if (ret < 0) {
1949 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1950 tracer_event_source_fd,
1951 lttng_domain_type_str(element->domain));
1952 cds_list_del(&element->node);
1953 free(element);
1954 goto end;
1955 }
1956
1957 element->is_fd_in_poll_set = true;
1958
1959 end:
1960 *_cmd_result = cmd_result;
1961 return ret;
1962 }
1963
1964 static
1965 int handle_notification_thread_command_remove_tracer_event_source(
1966 struct notification_thread_state *state,
1967 int tracer_event_source_fd,
1968 enum lttng_error_code *_cmd_result)
1969 {
1970 int ret = 0;
1971 bool found = false;
1972 enum lttng_error_code cmd_result = LTTNG_OK;
1973 struct notification_event_tracer_event_source_element *source_element = NULL, *tmp;
1974
1975 cds_list_for_each_entry_safe(source_element, tmp,
1976 &state->tracer_event_sources_list, node) {
1977 if (source_element->fd != tracer_event_source_fd) {
1978 continue;
1979 }
1980
1981 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
1982 tracer_event_source_fd,
1983 lttng_domain_type_str(source_element->domain));
1984 cds_list_del(&source_element->node);
1985 found = true;
1986 break;
1987 }
1988
1989 if (!found) {
1990 /*
1991 * This is temporarily allowed since the poll activity set is
1992 * not properly cleaned-up for the moment. This is adressed in
1993 * an upcoming fix.
1994 */
1995 source_element = NULL;
1996 goto end;
1997 }
1998
1999 if (!source_element->is_fd_in_poll_set) {
2000 /* Skip the poll set removal. */
2001 goto end;
2002 }
2003
2004 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2005 tracer_event_source_fd,
2006 lttng_domain_type_str(source_element->domain));
2007
2008 /* Removing the fd from the event poll set. */
2009 ret = lttng_poll_del(&state->events, tracer_event_source_fd);
2010 if (ret < 0) {
2011 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2012 tracer_event_source_fd,
2013 lttng_domain_type_str(source_element->domain));
2014 cmd_result = LTTNG_ERR_FATAL;
2015 goto end;
2016 }
2017
2018 source_element->is_fd_in_poll_set = false;
2019
2020 end:
2021 free(source_element);
2022 *_cmd_result = cmd_result;
2023 return ret;
2024 }
2025
2026 int handle_notification_thread_remove_tracer_event_source_no_result(
2027 struct notification_thread_state *state,
2028 int tracer_event_source_fd)
2029 {
2030 int ret;
2031 enum lttng_error_code cmd_result;
2032
2033 ret = handle_notification_thread_command_remove_tracer_event_source(
2034 state, tracer_event_source_fd, &cmd_result);
2035 (void) cmd_result;
2036 return ret;
2037 }
2038
2039 static int handle_notification_thread_command_list_triggers(
2040 struct notification_thread_handle *handle,
2041 struct notification_thread_state *state,
2042 uid_t client_uid,
2043 struct lttng_triggers **triggers,
2044 enum lttng_error_code *_cmd_result)
2045 {
2046 int ret = 0;
2047 enum lttng_error_code cmd_result = LTTNG_OK;
2048 struct cds_lfht_iter iter;
2049 struct lttng_trigger_ht_element *trigger_ht_element;
2050 struct lttng_triggers *local_triggers = NULL;
2051 const struct lttng_credentials *creds;
2052
2053 rcu_read_lock();
2054
2055 local_triggers = lttng_triggers_create();
2056 if (!local_triggers) {
2057 /* Not a fatal error. */
2058 cmd_result = LTTNG_ERR_NOMEM;
2059 goto end;
2060 }
2061
2062 cds_lfht_for_each_entry(state->triggers_ht, &iter,
2063 trigger_ht_element, node) {
2064 /*
2065 * Only return the triggers to which the client has access.
2066 * The root user has visibility over all triggers.
2067 */
2068 creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
2069 if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
2070 continue;
2071 }
2072
2073 ret = lttng_triggers_add(local_triggers,
2074 trigger_ht_element->trigger);
2075 if (ret < 0) {
2076 /* Not a fatal error. */
2077 ret = 0;
2078 cmd_result = LTTNG_ERR_NOMEM;
2079 goto end;
2080 }
2081 }
2082
2083 /* Transferring ownership to the caller. */
2084 *triggers = local_triggers;
2085 local_triggers = NULL;
2086
2087 end:
2088 rcu_read_unlock();
2089 lttng_triggers_destroy(local_triggers);
2090 *_cmd_result = cmd_result;
2091 return ret;
2092 }
2093
2094 static
2095 bool condition_is_supported(struct lttng_condition *condition)
2096 {
2097 bool is_supported;
2098
2099 switch (lttng_condition_get_type(condition)) {
2100 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2101 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2102 {
2103 int ret;
2104 enum lttng_domain_type domain;
2105
2106 ret = lttng_condition_buffer_usage_get_domain_type(condition,
2107 &domain);
2108 assert(ret == 0);
2109
2110 if (domain != LTTNG_DOMAIN_KERNEL) {
2111 is_supported = true;
2112 goto end;
2113 }
2114
2115 /*
2116 * Older kernel tracers don't expose the API to monitor their
2117 * buffers. Therefore, we reject triggers that require that
2118 * mechanism to be available to be evaluated.
2119 *
2120 * Assume unsupported on error.
2121 */
2122 is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2123 break;
2124 }
2125 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
2126 {
2127 const struct lttng_event_rule *event_rule;
2128 enum lttng_domain_type domain;
2129 const enum lttng_condition_status status =
2130 lttng_condition_event_rule_get_rule(
2131 condition, &event_rule);
2132
2133 assert(status == LTTNG_CONDITION_STATUS_OK);
2134
2135 domain = lttng_event_rule_get_domain_type(event_rule);
2136 if (domain != LTTNG_DOMAIN_KERNEL) {
2137 is_supported = true;
2138 goto end;
2139 }
2140
2141 /*
2142 * Older kernel tracers can't emit notification. Therefore, we
2143 * reject triggers that require that mechanism to be available
2144 * to be evaluated.
2145 *
2146 * Assume unsupported on error.
2147 */
2148 is_supported = kernel_supports_event_notifiers() == 1;
2149 break;
2150 }
2151 default:
2152 is_supported = true;
2153 }
2154 end:
2155 return is_supported;
2156 }
2157
2158 /* Must be called with RCU read lock held. */
2159 static
2160 int bind_trigger_to_matching_session(struct lttng_trigger *trigger,
2161 struct notification_thread_state *state)
2162 {
2163 int ret = 0;
2164 const struct lttng_condition *condition;
2165 const char *session_name;
2166 struct lttng_session_trigger_list *trigger_list;
2167
2168 condition = lttng_trigger_get_const_condition(trigger);
2169 switch (lttng_condition_get_type(condition)) {
2170 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
2171 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
2172 {
2173 enum lttng_condition_status status;
2174
2175 status = lttng_condition_session_rotation_get_session_name(
2176 condition, &session_name);
2177 if (status != LTTNG_CONDITION_STATUS_OK) {
2178 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2179 ret = -1;
2180 goto end;
2181 }
2182 break;
2183 }
2184 default:
2185 ret = -1;
2186 goto end;
2187 }
2188
2189 trigger_list = get_session_trigger_list(state, session_name);
2190 if (!trigger_list) {
2191 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2192 session_name);
2193 goto end;
2194
2195 }
2196
2197 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2198 session_name);
2199 ret = lttng_session_trigger_list_add(trigger_list, trigger);
2200 end:
2201 return ret;
2202 }
2203
2204 /* Must be called with RCU read lock held. */
2205 static
2206 int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
2207 struct notification_thread_state *state)
2208 {
2209 int ret = 0;
2210 struct cds_lfht_node *node;
2211 struct cds_lfht_iter iter;
2212 struct channel_info *channel;
2213
2214 cds_lfht_for_each_entry(state->channels_ht, &iter, channel,
2215 channels_ht_node) {
2216 struct lttng_trigger_list_element *trigger_list_element;
2217 struct lttng_channel_trigger_list *trigger_list;
2218 struct cds_lfht_iter lookup_iter;
2219
2220 if (!trigger_applies_to_channel(trigger, channel)) {
2221 continue;
2222 }
2223
2224 cds_lfht_lookup(state->channel_triggers_ht,
2225 hash_channel_key(&channel->key),
2226 match_channel_trigger_list,
2227 &channel->key,
2228 &lookup_iter);
2229 node = cds_lfht_iter_get_node(&lookup_iter);
2230 assert(node);
2231 trigger_list = caa_container_of(node,
2232 struct lttng_channel_trigger_list,
2233 channel_triggers_ht_node);
2234
2235 trigger_list_element = zmalloc(sizeof(*trigger_list_element));
2236 if (!trigger_list_element) {
2237 ret = -1;
2238 goto end;
2239 }
2240 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
2241 trigger_list_element->trigger = trigger;
2242 cds_list_add(&trigger_list_element->node, &trigger_list->list);
2243 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2244 channel->name);
2245 }
2246 end:
2247 return ret;
2248 }
2249
2250 static
2251 bool is_trigger_action_notify(const struct lttng_trigger *trigger)
2252 {
2253 bool is_notify = false;
2254 unsigned int i, count;
2255 enum lttng_action_status action_status;
2256 const struct lttng_action *action =
2257 lttng_trigger_get_const_action(trigger);
2258 enum lttng_action_type action_type;
2259
2260 assert(action);
2261 action_type = lttng_action_get_type(action);
2262 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2263 is_notify = true;
2264 goto end;
2265 } else if (action_type != LTTNG_ACTION_TYPE_GROUP) {
2266 goto end;
2267 }
2268
2269 action_status = lttng_action_group_get_count(action, &count);
2270 assert(action_status == LTTNG_ACTION_STATUS_OK);
2271
2272 for (i = 0; i < count; i++) {
2273 const struct lttng_action *inner_action =
2274 lttng_action_group_get_at_index(
2275 action, i);
2276
2277 action_type = lttng_action_get_type(inner_action);
2278 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2279 is_notify = true;
2280 goto end;
2281 }
2282 }
2283
2284 end:
2285 return is_notify;
2286 }
2287
2288 static bool trigger_name_taken(struct notification_thread_state *state,
2289 const struct lttng_trigger *trigger)
2290 {
2291 struct cds_lfht_iter iter;
2292
2293 /*
2294 * No duplicata is allowed in the triggers_by_name_uid_ht.
2295 * The match is done against the trigger name and uid.
2296 */
2297 cds_lfht_lookup(state->triggers_by_name_uid_ht,
2298 hash_trigger_by_name_uid(trigger),
2299 match_trigger_by_name_uid,
2300 trigger,
2301 &iter);
2302 return !!cds_lfht_iter_get_node(&iter);
2303 }
2304
2305 static
2306 enum lttng_error_code generate_trigger_name(
2307 struct notification_thread_state *state,
2308 struct lttng_trigger *trigger, const char **name)
2309 {
2310 enum lttng_error_code ret_code = LTTNG_OK;
2311 bool taken = false;
2312 enum lttng_trigger_status status;
2313
2314 do {
2315 const int ret = lttng_trigger_generate_name(trigger,
2316 state->trigger_id.name_offset++);
2317 if (ret) {
2318 /* The only reason this can fail right now. */
2319 ret_code = LTTNG_ERR_NOMEM;
2320 break;
2321 }
2322
2323 status = lttng_trigger_get_name(trigger, name);
2324 assert(status == LTTNG_TRIGGER_STATUS_OK);
2325
2326 taken = trigger_name_taken(state, trigger);
2327 } while (taken || state->trigger_id.name_offset == UINT64_MAX);
2328
2329 return ret_code;
2330 }
2331
2332 /*
2333 * FIXME A client's credentials are not checked when registering a trigger.
2334 *
2335 * The effects of this are benign since:
2336 * - The client will succeed in registering the trigger, as it is valid,
2337 * - The trigger will, internally, be bound to the channel/session,
2338 * - The notifications will not be sent since the client's credentials
2339 * are checked against the channel at that moment.
2340 *
2341 * If this function returns a non-zero value, it means something is
2342 * fundamentally broken and the whole subsystem/thread will be torn down.
2343 *
2344 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2345 * error code.
2346 */
2347 static
2348 int handle_notification_thread_command_register_trigger(
2349 struct notification_thread_state *state,
2350 struct lttng_trigger *trigger,
2351 enum lttng_error_code *cmd_result)
2352 {
2353 int ret = 0;
2354 struct lttng_condition *condition;
2355 struct notification_client *client;
2356 struct notification_client_list *client_list = NULL;
2357 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
2358 struct notification_client_list_element *client_list_element;
2359 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL;
2360 struct cds_lfht_node *node;
2361 struct cds_lfht_iter iter;
2362 const char* trigger_name;
2363 bool free_trigger = true;
2364 struct lttng_evaluation *evaluation = NULL;
2365 struct lttng_credentials object_creds;
2366 uid_t object_uid;
2367 gid_t object_gid;
2368 enum action_executor_status executor_status;
2369 const uint64_t trigger_tracer_token =
2370 state->trigger_id.next_tracer_token++;
2371
2372 rcu_read_lock();
2373
2374 /* Set the trigger's tracer token. */
2375 lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
2376
2377 if (lttng_trigger_get_name(trigger, &trigger_name) ==
2378 LTTNG_TRIGGER_STATUS_UNSET) {
2379 const enum lttng_error_code ret_code = generate_trigger_name(
2380 state, trigger, &trigger_name);
2381
2382 if (ret_code != LTTNG_OK) {
2383 /* Fatal error. */
2384 ret = -1;
2385 *cmd_result = ret_code;
2386 goto error;
2387 }
2388 } else if (trigger_name_taken(state, trigger)) {
2389 /* Not a fatal error. */
2390 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2391 ret = 0;
2392 goto error;
2393 }
2394
2395 condition = lttng_trigger_get_condition(trigger);
2396 assert(condition);
2397
2398 /* Some conditions require tracers to implement a minimal ABI version. */
2399 if (!condition_is_supported(condition)) {
2400 *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
2401 goto error;
2402 }
2403
2404 trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
2405 if (!trigger_ht_element) {
2406 ret = -1;
2407 goto error;
2408 }
2409
2410 /* Add trigger to the trigger_ht. */
2411 cds_lfht_node_init(&trigger_ht_element->node);
2412 cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
2413 trigger_ht_element->trigger = trigger;
2414
2415 node = cds_lfht_add_unique(state->triggers_ht,
2416 lttng_condition_hash(condition),
2417 match_trigger,
2418 trigger,
2419 &trigger_ht_element->node);
2420 if (node != &trigger_ht_element->node) {
2421 /* Not a fatal error, simply report it to the client. */
2422 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2423 goto error_free_ht_element;
2424 }
2425
2426 node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
2427 hash_trigger_by_name_uid(trigger),
2428 match_trigger_by_name_uid,
2429 trigger,
2430 &trigger_ht_element->node_by_name_uid);
2431 if (node != &trigger_ht_element->node_by_name_uid) {
2432 /* Not a fatal error, simply report it to the client. */
2433 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2434 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2435 goto error_free_ht_element;
2436 }
2437
2438 if (lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT) {
2439 trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element));
2440 if (!trigger_tokens_ht_element) {
2441 /* Fatal error. */
2442 ret = -1;
2443 cds_lfht_del(state->triggers_ht,
2444 &trigger_ht_element->node);
2445 cds_lfht_del(state->triggers_by_name_uid_ht,
2446 &trigger_ht_element->node_by_name_uid);
2447 goto error_free_ht_element;
2448 }
2449
2450 /* Add trigger token to the trigger_tokens_ht. */
2451 cds_lfht_node_init(&trigger_tokens_ht_element->node);
2452 trigger_tokens_ht_element->token =
2453 LTTNG_OPTIONAL_GET(trigger->tracer_token);
2454 trigger_tokens_ht_element->trigger = trigger;
2455
2456 node = cds_lfht_add_unique(state->trigger_tokens_ht,
2457 hash_key_u64(&trigger_tokens_ht_element->token,
2458 lttng_ht_seed),
2459 match_trigger_token,
2460 &trigger_tokens_ht_element->token,
2461 &trigger_tokens_ht_element->node);
2462 if (node != &trigger_tokens_ht_element->node) {
2463 /* Internal corruption, fatal error. */
2464 ret = -1;
2465 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2466 cds_lfht_del(state->triggers_ht,
2467 &trigger_ht_element->node);
2468 cds_lfht_del(state->triggers_by_name_uid_ht,
2469 &trigger_ht_element->node_by_name_uid);
2470 goto error_free_ht_element;
2471 }
2472 }
2473
2474 /*
2475 * Ownership of the trigger and of its wrapper was transfered to
2476 * the triggers_ht. Same for token ht element if necessary.
2477 */
2478 trigger_tokens_ht_element = NULL;
2479 trigger_ht_element = NULL;
2480 free_trigger = false;
2481
2482 /*
2483 * The rest only applies to triggers that have a "notify" action.
2484 * It is not skipped as this is the only action type currently
2485 * supported.
2486 */
2487 if (is_trigger_action_notify(trigger)) {
2488 client_list = notification_client_list_create(trigger);
2489 if (!client_list) {
2490 ret = -1;
2491 goto error_free_ht_element;
2492 }
2493
2494 /* Build a list of clients to which this new trigger applies. */
2495 cds_lfht_for_each_entry (state->client_socket_ht, &iter, client,
2496 client_socket_ht_node) {
2497 if (!trigger_applies_to_client(trigger, client)) {
2498 continue;
2499 }
2500
2501 client_list_element =
2502 zmalloc(sizeof(*client_list_element));
2503 if (!client_list_element) {
2504 ret = -1;
2505 goto error_put_client_list;
2506 }
2507
2508 CDS_INIT_LIST_HEAD(&client_list_element->node);
2509 client_list_element->client = client;
2510 cds_list_add(&client_list_element->node,
2511 &client_list->list);
2512 }
2513
2514 /*
2515 * Client list ownership transferred to the
2516 * notification_trigger_clients_ht.
2517 */
2518 publish_notification_client_list(state, client_list);
2519 }
2520
2521 switch (get_condition_binding_object(condition)) {
2522 case LTTNG_OBJECT_TYPE_SESSION:
2523 /* Add the trigger to the list if it matches a known session. */
2524 ret = bind_trigger_to_matching_session(trigger, state);
2525 if (ret) {
2526 goto error_put_client_list;
2527 }
2528 break;
2529 case LTTNG_OBJECT_TYPE_CHANNEL:
2530 /*
2531 * Add the trigger to list of triggers bound to the channels
2532 * currently known.
2533 */
2534 ret = bind_trigger_to_matching_channels(trigger, state);
2535 if (ret) {
2536 goto error_put_client_list;
2537 }
2538 break;
2539 case LTTNG_OBJECT_TYPE_NONE:
2540 break;
2541 default:
2542 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2543 ret = -1;
2544 goto error_put_client_list;
2545 }
2546
2547 /*
2548 * The new trigger's condition must be evaluated against the current
2549 * state.
2550 *
2551 * In the case of `notify` action, nothing preventing clients from
2552 * subscribing to a condition before the corresponding trigger is
2553 * registered, we have to evaluate this new condition right away.
2554 *
2555 * At some point, we were waiting for the next "evaluation" (e.g. on
2556 * reception of a channel sample) to evaluate this new condition, but
2557 * that was broken.
2558 *
2559 * The reason it was broken is that waiting for the next sample
2560 * does not allow us to properly handle transitions for edge-triggered
2561 * conditions.
2562 *
2563 * Consider this example: when we handle a new channel sample, we
2564 * evaluate each conditions twice: once with the previous state, and
2565 * again with the newest state. We then use those two results to
2566 * determine whether a state change happened: a condition was false and
2567 * became true. If a state change happened, we have to notify clients.
2568 *
2569 * Now, if a client subscribes to a given notification and registers
2570 * a trigger *after* that subscription, we have to make sure the
2571 * condition is evaluated at this point while considering only the
2572 * current state. Otherwise, the next evaluation cycle may only see
2573 * that the evaluations remain the same (true for samples n-1 and n) and
2574 * the client will never know that the condition has been met.
2575 */
2576 switch (get_condition_binding_object(condition)) {
2577 case LTTNG_OBJECT_TYPE_SESSION:
2578 ret = evaluate_session_condition_for_client(condition, state,
2579 &evaluation, &object_uid,
2580 &object_gid);
2581 break;
2582 case LTTNG_OBJECT_TYPE_CHANNEL:
2583 ret = evaluate_channel_condition_for_client(condition, state,
2584 &evaluation, &object_uid,
2585 &object_gid);
2586 break;
2587 case LTTNG_OBJECT_TYPE_NONE:
2588 ret = 0;
2589 break;
2590 case LTTNG_OBJECT_TYPE_UNKNOWN:
2591 default:
2592 ret = -1;
2593 break;
2594 }
2595
2596 if (ret) {
2597 /* Fatal error. */
2598 goto error_put_client_list;
2599 }
2600
2601 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2602 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
2603
2604 DBG("Newly registered trigger's condition evaluated to %s",
2605 evaluation ? "true" : "false");
2606 if (!evaluation) {
2607 /* Evaluation yielded nothing. Normal exit. */
2608 ret = 0;
2609 goto end;
2610 }
2611
2612 /*
2613 * Ownership of `evaluation` transferred to the action executor
2614 * no matter the result.
2615 */
2616 executor_status = action_executor_enqueue(state->executor, trigger,
2617 evaluation, &object_creds, client_list);
2618 evaluation = NULL;
2619 switch (executor_status) {
2620 case ACTION_EXECUTOR_STATUS_OK:
2621 break;
2622 case ACTION_EXECUTOR_STATUS_ERROR:
2623 case ACTION_EXECUTOR_STATUS_INVALID:
2624 /*
2625 * TODO Add trigger identification (name/id) when
2626 * it is added to the API.
2627 */
2628 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2629 ret = -1;
2630 goto error_put_client_list;
2631 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2632 /*
2633 * TODO Add trigger identification (name/id) when
2634 * it is added to the API.
2635 *
2636 * Not a fatal error.
2637 */
2638 WARN("No space left when enqueuing action associated to newly registered trigger");
2639 ret = 0;
2640 goto end;
2641 default:
2642 abort();
2643 }
2644
2645 end:
2646 *cmd_result = LTTNG_OK;
2647 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64,
2648 trigger_name, trigger_tracer_token);
2649
2650 error_put_client_list:
2651 notification_client_list_put(client_list);
2652
2653 error_free_ht_element:
2654 if (trigger_ht_element) {
2655 /* Delayed removal due to RCU constraint on delete. */
2656 call_rcu(&trigger_ht_element->rcu_node,
2657 free_lttng_trigger_ht_element_rcu);
2658 }
2659
2660 free(trigger_tokens_ht_element);
2661 error:
2662 if (free_trigger) {
2663 lttng_trigger_destroy(trigger);
2664 }
2665 rcu_read_unlock();
2666 return ret;
2667 }
2668
2669 static
2670 void free_lttng_trigger_ht_element_rcu(struct rcu_head *node)
2671 {
2672 free(caa_container_of(node, struct lttng_trigger_ht_element,
2673 rcu_node));
2674 }
2675
2676 static
2677 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node)
2678 {
2679 free(caa_container_of(node, struct notification_trigger_tokens_ht_element,
2680 rcu_node));
2681 }
2682
2683 static
2684 int handle_notification_thread_command_unregister_trigger(
2685 struct notification_thread_state *state,
2686 const struct lttng_trigger *trigger,
2687 enum lttng_error_code *_cmd_reply)
2688 {
2689 struct cds_lfht_iter iter;
2690 struct cds_lfht_node *triggers_ht_node;
2691 struct lttng_channel_trigger_list *trigger_list;
2692 struct notification_client_list *client_list;
2693 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
2694 const struct lttng_condition *condition = lttng_trigger_get_const_condition(
2695 trigger);
2696 enum lttng_error_code cmd_reply;
2697
2698 rcu_read_lock();
2699
2700 cds_lfht_lookup(state->triggers_ht,
2701 lttng_condition_hash(condition),
2702 match_trigger,
2703 trigger,
2704 &iter);
2705 triggers_ht_node = cds_lfht_iter_get_node(&iter);
2706 if (!triggers_ht_node) {
2707 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
2708 goto end;
2709 } else {
2710 cmd_reply = LTTNG_OK;
2711 }
2712
2713 /* Remove trigger from channel_triggers_ht. */
2714 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
2715 channel_triggers_ht_node) {
2716 struct lttng_trigger_list_element *trigger_element, *tmp;
2717
2718 cds_list_for_each_entry_safe(trigger_element, tmp,
2719 &trigger_list->list, node) {
2720 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
2721 continue;
2722 }
2723
2724 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2725 cds_list_del(&trigger_element->node);
2726 /* A trigger can only appear once per channel */
2727 break;
2728 }
2729 }
2730
2731 if (lttng_condition_get_type(condition) ==
2732 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT) {
2733 struct notification_trigger_tokens_ht_element
2734 *trigger_tokens_ht_element;
2735
2736 cds_lfht_for_each_entry (state->trigger_tokens_ht, &iter,
2737 trigger_tokens_ht_element, node) {
2738 if (!lttng_trigger_is_equal(trigger,
2739 trigger_tokens_ht_element->trigger)) {
2740 continue;
2741 }
2742
2743 DBG("[notification-thread] Removed trigger from tokens_ht");
2744 cds_lfht_del(state->trigger_tokens_ht,
2745 &trigger_tokens_ht_element->node);
2746 call_rcu(&trigger_tokens_ht_element->rcu_node,
2747 free_notification_trigger_tokens_ht_element_rcu);
2748
2749 break;
2750 }
2751 }
2752
2753 if (is_trigger_action_notify(trigger)) {
2754 /*
2755 * Remove and release the client list from
2756 * notification_trigger_clients_ht.
2757 */
2758 client_list = get_client_list_from_condition(state, condition);
2759 assert(client_list);
2760
2761 /* Put new reference and the hashtable's reference. */
2762 notification_client_list_put(client_list);
2763 notification_client_list_put(client_list);
2764 client_list = NULL;
2765 }
2766
2767 /* Remove trigger from triggers_ht. */
2768 trigger_ht_element = caa_container_of(triggers_ht_node,
2769 struct lttng_trigger_ht_element, node);
2770 cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
2771 cds_lfht_del(state->triggers_ht, triggers_ht_node);
2772
2773 /* Release the ownership of the trigger. */
2774 lttng_trigger_destroy(trigger_ht_element->trigger);
2775 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
2776 end:
2777 rcu_read_unlock();
2778 if (_cmd_reply) {
2779 *_cmd_reply = cmd_reply;
2780 }
2781 return 0;
2782 }
2783
2784 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2785 int handle_notification_thread_command(
2786 struct notification_thread_handle *handle,
2787 struct notification_thread_state *state)
2788 {
2789 int ret;
2790 uint64_t counter;
2791 struct notification_thread_command *cmd;
2792
2793 /* Read the event pipe to put it back into a quiescent state. */
2794 ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter,
2795 sizeof(counter));
2796 if (ret != sizeof(counter)) {
2797 goto error;
2798 }
2799
2800 pthread_mutex_lock(&handle->cmd_queue.lock);
2801 cmd = cds_list_first_entry(&handle->cmd_queue.list,
2802 struct notification_thread_command, cmd_list_node);
2803 cds_list_del(&cmd->cmd_list_node);
2804 pthread_mutex_unlock(&handle->cmd_queue.lock);
2805 switch (cmd->type) {
2806 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
2807 DBG("[notification-thread] Received register trigger command");
2808 ret = handle_notification_thread_command_register_trigger(state,
2809 cmd->parameters.register_trigger.trigger,
2810 &cmd->reply_code);
2811 break;
2812 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
2813 DBG("[notification-thread] Received unregister trigger command");
2814 ret = handle_notification_thread_command_unregister_trigger(
2815 state,
2816 cmd->parameters.unregister_trigger.trigger,
2817 &cmd->reply_code);
2818 break;
2819 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
2820 DBG("[notification-thread] Received add channel command");
2821 ret = handle_notification_thread_command_add_channel(
2822 state,
2823 cmd->parameters.add_channel.session.name,
2824 cmd->parameters.add_channel.session.uid,
2825 cmd->parameters.add_channel.session.gid,
2826 cmd->parameters.add_channel.channel.name,
2827 cmd->parameters.add_channel.channel.domain,
2828 cmd->parameters.add_channel.channel.key,
2829 cmd->parameters.add_channel.channel.capacity,
2830 &cmd->reply_code);
2831 break;
2832 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
2833 DBG("[notification-thread] Received remove channel command");
2834 ret = handle_notification_thread_command_remove_channel(
2835 state, cmd->parameters.remove_channel.key,
2836 cmd->parameters.remove_channel.domain,
2837 &cmd->reply_code);
2838 break;
2839 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
2840 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
2841 DBG("[notification-thread] Received session rotation %s command",
2842 cmd->type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING ?
2843 "ongoing" : "completed");
2844 ret = handle_notification_thread_command_session_rotation(
2845 state,
2846 cmd->type,
2847 cmd->parameters.session_rotation.session_name,
2848 cmd->parameters.session_rotation.uid,
2849 cmd->parameters.session_rotation.gid,
2850 cmd->parameters.session_rotation.trace_archive_chunk_id,
2851 cmd->parameters.session_rotation.location,
2852 &cmd->reply_code);
2853 break;
2854 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
2855 ret = handle_notification_thread_command_add_tracer_event_source(
2856 state,
2857 cmd->parameters.tracer_event_source.tracer_event_source_fd,
2858 cmd->parameters.tracer_event_source.domain,
2859 &cmd->reply_code);
2860 break;
2861 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
2862 ret = handle_notification_thread_command_remove_tracer_event_source(
2863 state,
2864 cmd->parameters.tracer_event_source.tracer_event_source_fd,
2865 &cmd->reply_code);
2866 break;
2867 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
2868 {
2869 struct lttng_triggers *triggers = NULL;
2870
2871 ret = handle_notification_thread_command_list_triggers(
2872 handle,
2873 state,
2874 cmd->parameters.list_triggers.uid,
2875 &triggers,
2876 &cmd->reply_code);
2877 cmd->reply.list_triggers.triggers = triggers;
2878 ret = 0;
2879 break;
2880 }
2881 case NOTIFICATION_COMMAND_TYPE_QUIT:
2882 DBG("[notification-thread] Received quit command");
2883 cmd->reply_code = LTTNG_OK;
2884 ret = 1;
2885 goto end;
2886 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
2887 {
2888 const enum client_transmission_status client_status =
2889 cmd->parameters.client_communication_update
2890 .status;
2891 const notification_client_id client_id =
2892 cmd->parameters.client_communication_update.id;
2893 struct notification_client *client;
2894
2895 rcu_read_lock();
2896 client = get_client_from_id(client_id, state);
2897
2898 if (!client) {
2899 /*
2900 * Client error was probably already picked-up by the
2901 * notification thread or it has disconnected
2902 * gracefully while this command was queued.
2903 */
2904 DBG("Failed to find notification client to update communication status, client id = %" PRIu64,
2905 client_id);
2906 ret = 0;
2907 } else {
2908 ret = client_handle_transmission_status(
2909 client, client_status, state);
2910 }
2911 rcu_read_unlock();
2912 break;
2913 }
2914 default:
2915 ERR("[notification-thread] Unknown internal command received");
2916 goto error_unlock;
2917 }
2918
2919 if (ret) {
2920 goto error_unlock;
2921 }
2922 end:
2923 if (cmd->is_async) {
2924 free(cmd);
2925 cmd = NULL;
2926 } else {
2927 lttng_waiter_wake_up(&cmd->reply_waiter);
2928 }
2929 return ret;
2930 error_unlock:
2931 /* Wake-up and return a fatal error to the calling thread. */
2932 lttng_waiter_wake_up(&cmd->reply_waiter);
2933 cmd->reply_code = LTTNG_ERR_FATAL;
2934 error:
2935 /* Indicate a fatal error to the caller. */
2936 return -1;
2937 }
2938
2939 static
2940 int socket_set_non_blocking(int socket)
2941 {
2942 int ret, flags;
2943
2944 /* Set the pipe as non-blocking. */
2945 ret = fcntl(socket, F_GETFL, 0);
2946 if (ret == -1) {
2947 PERROR("fcntl get socket flags");
2948 goto end;
2949 }
2950 flags = ret;
2951
2952 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
2953 if (ret == -1) {
2954 PERROR("fcntl set O_NONBLOCK socket flag");
2955 goto end;
2956 }
2957 DBG("Client socket (fd = %i) set as non-blocking", socket);
2958 end:
2959 return ret;
2960 }
2961
2962 static
2963 int client_reset_inbound_state(struct notification_client *client)
2964 {
2965 int ret;
2966
2967
2968 lttng_payload_clear(&client->communication.inbound.payload);
2969
2970 client->communication.inbound.bytes_to_receive =
2971 sizeof(struct lttng_notification_channel_message);
2972 client->communication.inbound.msg_type =
2973 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
2974 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
2975 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
2976 ret = lttng_dynamic_buffer_set_size(
2977 &client->communication.inbound.payload.buffer,
2978 client->communication.inbound.bytes_to_receive);
2979
2980 return ret;
2981 }
2982
2983 int handle_notification_thread_client_connect(
2984 struct notification_thread_state *state)
2985 {
2986 int ret;
2987 struct notification_client *client;
2988
2989 DBG("[notification-thread] Handling new notification channel client connection");
2990
2991 client = zmalloc(sizeof(*client));
2992 if (!client) {
2993 /* Fatal error. */
2994 ret = -1;
2995 goto error;
2996 }
2997
2998 pthread_mutex_init(&client->lock, NULL);
2999 client->id = state->next_notification_client_id++;
3000 CDS_INIT_LIST_HEAD(&client->condition_list);
3001 lttng_payload_init(&client->communication.inbound.payload);
3002 lttng_payload_init(&client->communication.outbound.payload);
3003 client->communication.inbound.expect_creds = true;
3004
3005 ret = client_reset_inbound_state(client);
3006 if (ret) {
3007 ERR("[notification-thread] Failed to reset client communication's inbound state");
3008 ret = 0;
3009 goto error;
3010 }
3011
3012 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
3013 if (ret < 0) {
3014 ERR("[notification-thread] Failed to accept new notification channel client connection");
3015 ret = 0;
3016 goto error;
3017 }
3018
3019 client->socket = ret;
3020
3021 ret = socket_set_non_blocking(client->socket);
3022 if (ret) {
3023 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3024 goto error;
3025 }
3026
3027 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
3028 if (ret < 0) {
3029 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3030 ret = 0;
3031 goto error;
3032 }
3033
3034 ret = lttng_poll_add(&state->events, client->socket,
3035 LPOLLIN | LPOLLERR |
3036 LPOLLHUP | LPOLLRDHUP);
3037 if (ret < 0) {
3038 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3039 ret = 0;
3040 goto error;
3041 }
3042 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3043 client->socket);