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