Add base support for event rule hit
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-events.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
10 #define _LGPL_SOURCE
11 #include <urcu.h>
12 #include <urcu/rculfhash.h>
13
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/notification/channel-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/event-rule/event-rule-internal.h>
33
34 #include <time.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <inttypes.h>
38 #include <fcntl.h>
39
40 #include "notification-thread.h"
41 #include "notification-thread-events.h"
42 #include "notification-thread-commands.h"
43 #include "lttng-sessiond.h"
44 #include "kernel.h"
45
46 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
47 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
48
49 enum lttng_object_type {
50 LTTNG_OBJECT_TYPE_UNKNOWN,
51 LTTNG_OBJECT_TYPE_NONE,
52 LTTNG_OBJECT_TYPE_CHANNEL,
53 LTTNG_OBJECT_TYPE_SESSION,
54 };
55
56 struct lttng_trigger_list_element {
57 /* No ownership of the trigger object is assumed. */
58 struct lttng_trigger *trigger;
59 struct cds_list_head node;
60 };
61
62 struct lttng_channel_trigger_list {
63 struct channel_key channel_key;
64 /* List of struct lttng_trigger_list_element. */
65 struct cds_list_head list;
66 /* Node in the channel_triggers_ht */
67 struct cds_lfht_node channel_triggers_ht_node;
68 /* call_rcu delayed reclaim. */
69 struct rcu_head rcu_node;
70 };
71
72 /*
73 * List of triggers applying to a given session.
74 *
75 * See:
76 * - lttng_session_trigger_list_create()
77 * - lttng_session_trigger_list_build()
78 * - lttng_session_trigger_list_destroy()
79 * - lttng_session_trigger_list_add()
80 */
81 struct lttng_session_trigger_list {
82 /*
83 * Not owned by this; points to the session_info structure's
84 * session name.
85 */
86 const char *session_name;
87 /* List of struct lttng_trigger_list_element. */
88 struct cds_list_head list;
89 /* Node in the session_triggers_ht */
90 struct cds_lfht_node session_triggers_ht_node;
91 /*
92 * Weak reference to the notification system's session triggers
93 * hashtable.
94 *
95 * The session trigger list structure structure is owned by
96 * the session's session_info.
97 *
98 * The session_info is kept alive the the channel_infos holding a
99 * reference to it (reference counting). When those channels are
100 * destroyed (at runtime or on teardown), the reference they hold
101 * to the session_info are released. On destruction of session_info,
102 * session_info_destroy() will remove the list of triggers applying
103 * to this session from the notification system's state.
104 *
105 * This implies that the session_triggers_ht must be destroyed
106 * after the channels.
107 */
108 struct cds_lfht *session_triggers_ht;
109 /* Used for delayed RCU reclaim. */
110 struct rcu_head rcu_node;
111 };
112
113 struct lttng_trigger_ht_element {
114 struct lttng_trigger *trigger;
115 struct cds_lfht_node node;
116 struct cds_lfht_node node_by_name_uid;
117 /* call_rcu delayed reclaim. */
118 struct rcu_head rcu_node;
119 };
120
121 struct lttng_condition_list_element {
122 struct lttng_condition *condition;
123 struct cds_list_head node;
124 };
125
126 struct channel_state_sample {
127 struct channel_key key;
128 struct cds_lfht_node channel_state_ht_node;
129 uint64_t highest_usage;
130 uint64_t lowest_usage;
131 uint64_t channel_total_consumed;
132 /* call_rcu delayed reclaim. */
133 struct rcu_head rcu_node;
134 };
135
136 static unsigned long hash_channel_key(struct channel_key *key);
137 static int evaluate_buffer_condition(const struct lttng_condition *condition,
138 struct lttng_evaluation **evaluation,
139 const struct notification_thread_state *state,
140 const struct channel_state_sample *previous_sample,
141 const struct channel_state_sample *latest_sample,
142 uint64_t previous_session_consumed_total,
143 uint64_t latest_session_consumed_total,
144 struct channel_info *channel_info);
145 static
146 int send_evaluation_to_clients(const struct lttng_trigger *trigger,
147 const struct lttng_evaluation *evaluation,
148 struct notification_client_list *client_list,
149 struct notification_thread_state *state,
150 uid_t channel_uid, gid_t channel_gid);
151
152
153 /* session_info API */
154 static
155 void session_info_destroy(void *_data);
156 static
157 void session_info_get(struct session_info *session_info);
158 static
159 void session_info_put(struct session_info *session_info);
160 static
161 struct session_info *session_info_create(const char *name,
162 uid_t uid, gid_t gid,
163 struct lttng_session_trigger_list *trigger_list,
164 struct cds_lfht *sessions_ht);
165 static
166 void session_info_add_channel(struct session_info *session_info,
167 struct channel_info *channel_info);
168 static
169 void session_info_remove_channel(struct session_info *session_info,
170 struct channel_info *channel_info);
171
172 /* lttng_session_trigger_list API */
173 static
174 struct lttng_session_trigger_list *lttng_session_trigger_list_create(
175 const char *session_name,
176 struct cds_lfht *session_triggers_ht);
177 static
178 struct lttng_session_trigger_list *lttng_session_trigger_list_build(
179 const struct notification_thread_state *state,
180 const char *session_name);
181 static
182 void lttng_session_trigger_list_destroy(
183 struct lttng_session_trigger_list *list);
184 static
185 int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
186 struct lttng_trigger *trigger);
187
188 static
189 int client_handle_transmission_status(
190 struct notification_client *client,
191 enum client_transmission_status transmission_status,
192 struct notification_thread_state *state);
193
194 static
195 void free_lttng_trigger_ht_element_rcu(struct rcu_head *node);
196
197 static
198 int match_client_socket(struct cds_lfht_node *node, const void *key)
199 {
200 /* This double-cast is intended to supress pointer-to-cast warning. */
201 const int socket = (int) (intptr_t) key;
202 const struct notification_client *client = caa_container_of(node,
203 struct notification_client, client_socket_ht_node);
204
205 return client->socket == socket;
206 }
207
208 static
209 int match_client_id(struct cds_lfht_node *node, const void *key)
210 {
211 /* This double-cast is intended to supress pointer-to-cast warning. */
212 const notification_client_id id = *((notification_client_id *) key);
213 const struct notification_client *client = caa_container_of(
214 node, struct notification_client, client_id_ht_node);
215
216 return client->id == id;
217 }
218
219 static
220 int match_channel_trigger_list(struct cds_lfht_node *node, const void *key)
221 {
222 struct channel_key *channel_key = (struct channel_key *) key;
223 struct lttng_channel_trigger_list *trigger_list;
224
225 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
226 channel_triggers_ht_node);
227
228 return !!((channel_key->key == trigger_list->channel_key.key) &&
229 (channel_key->domain == trigger_list->channel_key.domain));
230 }
231
232 static
233 int match_session_trigger_list(struct cds_lfht_node *node, const void *key)
234 {
235 const char *session_name = (const char *) key;
236 struct lttng_session_trigger_list *trigger_list;
237
238 trigger_list = caa_container_of(node, struct lttng_session_trigger_list,
239 session_triggers_ht_node);
240
241 return !!(strcmp(trigger_list->session_name, session_name) == 0);
242 }
243
244 static
245 int match_channel_state_sample(struct cds_lfht_node *node, const void *key)
246 {
247 struct channel_key *channel_key = (struct channel_key *) key;
248 struct channel_state_sample *sample;
249
250 sample = caa_container_of(node, struct channel_state_sample,
251 channel_state_ht_node);
252
253 return !!((channel_key->key == sample->key.key) &&
254 (channel_key->domain == sample->key.domain));
255 }
256
257 static
258 int match_channel_info(struct cds_lfht_node *node, const void *key)
259 {
260 struct channel_key *channel_key = (struct channel_key *) key;
261 struct channel_info *channel_info;
262
263 channel_info = caa_container_of(node, struct channel_info,
264 channels_ht_node);
265
266 return !!((channel_key->key == channel_info->key.key) &&
267 (channel_key->domain == channel_info->key.domain));
268 }
269
270 static
271 int match_trigger(struct cds_lfht_node *node, const void *key)
272 {
273 struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
274 struct lttng_trigger_ht_element *trigger_ht_element;
275
276 trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element,
277 node);
278
279 return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger);
280 }
281
282 static
283 int match_client_list_condition(struct cds_lfht_node *node, const void *key)
284 {
285 struct lttng_condition *condition_key = (struct lttng_condition *) key;
286 struct notification_client_list *client_list;
287 const struct lttng_condition *condition;
288
289 assert(condition_key);
290
291 client_list = caa_container_of(node, struct notification_client_list,
292 notification_trigger_clients_ht_node);
293 condition = lttng_trigger_get_const_condition(client_list->trigger);
294
295 return !!lttng_condition_is_equal(condition_key, condition);
296 }
297
298 static
299 int match_session(struct cds_lfht_node *node, const void *key)
300 {
301 const char *name = key;
302 struct session_info *session_info = caa_container_of(
303 node, struct session_info, sessions_ht_node);
304
305 return !strcmp(session_info->name, name);
306 }
307
308 /*
309 * Match trigger based on name and credentials only.
310 * Name duplication is NOT allowed for the same uid.
311 */
312 static
313 int match_trigger_by_name_uid(struct cds_lfht_node *node,
314 const void *key)
315 {
316 bool match = false;
317 const char *name;
318 const char *key_name;
319 enum lttng_trigger_status status;
320 const struct lttng_credentials *key_creds;
321 const struct lttng_credentials *node_creds;
322 const struct lttng_trigger *trigger_key =
323 (const struct lttng_trigger *) key;
324 const struct lttng_trigger_ht_element *trigger_ht_element =
325 caa_container_of(node,
326 struct lttng_trigger_ht_element,
327 node_by_name_uid);
328
329 status = lttng_trigger_get_name(trigger_ht_element->trigger, &name);
330 assert(status == LTTNG_TRIGGER_STATUS_OK);
331
332 status = lttng_trigger_get_name(trigger_key, &key_name);
333 assert(status == LTTNG_TRIGGER_STATUS_OK);
334
335 /* Compare the names. */
336 if (strcmp(name, key_name) != 0) {
337 goto end;
338 }
339
340 /* Compare the owners' UIDs. */
341 key_creds = lttng_trigger_get_credentials(trigger_key);
342 node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
343
344 match = lttng_credentials_is_equal_uid(key_creds, node_creds);
345
346 end:
347 return match;
348 }
349
350 /*
351 * Hash trigger based on name and credentials only.
352 */
353 static
354 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger)
355 {
356 unsigned long hash = 0;
357 const struct lttng_credentials *trigger_creds;
358 const char *trigger_name;
359 enum lttng_trigger_status status;
360
361 status = lttng_trigger_get_name(trigger, &trigger_name);
362 if (status == LTTNG_TRIGGER_STATUS_OK) {
363 hash = hash_key_str(trigger_name, lttng_ht_seed);
364 }
365
366 trigger_creds = lttng_trigger_get_credentials(trigger);
367 hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid),
368 lttng_ht_seed);
369
370 return hash;
371 }
372
373 static
374 unsigned long lttng_condition_buffer_usage_hash(
375 const struct lttng_condition *_condition)
376 {
377 unsigned long hash;
378 unsigned long condition_type;
379 struct lttng_condition_buffer_usage *condition;
380
381 condition = container_of(_condition,
382 struct lttng_condition_buffer_usage, parent);
383
384 condition_type = (unsigned long) condition->parent.type;
385 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
386 if (condition->session_name) {
387 hash ^= hash_key_str(condition->session_name, lttng_ht_seed);
388 }
389 if (condition->channel_name) {
390 hash ^= hash_key_str(condition->channel_name, lttng_ht_seed);
391 }
392 if (condition->domain.set) {
393 hash ^= hash_key_ulong(
394 (void *) condition->domain.type,
395 lttng_ht_seed);
396 }
397 if (condition->threshold_ratio.set) {
398 uint64_t val;
399
400 val = condition->threshold_ratio.value * (double) UINT32_MAX;
401 hash ^= hash_key_u64(&val, lttng_ht_seed);
402 } else if (condition->threshold_bytes.set) {
403 uint64_t val;
404
405 val = condition->threshold_bytes.value;
406 hash ^= hash_key_u64(&val, lttng_ht_seed);
407 }
408 return hash;
409 }
410
411 static
412 unsigned long lttng_condition_session_consumed_size_hash(
413 const struct lttng_condition *_condition)
414 {
415 unsigned long hash;
416 unsigned long condition_type =
417 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE;
418 struct lttng_condition_session_consumed_size *condition;
419 uint64_t val;
420
421 condition = container_of(_condition,
422 struct lttng_condition_session_consumed_size, parent);
423
424 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
425 if (condition->session_name) {
426 hash ^= hash_key_str(condition->session_name, lttng_ht_seed);
427 }
428 val = condition->consumed_threshold_bytes.value;
429 hash ^= hash_key_u64(&val, lttng_ht_seed);
430 return hash;
431 }
432
433 static
434 unsigned long lttng_condition_session_rotation_hash(
435 const struct lttng_condition *_condition)
436 {
437 unsigned long hash, condition_type;
438 struct lttng_condition_session_rotation *condition;
439
440 condition = container_of(_condition,
441 struct lttng_condition_session_rotation, parent);
442 condition_type = (unsigned long) condition->parent.type;
443 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
444 assert(condition->session_name);
445 hash ^= hash_key_str(condition->session_name, lttng_ht_seed);
446 return hash;
447 }
448
449 static
450 unsigned long lttng_condition_event_rule_hash(
451 const struct lttng_condition *condition)
452 {
453 unsigned long hash, condition_type;
454 enum lttng_condition_status condition_status;
455 const struct lttng_event_rule *event_rule;
456
457 condition_type = (unsigned long) condition->type;
458
459 condition_status = lttng_condition_event_rule_get_rule(condition,
460 &event_rule);
461 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
462
463 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
464 return hash ^ lttng_event_rule_hash(event_rule);
465 }
466
467 /*
468 * The lttng_condition hashing code is kept in this file (rather than
469 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
470 * don't want to link in liblttng-ctl.
471 */
472 static
473 unsigned long lttng_condition_hash(const struct lttng_condition *condition)
474 {
475 switch (condition->type) {
476 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
477 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
478 return lttng_condition_buffer_usage_hash(condition);
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
480 return lttng_condition_session_consumed_size_hash(condition);
481 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
483 return lttng_condition_session_rotation_hash(condition);
484 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
485 return lttng_condition_event_rule_hash(condition);
486 default:
487 ERR("[notification-thread] Unexpected condition type caught");
488 abort();
489 }
490 }
491
492 static
493 unsigned long hash_channel_key(struct channel_key *key)
494 {
495 unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed);
496 unsigned long domain_hash = hash_key_ulong(
497 (void *) (unsigned long) key->domain, lttng_ht_seed);
498
499 return key_hash ^ domain_hash;
500 }
501
502 static
503 unsigned long hash_client_socket(int socket)
504 {
505 return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed);
506 }
507
508 static
509 unsigned long hash_client_id(notification_client_id id)
510 {
511 return hash_key_u64(&id, lttng_ht_seed);
512 }
513
514 /*
515 * Get the type of object to which a given condition applies. Bindings let
516 * the notification system evaluate a trigger's condition when a given
517 * object's state is updated.
518 *
519 * For instance, a condition bound to a channel will be evaluated everytime
520 * the channel's state is changed by a channel monitoring sample.
521 */
522 static
523 enum lttng_object_type get_condition_binding_object(
524 const struct lttng_condition *condition)
525 {
526 switch (lttng_condition_get_type(condition)) {
527 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
528 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
529 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
530 return LTTNG_OBJECT_TYPE_CHANNEL;
531 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
532 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
533 return LTTNG_OBJECT_TYPE_SESSION;
534 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
535 return LTTNG_OBJECT_TYPE_NONE;
536 default:
537 return LTTNG_OBJECT_TYPE_UNKNOWN;
538 }
539 }
540
541 static
542 void free_channel_info_rcu(struct rcu_head *node)
543 {
544 free(caa_container_of(node, struct channel_info, rcu_node));
545 }
546
547 static
548 void channel_info_destroy(struct channel_info *channel_info)
549 {
550 if (!channel_info) {
551 return;
552 }
553
554 if (channel_info->session_info) {
555 session_info_remove_channel(channel_info->session_info,
556 channel_info);
557 session_info_put(channel_info->session_info);
558 }
559 if (channel_info->name) {
560 free(channel_info->name);
561 }
562 call_rcu(&channel_info->rcu_node, free_channel_info_rcu);
563 }
564
565 static
566 void free_session_info_rcu(struct rcu_head *node)
567 {
568 free(caa_container_of(node, struct session_info, rcu_node));
569 }
570
571 /* Don't call directly, use the ref-counting mechanism. */
572 static
573 void session_info_destroy(void *_data)
574 {
575 struct session_info *session_info = _data;
576 int ret;
577
578 assert(session_info);
579 if (session_info->channel_infos_ht) {
580 ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL);
581 if (ret) {
582 ERR("[notification-thread] Failed to destroy channel information hash table");
583 }
584 }
585 lttng_session_trigger_list_destroy(session_info->trigger_list);
586
587 rcu_read_lock();
588 cds_lfht_del(session_info->sessions_ht,
589 &session_info->sessions_ht_node);
590 rcu_read_unlock();
591 free(session_info->name);
592 call_rcu(&session_info->rcu_node, free_session_info_rcu);
593 }
594
595 static
596 void session_info_get(struct session_info *session_info)
597 {
598 if (!session_info) {
599 return;
600 }
601 lttng_ref_get(&session_info->ref);
602 }
603
604 static
605 void session_info_put(struct session_info *session_info)
606 {
607 if (!session_info) {
608 return;
609 }
610 lttng_ref_put(&session_info->ref);
611 }
612
613 static
614 struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid,
615 struct lttng_session_trigger_list *trigger_list,
616 struct cds_lfht *sessions_ht)
617 {
618 struct session_info *session_info;
619
620 assert(name);
621
622 session_info = zmalloc(sizeof(*session_info));
623 if (!session_info) {
624 goto end;
625 }
626 lttng_ref_init(&session_info->ref, session_info_destroy);
627
628 session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE,
629 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
630 if (!session_info->channel_infos_ht) {
631 goto error;
632 }
633
634 cds_lfht_node_init(&session_info->sessions_ht_node);
635 session_info->name = strdup(name);
636 if (!session_info->name) {
637 goto error;
638 }
639 session_info->uid = uid;
640 session_info->gid = gid;
641 session_info->trigger_list = trigger_list;
642 session_info->sessions_ht = sessions_ht;
643 end:
644 return session_info;
645 error:
646 session_info_put(session_info);
647 return NULL;
648 }
649
650 static
651 void session_info_add_channel(struct session_info *session_info,
652 struct channel_info *channel_info)
653 {
654 rcu_read_lock();
655 cds_lfht_add(session_info->channel_infos_ht,
656 hash_channel_key(&channel_info->key),
657 &channel_info->session_info_channels_ht_node);
658 rcu_read_unlock();
659 }
660
661 static
662 void session_info_remove_channel(struct session_info *session_info,
663 struct channel_info *channel_info)
664 {
665 rcu_read_lock();
666 cds_lfht_del(session_info->channel_infos_ht,
667 &channel_info->session_info_channels_ht_node);
668 rcu_read_unlock();
669 }
670
671 static
672 struct channel_info *channel_info_create(const char *channel_name,
673 struct channel_key *channel_key, uint64_t channel_capacity,
674 struct session_info *session_info)
675 {
676 struct channel_info *channel_info = zmalloc(sizeof(*channel_info));
677
678 if (!channel_info) {
679 goto end;
680 }
681
682 cds_lfht_node_init(&channel_info->channels_ht_node);
683 cds_lfht_node_init(&channel_info->session_info_channels_ht_node);
684 memcpy(&channel_info->key, channel_key, sizeof(*channel_key));
685 channel_info->capacity = channel_capacity;
686
687 channel_info->name = strdup(channel_name);
688 if (!channel_info->name) {
689 goto error;
690 }
691
692 /*
693 * Set the references between session and channel infos:
694 * - channel_info holds a strong reference to session_info
695 * - session_info holds a weak reference to channel_info
696 */
697 session_info_get(session_info);
698 session_info_add_channel(session_info, channel_info);
699 channel_info->session_info = session_info;
700 end:
701 return channel_info;
702 error:
703 channel_info_destroy(channel_info);
704 return NULL;
705 }
706
707 LTTNG_HIDDEN
708 bool notification_client_list_get(struct notification_client_list *list)
709 {
710 return urcu_ref_get_unless_zero(&list->ref);
711 }
712
713 static
714 void free_notification_client_list_rcu(struct rcu_head *node)
715 {
716 free(caa_container_of(node, struct notification_client_list,
717 rcu_node));
718 }
719
720 static
721 void notification_client_list_release(struct urcu_ref *list_ref)
722 {
723 struct notification_client_list *list =
724 container_of(list_ref, typeof(*list), ref);
725 struct notification_client_list_element *client_list_element, *tmp;
726
727 if (list->notification_trigger_clients_ht) {
728 rcu_read_lock();
729 cds_lfht_del(list->notification_trigger_clients_ht,
730 &list->notification_trigger_clients_ht_node);
731 rcu_read_unlock();
732 list->notification_trigger_clients_ht = NULL;
733 }
734 cds_list_for_each_entry_safe(client_list_element, tmp,
735 &list->list, node) {
736 free(client_list_element);
737 }
738 pthread_mutex_destroy(&list->lock);
739 call_rcu(&list->rcu_node, free_notification_client_list_rcu);
740 }
741
742 static
743 struct notification_client_list *notification_client_list_create(
744 const struct lttng_trigger *trigger)
745 {
746 struct notification_client_list *client_list =
747 zmalloc(sizeof(*client_list));
748
749 if (!client_list) {
750 goto error;
751 }
752 pthread_mutex_init(&client_list->lock, NULL);
753 urcu_ref_init(&client_list->ref);
754 cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node);
755 CDS_INIT_LIST_HEAD(&client_list->list);
756 client_list->trigger = trigger;
757 error:
758 return client_list;
759 }
760
761 static
762 void publish_notification_client_list(
763 struct notification_thread_state *state,
764 struct notification_client_list *list)
765 {
766 const struct lttng_condition *condition =
767 lttng_trigger_get_const_condition(list->trigger);
768
769 assert(!list->notification_trigger_clients_ht);
770 notification_client_list_get(list);
771
772 list->notification_trigger_clients_ht =
773 state->notification_trigger_clients_ht;
774
775 rcu_read_lock();
776 cds_lfht_add(state->notification_trigger_clients_ht,
777 lttng_condition_hash(condition),
778 &list->notification_trigger_clients_ht_node);
779 rcu_read_unlock();
780 }
781
782 LTTNG_HIDDEN
783 void notification_client_list_put(struct notification_client_list *list)
784 {
785 if (!list) {
786 return;
787 }
788 return urcu_ref_put(&list->ref, notification_client_list_release);
789 }
790
791 /* Provides a reference to the returned list. */
792 static
793 struct notification_client_list *get_client_list_from_condition(
794 struct notification_thread_state *state,
795 const struct lttng_condition *condition)
796 {
797 struct cds_lfht_node *node;
798 struct cds_lfht_iter iter;
799 struct notification_client_list *list = NULL;
800
801 rcu_read_lock();
802 cds_lfht_lookup(state->notification_trigger_clients_ht,
803 lttng_condition_hash(condition),
804 match_client_list_condition,
805 condition,
806 &iter);
807 node = cds_lfht_iter_get_node(&iter);
808 if (node) {
809 list = container_of(node, struct notification_client_list,
810 notification_trigger_clients_ht_node);
811 list = notification_client_list_get(list) ? list : NULL;
812 }
813
814 rcu_read_unlock();
815 return list;
816 }
817
818 static
819 int evaluate_channel_condition_for_client(
820 const struct lttng_condition *condition,
821 struct notification_thread_state *state,
822 struct lttng_evaluation **evaluation,
823 uid_t *session_uid, gid_t *session_gid)
824 {
825 int ret;
826 struct cds_lfht_iter iter;
827 struct cds_lfht_node *node;
828 struct channel_info *channel_info = NULL;
829 struct channel_key *channel_key = NULL;
830 struct channel_state_sample *last_sample = NULL;
831 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
832
833 rcu_read_lock();
834
835 /* Find the channel associated with the condition. */
836 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter,
837 channel_trigger_list, channel_triggers_ht_node) {
838 struct lttng_trigger_list_element *element;
839
840 cds_list_for_each_entry(element, &channel_trigger_list->list, node) {
841 const struct lttng_condition *current_condition =
842 lttng_trigger_get_const_condition(
843 element->trigger);
844
845 assert(current_condition);
846 if (!lttng_condition_is_equal(condition,
847 current_condition)) {
848 continue;
849 }
850
851 /* Found the trigger, save the channel key. */
852 channel_key = &channel_trigger_list->channel_key;
853 break;
854 }
855 if (channel_key) {
856 /* The channel key was found stop iteration. */
857 break;
858 }
859 }
860
861 if (!channel_key){
862 /* No channel found; normal exit. */
863 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
864 ret = 0;
865 goto end;
866 }
867
868 /* Fetch channel info for the matching channel. */
869 cds_lfht_lookup(state->channels_ht,
870 hash_channel_key(channel_key),
871 match_channel_info,
872 channel_key,
873 &iter);
874 node = cds_lfht_iter_get_node(&iter);
875 assert(node);
876 channel_info = caa_container_of(node, struct channel_info,
877 channels_ht_node);
878
879 /* Retrieve the channel's last sample, if it exists. */
880 cds_lfht_lookup(state->channel_state_ht,
881 hash_channel_key(channel_key),
882 match_channel_state_sample,
883 channel_key,
884 &iter);
885 node = cds_lfht_iter_get_node(&iter);
886 if (node) {
887 last_sample = caa_container_of(node,
888 struct channel_state_sample,
889 channel_state_ht_node);
890 } else {
891 /* Nothing to evaluate, no sample was ever taken. Normal exit */
892 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
893 ret = 0;
894 goto end;
895 }
896
897 ret = evaluate_buffer_condition(condition, evaluation, state,
898 NULL, last_sample,
899 0, channel_info->session_info->consumed_data_size,
900 channel_info);
901 if (ret) {
902 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
903 goto end;
904 }
905
906 *session_uid = channel_info->session_info->uid;
907 *session_gid = channel_info->session_info->gid;
908 end:
909 rcu_read_unlock();
910 return ret;
911 }
912
913 static
914 const char *get_condition_session_name(const struct lttng_condition *condition)
915 {
916 const char *session_name = NULL;
917 enum lttng_condition_status status;
918
919 switch (lttng_condition_get_type(condition)) {
920 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
921 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
922 status = lttng_condition_buffer_usage_get_session_name(
923 condition, &session_name);
924 break;
925 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
926 status = lttng_condition_session_consumed_size_get_session_name(
927 condition, &session_name);
928 break;
929 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
930 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
931 status = lttng_condition_session_rotation_get_session_name(
932 condition, &session_name);
933 break;
934 default:
935 abort();
936 }
937 if (status != LTTNG_CONDITION_STATUS_OK) {
938 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
939 goto end;
940 }
941 end:
942 return session_name;
943 }
944
945 static
946 int evaluate_session_condition_for_client(
947 const struct lttng_condition *condition,
948 struct notification_thread_state *state,
949 struct lttng_evaluation **evaluation,
950 uid_t *session_uid, gid_t *session_gid)
951 {
952 int ret;
953 struct cds_lfht_iter iter;
954 struct cds_lfht_node *node;
955 const char *session_name;
956 struct session_info *session_info = NULL;
957
958 rcu_read_lock();
959 session_name = get_condition_session_name(condition);
960
961 /* Find the session associated with the trigger. */
962 cds_lfht_lookup(state->sessions_ht,
963 hash_key_str(session_name, lttng_ht_seed),
964 match_session,
965 session_name,
966 &iter);
967 node = cds_lfht_iter_get_node(&iter);
968 if (!node) {
969 DBG("[notification-thread] No known session matching name \"%s\"",
970 session_name);
971 ret = 0;
972 goto end;
973 }
974
975 session_info = caa_container_of(node, struct session_info,
976 sessions_ht_node);
977 session_info_get(session_info);
978
979 /*
980 * Evaluation is performed in-line here since only one type of
981 * session-bound condition is handled for the moment.
982 */
983 switch (lttng_condition_get_type(condition)) {
984 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
985 if (!session_info->rotation.ongoing) {
986 ret = 0;
987 goto end_session_put;
988 }
989
990 *evaluation = lttng_evaluation_session_rotation_ongoing_create(
991 session_info->rotation.id);
992 if (!*evaluation) {
993 /* Fatal error. */
994 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
995 session_info->name);
996 ret = -1;
997 goto end_session_put;
998 }
999 ret = 0;
1000 break;
1001 default:
1002 ret = 0;
1003 goto end_session_put;
1004 }
1005
1006 *session_uid = session_info->uid;
1007 *session_gid = session_info->gid;
1008
1009 end_session_put:
1010 session_info_put(session_info);
1011 end:
1012 rcu_read_unlock();
1013 return ret;
1014 }
1015
1016 static
1017 int evaluate_condition_for_client(const struct lttng_trigger *trigger,
1018 const struct lttng_condition *condition,
1019 struct notification_client *client,
1020 struct notification_thread_state *state)
1021 {
1022 int ret;
1023 struct lttng_evaluation *evaluation = NULL;
1024 struct notification_client_list client_list = {
1025 .lock = PTHREAD_MUTEX_INITIALIZER,
1026 };
1027 struct notification_client_list_element client_list_element = { 0 };
1028 uid_t object_uid = 0;
1029 gid_t object_gid = 0;
1030
1031 assert(trigger);
1032 assert(condition);
1033 assert(client);
1034 assert(state);
1035
1036 switch (get_condition_binding_object(condition)) {
1037 case LTTNG_OBJECT_TYPE_SESSION:
1038 ret = evaluate_session_condition_for_client(condition, state,
1039 &evaluation, &object_uid, &object_gid);
1040 break;
1041 case LTTNG_OBJECT_TYPE_CHANNEL:
1042 ret = evaluate_channel_condition_for_client(condition, state,
1043 &evaluation, &object_uid, &object_gid);
1044 break;
1045 case LTTNG_OBJECT_TYPE_NONE:
1046 ret = 0;
1047 goto end;
1048 case LTTNG_OBJECT_TYPE_UNKNOWN:
1049 default:
1050 ret = -1;
1051 goto end;
1052 }
1053 if (ret) {
1054 /* Fatal error. */
1055 goto end;
1056 }
1057 if (!evaluation) {
1058 /* Evaluation yielded nothing. Normal exit. */
1059 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1060 ret = 0;
1061 goto end;
1062 }
1063
1064 /*
1065 * Create a temporary client list with the client currently
1066 * subscribing.
1067 */
1068 cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node);
1069 CDS_INIT_LIST_HEAD(&client_list.list);
1070 client_list.trigger = trigger;
1071
1072 CDS_INIT_LIST_HEAD(&client_list_element.node);
1073 client_list_element.client = client;
1074 cds_list_add(&client_list_element.node, &client_list.list);
1075
1076 /* Send evaluation result to the newly-subscribed client. */
1077 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1078 ret = send_evaluation_to_clients(trigger, evaluation, &client_list,
1079 state, object_uid, object_gid);
1080
1081 end:
1082 return ret;
1083 }
1084
1085 static
1086 int notification_thread_client_subscribe(struct notification_client *client,
1087 struct lttng_condition *condition,
1088 struct notification_thread_state *state,
1089 enum lttng_notification_channel_status *_status)
1090 {
1091 int ret = 0;
1092 struct notification_client_list *client_list = NULL;
1093 struct lttng_condition_list_element *condition_list_element = NULL;
1094 struct notification_client_list_element *client_list_element = NULL;
1095 enum lttng_notification_channel_status status =
1096 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1097
1098 /*
1099 * Ensure that the client has not already subscribed to this condition
1100 * before.
1101 */
1102 cds_list_for_each_entry(condition_list_element, &client->condition_list, node) {
1103 if (lttng_condition_is_equal(condition_list_element->condition,
1104 condition)) {
1105 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED;
1106 goto end;
1107 }
1108 }
1109
1110 condition_list_element = zmalloc(sizeof(*condition_list_element));
1111 if (!condition_list_element) {
1112 ret = -1;
1113 goto error;
1114 }
1115 client_list_element = zmalloc(sizeof(*client_list_element));
1116 if (!client_list_element) {
1117 ret = -1;
1118 goto error;
1119 }
1120
1121 /*
1122 * Add the newly-subscribed condition to the client's subscription list.
1123 */
1124 CDS_INIT_LIST_HEAD(&condition_list_element->node);
1125 condition_list_element->condition = condition;
1126 cds_list_add(&condition_list_element->node, &client->condition_list);
1127
1128 client_list = get_client_list_from_condition(state, condition);
1129 if (!client_list) {
1130 /*
1131 * No notification-emiting trigger registered with this
1132 * condition. We don't evaluate the condition right away
1133 * since this trigger is not registered yet.
1134 */
1135 free(client_list_element);
1136 goto end;
1137 }
1138
1139 /*
1140 * The condition to which the client just subscribed is evaluated
1141 * at this point so that conditions that are already TRUE result
1142 * in a notification being sent out.
1143 *
1144 * The client_list's trigger is used without locking the list itself.
1145 * This is correct since the list doesn't own the trigger and the
1146 * object is immutable.
1147 */
1148 if (evaluate_condition_for_client(client_list->trigger, condition,
1149 client, state)) {
1150 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1151 ret = -1;
1152 free(client_list_element);
1153 goto end;
1154 }
1155
1156 /*
1157 * Add the client to the list of clients interested in a given trigger
1158 * if a "notification" trigger with a corresponding condition was
1159 * added prior.
1160 */
1161 client_list_element->client = client;
1162 CDS_INIT_LIST_HEAD(&client_list_element->node);
1163
1164 pthread_mutex_lock(&client_list->lock);
1165 cds_list_add(&client_list_element->node, &client_list->list);
1166 pthread_mutex_unlock(&client_list->lock);
1167 end:
1168 if (_status) {
1169 *_status = status;
1170 }
1171 if (client_list) {
1172 notification_client_list_put(client_list);
1173 }
1174 return ret;
1175 error:
1176 free(condition_list_element);
1177 free(client_list_element);
1178 return ret;
1179 }
1180
1181 static
1182 int notification_thread_client_unsubscribe(
1183 struct notification_client *client,
1184 struct lttng_condition *condition,
1185 struct notification_thread_state *state,
1186 enum lttng_notification_channel_status *_status)
1187 {
1188 struct notification_client_list *client_list;
1189 struct lttng_condition_list_element *condition_list_element,
1190 *condition_tmp;
1191 struct notification_client_list_element *client_list_element,
1192 *client_tmp;
1193 bool condition_found = false;
1194 enum lttng_notification_channel_status status =
1195 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1196
1197 /* Remove the condition from the client's condition list. */
1198 cds_list_for_each_entry_safe(condition_list_element, condition_tmp,
1199 &client->condition_list, node) {
1200 if (!lttng_condition_is_equal(condition_list_element->condition,
1201 condition)) {
1202 continue;
1203 }
1204
1205 cds_list_del(&condition_list_element->node);
1206 /*
1207 * The caller may be iterating on the client's conditions to
1208 * tear down a client's connection. In this case, the condition
1209 * will be destroyed at the end.
1210 */
1211 if (condition != condition_list_element->condition) {
1212 lttng_condition_destroy(
1213 condition_list_element->condition);
1214 }
1215 free(condition_list_element);
1216 condition_found = true;
1217 break;
1218 }
1219
1220 if (!condition_found) {
1221 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
1222 goto end;
1223 }
1224
1225 /*
1226 * Remove the client from the list of clients interested the trigger
1227 * matching the condition.
1228 */
1229 client_list = get_client_list_from_condition(state, condition);
1230 if (!client_list) {
1231 goto end;
1232 }
1233
1234 pthread_mutex_lock(&client_list->lock);
1235 cds_list_for_each_entry_safe(client_list_element, client_tmp,
1236 &client_list->list, node) {
1237 if (client_list_element->client->id != client->id) {
1238 continue;
1239 }
1240 cds_list_del(&client_list_element->node);
1241 free(client_list_element);
1242 break;
1243 }
1244 pthread_mutex_unlock(&client_list->lock);
1245 notification_client_list_put(client_list);
1246 client_list = NULL;
1247 end:
1248 lttng_condition_destroy(condition);
1249 if (_status) {
1250 *_status = status;
1251 }
1252 return 0;
1253 }
1254
1255 static
1256 void free_notification_client_rcu(struct rcu_head *node)
1257 {
1258 free(caa_container_of(node, struct notification_client, rcu_node));
1259 }
1260
1261 static
1262 void notification_client_destroy(struct notification_client *client,
1263 struct notification_thread_state *state)
1264 {
1265 if (!client) {
1266 return;
1267 }
1268
1269 /*
1270 * The client object is not reachable by other threads, no need to lock
1271 * the client here.
1272 */
1273 if (client->socket >= 0) {
1274 (void) lttcomm_close_unix_sock(client->socket);
1275 client->socket = -1;
1276 }
1277 client->communication.active = false;
1278 lttng_payload_reset(&client->communication.inbound.payload);
1279 lttng_payload_reset(&client->communication.outbound.payload);
1280 pthread_mutex_destroy(&client->lock);
1281 call_rcu(&client->rcu_node, free_notification_client_rcu);
1282 }
1283
1284 /*
1285 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1286 * client pointer).
1287 */
1288 static
1289 struct notification_client *get_client_from_socket(int socket,
1290 struct notification_thread_state *state)
1291 {
1292 struct cds_lfht_iter iter;
1293 struct cds_lfht_node *node;
1294 struct notification_client *client = NULL;
1295
1296 cds_lfht_lookup(state->client_socket_ht,
1297 hash_client_socket(socket),
1298 match_client_socket,
1299 (void *) (unsigned long) socket,
1300 &iter);
1301 node = cds_lfht_iter_get_node(&iter);
1302 if (!node) {
1303 goto end;
1304 }
1305
1306 client = caa_container_of(node, struct notification_client,
1307 client_socket_ht_node);
1308 end:
1309 return client;
1310 }
1311
1312 /*
1313 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1314 * client pointer).
1315 */
1316 static
1317 struct notification_client *get_client_from_id(notification_client_id id,
1318 struct notification_thread_state *state)
1319 {
1320 struct cds_lfht_iter iter;
1321 struct cds_lfht_node *node;
1322 struct notification_client *client = NULL;
1323
1324 cds_lfht_lookup(state->client_id_ht,
1325 hash_client_id(id),
1326 match_client_id,
1327 &id,
1328 &iter);
1329 node = cds_lfht_iter_get_node(&iter);
1330 if (!node) {
1331 goto end;
1332 }
1333
1334 client = caa_container_of(node, struct notification_client,
1335 client_id_ht_node);
1336 end:
1337 return client;
1338 }
1339
1340 static
1341 bool buffer_usage_condition_applies_to_channel(
1342 const struct lttng_condition *condition,
1343 const struct channel_info *channel_info)
1344 {
1345 enum lttng_condition_status status;
1346 enum lttng_domain_type condition_domain;
1347 const char *condition_session_name = NULL;
1348 const char *condition_channel_name = NULL;
1349
1350 status = lttng_condition_buffer_usage_get_domain_type(condition,
1351 &condition_domain);
1352 assert(status == LTTNG_CONDITION_STATUS_OK);
1353 if (channel_info->key.domain != condition_domain) {
1354 goto fail;
1355 }
1356
1357 status = lttng_condition_buffer_usage_get_session_name(
1358 condition, &condition_session_name);
1359 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1360
1361 status = lttng_condition_buffer_usage_get_channel_name(
1362 condition, &condition_channel_name);
1363 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name);
1364
1365 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1366 goto fail;
1367 }
1368 if (strcmp(channel_info->name, condition_channel_name)) {
1369 goto fail;
1370 }
1371
1372 return true;
1373 fail:
1374 return false;
1375 }
1376
1377 static
1378 bool session_consumed_size_condition_applies_to_channel(
1379 const struct lttng_condition *condition,
1380 const struct channel_info *channel_info)
1381 {
1382 enum lttng_condition_status status;
1383 const char *condition_session_name = NULL;
1384
1385 status = lttng_condition_session_consumed_size_get_session_name(
1386 condition, &condition_session_name);
1387 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1388
1389 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1390 goto fail;
1391 }
1392
1393 return true;
1394 fail:
1395 return false;
1396 }
1397
1398 static
1399 bool trigger_applies_to_channel(const struct lttng_trigger *trigger,
1400 const struct channel_info *channel_info)
1401 {
1402 const struct lttng_condition *condition;
1403 bool trigger_applies;
1404
1405 condition = lttng_trigger_get_const_condition(trigger);
1406 if (!condition) {
1407 goto fail;
1408 }
1409
1410 switch (lttng_condition_get_type(condition)) {
1411 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1412 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1413 trigger_applies = buffer_usage_condition_applies_to_channel(
1414 condition, channel_info);
1415 break;
1416 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1417 trigger_applies = session_consumed_size_condition_applies_to_channel(
1418 condition, channel_info);
1419 break;
1420 default:
1421 goto fail;
1422 }
1423
1424 return trigger_applies;
1425 fail:
1426 return false;
1427 }
1428
1429 static
1430 bool trigger_applies_to_client(struct lttng_trigger *trigger,
1431 struct notification_client *client)
1432 {
1433 bool applies = false;
1434 struct lttng_condition_list_element *condition_list_element;
1435
1436 cds_list_for_each_entry(condition_list_element, &client->condition_list,
1437 node) {
1438 applies = lttng_condition_is_equal(
1439 condition_list_element->condition,
1440 lttng_trigger_get_condition(trigger));
1441 if (applies) {
1442 break;
1443 }
1444 }
1445 return applies;
1446 }
1447
1448 /* Must be called with RCU read lock held. */
1449 static
1450 struct lttng_session_trigger_list *get_session_trigger_list(
1451 struct notification_thread_state *state,
1452 const char *session_name)
1453 {
1454 struct lttng_session_trigger_list *list = NULL;
1455 struct cds_lfht_node *node;
1456 struct cds_lfht_iter iter;
1457
1458 cds_lfht_lookup(state->session_triggers_ht,
1459 hash_key_str(session_name, lttng_ht_seed),
1460 match_session_trigger_list,
1461 session_name,
1462 &iter);
1463 node = cds_lfht_iter_get_node(&iter);
1464 if (!node) {
1465 /*
1466 * Not an error, the list of triggers applying to that session
1467 * will be initialized when the session is created.
1468 */
1469 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1470 session_name);
1471 goto end;
1472 }
1473
1474 list = caa_container_of(node,
1475 struct lttng_session_trigger_list,
1476 session_triggers_ht_node);
1477 end:
1478 return list;
1479 }
1480
1481 /*
1482 * Allocate an empty lttng_session_trigger_list for the session named
1483 * 'session_name'.
1484 *
1485 * No ownership of 'session_name' is assumed by the session trigger list.
1486 * It is the caller's responsability to ensure the session name is alive
1487 * for as long as this list is.
1488 */
1489 static
1490 struct lttng_session_trigger_list *lttng_session_trigger_list_create(
1491 const char *session_name,
1492 struct cds_lfht *session_triggers_ht)
1493 {
1494 struct lttng_session_trigger_list *list;
1495
1496 list = zmalloc(sizeof(*list));
1497 if (!list) {
1498 goto end;
1499 }
1500 list->session_name = session_name;
1501 CDS_INIT_LIST_HEAD(&list->list);
1502 cds_lfht_node_init(&list->session_triggers_ht_node);
1503 list->session_triggers_ht = session_triggers_ht;
1504
1505 rcu_read_lock();
1506 /* Publish the list through the session_triggers_ht. */
1507 cds_lfht_add(session_triggers_ht,
1508 hash_key_str(session_name, lttng_ht_seed),
1509 &list->session_triggers_ht_node);
1510 rcu_read_unlock();
1511 end:
1512 return list;
1513 }
1514
1515 static
1516 void free_session_trigger_list_rcu(struct rcu_head *node)
1517 {
1518 free(caa_container_of(node, struct lttng_session_trigger_list,
1519 rcu_node));
1520 }
1521
1522 static
1523 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list)
1524 {
1525 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1526
1527 /* Empty the list element by element, and then free the list itself. */
1528 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1529 &list->list, node) {
1530 cds_list_del(&trigger_list_element->node);
1531 free(trigger_list_element);
1532 }
1533 rcu_read_lock();
1534 /* Unpublish the list from the session_triggers_ht. */
1535 cds_lfht_del(list->session_triggers_ht,
1536 &list->session_triggers_ht_node);
1537 rcu_read_unlock();
1538 call_rcu(&list->rcu_node, free_session_trigger_list_rcu);
1539 }
1540
1541 static
1542 int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
1543 struct lttng_trigger *trigger)
1544 {
1545 int ret = 0;
1546 struct lttng_trigger_list_element *new_element =
1547 zmalloc(sizeof(*new_element));
1548
1549 if (!new_element) {
1550 ret = -1;
1551 goto end;
1552 }
1553 CDS_INIT_LIST_HEAD(&new_element->node);
1554 new_element->trigger = trigger;
1555 cds_list_add(&new_element->node, &list->list);
1556 end:
1557 return ret;
1558 }
1559
1560 static
1561 bool trigger_applies_to_session(const struct lttng_trigger *trigger,
1562 const char *session_name)
1563 {
1564 bool applies = false;
1565 const struct lttng_condition *condition;
1566
1567 condition = lttng_trigger_get_const_condition(trigger);
1568 switch (lttng_condition_get_type(condition)) {
1569 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1570 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1571 {
1572 enum lttng_condition_status condition_status;
1573 const char *condition_session_name;
1574
1575 condition_status = lttng_condition_session_rotation_get_session_name(
1576 condition, &condition_session_name);
1577 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
1578 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1579 goto end;
1580 }
1581
1582 assert(condition_session_name);
1583 applies = !strcmp(condition_session_name, session_name);
1584 break;
1585 }
1586 default:
1587 goto end;
1588 }
1589 end:
1590 return applies;
1591 }
1592
1593 /*
1594 * Allocate and initialize an lttng_session_trigger_list which contains
1595 * all triggers that apply to the session named 'session_name'.
1596 *
1597 * No ownership of 'session_name' is assumed by the session trigger list.
1598 * It is the caller's responsability to ensure the session name is alive
1599 * for as long as this list is.
1600 */
1601 static
1602 struct lttng_session_trigger_list *lttng_session_trigger_list_build(
1603 const struct notification_thread_state *state,
1604 const char *session_name)
1605 {
1606 int trigger_count = 0;
1607 struct lttng_session_trigger_list *session_trigger_list = NULL;
1608 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1609 struct cds_lfht_iter iter;
1610
1611 session_trigger_list = lttng_session_trigger_list_create(session_name,
1612 state->session_triggers_ht);
1613
1614 /* Add all triggers applying to the session named 'session_name'. */
1615 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1616 node) {
1617 int ret;
1618
1619 if (!trigger_applies_to_session(trigger_ht_element->trigger,
1620 session_name)) {
1621 continue;
1622 }
1623
1624 ret = lttng_session_trigger_list_add(session_trigger_list,
1625 trigger_ht_element->trigger);
1626 if (ret) {
1627 goto error;
1628 }
1629
1630 trigger_count++;
1631 }
1632
1633 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1634 trigger_count);
1635 return session_trigger_list;
1636 error:
1637 lttng_session_trigger_list_destroy(session_trigger_list);
1638 return NULL;
1639 }
1640
1641 static
1642 struct session_info *find_or_create_session_info(
1643 struct notification_thread_state *state,
1644 const char *name, uid_t uid, gid_t gid)
1645 {
1646 struct session_info *session = NULL;
1647 struct cds_lfht_node *node;
1648 struct cds_lfht_iter iter;
1649 struct lttng_session_trigger_list *trigger_list;
1650
1651 rcu_read_lock();
1652 cds_lfht_lookup(state->sessions_ht,
1653 hash_key_str(name, lttng_ht_seed),
1654 match_session,
1655 name,
1656 &iter);
1657 node = cds_lfht_iter_get_node(&iter);
1658 if (node) {
1659 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1660 name, uid, gid);
1661 session = caa_container_of(node, struct session_info,
1662 sessions_ht_node);
1663 assert(session->uid == uid);
1664 assert(session->gid == gid);
1665 session_info_get(session);
1666 goto end;
1667 }
1668
1669 trigger_list = lttng_session_trigger_list_build(state, name);
1670 if (!trigger_list) {
1671 goto error;
1672 }
1673
1674 session = session_info_create(name, uid, gid, trigger_list,
1675 state->sessions_ht);
1676 if (!session) {
1677 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1678 name, uid, gid);
1679 lttng_session_trigger_list_destroy(trigger_list);
1680 goto error;
1681 }
1682 trigger_list = NULL;
1683
1684 cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed),
1685 &session->sessions_ht_node);
1686 end:
1687 rcu_read_unlock();
1688 return session;
1689 error:
1690 rcu_read_unlock();
1691 session_info_put(session);
1692 return NULL;
1693 }
1694
1695 static
1696 int handle_notification_thread_command_add_channel(
1697 struct notification_thread_state *state,
1698 const char *session_name, uid_t session_uid, gid_t session_gid,
1699 const char *channel_name, enum lttng_domain_type channel_domain,
1700 uint64_t channel_key_int, uint64_t channel_capacity,
1701 enum lttng_error_code *cmd_result)
1702 {
1703 struct cds_list_head trigger_list;
1704 struct channel_info *new_channel_info = NULL;
1705 struct channel_key channel_key = {
1706 .key = channel_key_int,
1707 .domain = channel_domain,
1708 };
1709 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
1710 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1711 int trigger_count = 0;
1712 struct cds_lfht_iter iter;
1713 struct session_info *session_info = NULL;
1714
1715 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain",
1716 channel_name, session_name, channel_key_int,
1717 channel_domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
1718
1719 CDS_INIT_LIST_HEAD(&trigger_list);
1720
1721 session_info = find_or_create_session_info(state, session_name,
1722 session_uid, session_gid);
1723 if (!session_info) {
1724 /* Allocation error or an internal error occurred. */
1725 goto error;
1726 }
1727
1728 new_channel_info = channel_info_create(channel_name, &channel_key,
1729 channel_capacity, session_info);
1730 if (!new_channel_info) {
1731 goto error;
1732 }
1733
1734 rcu_read_lock();
1735 /* Build a list of all triggers applying to the new channel. */
1736 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1737 node) {
1738 struct lttng_trigger_list_element *new_element;
1739
1740 if (!trigger_applies_to_channel(trigger_ht_element->trigger,
1741 new_channel_info)) {
1742 continue;
1743 }
1744
1745 new_element = zmalloc(sizeof(*new_element));
1746 if (!new_element) {
1747 rcu_read_unlock();
1748 goto error;
1749 }
1750 CDS_INIT_LIST_HEAD(&new_element->node);
1751 new_element->trigger = trigger_ht_element->trigger;
1752 cds_list_add(&new_element->node, &trigger_list);
1753 trigger_count++;
1754 }
1755 rcu_read_unlock();
1756
1757 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1758 trigger_count);
1759 channel_trigger_list = zmalloc(sizeof(*channel_trigger_list));
1760 if (!channel_trigger_list) {
1761 goto error;
1762 }
1763 channel_trigger_list->channel_key = new_channel_info->key;
1764 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
1765 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
1766 cds_list_splice(&trigger_list, &channel_trigger_list->list);
1767
1768 rcu_read_lock();
1769 /* Add channel to the channel_ht which owns the channel_infos. */
1770 cds_lfht_add(state->channels_ht,
1771 hash_channel_key(&new_channel_info->key),
1772 &new_channel_info->channels_ht_node);
1773 /*
1774 * Add the list of triggers associated with this channel to the
1775 * channel_triggers_ht.
1776 */
1777 cds_lfht_add(state->channel_triggers_ht,
1778 hash_channel_key(&new_channel_info->key),
1779 &channel_trigger_list->channel_triggers_ht_node);
1780 rcu_read_unlock();
1781 session_info_put(session_info);
1782 *cmd_result = LTTNG_OK;
1783 return 0;
1784 error:
1785 channel_info_destroy(new_channel_info);
1786 session_info_put(session_info);
1787 return 1;
1788 }
1789
1790 static
1791 void free_channel_trigger_list_rcu(struct rcu_head *node)
1792 {
1793 free(caa_container_of(node, struct lttng_channel_trigger_list,
1794 rcu_node));
1795 }
1796
1797 static
1798 void free_channel_state_sample_rcu(struct rcu_head *node)
1799 {
1800 free(caa_container_of(node, struct channel_state_sample,
1801 rcu_node));
1802 }
1803
1804 static
1805 int handle_notification_thread_command_remove_channel(
1806 struct notification_thread_state *state,
1807 uint64_t channel_key, enum lttng_domain_type domain,
1808 enum lttng_error_code *cmd_result)
1809 {
1810 struct cds_lfht_node *node;
1811 struct cds_lfht_iter iter;
1812 struct lttng_channel_trigger_list *trigger_list;
1813 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1814 struct channel_key key = { .key = channel_key, .domain = domain };
1815 struct channel_info *channel_info;
1816
1817 DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain",
1818 channel_key, domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
1819
1820 rcu_read_lock();
1821
1822 cds_lfht_lookup(state->channel_triggers_ht,
1823 hash_channel_key(&key),
1824 match_channel_trigger_list,
1825 &key,
1826 &iter);
1827 node = cds_lfht_iter_get_node(&iter);
1828 /*
1829 * There is a severe internal error if we are being asked to remove a
1830 * channel that doesn't exist.
1831 */
1832 if (!node) {
1833 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1834 goto end;
1835 }
1836
1837 /* Free the list of triggers associated with this channel. */
1838 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
1839 channel_triggers_ht_node);
1840 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1841 &trigger_list->list, node) {
1842 cds_list_del(&trigger_list_element->node);
1843 free(trigger_list_element);
1844 }
1845 cds_lfht_del(state->channel_triggers_ht, node);
1846 call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu);
1847
1848 /* Free sampled channel state. */
1849 cds_lfht_lookup(state->channel_state_ht,
1850 hash_channel_key(&key),
1851 match_channel_state_sample,
1852 &key,
1853 &iter);
1854 node = cds_lfht_iter_get_node(&iter);
1855 /*
1856 * This is expected to be NULL if the channel is destroyed before we
1857 * received a sample.
1858 */
1859 if (node) {
1860 struct channel_state_sample *sample = caa_container_of(node,
1861 struct channel_state_sample,
1862 channel_state_ht_node);
1863
1864 cds_lfht_del(state->channel_state_ht, node);
1865 call_rcu(&sample->rcu_node, free_channel_state_sample_rcu);
1866 }
1867
1868 /* Remove the channel from the channels_ht and free it. */
1869 cds_lfht_lookup(state->channels_ht,
1870 hash_channel_key(&key),
1871 match_channel_info,
1872 &key,
1873 &iter);
1874 node = cds_lfht_iter_get_node(&iter);
1875 assert(node);
1876 channel_info = caa_container_of(node, struct channel_info,
1877 channels_ht_node);
1878 cds_lfht_del(state->channels_ht, node);
1879 channel_info_destroy(channel_info);
1880 end:
1881 rcu_read_unlock();
1882 *cmd_result = LTTNG_OK;
1883 return 0;
1884 }
1885
1886 static
1887 int handle_notification_thread_command_session_rotation(
1888 struct notification_thread_state *state,
1889 enum notification_thread_command_type cmd_type,
1890 const char *session_name, uid_t session_uid, gid_t session_gid,
1891 uint64_t trace_archive_chunk_id,
1892 struct lttng_trace_archive_location *location,
1893 enum lttng_error_code *_cmd_result)
1894 {
1895 int ret = 0;
1896 enum lttng_error_code cmd_result = LTTNG_OK;
1897 struct lttng_session_trigger_list *trigger_list;
1898 struct lttng_trigger_list_element *trigger_list_element;
1899 struct session_info *session_info;
1900 const struct lttng_credentials session_creds = {
1901 .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid),
1902 .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid),
1903 };
1904
1905 rcu_read_lock();
1906
1907 session_info = find_or_create_session_info(state, session_name,
1908 session_uid, session_gid);
1909 if (!session_info) {
1910 /* Allocation error or an internal error occurred. */
1911 ret = -1;
1912 cmd_result = LTTNG_ERR_NOMEM;
1913 goto end;
1914 }
1915
1916 session_info->rotation.ongoing =
1917 cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
1918 session_info->rotation.id = trace_archive_chunk_id;
1919 trigger_list = get_session_trigger_list(state, session_name);
1920 if (!trigger_list) {
1921 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1922 session_name);
1923 goto end;
1924 }
1925
1926 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
1927 node) {
1928 const struct lttng_condition *condition;
1929 struct lttng_trigger *trigger;
1930 struct notification_client_list *client_list;
1931 struct lttng_evaluation *evaluation = NULL;
1932 enum lttng_condition_type condition_type;
1933 enum action_executor_status executor_status;
1934
1935 trigger = trigger_list_element->trigger;
1936 condition = lttng_trigger_get_const_condition(trigger);
1937 assert(condition);
1938 condition_type = lttng_condition_get_type(condition);
1939
1940 if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING &&
1941 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1942 continue;
1943 } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED &&
1944 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) {
1945 continue;
1946 }
1947
1948 client_list = get_client_list_from_condition(state, condition);
1949 if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1950 evaluation = lttng_evaluation_session_rotation_ongoing_create(
1951 trace_archive_chunk_id);
1952 } else {
1953 evaluation = lttng_evaluation_session_rotation_completed_create(
1954 trace_archive_chunk_id, location);
1955 }
1956
1957 if (!evaluation) {
1958 /* Internal error */
1959 ret = -1;
1960 cmd_result = LTTNG_ERR_UNK;
1961 goto put_list;
1962 }
1963
1964 /*
1965 * Ownership of `evaluation` transferred to the action executor
1966 * no matter the result.
1967 */
1968 executor_status = action_executor_enqueue(state->executor,
1969 trigger, evaluation, &session_creds,
1970 client_list);
1971 evaluation = NULL;
1972 switch (executor_status) {
1973 case ACTION_EXECUTOR_STATUS_OK:
1974 break;
1975 case ACTION_EXECUTOR_STATUS_ERROR:
1976 case ACTION_EXECUTOR_STATUS_INVALID:
1977 /*
1978 * TODO Add trigger identification (name/id) when
1979 * it is added to the API.
1980 */
1981 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1982 ret = -1;
1983 goto put_list;
1984 case ACTION_EXECUTOR_STATUS_OVERFLOW:
1985 /*
1986 * TODO Add trigger identification (name/id) when
1987 * it is added to the API.
1988 *
1989 * Not a fatal error.
1990 */
1991 WARN("No space left when enqueuing action associated with session rotation trigger");
1992 ret = 0;
1993 goto put_list;
1994 default:
1995 abort();
1996 }
1997
1998 put_list:
1999 notification_client_list_put(client_list);
2000 if (caa_unlikely(ret)) {
2001 break;
2002 }
2003 }
2004 end:
2005 session_info_put(session_info);
2006 *_cmd_result = cmd_result;
2007 rcu_read_unlock();
2008 return ret;
2009 }
2010
2011 static int handle_notification_thread_command_list_triggers(
2012 struct notification_thread_handle *handle,
2013 struct notification_thread_state *state,
2014 uid_t client_uid,
2015 struct lttng_triggers **triggers,
2016 enum lttng_error_code *_cmd_result)
2017 {
2018 int ret = 0;
2019 enum lttng_error_code cmd_result = LTTNG_OK;
2020 struct cds_lfht_iter iter;
2021 struct lttng_trigger_ht_element *trigger_ht_element;
2022 struct lttng_triggers *local_triggers = NULL;
2023 const struct lttng_credentials *creds;
2024
2025 rcu_read_lock();
2026
2027 local_triggers = lttng_triggers_create();
2028 if (!local_triggers) {
2029 /* Not a fatal error. */
2030 cmd_result = LTTNG_ERR_NOMEM;
2031 goto end;
2032 }
2033
2034 cds_lfht_for_each_entry(state->triggers_ht, &iter,
2035 trigger_ht_element, node) {
2036 /*
2037 * Only return the triggers to which the client has access.
2038 * The root user has visibility over all triggers.
2039 */
2040 creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
2041 if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
2042 continue;
2043 }
2044
2045 ret = lttng_triggers_add(local_triggers,
2046 trigger_ht_element->trigger);
2047 if (ret < 0) {
2048 /* Not a fatal error. */
2049 ret = 0;
2050 cmd_result = LTTNG_ERR_NOMEM;
2051 goto end;
2052 }
2053 }
2054
2055 /* Transferring ownership to the caller. */
2056 *triggers = local_triggers;
2057 local_triggers = NULL;
2058
2059 end:
2060 rcu_read_unlock();
2061 lttng_triggers_destroy(local_triggers);
2062 *_cmd_result = cmd_result;
2063 return ret;
2064 }
2065
2066 static
2067 bool condition_is_supported(struct lttng_condition *condition)
2068 {
2069 bool is_supported;
2070
2071 switch (lttng_condition_get_type(condition)) {
2072 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2073 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2074 {
2075 int ret;
2076 enum lttng_domain_type domain;
2077
2078 ret = lttng_condition_buffer_usage_get_domain_type(condition,
2079 &domain);
2080 assert(ret == 0);
2081
2082 if (domain != LTTNG_DOMAIN_KERNEL) {
2083 is_supported = true;
2084 goto end;
2085 }
2086
2087 /*
2088 * Older kernel tracers don't expose the API to monitor their
2089 * buffers. Therefore, we reject triggers that require that
2090 * mechanism to be available to be evaluated.
2091 *
2092 * Assume unsupported on error.
2093 */
2094 is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2095 break;
2096 }
2097 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
2098 {
2099 const struct lttng_event_rule *event_rule;
2100 enum lttng_domain_type domain;
2101 const enum lttng_condition_status status =
2102 lttng_condition_event_rule_get_rule(
2103 condition, &event_rule);
2104
2105 assert(status == LTTNG_CONDITION_STATUS_OK);
2106
2107 domain = lttng_event_rule_get_domain_type(event_rule);
2108 if (domain != LTTNG_DOMAIN_KERNEL) {
2109 is_supported = true;
2110 goto end;
2111 }
2112
2113 /*
2114 * Older kernel tracers can't emit notification. Therefore, we
2115 * reject triggers that require that mechanism to be available
2116 * to be evaluated.
2117 *
2118 * Assume unsupported on error.
2119 */
2120 is_supported = kernel_supports_event_notifiers() == 1;
2121 break;
2122 }
2123 default:
2124 is_supported = true;
2125 }
2126 end:
2127 return is_supported;
2128 }
2129
2130 /* Must be called with RCU read lock held. */
2131 static
2132 int bind_trigger_to_matching_session(struct lttng_trigger *trigger,
2133 struct notification_thread_state *state)
2134 {
2135 int ret = 0;
2136 const struct lttng_condition *condition;
2137 const char *session_name;
2138 struct lttng_session_trigger_list *trigger_list;
2139
2140 condition = lttng_trigger_get_const_condition(trigger);
2141 switch (lttng_condition_get_type(condition)) {
2142 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
2143 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
2144 {
2145 enum lttng_condition_status status;
2146
2147 status = lttng_condition_session_rotation_get_session_name(
2148 condition, &session_name);
2149 if (status != LTTNG_CONDITION_STATUS_OK) {
2150 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2151 ret = -1;
2152 goto end;
2153 }
2154 break;
2155 }
2156 default:
2157 ret = -1;
2158 goto end;
2159 }
2160
2161 trigger_list = get_session_trigger_list(state, session_name);
2162 if (!trigger_list) {
2163 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2164 session_name);
2165 goto end;
2166
2167 }
2168
2169 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2170 session_name);
2171 ret = lttng_session_trigger_list_add(trigger_list, trigger);
2172 end:
2173 return ret;
2174 }
2175
2176 /* Must be called with RCU read lock held. */
2177 static
2178 int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
2179 struct notification_thread_state *state)
2180 {
2181 int ret = 0;
2182 struct cds_lfht_node *node;
2183 struct cds_lfht_iter iter;
2184 struct channel_info *channel;
2185
2186 cds_lfht_for_each_entry(state->channels_ht, &iter, channel,
2187 channels_ht_node) {
2188 struct lttng_trigger_list_element *trigger_list_element;
2189 struct lttng_channel_trigger_list *trigger_list;
2190 struct cds_lfht_iter lookup_iter;
2191
2192 if (!trigger_applies_to_channel(trigger, channel)) {
2193 continue;
2194 }
2195
2196 cds_lfht_lookup(state->channel_triggers_ht,
2197 hash_channel_key(&channel->key),
2198 match_channel_trigger_list,
2199 &channel->key,
2200 &lookup_iter);
2201 node = cds_lfht_iter_get_node(&lookup_iter);
2202 assert(node);
2203 trigger_list = caa_container_of(node,
2204 struct lttng_channel_trigger_list,
2205 channel_triggers_ht_node);
2206
2207 trigger_list_element = zmalloc(sizeof(*trigger_list_element));
2208 if (!trigger_list_element) {
2209 ret = -1;
2210 goto end;
2211 }
2212 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
2213 trigger_list_element->trigger = trigger;
2214 cds_list_add(&trigger_list_element->node, &trigger_list->list);
2215 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2216 channel->name);
2217 }
2218 end:
2219 return ret;
2220 }
2221
2222 static
2223 bool is_trigger_action_notify(const struct lttng_trigger *trigger)
2224 {
2225 bool is_notify = false;
2226 unsigned int i, count;
2227 enum lttng_action_status action_status;
2228 const struct lttng_action *action =
2229 lttng_trigger_get_const_action(trigger);
2230 enum lttng_action_type action_type;
2231
2232 assert(action);
2233 action_type = lttng_action_get_type(action);
2234 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2235 is_notify = true;
2236 goto end;
2237 } else if (action_type != LTTNG_ACTION_TYPE_GROUP) {
2238 goto end;
2239 }
2240
2241 action_status = lttng_action_group_get_count(action, &count);
2242 assert(action_status == LTTNG_ACTION_STATUS_OK);
2243
2244 for (i = 0; i < count; i++) {
2245 const struct lttng_action *inner_action =
2246 lttng_action_group_get_at_index(
2247 action, i);
2248
2249 action_type = lttng_action_get_type(inner_action);
2250 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2251 is_notify = true;
2252 goto end;
2253 }
2254 }
2255
2256 end:
2257 return is_notify;
2258 }
2259
2260 static bool trigger_name_taken(struct notification_thread_state *state,
2261 const struct lttng_trigger *trigger)
2262 {
2263 struct cds_lfht_iter iter;
2264
2265 /*
2266 * No duplicata is allowed in the triggers_by_name_uid_ht.
2267 * The match is done against the trigger name and uid.
2268 */
2269 cds_lfht_lookup(state->triggers_by_name_uid_ht,
2270 hash_trigger_by_name_uid(trigger),
2271 match_trigger_by_name_uid,
2272 trigger,
2273 &iter);
2274 return !!cds_lfht_iter_get_node(&iter);
2275 }
2276
2277 static
2278 enum lttng_error_code generate_trigger_name(
2279 struct notification_thread_state *state,
2280 struct lttng_trigger *trigger, const char **name)
2281 {
2282 enum lttng_error_code ret_code = LTTNG_OK;
2283 bool taken = false;
2284 enum lttng_trigger_status status;
2285
2286 do {
2287 const int ret = lttng_trigger_generate_name(trigger,
2288 state->trigger_id.name_offset++);
2289 if (ret) {
2290 /* The only reason this can fail right now. */
2291 ret_code = LTTNG_ERR_NOMEM;
2292 break;
2293 }
2294
2295 status = lttng_trigger_get_name(trigger, name);
2296 assert(status == LTTNG_TRIGGER_STATUS_OK);
2297
2298 taken = trigger_name_taken(state, trigger);
2299 } while (taken || state->trigger_id.name_offset == UINT64_MAX);
2300
2301 return ret_code;
2302 }
2303
2304 /*
2305 * FIXME A client's credentials are not checked when registering a trigger.
2306 *
2307 * The effects of this are benign since:
2308 * - The client will succeed in registering the trigger, as it is valid,
2309 * - The trigger will, internally, be bound to the channel/session,
2310 * - The notifications will not be sent since the client's credentials
2311 * are checked against the channel at that moment.
2312 *
2313 * If this function returns a non-zero value, it means something is
2314 * fundamentally broken and the whole subsystem/thread will be torn down.
2315 *
2316 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2317 * error code.
2318 */
2319 static
2320 int handle_notification_thread_command_register_trigger(
2321 struct notification_thread_state *state,
2322 struct lttng_trigger *trigger,
2323 enum lttng_error_code *cmd_result)
2324 {
2325 int ret = 0;
2326 struct lttng_condition *condition;
2327 struct notification_client *client;
2328 struct notification_client_list *client_list = NULL;
2329 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
2330 struct notification_client_list_element *client_list_element;
2331 struct cds_lfht_node *node;
2332 struct cds_lfht_iter iter;
2333 const char* trigger_name;
2334 bool free_trigger = true;
2335 struct lttng_evaluation *evaluation = NULL;
2336 struct lttng_credentials object_creds;
2337 uid_t object_uid;
2338 gid_t object_gid;
2339 enum action_executor_status executor_status;
2340 const uint64_t trigger_tracer_token =
2341 state->trigger_id.next_tracer_token++;
2342
2343 rcu_read_lock();
2344
2345 /* Set the trigger's tracer token. */
2346 lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
2347
2348 if (lttng_trigger_get_name(trigger, &trigger_name) ==
2349 LTTNG_TRIGGER_STATUS_UNSET) {
2350 const enum lttng_error_code ret_code = generate_trigger_name(
2351 state, trigger, &trigger_name);
2352
2353 if (ret_code != LTTNG_OK) {
2354 /* Fatal error. */
2355 ret = -1;
2356 *cmd_result = ret_code;
2357 goto error;
2358 }
2359 } else if (trigger_name_taken(state, trigger)) {
2360 /* Not a fatal error. */
2361 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2362 ret = 0;
2363 goto error;
2364 }
2365
2366 condition = lttng_trigger_get_condition(trigger);
2367 assert(condition);
2368
2369 /* Some conditions require tracers to implement a minimal ABI version. */
2370 if (!condition_is_supported(condition)) {
2371 *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
2372 goto error;
2373 }
2374
2375 trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
2376 if (!trigger_ht_element) {
2377 ret = -1;
2378 goto error;
2379 }
2380
2381 /* Add trigger to the trigger_ht. */
2382 cds_lfht_node_init(&trigger_ht_element->node);
2383 cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
2384 trigger_ht_element->trigger = trigger;
2385
2386 node = cds_lfht_add_unique(state->triggers_ht,
2387 lttng_condition_hash(condition),
2388 match_trigger,
2389 trigger,
2390 &trigger_ht_element->node);
2391 if (node != &trigger_ht_element->node) {
2392 /* Not a fatal error, simply report it to the client. */
2393 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2394 goto error_free_ht_element;
2395 }
2396
2397 node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
2398 hash_trigger_by_name_uid(trigger),
2399 match_trigger_by_name_uid,
2400 trigger,
2401 &trigger_ht_element->node_by_name_uid);
2402 if (node != &trigger_ht_element->node_by_name_uid) {
2403 /* Not a fatal error, simply report it to the client. */
2404 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2405 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2406 goto error_free_ht_element;
2407 }
2408
2409 /*
2410 * Ownership of the trigger and of its wrapper was transfered to
2411 * the triggers_ht.
2412 */
2413 trigger_ht_element = NULL;
2414 free_trigger = false;
2415
2416 /*
2417 * The rest only applies to triggers that have a "notify" action.
2418 * It is not skipped as this is the only action type currently
2419 * supported.
2420 */
2421 if (is_trigger_action_notify(trigger)) {
2422 client_list = notification_client_list_create(trigger);
2423 if (!client_list) {
2424 ret = -1;
2425 goto error_free_ht_element;
2426 }
2427
2428 /* Build a list of clients to which this new trigger applies. */
2429 cds_lfht_for_each_entry (state->client_socket_ht, &iter, client,
2430 client_socket_ht_node) {
2431 if (!trigger_applies_to_client(trigger, client)) {
2432 continue;
2433 }
2434
2435 client_list_element =
2436 zmalloc(sizeof(*client_list_element));
2437 if (!client_list_element) {
2438 ret = -1;
2439 goto error_put_client_list;
2440 }
2441
2442 CDS_INIT_LIST_HEAD(&client_list_element->node);
2443 client_list_element->client = client;
2444 cds_list_add(&client_list_element->node,
2445 &client_list->list);
2446 }
2447
2448 /*
2449 * Client list ownership transferred to the
2450 * notification_trigger_clients_ht.
2451 */
2452 publish_notification_client_list(state, client_list);
2453 }
2454
2455 switch (get_condition_binding_object(condition)) {
2456 case LTTNG_OBJECT_TYPE_SESSION:
2457 /* Add the trigger to the list if it matches a known session. */
2458 ret = bind_trigger_to_matching_session(trigger, state);
2459 if (ret) {
2460 goto error_put_client_list;
2461 }
2462 break;
2463 case LTTNG_OBJECT_TYPE_CHANNEL:
2464 /*
2465 * Add the trigger to list of triggers bound to the channels
2466 * currently known.
2467 */
2468 ret = bind_trigger_to_matching_channels(trigger, state);
2469 if (ret) {
2470 goto error_put_client_list;
2471 }
2472 break;
2473 case LTTNG_OBJECT_TYPE_NONE:
2474 break;
2475 default:
2476 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2477 ret = -1;
2478 goto error_put_client_list;
2479 }
2480
2481 /*
2482 * The new trigger's condition must be evaluated against the current
2483 * state.
2484 *
2485 * In the case of `notify` action, nothing preventing clients from
2486 * subscribing to a condition before the corresponding trigger is
2487 * registered, we have to evaluate this new condition right away.
2488 *
2489 * At some point, we were waiting for the next "evaluation" (e.g. on
2490 * reception of a channel sample) to evaluate this new condition, but
2491 * that was broken.
2492 *
2493 * The reason it was broken is that waiting for the next sample
2494 * does not allow us to properly handle transitions for edge-triggered
2495 * conditions.
2496 *
2497 * Consider this example: when we handle a new channel sample, we
2498 * evaluate each conditions twice: once with the previous state, and
2499 * again with the newest state. We then use those two results to
2500 * determine whether a state change happened: a condition was false and
2501 * became true. If a state change happened, we have to notify clients.
2502 *
2503 * Now, if a client subscribes to a given notification and registers
2504 * a trigger *after* that subscription, we have to make sure the
2505 * condition is evaluated at this point while considering only the
2506 * current state. Otherwise, the next evaluation cycle may only see
2507 * that the evaluations remain the same (true for samples n-1 and n) and
2508 * the client will never know that the condition has been met.
2509 */
2510 switch (get_condition_binding_object(condition)) {
2511 case LTTNG_OBJECT_TYPE_SESSION:
2512 ret = evaluate_session_condition_for_client(condition, state,
2513 &evaluation, &object_uid,
2514 &object_gid);
2515 break;
2516 case LTTNG_OBJECT_TYPE_CHANNEL:
2517 ret = evaluate_channel_condition_for_client(condition, state,
2518 &evaluation, &object_uid,
2519 &object_gid);
2520 break;
2521 case LTTNG_OBJECT_TYPE_NONE:
2522 ret = 0;
2523 break;
2524 case LTTNG_OBJECT_TYPE_UNKNOWN:
2525 default:
2526 ret = -1;
2527 break;
2528 }
2529
2530 if (ret) {
2531 /* Fatal error. */
2532 goto error_put_client_list;
2533 }
2534
2535 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2536 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
2537
2538 DBG("Newly registered trigger's condition evaluated to %s",
2539 evaluation ? "true" : "false");
2540 if (!evaluation) {
2541 /* Evaluation yielded nothing. Normal exit. */
2542 ret = 0;
2543 goto end;
2544 }
2545
2546 /*
2547 * Ownership of `evaluation` transferred to the action executor
2548 * no matter the result.
2549 */
2550 executor_status = action_executor_enqueue(state->executor, trigger,
2551 evaluation, &object_creds, client_list);
2552 evaluation = NULL;
2553 switch (executor_status) {
2554 case ACTION_EXECUTOR_STATUS_OK:
2555 break;
2556 case ACTION_EXECUTOR_STATUS_ERROR:
2557 case ACTION_EXECUTOR_STATUS_INVALID:
2558 /*
2559 * TODO Add trigger identification (name/id) when
2560 * it is added to the API.
2561 */
2562 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2563 ret = -1;
2564 goto error_put_client_list;
2565 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2566 /*
2567 * TODO Add trigger identification (name/id) when
2568 * it is added to the API.
2569 *
2570 * Not a fatal error.
2571 */
2572 WARN("No space left when enqueuing action associated to newly registered trigger");
2573 ret = 0;
2574 goto end;
2575 default:
2576 abort();
2577 }
2578
2579 end:
2580 *cmd_result = LTTNG_OK;
2581 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64,
2582 trigger_name, trigger_tracer_token);
2583
2584 error_put_client_list:
2585 notification_client_list_put(client_list);
2586
2587 error_free_ht_element:
2588 if (trigger_ht_element) {
2589 /* Delayed removal due to RCU constraint on delete. */
2590 call_rcu(&trigger_ht_element->rcu_node,
2591 free_lttng_trigger_ht_element_rcu);
2592 }
2593
2594 error:
2595 if (free_trigger) {
2596 lttng_trigger_destroy(trigger);
2597 }
2598 rcu_read_unlock();
2599 return ret;
2600 }
2601
2602 static
2603 void free_lttng_trigger_ht_element_rcu(struct rcu_head *node)
2604 {
2605 free(caa_container_of(node, struct lttng_trigger_ht_element,
2606 rcu_node));
2607 }
2608
2609 static
2610 int handle_notification_thread_command_unregister_trigger(
2611 struct notification_thread_state *state,
2612 struct lttng_trigger *trigger,
2613 enum lttng_error_code *_cmd_reply)
2614 {
2615 struct cds_lfht_iter iter;
2616 struct cds_lfht_node *triggers_ht_node;
2617 struct lttng_channel_trigger_list *trigger_list;
2618 struct notification_client_list *client_list;
2619 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
2620 struct lttng_condition *condition = lttng_trigger_get_condition(
2621 trigger);
2622 enum lttng_error_code cmd_reply;
2623
2624 rcu_read_lock();
2625
2626 cds_lfht_lookup(state->triggers_ht,
2627 lttng_condition_hash(condition),
2628 match_trigger,
2629 trigger,
2630 &iter);
2631 triggers_ht_node = cds_lfht_iter_get_node(&iter);
2632 if (!triggers_ht_node) {
2633 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
2634 goto end;
2635 } else {
2636 cmd_reply = LTTNG_OK;
2637 }
2638
2639 /* Remove trigger from channel_triggers_ht. */
2640 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
2641 channel_triggers_ht_node) {
2642 struct lttng_trigger_list_element *trigger_element, *tmp;
2643
2644 cds_list_for_each_entry_safe(trigger_element, tmp,
2645 &trigger_list->list, node) {
2646 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
2647 continue;
2648 }
2649
2650 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2651 cds_list_del(&trigger_element->node);
2652 /* A trigger can only appear once per channel */
2653 break;
2654 }
2655 }
2656
2657 if (is_trigger_action_notify(trigger)) {
2658 /*
2659 * Remove and release the client list from
2660 * notification_trigger_clients_ht.
2661 */
2662 client_list = get_client_list_from_condition(state, condition);
2663 assert(client_list);
2664
2665 /* Put new reference and the hashtable's reference. */
2666 notification_client_list_put(client_list);
2667 notification_client_list_put(client_list);
2668 client_list = NULL;
2669 }
2670
2671 /* Remove trigger from triggers_ht. */
2672 trigger_ht_element = caa_container_of(triggers_ht_node,
2673 struct lttng_trigger_ht_element, node);
2674 cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
2675 cds_lfht_del(state->triggers_ht, triggers_ht_node);
2676
2677 /* Release the ownership of the trigger. */
2678 lttng_trigger_destroy(trigger_ht_element->trigger);
2679 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
2680 end:
2681 rcu_read_unlock();
2682 if (_cmd_reply) {
2683 *_cmd_reply = cmd_reply;
2684 }
2685 return 0;
2686 }
2687
2688 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2689 int handle_notification_thread_command(
2690 struct notification_thread_handle *handle,
2691 struct notification_thread_state *state)
2692 {
2693 int ret;
2694 uint64_t counter;
2695 struct notification_thread_command *cmd;
2696
2697 /* Read the event pipe to put it back into a quiescent state. */
2698 ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter,
2699 sizeof(counter));
2700 if (ret != sizeof(counter)) {
2701 goto error;
2702 }
2703
2704 pthread_mutex_lock(&handle->cmd_queue.lock);
2705 cmd = cds_list_first_entry(&handle->cmd_queue.list,
2706 struct notification_thread_command, cmd_list_node);
2707 cds_list_del(&cmd->cmd_list_node);
2708 pthread_mutex_unlock(&handle->cmd_queue.lock);
2709 switch (cmd->type) {
2710 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
2711 DBG("[notification-thread] Received register trigger command");
2712 ret = handle_notification_thread_command_register_trigger(
2713 state, cmd->parameters.trigger,
2714 &cmd->reply_code);
2715 break;
2716 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
2717 DBG("[notification-thread] Received unregister trigger command");
2718 ret = handle_notification_thread_command_unregister_trigger(
2719 state, cmd->parameters.trigger,
2720 &cmd->reply_code);
2721 break;
2722 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
2723 DBG("[notification-thread] Received add channel command");
2724 ret = handle_notification_thread_command_add_channel(
2725 state,
2726 cmd->parameters.add_channel.session.name,
2727 cmd->parameters.add_channel.session.uid,
2728 cmd->parameters.add_channel.session.gid,
2729 cmd->parameters.add_channel.channel.name,
2730 cmd->parameters.add_channel.channel.domain,
2731 cmd->parameters.add_channel.channel.key,
2732 cmd->parameters.add_channel.channel.capacity,
2733 &cmd->reply_code);
2734 break;
2735 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
2736 DBG("[notification-thread] Received remove channel command");
2737 ret = handle_notification_thread_command_remove_channel(
2738 state, cmd->parameters.remove_channel.key,
2739 cmd->parameters.remove_channel.domain,
2740 &cmd->reply_code);
2741 break;
2742 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
2743 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
2744 DBG("[notification-thread] Received session rotation %s command",
2745 cmd->type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING ?
2746 "ongoing" : "completed");
2747 ret = handle_notification_thread_command_session_rotation(
2748 state,
2749 cmd->type,
2750 cmd->parameters.session_rotation.session_name,
2751 cmd->parameters.session_rotation.uid,
2752 cmd->parameters.session_rotation.gid,
2753 cmd->parameters.session_rotation.trace_archive_chunk_id,
2754 cmd->parameters.session_rotation.location,
2755 &cmd->reply_code);
2756 break;
2757 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
2758 {
2759 struct lttng_triggers *triggers = NULL;
2760
2761 ret = handle_notification_thread_command_list_triggers(
2762 handle,
2763 state,
2764 cmd->parameters.list_triggers.uid,
2765 &triggers,
2766 &cmd->reply_code);
2767 cmd->reply.list_triggers.triggers = triggers;
2768 ret = 0;
2769 break;
2770 }
2771 case NOTIFICATION_COMMAND_TYPE_QUIT:
2772 DBG("[notification-thread] Received quit command");
2773 cmd->reply_code = LTTNG_OK;
2774 ret = 1;
2775 goto end;
2776 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
2777 {
2778 const enum client_transmission_status client_status =
2779 cmd->parameters.client_communication_update
2780 .status;
2781 const notification_client_id client_id =
2782 cmd->parameters.client_communication_update.id;
2783 struct notification_client *client;
2784
2785 rcu_read_lock();
2786 client = get_client_from_id(client_id, state);
2787
2788 if (!client) {
2789 /*
2790 * Client error was probably already picked-up by the
2791 * notification thread or it has disconnected
2792 * gracefully while this command was queued.
2793 */
2794 DBG("Failed to find notification client to update communication status, client id = %" PRIu64,
2795 client_id);
2796 ret = 0;
2797 } else {
2798 ret = client_handle_transmission_status(
2799 client, client_status, state);
2800 }
2801 rcu_read_unlock();
2802 break;
2803 }
2804 default:
2805 ERR("[notification-thread] Unknown internal command received");
2806 goto error_unlock;
2807 }
2808
2809 if (ret) {
2810 goto error_unlock;
2811 }
2812 end:
2813 if (cmd->is_async) {
2814 free(cmd);
2815 cmd = NULL;
2816 } else {
2817 lttng_waiter_wake_up(&cmd->reply_waiter);
2818 }
2819 return ret;
2820 error_unlock:
2821 /* Wake-up and return a fatal error to the calling thread. */
2822 lttng_waiter_wake_up(&cmd->reply_waiter);
2823 cmd->reply_code = LTTNG_ERR_FATAL;
2824 error:
2825 /* Indicate a fatal error to the caller. */
2826 return -1;
2827 }
2828
2829 static
2830 int socket_set_non_blocking(int socket)
2831 {
2832 int ret, flags;
2833
2834 /* Set the pipe as non-blocking. */
2835 ret = fcntl(socket, F_GETFL, 0);
2836 if (ret == -1) {
2837 PERROR("fcntl get socket flags");
2838 goto end;
2839 }
2840 flags = ret;
2841
2842 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
2843 if (ret == -1) {
2844 PERROR("fcntl set O_NONBLOCK socket flag");
2845 goto end;
2846 }
2847 DBG("Client socket (fd = %i) set as non-blocking", socket);
2848 end:
2849 return ret;
2850 }
2851
2852 static
2853 int client_reset_inbound_state(struct notification_client *client)
2854 {
2855 int ret;
2856
2857
2858 lttng_payload_clear(&client->communication.inbound.payload);
2859
2860 client->communication.inbound.bytes_to_receive =
2861 sizeof(struct lttng_notification_channel_message);
2862 client->communication.inbound.msg_type =
2863 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
2864 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
2865 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
2866 ret = lttng_dynamic_buffer_set_size(
2867 &client->communication.inbound.payload.buffer,
2868 client->communication.inbound.bytes_to_receive);
2869
2870 return ret;
2871 }
2872
2873 int handle_notification_thread_client_connect(
2874 struct notification_thread_state *state)
2875 {
2876 int ret;
2877 struct notification_client *client;
2878
2879 DBG("[notification-thread] Handling new notification channel client connection");
2880
2881 client = zmalloc(sizeof(*client));
2882 if (!client) {
2883 /* Fatal error. */
2884 ret = -1;
2885 goto error;
2886 }
2887
2888 pthread_mutex_init(&client->lock, NULL);
2889 client->id = state->next_notification_client_id++;
2890 CDS_INIT_LIST_HEAD(&client->condition_list);
2891 lttng_payload_init(&client->communication.inbound.payload);
2892 lttng_payload_init(&client->communication.outbound.payload);
2893 client->communication.inbound.expect_creds = true;
2894
2895 ret = client_reset_inbound_state(client);
2896 if (ret) {
2897 ERR("[notification-thread] Failed to reset client communication's inbound state");
2898 ret = 0;
2899 goto error;
2900 }
2901
2902 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
2903 if (ret < 0) {
2904 ERR("[notification-thread] Failed to accept new notification channel client connection");
2905 ret = 0;
2906 goto error;
2907 }
2908
2909 client->socket = ret;
2910
2911 ret = socket_set_non_blocking(client->socket);
2912 if (ret) {
2913 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2914 goto error;
2915 }
2916
2917 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
2918 if (ret < 0) {
2919 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2920 ret = 0;
2921 goto error;
2922 }
2923
2924 ret = lttng_poll_add(&state->events, client->socket,
2925 LPOLLIN | LPOLLERR |
2926 LPOLLHUP | LPOLLRDHUP);
2927 if (ret < 0) {
2928 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2929 ret = 0;
2930 goto error;
2931 }
2932 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2933 client->socket);
2934
2935 rcu_read_lock();
2936 cds_lfht_add(state->client_socket_ht,
2937 hash_client_socket(client->socket),
2938 &client->client_socket_ht_node);
2939 cds_lfht_add(state->client_id_ht,
2940 hash_client_id(client->id),
2941 &client->client_id_ht_node);
2942 rcu_read_unlock();
2943
2944 return ret;
2945
2946 error:
2947 notification_client_destroy(client, state);
2948 return ret;
2949 }
2950
2951 /*
2952 * RCU read-lock must be held by the caller.
2953 * Client lock must _not_ be held by the caller.
2954 */
2955 static
2956 int notification_thread_client_disconnect(
2957 struct notification_client *client,
2958 struct notification_thread_state *state)
2959 {
2960 int ret;
2961 struct lttng_condition_list_element *condition_list_element, *tmp;
2962
2963 /* Acquire the client lock to disable its communication atomically. */
2964 pthread_mutex_lock(&client->lock);
2965 client->communication.active = false;
2966 cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node);
2967 cds_lfht_del(state->client_id_ht, &client->client_id_ht_node);
2968 pthread_mutex_unlock(&client->lock);
2969
2970 ret = lttng_poll_del(&state->events, client->socket);
2971 if (ret) {
2972 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2973 client->socket);
2974 }
2975
2976 /* Release all conditions to which the client was subscribed. */
2977 cds_list_for_each_entry_safe(condition_list_element, tmp,
2978 &client->condition_list, node) {
2979 (void) notification_thread_client_unsubscribe(client,
2980 condition_list_element->condition, state, NULL);
2981 }
2982
2983 /*
2984 * Client no longer accessible to other threads (through the
2985 * client lists).
2986 */
2987 notification_client_destroy(client, state);
2988 return ret;
2989 }
2990
2991 int handle_notification_thread_client_disconnect(
2992 int client_socket, struct notification_thread_state *state)
2993 {
2994 int ret = 0;
2995 struct notification_client *client;
2996
2997 rcu_read_lock();
2998 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2999 client_socket);
3000 client = get_client_from_socket(client_socket, state);
3001 if (!client) {
3002 /* Internal state corruption, fatal error. */
3003 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3004 client_socket);
3005 ret = -1;
3006 goto end;
3007 }
3008
3009 ret = notification_thread_client_disconnect(client, state);
3010 end:
3011 rcu_read_unlock();
3012 return ret;
3013 }
3014
3015 int handle_notification_thread_client_disconnect_all(
3016 struct notification_thread_state *state)
3017 {
3018 struct cds_lfht_iter iter;
3019 struct notification_client *client;
3020 bool error_encoutered = false;
3021
3022 rcu_read_lock();
3023 DBG("[notification-thread] Closing all client connections");
3024 cds_lfht_for_each_entry(state->client_socket_ht, &iter, client,
3025 client_socket_ht_node) {
3026 int ret;
3027
3028 ret = notification_thread_client_disconnect(
3029 client, state);
3030 if (ret) {
3031 error_encoutered = true;
3032 }
3033 }
3034 rcu_read_unlock();
3035 return error_encoutered ? 1 : 0;
3036 }
3037
3038 int handle_notification_thread_trigger_unregister_all(
3039 struct notification_thread_state *state)
3040 {
3041 bool error_occurred = false;
3042 struct cds_lfht_iter iter;
3043 struct lttng_trigger_ht_element *trigger_ht_element;
3044
3045