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