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>
17 #include <common/payload.h>
18 #include <common/payload-view.h>
19 #include <common/unix.h>
21 #include "lttng-ctl-helper.h"
22 #include <common/compat/poll.h>
25 int handshake(struct lttng_notification_channel
*channel
);
28 * Populates the reception buffer with the next complete message.
29 * The caller must acquire the channel's lock.
32 int receive_message(struct lttng_notification_channel
*channel
)
35 struct lttng_notification_channel_message msg
;
37 lttng_payload_clear(&channel
->reception_payload
);
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_payload
.buffer
, &msg
,
57 /* Reserve space for the payload. */
58 ret
= lttng_dynamic_buffer_set_size(&channel
->reception_payload
.buffer
,
59 channel
->reception_payload
.buffer
.size
+ msg
.size
);
64 /* Receive message payload. */
65 ret
= lttcomm_recv_unix_sock(channel
->socket
,
66 channel
->reception_payload
.buffer
.data
+ sizeof(msg
), msg
.size
);
67 if (ret
< (ssize_t
) msg
.size
) {
72 /* Receive message fds. */
74 ret
= lttcomm_recv_payload_fds_unix_sock(channel
->socket
,
75 msg
.fds
, &channel
->reception_payload
);
76 if (ret
< sizeof(int) * msg
.fds
) {
85 lttng_payload_clear(&channel
->reception_payload
);
90 enum lttng_notification_channel_message_type
get_current_message_type(
91 struct lttng_notification_channel
*channel
)
93 struct lttng_notification_channel_message
*msg
;
95 assert(channel
->reception_payload
.buffer
.size
>= sizeof(*msg
));
97 msg
= (struct lttng_notification_channel_message
*)
98 channel
->reception_payload
.buffer
.data
;
99 return (enum lttng_notification_channel_message_type
) msg
->type
;
103 struct lttng_notification
*create_notification_from_current_message(
104 struct lttng_notification_channel
*channel
)
107 struct lttng_notification
*notification
= NULL
;
109 if (channel
->reception_payload
.buffer
.size
<=
110 sizeof(struct lttng_notification_channel_message
)) {
115 struct lttng_payload_view view
= lttng_payload_view_from_payload(
116 &channel
->reception_payload
,
117 sizeof(struct lttng_notification_channel_message
),
120 ret
= lttng_notification_create_from_payload(
121 &view
, ¬ification
);
124 if (ret
!= channel
->reception_payload
.buffer
.size
-
125 sizeof(struct lttng_notification_channel_message
)) {
126 lttng_notification_destroy(notification
);
134 struct lttng_notification_channel
*lttng_notification_channel_create(
135 struct lttng_endpoint
*endpoint
)
138 bool is_in_tracing_group
= false, is_root
= false;
139 char *sock_path
= NULL
;
140 struct lttng_notification_channel
*channel
= NULL
;
143 endpoint
!= lttng_session_daemon_notification_endpoint
) {
147 sock_path
= zmalloc(LTTNG_PATH_MAX
);
152 channel
= zmalloc(sizeof(struct lttng_notification_channel
));
156 channel
->socket
= -1;
157 pthread_mutex_init(&channel
->lock
, NULL
);
158 lttng_payload_init(&channel
->reception_payload
);
159 CDS_INIT_LIST_HEAD(&channel
->pending_notifications
.list
);
161 is_root
= (getuid() == 0);
163 is_in_tracing_group
= lttng_check_tracing_group();
166 if (is_root
|| is_in_tracing_group
) {
167 lttng_ctl_copy_string(sock_path
,
168 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
,
170 ret
= lttcomm_connect_unix_sock(sock_path
);
177 /* Fallback to local session daemon. */
178 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
179 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
180 utils_get_home_dir());
181 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
185 ret
= lttcomm_connect_unix_sock(sock_path
);
192 channel
->socket
= fd
;
194 ret
= handshake(channel
);
202 lttng_notification_channel_destroy(channel
);
207 enum lttng_notification_channel_status
208 lttng_notification_channel_get_next_notification(
209 struct lttng_notification_channel
*channel
,
210 struct lttng_notification
**_notification
)
213 struct lttng_notification
*notification
= NULL
;
214 enum lttng_notification_channel_status status
=
215 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
216 struct lttng_poll_event events
;
218 if (!channel
|| !_notification
) {
219 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
223 pthread_mutex_lock(&channel
->lock
);
225 if (channel
->pending_notifications
.count
) {
226 struct pending_notification
*pending_notification
;
228 assert(!cds_list_empty(&channel
->pending_notifications
.list
));
230 /* Deliver one of the pending notifications. */
231 pending_notification
= cds_list_first_entry(
232 &channel
->pending_notifications
.list
,
233 struct pending_notification
,
235 notification
= pending_notification
->notification
;
237 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
239 cds_list_del(&pending_notification
->node
);
240 channel
->pending_notifications
.count
--;
241 free(pending_notification
);
246 * Block on interruptible epoll/poll() instead of the message reception
247 * itself as the recvmsg() wrappers always restart on EINTR. We choose
248 * to wait using interruptible epoll/poll() in order to:
249 * 1) Return if a signal occurs,
250 * 2) Not deal with partially received messages.
252 * The drawback to this approach is that we assume that messages
253 * are complete/well formed. If a message is shorter than its
254 * announced length, receive_message() will block on recvmsg()
255 * and never return (even if a signal is received).
257 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
259 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
262 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
264 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
267 ret
= lttng_poll_wait_interruptible(&events
, -1);
269 status
= (ret
== -1 && errno
== EINTR
) ?
270 LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
:
271 LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
275 ret
= receive_message(channel
);
277 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
281 switch (get_current_message_type(channel
)) {
282 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
283 notification
= create_notification_from_current_message(
286 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
290 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
291 /* No payload to consume. */
292 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
295 /* Protocol error. */
296 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
301 lttng_poll_clean(&events
);
303 pthread_mutex_unlock(&channel
->lock
);
304 *_notification
= notification
;
310 int enqueue_dropped_notification(
311 struct lttng_notification_channel
*channel
)
314 struct pending_notification
*pending_notification
;
315 struct cds_list_head
*last_element
=
316 channel
->pending_notifications
.list
.prev
;
318 pending_notification
= caa_container_of(last_element
,
319 struct pending_notification
, node
);
320 if (!pending_notification
->notification
) {
322 * The last enqueued notification indicates dropped
323 * notifications; there is nothing to do as we group
324 * dropped notifications together.
329 if (channel
->pending_notifications
.count
>=
330 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
&&
331 pending_notification
->notification
) {
333 * Discard the last enqueued notification to indicate
334 * that notifications were dropped at this point.
336 lttng_notification_destroy(
337 pending_notification
->notification
);
338 pending_notification
->notification
= NULL
;
342 pending_notification
= zmalloc(sizeof(*pending_notification
));
343 if (!pending_notification
) {
347 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
348 cds_list_add(&pending_notification
->node
,
349 &channel
->pending_notifications
.list
);
350 channel
->pending_notifications
.count
++;
356 int enqueue_notification_from_current_message(
357 struct lttng_notification_channel
*channel
)
360 struct lttng_notification
*notification
;
361 struct pending_notification
*pending_notification
;
363 if (channel
->pending_notifications
.count
>=
364 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
) {
365 /* Drop the notification. */
366 ret
= enqueue_dropped_notification(channel
);
370 pending_notification
= zmalloc(sizeof(*pending_notification
));
371 if (!pending_notification
) {
375 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
377 notification
= create_notification_from_current_message(channel
);
383 pending_notification
->notification
= notification
;
384 cds_list_add(&pending_notification
->node
,
385 &channel
->pending_notifications
.list
);
386 channel
->pending_notifications
.count
++;
390 free(pending_notification
);
394 enum lttng_notification_channel_status
395 lttng_notification_channel_has_pending_notification(
396 struct lttng_notification_channel
*channel
,
397 bool *_notification_pending
)
400 enum lttng_notification_channel_status status
=
401 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
402 struct lttng_poll_event events
;
404 if (!channel
|| !_notification_pending
) {
405 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
409 pthread_mutex_lock(&channel
->lock
);
411 if (channel
->pending_notifications
.count
) {
412 *_notification_pending
= true;
416 if (channel
->socket
< 0) {
417 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
;
422 * Check, without blocking, if data is available on the channel's
423 * socket. If there is data available, it is safe to read (blocking)
424 * on the socket for a message from the session daemon.
426 * Since all commands wait for the session daemon's reply before
427 * releasing the channel's lock, the protocol only allows for
428 * notifications and "notification dropped" messages to come
429 * through. If we receive a different message type, it is
430 * considered a protocol error.
432 * Note that this function is not guaranteed not to block. This
433 * will block until our peer (the session daemon) has sent a complete
434 * message if we see data available on the socket. If the peer does
435 * not respect the protocol, this may block indefinitely.
437 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
439 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
442 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
444 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
447 /* timeout = 0: return immediately. */
448 ret
= lttng_poll_wait_interruptible(&events
, 0);
450 /* No data available. */
451 *_notification_pending
= false;
453 } else if (ret
< 0) {
454 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
458 /* Data available on socket. */
459 ret
= receive_message(channel
);
461 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
465 switch (get_current_message_type(channel
)) {
466 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
467 ret
= enqueue_notification_from_current_message(channel
);
471 *_notification_pending
= true;
473 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
474 ret
= enqueue_dropped_notification(channel
);
478 *_notification_pending
= true;
481 /* Protocol error. */
482 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
487 lttng_poll_clean(&events
);
489 pthread_mutex_unlock(&channel
->lock
);
495 int receive_command_reply(struct lttng_notification_channel
*channel
,
496 enum lttng_notification_channel_status
*status
)
499 struct lttng_notification_channel_command_reply
*reply
;
502 enum lttng_notification_channel_message_type msg_type
;
504 ret
= receive_message(channel
);
509 msg_type
= get_current_message_type(channel
);
511 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
:
513 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
514 ret
= enqueue_notification_from_current_message(
520 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
521 ret
= enqueue_dropped_notification(channel
);
526 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
528 struct lttng_notification_channel_command_handshake
*handshake
;
530 handshake
= (struct lttng_notification_channel_command_handshake
*)
531 (channel
->reception_payload
.buffer
.data
+
532 sizeof(struct lttng_notification_channel_message
));
533 channel
->version
.major
= handshake
->major
;
534 channel
->version
.minor
= handshake
->minor
;
535 channel
->version
.set
= true;
545 if (channel
->reception_payload
.buffer
.size
<
546 (sizeof(struct lttng_notification_channel_message
) +
548 /* Invalid message received. */
553 reply
= (struct lttng_notification_channel_command_reply
*)
554 (channel
->reception_payload
.buffer
.data
+
555 sizeof(struct lttng_notification_channel_message
));
556 *status
= (enum lttng_notification_channel_status
) reply
->status
;
562 int handshake(struct lttng_notification_channel
*channel
)
565 enum lttng_notification_channel_status status
=
566 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
567 struct lttng_notification_channel_command_handshake handshake
= {
568 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
569 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
571 struct lttng_notification_channel_message msg_header
= {
572 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
573 .size
= sizeof(handshake
),
575 char send_buffer
[sizeof(msg_header
) + sizeof(handshake
)];
577 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
578 memcpy(send_buffer
+ sizeof(msg_header
), &handshake
, sizeof(handshake
));
580 pthread_mutex_lock(&channel
->lock
);
582 ret
= lttcomm_send_creds_unix_sock(channel
->socket
, send_buffer
,
583 sizeof(send_buffer
));
588 /* Receive handshake info from the sessiond. */
589 ret
= receive_command_reply(channel
, &status
);
594 if (!channel
->version
.set
) {
599 if (channel
->version
.major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
605 pthread_mutex_unlock(&channel
->lock
);
610 enum lttng_notification_channel_status
send_condition_command(
611 struct lttng_notification_channel
*channel
,
612 enum lttng_notification_channel_message_type type
,
613 const struct lttng_condition
*condition
)
617 enum lttng_notification_channel_status status
=
618 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
619 struct lttng_payload payload
;
620 struct lttng_notification_channel_message cmd_header
= {
621 .type
= (int8_t) type
,
624 lttng_payload_init(&payload
);
627 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
631 assert(type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
||
632 type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
);
634 pthread_mutex_lock(&channel
->lock
);
635 socket
= channel
->socket
;
637 if (!lttng_condition_validate(condition
)) {
638 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
642 ret
= lttng_dynamic_buffer_append(&payload
.buffer
, &cmd_header
,
645 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
649 ret
= lttng_condition_serialize(condition
, &payload
);
651 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
655 /* Update payload length. */
656 ((struct lttng_notification_channel_message
*) payload
.buffer
.data
)->size
=
657 (uint32_t) (payload
.buffer
.size
- sizeof(cmd_header
));
660 struct lttng_payload_view pv
=
661 lttng_payload_view_from_payload(
664 lttng_payload_view_get_fd_handle_count(&pv
);
666 /* Update fd count. */
667 ((struct lttng_notification_channel_message
*) payload
.buffer
.data
)->fds
=
670 ret
= lttcomm_send_unix_sock(
671 socket
, pv
.buffer
.data
, pv
.buffer
.size
);
673 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
677 /* Pass fds if present. */
679 ret
= lttcomm_send_payload_view_fds_unix_sock(socket
,
682 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
688 ret
= receive_command_reply(channel
, &status
);
690 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
694 pthread_mutex_unlock(&channel
->lock
);
696 lttng_payload_reset(&payload
);
700 enum lttng_notification_channel_status
lttng_notification_channel_subscribe(
701 struct lttng_notification_channel
*channel
,
702 const struct lttng_condition
*condition
)
704 return send_condition_command(channel
,
705 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
,
709 enum lttng_notification_channel_status
lttng_notification_channel_unsubscribe(
710 struct lttng_notification_channel
*channel
,
711 const struct lttng_condition
*condition
)
713 return send_condition_command(channel
,
714 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
,
718 void lttng_notification_channel_destroy(
719 struct lttng_notification_channel
*channel
)
725 if (channel
->socket
>= 0) {
726 (void) lttcomm_close_unix_sock(channel
->socket
);
728 pthread_mutex_destroy(&channel
->lock
);
729 lttng_payload_reset(&channel
->reception_payload
);