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