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