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