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