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