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