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