2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/notification/notification-internal.h>
9 #include <lttng/notification/channel-internal.h>
10 #include <lttng/condition/condition-internal.h>
11 #include <lttng/endpoint.h>
12 #include <common/defaults.h>
13 #include <common/error.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/utils.h>
16 #include <common/defaults.h>
18 #include "lttng-ctl-helper.h"
19 #include <common/compat/poll.h>
22 int handshake(struct lttng_notification_channel
*channel
);
25 * Populates the reception buffer with the next complete message.
26 * The caller must acquire the channel's lock.
29 int receive_message(struct lttng_notification_channel
*channel
)
32 struct lttng_notification_channel_message msg
;
34 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
39 ret
= lttcomm_recv_unix_sock(channel
->socket
, &msg
, sizeof(msg
));
45 if (msg
.size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
50 /* Add message header at buffer's start. */
51 ret
= lttng_dynamic_buffer_append(&channel
->reception_buffer
, &msg
,
57 /* Reserve space for the payload. */
58 ret
= lttng_dynamic_buffer_set_size(&channel
->reception_buffer
,
59 channel
->reception_buffer
.size
+ msg
.size
);
64 /* Receive message payload. */
65 ret
= lttcomm_recv_unix_sock(channel
->socket
,
66 channel
->reception_buffer
.data
+ sizeof(msg
), msg
.size
);
67 if (ret
< (ssize_t
) msg
.size
) {
75 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
82 enum lttng_notification_channel_message_type
get_current_message_type(
83 struct lttng_notification_channel
*channel
)
85 struct lttng_notification_channel_message
*msg
;
87 assert(channel
->reception_buffer
.size
>= sizeof(*msg
));
89 msg
= (struct lttng_notification_channel_message
*)
90 channel
->reception_buffer
.data
;
91 return (enum lttng_notification_channel_message_type
) msg
->type
;
95 struct lttng_notification
*create_notification_from_current_message(
96 struct lttng_notification_channel
*channel
)
99 struct lttng_notification
*notification
= NULL
;
100 struct lttng_buffer_view view
;
102 if (channel
->reception_buffer
.size
<=
103 sizeof(struct lttng_notification_channel_message
)) {
107 view
= lttng_buffer_view_from_dynamic_buffer(&channel
->reception_buffer
,
108 sizeof(struct lttng_notification_channel_message
), -1);
110 ret
= lttng_notification_create_from_buffer(&view
, ¬ification
);
111 if (ret
!= channel
->reception_buffer
.size
-
112 sizeof(struct lttng_notification_channel_message
)) {
113 lttng_notification_destroy(notification
);
121 struct lttng_notification_channel
*lttng_notification_channel_create(
122 struct lttng_endpoint
*endpoint
)
125 bool is_in_tracing_group
= false, is_root
= false;
126 char *sock_path
= NULL
;
127 struct lttng_notification_channel
*channel
= NULL
;
130 endpoint
!= lttng_session_daemon_notification_endpoint
) {
134 sock_path
= zmalloc(LTTNG_PATH_MAX
);
139 channel
= zmalloc(sizeof(struct lttng_notification_channel
));
143 channel
->socket
= -1;
144 pthread_mutex_init(&channel
->lock
, NULL
);
145 lttng_dynamic_buffer_init(&channel
->reception_buffer
);
146 CDS_INIT_LIST_HEAD(&channel
->pending_notifications
.list
);
148 is_root
= (getuid() == 0);
150 is_in_tracing_group
= lttng_check_tracing_group();
153 if (is_root
|| is_in_tracing_group
) {
154 lttng_ctl_copy_string(sock_path
,
155 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
,
157 ret
= lttcomm_connect_unix_sock(sock_path
);
164 /* Fallback to local session daemon. */
165 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
166 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
167 utils_get_home_dir());
168 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
172 ret
= lttcomm_connect_unix_sock(sock_path
);
179 channel
->socket
= fd
;
181 ret
= handshake(channel
);
189 lttng_notification_channel_destroy(channel
);
194 enum lttng_notification_channel_status
195 lttng_notification_channel_get_next_notification(
196 struct lttng_notification_channel
*channel
,
197 struct lttng_notification
**_notification
)
200 struct lttng_notification
*notification
= NULL
;
201 enum lttng_notification_channel_status status
=
202 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
203 struct lttng_poll_event events
;
205 if (!channel
|| !_notification
) {
206 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
210 pthread_mutex_lock(&channel
->lock
);
212 if (channel
->pending_notifications
.count
) {
213 struct pending_notification
*pending_notification
;
215 assert(!cds_list_empty(&channel
->pending_notifications
.list
));
217 /* Deliver one of the pending notifications. */
218 pending_notification
= cds_list_first_entry(
219 &channel
->pending_notifications
.list
,
220 struct pending_notification
,
222 notification
= pending_notification
->notification
;
224 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
226 cds_list_del(&pending_notification
->node
);
227 channel
->pending_notifications
.count
--;
228 free(pending_notification
);
233 * Block on interruptible epoll/poll() instead of the message reception
234 * itself as the recvmsg() wrappers always restart on EINTR. We choose
235 * to wait using interruptible epoll/poll() in order to:
236 * 1) Return if a signal occurs,
237 * 2) Not deal with partially received messages.
239 * The drawback to this approach is that we assume that messages
240 * are complete/well formed. If a message is shorter than its
241 * announced length, receive_message() will block on recvmsg()
242 * and never return (even if a signal is received).
244 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
246 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
249 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
251 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
254 ret
= lttng_poll_wait_interruptible(&events
, -1);
256 status
= (ret
== -1 && errno
== EINTR
) ?
257 LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
:
258 LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
262 ret
= receive_message(channel
);
264 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
268 switch (get_current_message_type(channel
)) {
269 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
270 notification
= create_notification_from_current_message(
273 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
277 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
278 /* No payload to consume. */
279 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
282 /* Protocol error. */
283 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
288 lttng_poll_clean(&events
);
290 pthread_mutex_unlock(&channel
->lock
);
291 *_notification
= notification
;
297 int enqueue_dropped_notification(
298 struct lttng_notification_channel
*channel
)
301 struct pending_notification
*pending_notification
;
302 struct cds_list_head
*last_element
=
303 channel
->pending_notifications
.list
.prev
;
305 pending_notification
= caa_container_of(last_element
,
306 struct pending_notification
, node
);
307 if (!pending_notification
->notification
) {
309 * The last enqueued notification indicates dropped
310 * notifications; there is nothing to do as we group
311 * dropped notifications together.
316 if (channel
->pending_notifications
.count
>=
317 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
&&
318 pending_notification
->notification
) {
320 * Discard the last enqueued notification to indicate
321 * that notifications were dropped at this point.
323 lttng_notification_destroy(
324 pending_notification
->notification
);
325 pending_notification
->notification
= NULL
;
329 pending_notification
= zmalloc(sizeof(*pending_notification
));
330 if (!pending_notification
) {
334 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
335 cds_list_add(&pending_notification
->node
,
336 &channel
->pending_notifications
.list
);
337 channel
->pending_notifications
.count
++;
343 int enqueue_notification_from_current_message(
344 struct lttng_notification_channel
*channel
)
347 struct lttng_notification
*notification
;
348 struct pending_notification
*pending_notification
;
350 if (channel
->pending_notifications
.count
>=
351 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
) {
352 /* Drop the notification. */
353 ret
= enqueue_dropped_notification(channel
);
357 pending_notification
= zmalloc(sizeof(*pending_notification
));
358 if (!pending_notification
) {
362 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
364 notification
= create_notification_from_current_message(channel
);
370 pending_notification
->notification
= notification
;
371 cds_list_add(&pending_notification
->node
,
372 &channel
->pending_notifications
.list
);
373 channel
->pending_notifications
.count
++;
377 free(pending_notification
);
381 enum lttng_notification_channel_status
382 lttng_notification_channel_has_pending_notification(
383 struct lttng_notification_channel
*channel
,
384 bool *_notification_pending
)
387 enum lttng_notification_channel_status status
=
388 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
389 struct lttng_poll_event events
;
391 if (!channel
|| !_notification_pending
) {
392 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
396 pthread_mutex_lock(&channel
->lock
);
398 if (channel
->pending_notifications
.count
) {
399 *_notification_pending
= true;
403 if (channel
->socket
< 0) {
404 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
;
409 * Check, without blocking, if data is available on the channel's
410 * socket. If there is data available, it is safe to read (blocking)
411 * on the socket for a message from the session daemon.
413 * Since all commands wait for the session daemon's reply before
414 * releasing the channel's lock, the protocol only allows for
415 * notifications and "notification dropped" messages to come
416 * through. If we receive a different message type, it is
417 * considered a protocol error.
419 * Note that this function is not guaranteed not to block. This
420 * will block until our peer (the session daemon) has sent a complete
421 * message if we see data available on the socket. If the peer does
422 * not respect the protocol, this may block indefinitely.
424 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
426 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
429 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
431 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
434 /* timeout = 0: return immediately. */
435 ret
= lttng_poll_wait_interruptible(&events
, 0);
437 /* No data available. */
438 *_notification_pending
= false;
440 } else if (ret
< 0) {
441 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
445 /* Data available on socket. */
446 ret
= receive_message(channel
);
448 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
452 switch (get_current_message_type(channel
)) {
453 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
454 ret
= enqueue_notification_from_current_message(channel
);
458 *_notification_pending
= true;
460 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
461 ret
= enqueue_dropped_notification(channel
);
465 *_notification_pending
= true;
468 /* Protocol error. */
469 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
474 lttng_poll_clean(&events
);
476 pthread_mutex_unlock(&channel
->lock
);
482 int receive_command_reply(struct lttng_notification_channel
*channel
,
483 enum lttng_notification_channel_status
*status
)
486 struct lttng_notification_channel_command_reply
*reply
;
489 enum lttng_notification_channel_message_type msg_type
;
491 ret
= receive_message(channel
);
496 msg_type
= get_current_message_type(channel
);
498 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
:
500 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
501 ret
= enqueue_notification_from_current_message(
507 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
508 ret
= enqueue_dropped_notification(channel
);
513 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
515 struct lttng_notification_channel_command_handshake
*handshake
;
517 handshake
= (struct lttng_notification_channel_command_handshake
*)
518 (channel
->reception_buffer
.data
+
519 sizeof(struct lttng_notification_channel_message
));
520 channel
->version
.major
= handshake
->major
;
521 channel
->version
.minor
= handshake
->minor
;
522 channel
->version
.set
= true;
532 if (channel
->reception_buffer
.size
<
533 (sizeof(struct lttng_notification_channel_message
) +
535 /* Invalid message received. */
540 reply
= (struct lttng_notification_channel_command_reply
*)
541 (channel
->reception_buffer
.data
+
542 sizeof(struct lttng_notification_channel_message
));
543 *status
= (enum lttng_notification_channel_status
) reply
->status
;
549 int handshake(struct lttng_notification_channel
*channel
)
552 enum lttng_notification_channel_status status
=
553 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
554 struct lttng_notification_channel_command_handshake handshake
= {
555 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
556 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
558 struct lttng_notification_channel_message msg_header
= {
559 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
560 .size
= sizeof(handshake
),
562 char send_buffer
[sizeof(msg_header
) + sizeof(handshake
)];
564 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
565 memcpy(send_buffer
+ sizeof(msg_header
), &handshake
, sizeof(handshake
));
567 pthread_mutex_lock(&channel
->lock
);
569 ret
= lttcomm_send_creds_unix_sock(channel
->socket
, send_buffer
,
570 sizeof(send_buffer
));
575 /* Receive handshake info from the sessiond. */
576 ret
= receive_command_reply(channel
, &status
);
581 if (!channel
->version
.set
) {
586 if (channel
->version
.major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
592 pthread_mutex_unlock(&channel
->lock
);
597 enum lttng_notification_channel_status
send_condition_command(
598 struct lttng_notification_channel
*channel
,
599 enum lttng_notification_channel_message_type type
,
600 const struct lttng_condition
*condition
)
604 enum lttng_notification_channel_status status
=
605 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
606 struct lttng_dynamic_buffer buffer
;
607 struct lttng_notification_channel_message cmd_header
= {
608 .type
= (int8_t) type
,
611 lttng_dynamic_buffer_init(&buffer
);
614 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
618 assert(type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
||
619 type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
);
621 pthread_mutex_lock(&channel
->lock
);
622 socket
= channel
->socket
;
623 if (!lttng_condition_validate(condition
)) {
624 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
628 ret
= lttng_dynamic_buffer_append(&buffer
, &cmd_header
,
631 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
635 ret
= lttng_condition_serialize(condition
, &buffer
);
637 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
641 /* Update payload length. */
642 ((struct lttng_notification_channel_message
*) buffer
.data
)->size
=
643 (uint32_t) (buffer
.size
- sizeof(cmd_header
));
645 ret
= lttcomm_send_unix_sock(socket
, buffer
.data
, buffer
.size
);
647 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
651 ret
= receive_command_reply(channel
, &status
);
653 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
657 pthread_mutex_unlock(&channel
->lock
);
659 lttng_dynamic_buffer_reset(&buffer
);
663 enum lttng_notification_channel_status
lttng_notification_channel_subscribe(
664 struct lttng_notification_channel
*channel
,
665 const struct lttng_condition
*condition
)
667 return send_condition_command(channel
,
668 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
,
672 enum lttng_notification_channel_status
lttng_notification_channel_unsubscribe(
673 struct lttng_notification_channel
*channel
,
674 const struct lttng_condition
*condition
)
676 return send_condition_command(channel
,
677 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
,
681 void lttng_notification_channel_destroy(
682 struct lttng_notification_channel
*channel
)
688 if (channel
->socket
>= 0) {
689 (void) lttcomm_close_unix_sock(channel
->socket
);
691 pthread_mutex_destroy(&channel
->lock
);
692 lttng_dynamic_buffer_reset(&channel
->reception_buffer
);