Optimization: remove unnecessary buffer resizes on partial recvs
[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 "notification-thread.h"
23 #include "notification-thread-events.h"
24 #include "notification-thread-commands.h"
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/futex.h>
28 #include <common/unix.h>
29 #include <common/dynamic-buffer.h>
30 #include <common/hashtable/utils.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/macros.h>
33 #include <lttng/condition/condition.h>
34 #include <lttng/action/action.h>
35 #include <lttng/notification/notification-internal.h>
36 #include <lttng/condition/condition-internal.h>
37 #include <lttng/condition/buffer-usage-internal.h>
38 #include <lttng/notification/channel-internal.h>
39 #include <time.h>
40 #include <unistd.h>
41 #include <assert.h>
42 #include <inttypes.h>
43 #include <fcntl.h>
44
45 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
46 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
47
48 struct lttng_trigger_list_element {
49 struct lttng_trigger *trigger;
50 struct cds_list_head node;
51 };
52
53 struct lttng_channel_trigger_list {
54 struct channel_key channel_key;
55 struct cds_list_head list;
56 struct cds_lfht_node channel_triggers_ht_node;
57 };
58
59 struct lttng_trigger_ht_element {
60 struct lttng_trigger *trigger;
61 struct cds_lfht_node node;
62 };
63
64 struct lttng_condition_list_element {
65 struct lttng_condition *condition;
66 struct cds_list_head node;
67 };
68
69 struct notification_client_list_element {
70 struct notification_client *client;
71 struct cds_list_head node;
72 };
73
74 struct notification_client_list {
75 struct lttng_trigger *trigger;
76 struct cds_list_head list;
77 struct cds_lfht_node notification_trigger_ht_node;
78 };
79
80 struct notification_client {
81 int socket;
82 /* Client protocol version. */
83 uint8_t major, minor;
84 uid_t uid;
85 gid_t gid;
86 /*
87 * Indicates if the credentials and versions of the client has been
88 * checked.
89 */
90 bool validated;
91 /*
92 * Conditions to which the client's notification channel is subscribed.
93 * List of struct lttng_condition_list_node. The condition member is
94 * owned by the client.
95 */
96 struct cds_list_head condition_list;
97 struct cds_lfht_node client_socket_ht_node;
98 struct {
99 struct {
100 /*
101 * During the reception of a message, the reception
102 * buffers' "size" is set to contain the current
103 * message's complete payload.
104 */
105 struct lttng_dynamic_buffer buffer;
106 /* Bytes left to receive for the current message. */
107 size_t bytes_to_receive;
108 /* Type of the message being received. */
109 enum lttng_notification_channel_message_type msg_type;
110 /*
111 * Indicates whether or not credentials are expected
112 * from the client.
113 */
114 bool receive_creds;
115 /*
116 * Indicates whether or not credentials were received
117 * from the client.
118 */
119 bool creds_received;
120 /* Only used during credentials reception. */
121 lttng_sock_cred creds;
122 } inbound;
123 struct {
124 /*
125 * Indicates whether or not a notification addressed to
126 * this client was dropped because a command reply was
127 * already buffered.
128 *
129 * A notification is dropped whenever the buffer is not
130 * empty.
131 */
132 bool dropped_notification;
133 /*
134 * Indicates whether or not a command reply is already
135 * buffered. In this case, it means that the client is
136 * not consuming command replies before emitting a new
137 * one. This could be caused by a protocol error or a
138 * misbehaving/malicious client.
139 */
140 bool queued_command_reply;
141 struct lttng_dynamic_buffer buffer;
142 } outbound;
143 } communication;
144 };
145
146 struct channel_state_sample {
147 struct channel_key key;
148 struct cds_lfht_node channel_state_ht_node;
149 uint64_t highest_usage;
150 uint64_t lowest_usage;
151 };
152
153 static
154 int match_client(struct cds_lfht_node *node, const void *key)
155 {
156 /* This double-cast is intended to supress pointer-to-cast warning. */
157 int socket = (int) (intptr_t) key;
158 struct notification_client *client;
159
160 client = caa_container_of(node, struct notification_client,
161 client_socket_ht_node);
162
163 return !!(client->socket == socket);
164 }
165
166 static
167 int match_channel_trigger_list(struct cds_lfht_node *node, const void *key)
168 {
169 struct channel_key *channel_key = (struct channel_key *) key;
170 struct lttng_channel_trigger_list *trigger_list;
171
172 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
173 channel_triggers_ht_node);
174
175 return !!((channel_key->key == trigger_list->channel_key.key) &&
176 (channel_key->domain == trigger_list->channel_key.domain));
177 }
178
179 static
180 int match_channel_state_sample(struct cds_lfht_node *node, const void *key)
181 {
182 struct channel_key *channel_key = (struct channel_key *) key;
183 struct channel_state_sample *sample;
184
185 sample = caa_container_of(node, struct channel_state_sample,
186 channel_state_ht_node);
187
188 return !!((channel_key->key == sample->key.key) &&
189 (channel_key->domain == sample->key.domain));
190 }
191
192 static
193 int match_channel_info(struct cds_lfht_node *node, const void *key)
194 {
195 struct channel_key *channel_key = (struct channel_key *) key;
196 struct channel_info *channel_info;
197
198 channel_info = caa_container_of(node, struct channel_info,
199 channels_ht_node);
200
201 return !!((channel_key->key == channel_info->key.key) &&
202 (channel_key->domain == channel_info->key.domain));
203 }
204
205 static
206 int match_condition(struct cds_lfht_node *node, const void *key)
207 {
208 struct lttng_condition *condition_key = (struct lttng_condition *) key;
209 struct lttng_trigger_ht_element *trigger;
210 struct lttng_condition *condition;
211
212 trigger = caa_container_of(node, struct lttng_trigger_ht_element,
213 node);
214 condition = lttng_trigger_get_condition(trigger->trigger);
215 assert(condition);
216
217 return !!lttng_condition_is_equal(condition_key, condition);
218 }
219
220 static
221 int match_client_list(struct cds_lfht_node *node, const void *key)
222 {
223 struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
224 struct notification_client_list *client_list;
225 struct lttng_condition *condition;
226 struct lttng_condition *condition_key = lttng_trigger_get_condition(
227 trigger_key);
228
229 assert(condition_key);
230
231 client_list = caa_container_of(node, struct notification_client_list,
232 notification_trigger_ht_node);
233 condition = lttng_trigger_get_condition(client_list->trigger);
234
235 return !!lttng_condition_is_equal(condition_key, condition);
236 }
237
238 static
239 int match_client_list_condition(struct cds_lfht_node *node, const void *key)
240 {
241 struct lttng_condition *condition_key = (struct lttng_condition *) key;
242 struct notification_client_list *client_list;
243 struct lttng_condition *condition;
244
245 assert(condition_key);
246
247 client_list = caa_container_of(node, struct notification_client_list,
248 notification_trigger_ht_node);
249 condition = lttng_trigger_get_condition(client_list->trigger);
250
251 return !!lttng_condition_is_equal(condition_key, condition);
252 }
253
254 static
255 unsigned long lttng_condition_buffer_usage_hash(
256 struct lttng_condition *_condition)
257 {
258 unsigned long hash = 0;
259 struct lttng_condition_buffer_usage *condition;
260
261 condition = container_of(_condition,
262 struct lttng_condition_buffer_usage, parent);
263
264 if (condition->session_name) {
265 hash ^= hash_key_str(condition->session_name, lttng_ht_seed);
266 }
267 if (condition->channel_name) {
268 hash ^= hash_key_str(condition->channel_name, lttng_ht_seed);
269 }
270 if (condition->domain.set) {
271 hash ^= hash_key_ulong(
272 (void *) condition->domain.type,
273 lttng_ht_seed);
274 }
275 if (condition->threshold_ratio.set) {
276 uint64_t val;
277
278 val = condition->threshold_ratio.value * (double) UINT32_MAX;
279 hash ^= hash_key_u64(&val, lttng_ht_seed);
280 } else if (condition->threshold_ratio.set) {
281 uint64_t val;
282
283 val = condition->threshold_bytes.value;
284 hash ^= hash_key_u64(&val, lttng_ht_seed);
285 }
286 return hash;
287 }
288
289 /*
290 * The lttng_condition hashing code is kept in this file (rather than
291 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
292 * don't want to link in liblttng-ctl.
293 */
294 static
295 unsigned long lttng_condition_hash(struct lttng_condition *condition)
296 {
297 switch (condition->type) {
298 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
299 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
300 return lttng_condition_buffer_usage_hash(condition);
301 default:
302 ERR("[notification-thread] Unexpected condition type caught");
303 abort();
304 }
305 }
306
307 static
308 void channel_info_destroy(struct channel_info *channel_info)
309 {
310 if (!channel_info) {
311 return;
312 }
313
314 if (channel_info->session_name) {
315 free(channel_info->session_name);
316 }
317 if (channel_info->channel_name) {
318 free(channel_info->channel_name);
319 }
320 free(channel_info);
321 }
322
323 static
324 struct channel_info *channel_info_copy(struct channel_info *channel_info)
325 {
326 struct channel_info *copy = zmalloc(sizeof(*channel_info));
327
328 assert(channel_info);
329 assert(channel_info->session_name);
330 assert(channel_info->channel_name);
331
332 if (!copy) {
333 goto end;
334 }
335
336 memcpy(copy, channel_info, sizeof(*channel_info));
337 copy->session_name = NULL;
338 copy->channel_name = NULL;
339
340 copy->session_name = strdup(channel_info->session_name);
341 if (!copy->session_name) {
342 goto error;
343 }
344 copy->channel_name = strdup(channel_info->channel_name);
345 if (!copy->channel_name) {
346 goto error;
347 }
348 cds_lfht_node_init(&channel_info->channels_ht_node);
349 end:
350 return copy;
351 error:
352 channel_info_destroy(copy);
353 return NULL;
354 }
355
356 static
357 int notification_thread_client_subscribe(struct notification_client *client,
358 struct lttng_condition *condition,
359 struct notification_thread_state *state,
360 enum lttng_notification_channel_status *_status)
361 {
362 int ret = 0;
363 struct cds_lfht_iter iter;
364 struct cds_lfht_node *node;
365 struct notification_client_list *client_list;
366 struct lttng_condition_list_element *condition_list_element = NULL;
367 struct notification_client_list_element *client_list_element = NULL;
368 enum lttng_notification_channel_status status =
369 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
370
371 /*
372 * Ensure that the client has not already subscribed to this condition
373 * before.
374 */
375 cds_list_for_each_entry(condition_list_element, &client->condition_list, node) {
376 if (lttng_condition_is_equal(condition_list_element->condition,
377 condition)) {
378 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED;
379 goto end;
380 }
381 }
382
383 condition_list_element = zmalloc(sizeof(*condition_list_element));
384 if (!condition_list_element) {
385 ret = -1;
386 goto error;
387 }
388 client_list_element = zmalloc(sizeof(*client_list_element));
389 if (!client_list_element) {
390 ret = -1;
391 goto error;
392 }
393
394 rcu_read_lock();
395
396 /*
397 * Add the newly-subscribed condition to the client's subscription list.
398 */
399 CDS_INIT_LIST_HEAD(&condition_list_element->node);
400 condition_list_element->condition = condition;
401 cds_list_add(&condition_list_element->node, &client->condition_list);
402
403 /*
404 * Add the client to the list of clients interested in a given trigger
405 * if a "notification" trigger with a corresponding condition was
406 * added prior.
407 */
408 cds_lfht_lookup(state->notification_trigger_clients_ht,
409 lttng_condition_hash(condition),
410 match_client_list_condition,
411 condition,
412 &iter);
413 node = cds_lfht_iter_get_node(&iter);
414 if (!node) {
415 free(client_list_element);
416 goto end_unlock;
417 }
418
419 client_list = caa_container_of(node, struct notification_client_list,
420 notification_trigger_ht_node);
421 client_list_element->client = client;
422 CDS_INIT_LIST_HEAD(&client_list_element->node);
423 cds_list_add(&client_list_element->node, &client_list->list);
424 end_unlock:
425 rcu_read_unlock();
426 end:
427 if (_status) {
428 *_status = status;
429 }
430 return ret;
431 error:
432 free(condition_list_element);
433 free(client_list_element);
434 return ret;
435 }
436
437 static
438 int notification_thread_client_unsubscribe(
439 struct notification_client *client,
440 struct lttng_condition *condition,
441 struct notification_thread_state *state,
442 enum lttng_notification_channel_status *_status)
443 {
444 struct cds_lfht_iter iter;
445 struct cds_lfht_node *node;
446 struct notification_client_list *client_list;
447 struct lttng_condition_list_element *condition_list_element,
448 *condition_tmp;
449 struct notification_client_list_element *client_list_element,
450 *client_tmp;
451 bool condition_found = false;
452 enum lttng_notification_channel_status status =
453 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
454
455 /* Remove the condition from the client's condition list. */
456 cds_list_for_each_entry_safe(condition_list_element, condition_tmp,
457 &client->condition_list, node) {
458 if (!lttng_condition_is_equal(condition_list_element->condition,
459 condition)) {
460 continue;
461 }
462
463 cds_list_del(&condition_list_element->node);
464 /*
465 * The caller may be iterating on the client's conditions to
466 * tear down a client's connection. In this case, the condition
467 * will be destroyed at the end.
468 */
469 if (condition != condition_list_element->condition) {
470 lttng_condition_destroy(
471 condition_list_element->condition);
472 }
473 free(condition_list_element);
474 condition_found = true;
475 break;
476 }
477
478 if (!condition_found) {
479 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
480 goto end;
481 }
482
483 /*
484 * Remove the client from the list of clients interested the trigger
485 * matching the condition.
486 */
487 rcu_read_lock();
488 cds_lfht_lookup(state->notification_trigger_clients_ht,
489 lttng_condition_hash(condition),
490 match_client_list_condition,
491 condition,
492 &iter);
493 node = cds_lfht_iter_get_node(&iter);
494 if (!node) {
495 goto end_unlock;
496 }
497
498 client_list = caa_container_of(node, struct notification_client_list,
499 notification_trigger_ht_node);
500 cds_list_for_each_entry_safe(client_list_element, client_tmp,
501 &client_list->list, node) {
502 if (client_list_element->client->socket != client->socket) {
503 continue;
504 }
505 cds_list_del(&client_list_element->node);
506 free(client_list_element);
507 break;
508 }
509 end_unlock:
510 rcu_read_unlock();
511 end:
512 lttng_condition_destroy(condition);
513 if (_status) {
514 *_status = status;
515 }
516 return 0;
517 }
518
519 static
520 void notification_client_destroy(struct notification_client *client,
521 struct notification_thread_state *state)
522 {
523 struct lttng_condition_list_element *condition_list_element, *tmp;
524
525 if (!client) {
526 return;
527 }
528
529 /* Release all conditions to which the client was subscribed. */
530 cds_list_for_each_entry_safe(condition_list_element, tmp,
531 &client->condition_list, node) {
532 (void) notification_thread_client_unsubscribe(client,
533 condition_list_element->condition, state, NULL);
534 }
535
536 if (client->socket >= 0) {
537 (void) lttcomm_close_unix_sock(client->socket);
538 }
539 lttng_dynamic_buffer_reset(&client->communication.inbound.buffer);
540 lttng_dynamic_buffer_reset(&client->communication.outbound.buffer);
541 free(client);
542 }
543
544 /*
545 * Call with rcu_read_lock held (and hold for the lifetime of the returned
546 * client pointer).
547 */
548 static
549 struct notification_client *get_client_from_socket(int socket,
550 struct notification_thread_state *state)
551 {
552 struct cds_lfht_iter iter;
553 struct cds_lfht_node *node;
554 struct notification_client *client = NULL;
555
556 cds_lfht_lookup(state->client_socket_ht,
557 hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed),
558 match_client,
559 (void *) (unsigned long) socket,
560 &iter);
561 node = cds_lfht_iter_get_node(&iter);
562 if (!node) {
563 goto end;
564 }
565
566 client = caa_container_of(node, struct notification_client,
567 client_socket_ht_node);
568 end:
569 return client;
570 }
571
572 static
573 bool trigger_applies_to_channel(struct lttng_trigger *trigger,
574 struct channel_info *info)
575 {
576 enum lttng_condition_status status;
577 struct lttng_condition *condition;
578 const char *trigger_session_name = NULL;
579 const char *trigger_channel_name = NULL;
580 enum lttng_domain_type trigger_domain;
581
582 condition = lttng_trigger_get_condition(trigger);
583 if (!condition) {
584 goto fail;
585 }
586
587 switch (lttng_condition_get_type(condition)) {
588 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
589 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
590 break;
591 default:
592 goto fail;
593 }
594
595 status = lttng_condition_buffer_usage_get_domain_type(condition,
596 &trigger_domain);
597 assert(status == LTTNG_CONDITION_STATUS_OK);
598 if (info->key.domain != trigger_domain) {
599 goto fail;
600 }
601
602 status = lttng_condition_buffer_usage_get_session_name(
603 condition, &trigger_session_name);
604 assert((status == LTTNG_CONDITION_STATUS_OK) && trigger_session_name);
605
606 status = lttng_condition_buffer_usage_get_channel_name(
607 condition, &trigger_channel_name);
608 assert((status == LTTNG_CONDITION_STATUS_OK) && trigger_channel_name);
609
610 if (strcmp(info->session_name, trigger_session_name)) {
611 goto fail;
612 }
613 if (strcmp(info->channel_name, trigger_channel_name)) {
614 goto fail;
615 }
616
617 return true;
618 fail:
619 return false;
620 }
621
622 static
623 bool trigger_applies_to_client(struct lttng_trigger *trigger,
624 struct notification_client *client)
625 {
626 bool applies = false;
627 struct lttng_condition_list_element *condition_list_element;
628
629 cds_list_for_each_entry(condition_list_element, &client->condition_list,
630 node) {
631 applies = lttng_condition_is_equal(
632 condition_list_element->condition,
633 lttng_trigger_get_condition(trigger));
634 if (applies) {
635 break;
636 }
637 }
638 return applies;
639 }
640
641 static
642 unsigned long hash_channel_key(struct channel_key *key)
643 {
644 return hash_key_u64(&key->key, lttng_ht_seed) ^ hash_key_ulong(
645 (void *) (unsigned long) key->domain, lttng_ht_seed);
646 }
647
648 static
649 int handle_notification_thread_command_add_channel(
650 struct notification_thread_state *state,
651 struct channel_info *channel_info,
652 enum lttng_error_code *cmd_result)
653 {
654 struct cds_list_head trigger_list;
655 struct channel_info *new_channel_info;
656 struct channel_key *channel_key;
657 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
658 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
659 int trigger_count = 0;
660 struct cds_lfht_iter iter;
661
662 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain",
663 channel_info->channel_name, channel_info->session_name,
664 channel_info->key.key, channel_info->key.domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
665
666 CDS_INIT_LIST_HEAD(&trigger_list);
667
668 new_channel_info = channel_info_copy(channel_info);
669 if (!new_channel_info) {
670 goto error;
671 }
672
673 channel_key = &new_channel_info->key;
674
675 /* Build a list of all triggers applying to the new channel. */
676 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
677 node) {
678 struct lttng_trigger_list_element *new_element;
679
680 if (!trigger_applies_to_channel(trigger_ht_element->trigger,
681 channel_info)) {
682 continue;
683 }
684
685 new_element = zmalloc(sizeof(*new_element));
686 if (!new_element) {
687 goto error;
688 }
689 CDS_INIT_LIST_HEAD(&new_element->node);
690 new_element->trigger = trigger_ht_element->trigger;
691 cds_list_add(&new_element->node, &trigger_list);
692 trigger_count++;
693 }
694
695 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
696 trigger_count);
697 channel_trigger_list = zmalloc(sizeof(*channel_trigger_list));
698 if (!channel_trigger_list) {
699 goto error;
700 }
701 channel_trigger_list->channel_key = *channel_key;
702 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
703 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
704 cds_list_splice(&trigger_list, &channel_trigger_list->list);
705
706 rcu_read_lock();
707 /* Add channel to the channel_ht which owns the channel_infos. */
708 cds_lfht_add(state->channels_ht,
709 hash_channel_key(channel_key),
710 &new_channel_info->channels_ht_node);
711 /*
712 * Add the list of triggers associated with this channel to the
713 * channel_triggers_ht.
714 */
715 cds_lfht_add(state->channel_triggers_ht,
716 hash_channel_key(channel_key),
717 &channel_trigger_list->channel_triggers_ht_node);
718 rcu_read_unlock();
719 *cmd_result = LTTNG_OK;
720 return 0;
721 error:
722 /* Empty trigger list */
723 channel_info_destroy(new_channel_info);
724 return 1;
725 }
726
727 static
728 int handle_notification_thread_command_remove_channel(
729 struct notification_thread_state *state,
730 uint64_t channel_key, enum lttng_domain_type domain,
731 enum lttng_error_code *cmd_result)
732 {
733 struct cds_lfht_node *node;
734 struct cds_lfht_iter iter;
735 struct lttng_channel_trigger_list *trigger_list;
736 struct lttng_trigger_list_element *trigger_list_element, *tmp;
737 struct channel_key key = { .key = channel_key, .domain = domain };
738 struct channel_info *channel_info;
739
740 DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain",
741 channel_key, domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space");
742
743 rcu_read_lock();
744
745 cds_lfht_lookup(state->channel_triggers_ht,
746 hash_channel_key(&key),
747 match_channel_trigger_list,
748 &key,
749 &iter);
750 node = cds_lfht_iter_get_node(&iter);
751 /*
752 * There is a severe internal error if we are being asked to remove a
753 * channel that doesn't exist.
754 */
755 if (!node) {
756 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
757 goto end;
758 }
759
760 /* Free the list of triggers associated with this channel. */
761 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
762 channel_triggers_ht_node);
763 cds_list_for_each_entry_safe(trigger_list_element, tmp,
764 &trigger_list->list, node) {
765 cds_list_del(&trigger_list_element->node);
766 free(trigger_list_element);
767 }
768 cds_lfht_del(state->channel_triggers_ht, node);
769 free(trigger_list);
770
771 /* Free sampled channel state. */
772 cds_lfht_lookup(state->channel_state_ht,
773 hash_channel_key(&key),
774 match_channel_state_sample,
775 &key,
776 &iter);
777 node = cds_lfht_iter_get_node(&iter);
778 /*
779 * This is expected to be NULL if the channel is destroyed before we
780 * received a sample.
781 */
782 if (node) {
783 struct channel_state_sample *sample = caa_container_of(node,
784 struct channel_state_sample,
785 channel_state_ht_node);
786
787 cds_lfht_del(state->channel_state_ht, node);
788 free(sample);
789 }
790
791 /* Remove the channel from the channels_ht and free it. */
792 cds_lfht_lookup(state->channels_ht,
793 hash_channel_key(&key),
794 match_channel_info,
795 &key,
796 &iter);
797 node = cds_lfht_iter_get_node(&iter);
798 assert(node);
799 channel_info = caa_container_of(node, struct channel_info,
800 channels_ht_node);
801 cds_lfht_del(state->channels_ht, node);
802 channel_info_destroy(channel_info);
803 end:
804 rcu_read_unlock();
805 *cmd_result = LTTNG_OK;
806 return 0;
807 }
808
809 /*
810 * FIXME A client's credentials are not checked when registering a trigger, nor
811 * are they stored alongside with the trigger.
812 *
813 * The effects of this are benign:
814 * - The client will succeed in registering the trigger, as it is valid,
815 * - The trigger will, internally, be bound to the channel,
816 * - The notifications will not be sent since the client's credentials
817 * are checked against the channel at that moment.
818 */
819 static
820 int handle_notification_thread_command_register_trigger(
821 struct notification_thread_state *state,
822 struct lttng_trigger *trigger,
823 enum lttng_error_code *cmd_result)
824 {
825 int ret = 0;
826 struct lttng_condition *condition;
827 struct notification_client *client;
828 struct notification_client_list *client_list = NULL;
829 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
830 struct notification_client_list_element *client_list_element, *tmp;
831 struct cds_lfht_node *node;
832 struct cds_lfht_iter iter;
833 struct channel_info *channel;
834 bool free_trigger = true;
835
836 rcu_read_lock();
837
838 condition = lttng_trigger_get_condition(trigger);
839 trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
840 if (!trigger_ht_element) {
841 ret = -1;
842 goto error;
843 }
844
845 /* Add trigger to the trigger_ht. */
846 cds_lfht_node_init(&trigger_ht_element->node);
847 trigger_ht_element->trigger = trigger;
848
849 node = cds_lfht_add_unique(state->triggers_ht,
850 lttng_condition_hash(condition),
851 match_condition,
852 condition,
853 &trigger_ht_element->node);
854 if (node != &trigger_ht_element->node) {
855 /* Not a fatal error, simply report it to the client. */
856 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
857 goto error_free_ht_element;
858 }
859
860 /*
861 * Ownership of the trigger and of its wrapper was transfered to
862 * the triggers_ht.
863 */
864 trigger_ht_element = NULL;
865 free_trigger = false;
866
867 /*
868 * The rest only applies to triggers that have a "notify" action.
869 * It is not skipped as this is the only action type currently
870 * supported.
871 */
872 client_list = zmalloc(sizeof(*client_list));
873 if (!client_list) {
874 ret = -1;
875 goto error_free_ht_element;
876 }
877 cds_lfht_node_init(&client_list->notification_trigger_ht_node);
878 CDS_INIT_LIST_HEAD(&client_list->list);
879 client_list->trigger = trigger;
880
881 /* Build a list of clients to which this new trigger applies. */
882 cds_lfht_for_each_entry(state->client_socket_ht, &iter, client,
883 client_socket_ht_node) {
884 if (!trigger_applies_to_client(trigger, client)) {
885 continue;
886 }
887
888 client_list_element = zmalloc(sizeof(*client_list_element));
889 if (!client_list_element) {
890 ret = -1;
891 goto error_free_client_list;
892 }
893 CDS_INIT_LIST_HEAD(&client_list_element->node);
894 client_list_element->client = client;
895 cds_list_add(&client_list_element->node, &client_list->list);
896 }
897
898 cds_lfht_add(state->notification_trigger_clients_ht,
899 lttng_condition_hash(condition),
900 &client_list->notification_trigger_ht_node);
901 /*
902 * Client list ownership transferred to the
903 * notification_trigger_clients_ht.
904 */
905 client_list = NULL;
906
907 /*
908 * Add the trigger to list of triggers bound to the channels currently
909 * known.
910 */
911 cds_lfht_for_each_entry(state->channels_ht, &iter, channel,
912 channels_ht_node) {
913 struct lttng_trigger_list_element *trigger_list_element;
914 struct lttng_channel_trigger_list *trigger_list;
915
916 if (!trigger_applies_to_channel(trigger, channel)) {
917 continue;
918 }
919
920 cds_lfht_lookup(state->channel_triggers_ht,
921 hash_channel_key(&channel->key),
922 match_channel_trigger_list,
923 &channel->key,
924 &iter);
925 node = cds_lfht_iter_get_node(&iter);
926 assert(node);
927 /* Free the list of triggers associated with this channel. */
928 trigger_list = caa_container_of(node,
929 struct lttng_channel_trigger_list,
930 channel_triggers_ht_node);
931
932 trigger_list_element = zmalloc(sizeof(*trigger_list_element));
933 if (!trigger_list_element) {
934 ret = -1;
935 goto error_free_client_list;
936 }
937 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
938 trigger_list_element->trigger = trigger;
939 cds_list_add(&trigger_list_element->node, &trigger_list->list);
940 /* A trigger can only apply to one channel. */
941 break;
942 }
943
944 *cmd_result = LTTNG_OK;
945 error_free_client_list:
946 if (client_list) {
947 cds_list_for_each_entry_safe(client_list_element, tmp,
948 &client_list->list, node) {
949 free(client_list_element);
950 }
951 free(client_list);
952 }
953 error_free_ht_element:
954 free(trigger_ht_element);
955 error:
956 if (free_trigger) {
957 struct lttng_action *action = lttng_trigger_get_action(trigger);
958
959 lttng_condition_destroy(condition);
960 lttng_action_destroy(action);
961 lttng_trigger_destroy(trigger);
962 }
963 rcu_read_unlock();
964 return ret;
965 }
966
967 static
968 int handle_notification_thread_command_unregister_trigger(
969 struct notification_thread_state *state,
970 struct lttng_trigger *trigger,
971 enum lttng_error_code *_cmd_reply)
972 {
973 struct cds_lfht_iter iter;
974 struct cds_lfht_node *node, *triggers_ht_node;
975 struct lttng_channel_trigger_list *trigger_list;
976 struct notification_client_list *client_list;
977 struct notification_client_list_element *client_list_element, *tmp;
978 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
979 struct lttng_condition *condition = lttng_trigger_get_condition(
980 trigger);
981 struct lttng_action *action;
982 enum lttng_error_code cmd_reply;
983
984 rcu_read_lock();
985
986 cds_lfht_lookup(state->triggers_ht,
987 lttng_condition_hash(condition),
988 match_condition,
989 condition,
990 &iter);
991 triggers_ht_node = cds_lfht_iter_get_node(&iter);
992 if (!triggers_ht_node) {
993 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
994 goto end;
995 } else {
996 cmd_reply = LTTNG_OK;
997 }
998
999 /* Remove trigger from channel_triggers_ht. */
1000 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
1001 channel_triggers_ht_node) {
1002 struct lttng_trigger_list_element *trigger_element, *tmp;
1003
1004 cds_list_for_each_entry_safe(trigger_element, tmp,
1005 &trigger_list->list, node) {
1006 struct lttng_condition *current_condition =
1007 lttng_trigger_get_condition(
1008 trigger_element->trigger);
1009
1010 assert(current_condition);
1011 if (!lttng_condition_is_equal(condition,
1012 current_condition)) {
1013 continue;
1014 }
1015
1016 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1017 cds_list_del(&trigger_element->node);
1018 }
1019 }
1020
1021 /*
1022 * Remove and release the client list from
1023 * notification_trigger_clients_ht.
1024 */
1025 cds_lfht_lookup(state->notification_trigger_clients_ht,
1026 lttng_condition_hash(condition),
1027 match_client_list,
1028 trigger,
1029 &iter);
1030 node = cds_lfht_iter_get_node(&iter);
1031 assert(node);
1032 client_list = caa_container_of(node, struct notification_client_list,
1033 notification_trigger_ht_node);
1034 cds_list_for_each_entry_safe(client_list_element, tmp,
1035 &client_list->list, node) {
1036 free(client_list_element);
1037 }
1038 cds_lfht_del(state->notification_trigger_clients_ht, node);
1039 free(client_list);
1040
1041 /* Remove trigger from triggers_ht. */
1042 trigger_ht_element = caa_container_of(triggers_ht_node,
1043 struct lttng_trigger_ht_element, node);
1044 cds_lfht_del(state->triggers_ht, triggers_ht_node);
1045
1046 condition = lttng_trigger_get_condition(trigger_ht_element->trigger);
1047 lttng_condition_destroy(condition);
1048 action = lttng_trigger_get_action(trigger_ht_element->trigger);
1049 lttng_action_destroy(action);
1050 lttng_trigger_destroy(trigger_ht_element->trigger);
1051 free(trigger_ht_element);
1052 end:
1053 rcu_read_unlock();
1054 if (_cmd_reply) {
1055 *_cmd_reply = cmd_reply;
1056 }
1057 return 0;
1058 }
1059
1060 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1061 int handle_notification_thread_command(
1062 struct notification_thread_handle *handle,
1063 struct notification_thread_state *state)
1064 {
1065 int ret;
1066 uint64_t counter;
1067 struct notification_thread_command *cmd;
1068
1069 /* Read event_fd to put it back into a quiescent state. */
1070 ret = read(handle->cmd_queue.event_fd, &counter, sizeof(counter));
1071 if (ret == -1) {
1072 goto error;
1073 }
1074
1075 pthread_mutex_lock(&handle->cmd_queue.lock);
1076 cmd = cds_list_first_entry(&handle->cmd_queue.list,
1077 struct notification_thread_command, cmd_list_node);
1078 switch (cmd->type) {
1079 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
1080 DBG("[notification-thread] Received register trigger command");
1081 ret = handle_notification_thread_command_register_trigger(
1082 state, cmd->parameters.trigger,
1083 &cmd->reply_code);
1084 break;
1085 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
1086 DBG("[notification-thread] Received unregister trigger command");
1087 ret = handle_notification_thread_command_unregister_trigger(
1088 state, cmd->parameters.trigger,
1089 &cmd->reply_code);
1090 break;
1091 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
1092 DBG("[notification-thread] Received add channel command");
1093 ret = handle_notification_thread_command_add_channel(
1094 state, &cmd->parameters.add_channel,
1095 &cmd->reply_code);
1096 break;
1097 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
1098 DBG("[notification-thread] Received remove channel command");
1099 ret = handle_notification_thread_command_remove_channel(
1100 state, cmd->parameters.remove_channel.key,
1101 cmd->parameters.remove_channel.domain,
1102 &cmd->reply_code);
1103 break;
1104 case NOTIFICATION_COMMAND_TYPE_QUIT:
1105 DBG("[notification-thread] Received quit command");
1106 cmd->reply_code = LTTNG_OK;
1107 ret = 1;
1108 goto end;
1109 default:
1110 ERR("[notification-thread] Unknown internal command received");
1111 goto error_unlock;
1112 }
1113
1114 if (ret) {
1115 goto error_unlock;
1116 }
1117 end:
1118 cds_list_del(&cmd->cmd_list_node);
1119 lttng_waiter_wake_up(&cmd->reply_waiter);
1120 pthread_mutex_unlock(&handle->cmd_queue.lock);
1121 return ret;
1122 error_unlock:
1123 /* Wake-up and return a fatal error to the calling thread. */
1124 lttng_waiter_wake_up(&cmd->reply_waiter);
1125 pthread_mutex_unlock(&handle->cmd_queue.lock);
1126 cmd->reply_code = LTTNG_ERR_FATAL;
1127 error:
1128 /* Indicate a fatal error to the caller. */
1129 return -1;
1130 }
1131
1132 static
1133 unsigned long hash_client_socket(int socket)
1134 {
1135 return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed);
1136 }
1137
1138 static
1139 int socket_set_non_blocking(int socket)
1140 {
1141 int ret, flags;
1142
1143 /* Set the pipe as non-blocking. */
1144 ret = fcntl(socket, F_GETFL, 0);
1145 if (ret == -1) {
1146 PERROR("fcntl get socket flags");
1147 goto end;
1148 }
1149 flags = ret;
1150
1151 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
1152 if (ret == -1) {
1153 PERROR("fcntl set O_NONBLOCK socket flag");
1154 goto end;
1155 }
1156 DBG("Client socket (fd = %i) set as non-blocking", socket);
1157 end:
1158 return ret;
1159 }
1160
1161 static
1162 int client_reset_inbound_state(struct notification_client *client)
1163 {
1164 int ret;
1165
1166 ret = lttng_dynamic_buffer_set_size(
1167 &client->communication.inbound.buffer, 0);
1168 assert(!ret);
1169
1170 client->communication.inbound.bytes_to_receive =
1171 sizeof(struct lttng_notification_channel_message);
1172 client->communication.inbound.msg_type =
1173 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
1174 client->communication.inbound.receive_creds = false;
1175 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
1176 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
1177 ret = lttng_dynamic_buffer_set_size(
1178 &client->communication.inbound.buffer,
1179 client->communication.inbound.bytes_to_receive);
1180 return ret;
1181 }
1182
1183 int handle_notification_thread_client_connect(
1184 struct notification_thread_state *state)
1185 {
1186 int ret;
1187 struct notification_client *client;
1188
1189 DBG("[notification-thread] Handling new notification channel client connection");
1190
1191 client = zmalloc(sizeof(*client));
1192 if (!client) {
1193 /* Fatal error. */
1194 ret = -1;
1195 goto error;
1196 }
1197 CDS_INIT_LIST_HEAD(&client->condition_list);
1198 lttng_dynamic_buffer_init(&client->communication.inbound.buffer);
1199 lttng_dynamic_buffer_init(&client->communication.outbound.buffer);
1200 ret = client_reset_inbound_state(client);
1201 if (ret) {
1202 ERR("[notification-thread] Failed to reset client communication's inbound state");
1203 ret = 0;
1204 goto error;
1205 }
1206
1207 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
1208 if (ret < 0) {
1209 ERR("[notification-thread] Failed to accept new notification channel client connection");
1210 ret = 0;
1211 goto error;
1212 }
1213
1214 client->socket = ret;
1215
1216 ret = socket_set_non_blocking(client->socket);
1217 if (ret) {
1218 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1219 goto error;
1220 }
1221
1222 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
1223 if (ret < 0) {
1224 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1225 ret = 0;
1226 goto error;
1227 }
1228
1229 ret = lttng_poll_add(&state->events, client->socket,
1230 LPOLLIN | LPOLLERR |
1231 LPOLLHUP | LPOLLRDHUP);
1232 if (ret < 0) {
1233 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1234 ret = 0;
1235 goto error;
1236 }
1237 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1238 client->socket);
1239
1240 rcu_read_lock();
1241 cds_lfht_add(state->client_socket_ht,
1242 hash_client_socket(client->socket),
1243 &client->client_socket_ht_node);
1244 rcu_read_unlock();
1245
1246 return ret;
1247 error:
1248 notification_client_destroy(client, state);
1249 return ret;
1250 }
1251
1252 int handle_notification_thread_client_disconnect(
1253 int client_socket,
1254 struct notification_thread_state *state)
1255 {
1256 int ret = 0;
1257 struct notification_client *client;
1258
1259 rcu_read_lock();
1260 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1261 client_socket);
1262 client = get_client_from_socket(client_socket, state);
1263 if (!client) {
1264 /* Internal state corruption, fatal error. */
1265 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1266 client_socket);
1267 ret = -1;
1268 goto end;
1269 }
1270
1271 ret = lttng_poll_del(&state->events, client_socket);
1272 if (ret) {
1273 ERR("[notification-thread] Failed to remove client socket from poll set");
1274 }
1275 cds_lfht_del(state->client_socket_ht,
1276 &client->client_socket_ht_node);
1277 notification_client_destroy(client, state);
1278 end:
1279 rcu_read_unlock();
1280 return ret;
1281 }
1282
1283 int handle_notification_thread_client_disconnect_all(
1284 struct notification_thread_state *state)
1285 {
1286 struct cds_lfht_iter iter;
1287 struct notification_client *client;
1288 bool error_encoutered = false;
1289
1290 rcu_read_lock();
1291 DBG("[notification-thread] Closing all client connections");
1292 cds_lfht_for_each_entry(state->client_socket_ht, &iter, client,
1293 client_socket_ht_node) {
1294 int ret;
1295
1296 ret = handle_notification_thread_client_disconnect(
1297 client->socket, state);
1298 if (ret) {
1299 error_encoutered = true;
1300 }
1301 }
1302 rcu_read_unlock();
1303 return error_encoutered ? 1 : 0;
1304 }
1305
1306 int handle_notification_thread_trigger_unregister_all(
1307 struct notification_thread_state *state)
1308 {
1309 bool error_occured = false;
1310 struct cds_lfht_iter iter;
1311 struct lttng_trigger_ht_element *trigger_ht_element;
1312
1313 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1314 node) {
1315 int ret = handle_notification_thread_command_unregister_trigger(
1316 state, trigger_ht_element->trigger, NULL);
1317 if (ret) {
1318 error_occured = true;
1319 }
1320 }
1321 return error_occured ? -1 : 0;
1322 }
1323
1324 static
1325 int client_flush_outgoing_queue(struct notification_client *client,
1326 struct notification_thread_state *state)
1327 {
1328 ssize_t ret;
1329 size_t to_send_count;
1330
1331 assert(client->communication.outbound.buffer.size != 0);
1332 to_send_count = client->communication.outbound.buffer.size;
1333 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1334 client->socket);
1335
1336 ret = lttcomm_send_unix_sock_non_block(client->socket,
1337 client->communication.outbound.buffer.data,
1338 to_send_count);
1339 if ((ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) ||
1340 (ret > 0 && ret < to_send_count)) {
1341 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1342 client->socket);
1343 to_send_count -= max(ret, 0);
1344
1345 memcpy(client->communication.outbound.buffer.data,
1346 client->communication.outbound.buffer.data +
1347 client->communication.outbound.buffer.size - to_send_count,
1348 to_send_count);
1349 ret = lttng_dynamic_buffer_set_size(
1350 &client->communication.outbound.buffer,
1351 to_send_count);
1352 if (ret) {
1353 goto error;
1354 }
1355
1356 /*
1357 * We want to be notified whenever there is buffer space
1358 * available to send the rest of the payload.
1359 */
1360 ret = lttng_poll_mod(&state->events, client->socket,
1361 CLIENT_POLL_MASK_IN_OUT);
1362 if (ret) {
1363 goto error;
1364 }
1365 } else if (ret < 0) {
1366 /* Generic error, disconnect the client. */
1367 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1368 client->socket);
1369 ret = handle_notification_thread_client_disconnect(
1370 client->socket, state);
1371 if (ret) {
1372 goto error;
1373 }
1374 } else {
1375 /* No error and flushed the queue completely. */
1376 ret = lttng_dynamic_buffer_set_size(
1377 &client->communication.outbound.buffer, 0);
1378 if (ret) {
1379 goto error;
1380 }
1381 ret = lttng_poll_mod(&state->events, client->socket,
1382 CLIENT_POLL_MASK_IN);
1383 if (ret) {
1384 goto error;
1385 }
1386
1387 client->communication.outbound.queued_command_reply = false;
1388 client->communication.outbound.dropped_notification = false;
1389 }
1390
1391 return 0;
1392 error:
1393 return -1;
1394 }
1395
1396 static
1397 int client_send_command_reply(struct notification_client *client,
1398 struct notification_thread_state *state,
1399 enum lttng_notification_channel_status status)
1400 {
1401 int ret;
1402 struct lttng_notification_channel_command_reply reply = {
1403 .status = (int8_t) status,
1404 };
1405 struct lttng_notification_channel_message msg = {
1406 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
1407 .size = sizeof(reply),
1408 };
1409 char buffer[sizeof(msg) + sizeof(reply)];
1410
1411 if (client->communication.outbound.queued_command_reply) {
1412 /* Protocol error. */
1413 goto error;
1414 }
1415
1416 memcpy(buffer, &msg, sizeof(msg));
1417 memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
1418 DBG("[notification-thread] Send command reply (%i)", (int) status);
1419
1420 /* Enqueue buffer to outgoing queue and flush it. */
1421 ret = lttng_dynamic_buffer_append(
1422 &client->communication.outbound.buffer,
1423 buffer, sizeof(buffer));
1424 if (ret) {
1425 goto error;
1426 }
1427
1428 ret = client_flush_outgoing_queue(client, state);
1429 if (ret) {
1430 goto error;
1431 }
1432
1433 if (client->communication.outbound.buffer.size != 0) {
1434 /* Queue could not be emptied. */
1435 client->communication.outbound.queued_command_reply = true;
1436 }
1437
1438 return 0;
1439 error:
1440 return -1;
1441 }
1442
1443 static
1444 int client_dispatch_message(struct notification_client *client,
1445 struct notification_thread_state *state)
1446 {
1447 int ret = 0;
1448
1449 if (client->communication.inbound.msg_type !=
1450 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE &&
1451 client->communication.inbound.msg_type !=
1452 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN &&
1453 !client->validated) {
1454 WARN("[notification-thread] client attempted a command before handshake");
1455 ret = -1;
1456 goto end;
1457 }
1458
1459 switch (client->communication.inbound.msg_type) {
1460 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN:
1461 {
1462 /*
1463 * Receiving message header. The function will be called again
1464 * once the rest of the message as been received and can be
1465 * interpreted.
1466 */
1467 const struct lttng_notification_channel_message *msg;
1468
1469 assert(sizeof(*msg) ==
1470 client->communication.inbound.buffer.size);
1471 msg = (const struct lttng_notification_channel_message *)
1472 client->communication.inbound.buffer.data;
1473
1474 if (msg->size == 0 || msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
1475 ERR("[notification-thread] Invalid notification channel message: length = %u", msg->size);
1476 ret = -1;
1477 goto end;
1478 }
1479
1480 switch (msg->type) {
1481 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
1482 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
1483 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
1484 break;
1485 default:
1486 ret = -1;
1487 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1488 goto end;
1489 }
1490
1491 client->communication.inbound.bytes_to_receive = msg->size;
1492 client->communication.inbound.msg_type =
1493 (enum lttng_notification_channel_message_type) msg->type;
1494 if (client->communication.inbound.msg_type ==
1495 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE) {
1496 client->communication.inbound.receive_creds = true;
1497 }
1498 ret = lttng_dynamic_buffer_set_size(
1499 &client->communication.inbound.buffer, msg->size);
1500 if (ret) {
1501 goto end;
1502 }
1503 break;
1504 }
1505 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
1506 {
1507 struct lttng_notification_channel_command_handshake *handshake_client;
1508 struct lttng_notification_channel_command_handshake handshake_reply = {
1509 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
1510 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
1511 };
1512 struct lttng_notification_channel_message msg_header = {
1513 .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
1514 .size = sizeof(handshake_reply),
1515 };
1516 enum lttng_notification_channel_status status =
1517 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1518 char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
1519
1520 memcpy(send_buffer, &msg_header, sizeof(msg_header));
1521 memcpy(send_buffer + sizeof(msg_header), &handshake_reply,
1522 sizeof(handshake_reply));
1523
1524 handshake_client =
1525 (struct lttng_notification_channel_command_handshake *)
1526 client->communication.inbound.buffer.data;
1527 client->major = handshake_client->major;
1528 client->minor = handshake_client->minor;
1529 if (!client->communication.inbound.creds_received) {
1530 ERR("[notification-thread] No credentials received from client");
1531 ret = -1;
1532 goto end;
1533 }
1534
1535 client->uid = LTTNG_SOCK_GET_UID_CRED(
1536 &client->communication.inbound.creds);
1537 client->gid = LTTNG_SOCK_GET_GID_CRED(
1538 &client->communication.inbound.creds);
1539 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1540 client->uid, client->gid, (int) client->major,
1541 (int) client->minor);
1542
1543 if (handshake_client->major != LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
1544 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
1545 }
1546
1547 ret = lttng_dynamic_buffer_append(&client->communication.outbound.buffer,
1548 send_buffer, sizeof(send_buffer));
1549 if (ret) {
1550 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1551 goto end;
1552 }
1553
1554 ret = client_flush_outgoing_queue(client, state);
1555 if (ret) {
1556 goto end;
1557 }
1558
1559 ret = client_send_command_reply(client, state, status);
1560 if (ret) {
1561 ERR("[notification-thread] Failed to send reply to notification channel client");
1562 goto end;
1563 }
1564
1565 /* Set reception state to receive the next message header. */
1566 ret = client_reset_inbound_state(client);
1567 if (ret) {
1568 ERR("[notification-thread] Failed to reset client communication's inbound state");
1569 goto end;
1570 }
1571 client->validated = true;
1572 break;
1573 }
1574 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
1575 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
1576 {
1577 struct lttng_condition *condition;
1578 enum lttng_notification_channel_status status =
1579 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1580 const struct lttng_buffer_view condition_view =
1581 lttng_buffer_view_from_dynamic_buffer(
1582 &client->communication.inbound.buffer,
1583 0, -1);
1584 size_t expected_condition_size =
1585 client->communication.inbound.buffer.size;
1586
1587 ret = lttng_condition_create_from_buffer(&condition_view,
1588 &condition);
1589 if (ret != expected_condition_size) {
1590 ERR("[notification-thread] Malformed condition received from client");
1591 goto end;
1592 }
1593
1594 if (client->communication.inbound.msg_type ==
1595 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
1596 /*
1597 * FIXME The current state should be evaluated on
1598 * subscription.
1599 */
1600 ret = notification_thread_client_subscribe(client,
1601 condition, state, &status);
1602 } else {
1603 ret = notification_thread_client_unsubscribe(client,
1604 condition, state, &status);
1605 }
1606 if (ret) {
1607 goto end;
1608 }
1609
1610 ret = client_send_command_reply(client, state, status);
1611 if (ret) {
1612 ERR("[notification-thread] Failed to send reply to notification channel client");
1613 goto end;
1614 }
1615
1616 /* Set reception state to receive the next message header. */
1617 ret = client_reset_inbound_state(client);
1618 if (ret) {
1619 ERR("[notification-thread] Failed to reset client communication's inbound state");
1620 goto end;
1621 }
1622 break;
1623 }
1624 default:
1625 abort();
1626 }
1627 end:
1628 return ret;
1629 }
1630
1631 /* Incoming data from client. */
1632 int handle_notification_thread_client_in(
1633 struct notification_thread_state *state, int socket)
1634 {
1635 int ret = 0;
1636 struct notification_client *client;
1637 ssize_t recv_ret;
1638 size_t offset;
1639
1640 client = get_client_from_socket(socket, state);
1641 if (!client) {
1642 /* Internal error, abort. */
1643 ret = -1;
1644 goto end;
1645 }
1646
1647 offset = client->communication.inbound.buffer.size -
1648 client->communication.inbound.bytes_to_receive;
1649 if (client->communication.inbound.receive_creds) {
1650 recv_ret = lttcomm_recv_creds_unix_sock(socket,
1651 client->communication.inbound.buffer.data + offset,
1652 client->communication.inbound.bytes_to_receive,
1653 &client->communication.inbound.creds);
1654 if (recv_ret > 0) {
1655 client->communication.inbound.receive_creds = false;
1656 client->communication.inbound.creds_received = true;
1657 }
1658 } else {
1659 recv_ret = lttcomm_recv_unix_sock_non_block(socket,
1660 client->communication.inbound.buffer.data + offset,
1661 client->communication.inbound.bytes_to_receive);
1662 }
1663 if (recv_ret < 0) {
1664 goto error_disconnect_client;
1665 }
1666
1667 client->communication.inbound.bytes_to_receive -= recv_ret;
1668 if (client->communication.inbound.bytes_to_receive == 0) {
1669 ret = client_dispatch_message(client, state);
1670 if (ret) {
1671 /*
1672 * Only returns an error if this client must be
1673 * disconnected.
1674 */
1675 goto error_disconnect_client;
1676 }
1677 } else {
1678 goto end;
1679 }
1680 end:
1681 return ret;
1682 error_disconnect_client:
1683 ret = handle_notification_thread_client_disconnect(socket, state);
1684 return ret;
1685 }
1686
1687 /* Client ready to receive outgoing data. */
1688 int handle_notification_thread_client_out(
1689 struct notification_thread_state *state, int socket)
1690 {
1691 int ret;
1692 struct notification_client *client;
1693
1694 client = get_client_from_socket(socket, state);
1695 if (!client) {
1696 /* Internal error, abort. */
1697 ret = -1;
1698 goto end;
1699 }
1700
1701 ret = client_flush_outgoing_queue(client, state);
1702 if (ret) {
1703 goto end;
1704 }
1705 end:
1706 return ret;
1707 }
1708
1709 static
1710 bool evaluate_buffer_usage_condition(struct lttng_condition *condition,
1711 struct channel_state_sample *sample, uint64_t buffer_capacity)
1712 {
1713 bool result = false;
1714 uint64_t threshold;
1715 enum lttng_condition_type condition_type;
1716 struct lttng_condition_buffer_usage *use_condition = container_of(
1717 condition, struct lttng_condition_buffer_usage,
1718 parent);
1719
1720 if (!sample) {
1721 goto end;
1722 }
1723
1724 if (use_condition->threshold_bytes.set) {
1725 threshold = use_condition->threshold_bytes.value;
1726 } else {
1727 /*
1728 * Threshold was expressed as a ratio.
1729 *
1730 * TODO the threshold (in bytes) of conditions expressed
1731 * as a ratio of total buffer size could be cached to
1732 * forego this double-multiplication or it could be performed
1733 * as fixed-point math.
1734 *
1735 * Note that caching should accomodate the case where the
1736 * condition applies to multiple channels (i.e. don't assume
1737 * that all channels matching my_chann* have the same size...)
1738 */
1739 threshold = (uint64_t) (use_condition->threshold_ratio.value *
1740 (double) buffer_capacity);
1741 }
1742
1743 condition_type = lttng_condition_get_type(condition);
1744 if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) {
1745 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
1746 threshold, sample->highest_usage);
1747
1748 /*
1749 * The low condition should only be triggered once _all_ of the
1750 * streams in a channel have gone below the "low" threshold.
1751 */
1752 if (sample->highest_usage <= threshold) {
1753 result = true;
1754 }
1755 } else {
1756 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
1757 threshold, sample->highest_usage);
1758
1759 /*
1760 * For high buffer usage scenarios, we want to trigger whenever
1761 * _any_ of the streams has reached the "high" threshold.
1762 */
1763 if (sample->highest_usage >= threshold) {
1764 result = true;
1765 }
1766 }
1767 end:
1768 return result;
1769 }
1770
1771 static
1772 int evaluate_condition(struct lttng_condition *condition,
1773 struct lttng_evaluation **evaluation,
1774 struct notification_thread_state *state,
1775 struct channel_state_sample *previous_sample,
1776 struct channel_state_sample *latest_sample,
1777 uint64_t buffer_capacity)
1778 {
1779 int ret = 0;
1780 enum lttng_condition_type condition_type;
1781 bool previous_sample_result;
1782 bool latest_sample_result;
1783
1784 condition_type = lttng_condition_get_type(condition);
1785 /* No other condition type supported for the moment. */
1786 assert(condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW ||
1787 condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH);
1788
1789 previous_sample_result = evaluate_buffer_usage_condition(condition,
1790 previous_sample, buffer_capacity);
1791 latest_sample_result = evaluate_buffer_usage_condition(condition,
1792 latest_sample, buffer_capacity);
1793
1794 if (!latest_sample_result ||
1795 (previous_sample_result == latest_sample_result)) {
1796 /*
1797 * Only trigger on a condition evaluation transition.
1798 *
1799 * NOTE: This edge-triggered logic may not be appropriate for
1800 * future condition types.
1801 */
1802 goto end;
1803 }
1804
1805 if (evaluation && latest_sample_result) {
1806 *evaluation = lttng_evaluation_buffer_usage_create(
1807 condition_type,
1808 latest_sample->highest_usage,
1809 buffer_capacity);
1810 if (!*evaluation) {
1811 ret = -1;
1812 goto end;
1813 }
1814 }
1815 end:
1816 return ret;
1817 }
1818
1819 static
1820 int client_enqueue_dropped_notification(struct notification_client *client,
1821 struct notification_thread_state *state)
1822 {
1823 int ret;
1824 struct lttng_notification_channel_message msg = {
1825 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED,
1826 .size = 0,
1827 };
1828
1829 ret = lttng_dynamic_buffer_append(
1830 &client->communication.outbound.buffer, &msg,
1831 sizeof(msg));
1832 return ret;
1833 }
1834
1835 static
1836 int send_evaluation_to_clients(struct lttng_trigger *trigger,
1837 struct lttng_evaluation *evaluation,
1838 struct notification_client_list* client_list,
1839 struct notification_thread_state *state,
1840 uid_t channel_uid, gid_t channel_gid)
1841 {
1842 int ret = 0;
1843 struct lttng_dynamic_buffer msg_buffer;
1844 struct notification_client_list_element *client_list_element, *tmp;
1845 struct lttng_notification *notification;
1846 struct lttng_condition *condition;
1847 ssize_t expected_notification_size, notification_size;
1848 struct lttng_notification_channel_message msg;
1849
1850 lttng_dynamic_buffer_init(&msg_buffer);
1851
1852 condition = lttng_trigger_get_condition(trigger);
1853 assert(condition);
1854
1855 notification = lttng_notification_create(condition, evaluation);
1856 if (!notification) {
1857 ret = -1;
1858 goto end;
1859 }
1860
1861 expected_notification_size = lttng_notification_serialize(notification,
1862 NULL);
1863 if (expected_notification_size < 0) {
1864 ERR("[notification-thread] Failed to get size of serialized notification");
1865 ret = -1;
1866 goto end;
1867 }
1868
1869 msg.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION;
1870 msg.size = (uint32_t) expected_notification_size;
1871 ret = lttng_dynamic_buffer_append(&msg_buffer, &msg, sizeof(msg));
1872 if (ret) {
1873 goto end;
1874 }
1875
1876 ret = lttng_dynamic_buffer_set_size(&msg_buffer,
1877 msg_buffer.size + expected_notification_size);
1878 if (ret) {
1879 goto end;
1880 }
1881
1882 notification_size = lttng_notification_serialize(notification,
1883 msg_buffer.data + sizeof(msg));
1884 if (notification_size != expected_notification_size) {
1885 ERR("[notification-thread] Failed to serialize notification");
1886 ret = -1;
1887 goto end;
1888 }
1889
1890 cds_list_for_each_entry_safe(client_list_element, tmp,
1891 &client_list->list, node) {
1892 struct notification_client *client =
1893 client_list_element->client;
1894
1895 if (client->uid != channel_uid && client->gid != channel_gid &&
1896 client->uid != 0) {
1897 /* Client is not allowed to monitor this channel. */
1898 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
1899 continue;
1900 }
1901
1902 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
1903 client->socket, msg_buffer.size);
1904 if (client->communication.outbound.buffer.size) {
1905 /*
1906 * Outgoing data is already buffered for this client;
1907 * drop the notification and enqueue a "dropped
1908 * notification" message if this is the first dropped
1909 * notification since the socket spilled-over to the
1910 * queue.
1911 */
1912 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
1913 client->socket);
1914 if (!client->communication.outbound.dropped_notification) {
1915 client->communication.outbound.dropped_notification = true;
1916 ret = client_enqueue_dropped_notification(
1917 client, state);
1918 if (ret) {
1919 goto end;
1920 }
1921 }
1922 continue;
1923 }
1924
1925 ret = lttng_dynamic_buffer_append_buffer(
1926 &client->communication.outbound.buffer,
1927 &msg_buffer);
1928 if (ret) {
1929 goto end;
1930 }
1931
1932 ret = client_flush_outgoing_queue(client, state);
1933 if (ret) {
1934 goto end;
1935 }
1936 }
1937 ret = 0;
1938 end:
1939 lttng_notification_destroy(notification);
1940 lttng_dynamic_buffer_reset(&msg_buffer);
1941 return ret;
1942 }
1943
1944 int handle_notification_thread_channel_sample(
1945 struct notification_thread_state *state, int pipe,
1946 enum lttng_domain_type domain)
1947 {
1948 int ret = 0;
1949 struct lttcomm_consumer_channel_monitor_msg sample_msg;
1950 struct channel_state_sample previous_sample, latest_sample;
1951 struct channel_info *channel_info;
1952 struct cds_lfht_node *node;
1953 struct cds_lfht_iter iter;
1954 struct lttng_channel_trigger_list *trigger_list;
1955 struct lttng_trigger_list_element *trigger_list_element;
1956 bool previous_sample_available = false;
1957
1958 /*
1959 * The monitoring pipe only holds messages smaller than PIPE_BUF,
1960 * ensuring that read/write of sampling messages are atomic.
1961 */
1962 ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg));
1963 if (ret != sizeof(sample_msg)) {
1964 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
1965 pipe);
1966 ret = -1;
1967 goto end;
1968 }
1969
1970 ret = 0;
1971 latest_sample.key.key = sample_msg.key;
1972 latest_sample.key.domain = domain;
1973 latest_sample.highest_usage = sample_msg.highest;
1974 latest_sample.lowest_usage = sample_msg.lowest;
1975
1976 rcu_read_lock();
1977
1978 /* Retrieve the channel's informations */
1979 cds_lfht_lookup(state->channels_ht,
1980 hash_channel_key(&latest_sample.key),
1981 match_channel_info,
1982 &latest_sample.key,
1983 &iter);
1984 node = cds_lfht_iter_get_node(&iter);
1985 if (!node) {
1986 /*
1987 * Not an error since the consumer can push a sample to the pipe
1988 * and the rest of the session daemon could notify us of the
1989 * channel's destruction before we get a chance to process that
1990 * sample.
1991 */
1992 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain",
1993 latest_sample.key.key,
1994 domain == LTTNG_DOMAIN_KERNEL ? "kernel" :
1995 "user space");
1996 goto end_unlock;
1997 }
1998 channel_info = caa_container_of(node, struct channel_info,
1999 channels_ht_node);
2000 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64")",
2001 channel_info->channel_name,
2002 latest_sample.key.key,
2003 channel_info->session_name,
2004 latest_sample.highest_usage,
2005 latest_sample.lowest_usage);
2006
2007 /* Retrieve the channel's last sample, if it exists, and update it. */
2008 cds_lfht_lookup(state->channel_state_ht,
2009 hash_channel_key(&latest_sample.key),
2010 match_channel_state_sample,
2011 &latest_sample.key,
2012 &iter);
2013 node = cds_lfht_iter_get_node(&iter);
2014 if (node) {
2015 struct channel_state_sample *stored_sample;
2016
2017 /* Update the sample stored. */
2018 stored_sample = caa_container_of(node,
2019 struct channel_state_sample,
2020 channel_state_ht_node);
2021 memcpy(&previous_sample, stored_sample,
2022 sizeof(previous_sample));
2023 stored_sample->highest_usage = latest_sample.highest_usage;
2024 stored_sample->lowest_usage = latest_sample.lowest_usage;
2025 previous_sample_available = true;
2026 } else {
2027 /*
2028 * This is the channel's first sample, allocate space for and
2029 * store the new sample.
2030 */
2031 struct channel_state_sample *stored_sample;
2032
2033 stored_sample = zmalloc(sizeof(*stored_sample));
2034 if (!stored_sample) {
2035 ret = -1;
2036 goto end_unlock;
2037 }
2038
2039 memcpy(stored_sample, &latest_sample, sizeof(*stored_sample));
2040 cds_lfht_node_init(&stored_sample->channel_state_ht_node);
2041 cds_lfht_add(state->channel_state_ht,
2042 hash_channel_key(&stored_sample->key),
2043 &stored_sample->channel_state_ht_node);
2044 }
2045
2046 /* Find triggers associated with this channel. */
2047 cds_lfht_lookup(state->channel_triggers_ht,
2048 hash_channel_key(&latest_sample.key),
2049 match_channel_trigger_list,
2050 &latest_sample.key,
2051 &iter);
2052 node = cds_lfht_iter_get_node(&iter);
2053 if (!node) {
2054 goto end_unlock;
2055 }
2056
2057 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
2058 channel_triggers_ht_node);
2059 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
2060 node) {
2061 struct lttng_condition *condition;
2062 struct lttng_action *action;
2063 struct lttng_trigger *trigger;
2064 struct notification_client_list *client_list;
2065 struct lttng_evaluation *evaluation = NULL;
2066
2067 trigger = trigger_list_element->trigger;
2068 condition = lttng_trigger_get_condition(trigger);
2069 assert(condition);
2070 action = lttng_trigger_get_action(trigger);
2071
2072 /* Notify actions are the only type currently supported. */
2073 assert(lttng_action_get_type(action) ==
2074 LTTNG_ACTION_TYPE_NOTIFY);
2075
2076 /*
2077 * Check if any client is subscribed to the result of this
2078 * evaluation.
2079 */
2080 cds_lfht_lookup(state->notification_trigger_clients_ht,
2081 lttng_condition_hash(condition),
2082 match_client_list,
2083 trigger,
2084 &iter);
2085 node = cds_lfht_iter_get_node(&iter);
2086 assert(node);
2087
2088 client_list = caa_container_of(node,
2089 struct notification_client_list,
2090 notification_trigger_ht_node);
2091 if (cds_list_empty(&client_list->list)) {
2092 /*
2093 * No clients interested in the evaluation's result,
2094 * skip it.
2095 */
2096 continue;
2097 }
2098
2099 ret = evaluate_condition(condition, &evaluation, state,
2100 previous_sample_available ? &previous_sample : NULL,
2101 &latest_sample, channel_info->capacity);
2102 if (ret) {
2103 goto end_unlock;
2104 }
2105
2106 if (!evaluation) {
2107 continue;
2108 }
2109
2110 /* Dispatch evaluation result to all clients. */
2111 ret = send_evaluation_to_clients(trigger_list_element->trigger,
2112 evaluation, client_list, state,
2113 channel_info->uid, channel_info->gid);
2114 if (ret) {
2115 goto end_unlock;
2116 }
2117 }
2118 end_unlock:
2119 rcu_read_unlock();
2120 end:
2121 return ret;
2122 }
This page took 0.110777 seconds and 6 git commands to generate.