2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library 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 Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/notification/notification-internal.h>
19 #include <lttng/notification/channel-internal.h>
20 #include <lttng/condition/condition-internal.h>
21 #include <lttng/endpoint.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/dynamic-buffer.h>
25 #include <common/utils.h>
26 #include <common/defaults.h>
28 #include "lttng-ctl-helper.h"
29 #include <common/compat/poll.h>
32 int handshake(struct lttng_notification_channel
*channel
);
35 * Populates the reception buffer with the next complete message.
36 * The caller must acquire the channel's lock.
39 int receive_message(struct lttng_notification_channel
*channel
)
42 struct lttng_notification_channel_message msg
;
44 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
49 ret
= lttcomm_recv_unix_sock(channel
->socket
, &msg
, sizeof(msg
));
55 if (msg
.size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
60 /* Add message header at buffer's start. */
61 ret
= lttng_dynamic_buffer_append(&channel
->reception_buffer
, &msg
,
67 /* Reserve space for the payload. */
68 ret
= lttng_dynamic_buffer_set_size(&channel
->reception_buffer
,
69 channel
->reception_buffer
.size
+ msg
.size
);
74 /* Receive message payload. */
75 ret
= lttcomm_recv_unix_sock(channel
->socket
,
76 channel
->reception_buffer
.data
+ sizeof(msg
), msg
.size
);
77 if (ret
< (ssize_t
) msg
.size
) {
85 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
92 enum lttng_notification_channel_message_type
get_current_message_type(
93 struct lttng_notification_channel
*channel
)
95 struct lttng_notification_channel_message
*msg
;
97 assert(channel
->reception_buffer
.size
>= sizeof(*msg
));
99 msg
= (struct lttng_notification_channel_message
*)
100 channel
->reception_buffer
.data
;
101 return (enum lttng_notification_channel_message_type
) msg
->type
;
105 struct lttng_notification
*create_notification_from_current_message(
106 struct lttng_notification_channel
*channel
)
109 struct lttng_notification
*notification
= NULL
;
110 struct lttng_buffer_view view
;
112 if (channel
->reception_buffer
.size
<=
113 sizeof(struct lttng_notification_channel_message
)) {
117 view
= lttng_buffer_view_from_dynamic_buffer(&channel
->reception_buffer
,
118 sizeof(struct lttng_notification_channel_message
), -1);
120 ret
= lttng_notification_create_from_buffer(&view
, ¬ification
);
121 if (ret
!= channel
->reception_buffer
.size
-
122 sizeof(struct lttng_notification_channel_message
)) {
123 lttng_notification_destroy(notification
);
131 struct lttng_notification_channel
*lttng_notification_channel_create(
132 struct lttng_endpoint
*endpoint
)
135 bool is_in_tracing_group
= false, is_root
= false;
136 char *sock_path
= NULL
;
137 struct lttng_notification_channel
*channel
= NULL
;
140 endpoint
!= lttng_session_daemon_notification_endpoint
) {
144 sock_path
= zmalloc(LTTNG_PATH_MAX
);
149 channel
= zmalloc(sizeof(struct lttng_notification_channel
));
153 channel
->socket
= -1;
154 pthread_mutex_init(&channel
->lock
, NULL
);
155 lttng_dynamic_buffer_init(&channel
->reception_buffer
);
156 CDS_INIT_LIST_HEAD(&channel
->pending_notifications
.list
);
158 is_root
= (getuid() == 0);
160 is_in_tracing_group
= lttng_check_tracing_group();
163 if (is_root
|| is_in_tracing_group
) {
164 lttng_ctl_copy_string(sock_path
,
165 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
,
167 ret
= lttcomm_connect_unix_sock(sock_path
);
174 /* Fallback to local session daemon. */
175 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
176 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
177 utils_get_home_dir());
178 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
182 ret
= lttcomm_connect_unix_sock(sock_path
);
189 channel
->socket
= fd
;
191 ret
= handshake(channel
);
199 lttng_notification_channel_destroy(channel
);
204 enum lttng_notification_channel_status
205 lttng_notification_channel_get_next_notification(
206 struct lttng_notification_channel
*channel
,
207 struct lttng_notification
**_notification
)
210 struct lttng_notification
*notification
= NULL
;
211 enum lttng_notification_channel_status status
=
212 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
213 struct lttng_poll_event events
;
215 if (!channel
|| !_notification
) {
216 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
220 pthread_mutex_lock(&channel
->lock
);
222 if (channel
->pending_notifications
.count
) {
223 struct pending_notification
*pending_notification
;
225 assert(!cds_list_empty(&channel
->pending_notifications
.list
));
227 /* Deliver one of the pending notifications. */
228 pending_notification
= cds_list_first_entry(
229 &channel
->pending_notifications
.list
,
230 struct pending_notification
,
232 notification
= pending_notification
->notification
;
234 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
236 cds_list_del(&pending_notification
->node
);
237 channel
->pending_notifications
.count
--;
238 free(pending_notification
);
243 * Block on interruptible epoll/poll() instead of the message reception
244 * itself as the recvmsg() wrappers always restart on EINTR. We choose
245 * to wait using interruptible epoll/poll() in order to:
246 * 1) Return if a signal occurs,
247 * 2) Not deal with partially received messages.
249 * The drawback to this approach is that we assume that messages
250 * are complete/well formed. If a message is shorter than its
251 * announced length, receive_message() will block on recvmsg()
252 * and never return (even if a signal is received).
254 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
256 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
259 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
261 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
264 ret
= lttng_poll_wait_interruptible(&events
, -1);
266 status
= (ret
== -1 && errno
== EINTR
) ?
267 LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
:
268 LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
272 ret
= receive_message(channel
);
274 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
278 switch (get_current_message_type(channel
)) {
279 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
280 notification
= create_notification_from_current_message(
283 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
287 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
288 /* No payload to consume. */
289 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
292 /* Protocol error. */
293 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
298 lttng_poll_clean(&events
);
300 pthread_mutex_unlock(&channel
->lock
);
301 *_notification
= notification
;
307 int enqueue_dropped_notification(
308 struct lttng_notification_channel
*channel
)
311 struct pending_notification
*pending_notification
;
312 struct cds_list_head
*last_element
=
313 channel
->pending_notifications
.list
.prev
;
315 pending_notification
= caa_container_of(last_element
,
316 struct pending_notification
, node
);
317 if (!pending_notification
->notification
) {
319 * The last enqueued notification indicates dropped
320 * notifications; there is nothing to do as we group
321 * dropped notifications together.
326 if (channel
->pending_notifications
.count
>=
327 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
&&
328 pending_notification
->notification
) {
330 * Discard the last enqueued notification to indicate
331 * that notifications were dropped at this point.
333 lttng_notification_destroy(
334 pending_notification
->notification
);
335 pending_notification
->notification
= NULL
;
339 pending_notification
= zmalloc(sizeof(*pending_notification
));
340 if (!pending_notification
) {
344 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
345 cds_list_add(&pending_notification
->node
,
346 &channel
->pending_notifications
.list
);
347 channel
->pending_notifications
.count
++;
353 int enqueue_notification_from_current_message(
354 struct lttng_notification_channel
*channel
)
357 struct lttng_notification
*notification
;
358 struct pending_notification
*pending_notification
;
360 if (channel
->pending_notifications
.count
>=
361 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
) {
362 /* Drop the notification. */
363 ret
= enqueue_dropped_notification(channel
);
367 pending_notification
= zmalloc(sizeof(*pending_notification
));
368 if (!pending_notification
) {
372 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
374 notification
= create_notification_from_current_message(channel
);
380 pending_notification
->notification
= notification
;
381 cds_list_add(&pending_notification
->node
,
382 &channel
->pending_notifications
.list
);
383 channel
->pending_notifications
.count
++;
387 free(pending_notification
);
391 enum lttng_notification_channel_status
392 lttng_notification_channel_has_pending_notification(
393 struct lttng_notification_channel
*channel
,
394 bool *_notification_pending
)
397 enum lttng_notification_channel_status status
=
398 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
399 struct lttng_poll_event events
;
401 if (!channel
|| !_notification_pending
) {
402 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
406 pthread_mutex_lock(&channel
->lock
);
408 if (channel
->pending_notifications
.count
) {
409 *_notification_pending
= true;
413 if (channel
->socket
< 0) {
414 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
;
419 * Check, without blocking, if data is available on the channel's
420 * socket. If there is data available, it is safe to read (blocking)
421 * on the socket for a message from the session daemon.
423 * Since all commands wait for the session daemon's reply before
424 * releasing the channel's lock, the protocol only allows for
425 * notifications and "notification dropped" messages to come
426 * through. If we receive a different message type, it is
427 * considered a protocol error.
429 * Note that this function is not guaranteed not to block. This
430 * will block until our peer (the session daemon) has sent a complete
431 * message if we see data available on the socket. If the peer does
432 * not respect the protocol, this may block indefinitely.
434 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
436 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
439 ret
= lttng_poll_add(&events
, channel
->socket
, LPOLLIN
| LPOLLERR
);
441 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
444 /* timeout = 0: return immediately. */
445 ret
= lttng_poll_wait_interruptible(&events
, 0);
447 /* No data available. */
448 *_notification_pending
= false;
450 } else if (ret
< 0) {
451 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
455 /* Data available on socket. */
456 ret
= receive_message(channel
);
458 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
462 switch (get_current_message_type(channel
)) {
463 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
464 ret
= enqueue_notification_from_current_message(channel
);
468 *_notification_pending
= true;
470 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
471 ret
= enqueue_dropped_notification(channel
);
475 *_notification_pending
= true;
478 /* Protocol error. */
479 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
484 lttng_poll_clean(&events
);
486 pthread_mutex_unlock(&channel
->lock
);
492 int receive_command_reply(struct lttng_notification_channel
*channel
,
493 enum lttng_notification_channel_status
*status
)
496 struct lttng_notification_channel_command_reply
*reply
;
499 enum lttng_notification_channel_message_type msg_type
;
501 ret
= receive_message(channel
);
506 msg_type
= get_current_message_type(channel
);
508 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
:
510 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
511 ret
= enqueue_notification_from_current_message(
517 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
518 ret
= enqueue_dropped_notification(channel
);
523 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
525 struct lttng_notification_channel_command_handshake
*handshake
;
527 handshake
= (struct lttng_notification_channel_command_handshake
*)
528 (channel
->reception_buffer
.data
+
529 sizeof(struct lttng_notification_channel_message
));
530 channel
->version
.major
= handshake
->major
;
531 channel
->version
.minor
= handshake
->minor
;
532 channel
->version
.set
= true;
542 if (channel
->reception_buffer
.size
<
543 (sizeof(struct lttng_notification_channel_message
) +
545 /* Invalid message received. */
550 reply
= (struct lttng_notification_channel_command_reply
*)
551 (channel
->reception_buffer
.data
+
552 sizeof(struct lttng_notification_channel_message
));
553 *status
= (enum lttng_notification_channel_status
) reply
->status
;
559 int handshake(struct lttng_notification_channel
*channel
)
562 enum lttng_notification_channel_status status
=
563 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
564 struct lttng_notification_channel_command_handshake handshake
= {
565 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
566 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
568 struct lttng_notification_channel_message msg_header
= {
569 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
570 .size
= sizeof(handshake
),
572 char send_buffer
[sizeof(msg_header
) + sizeof(handshake
)];
574 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
575 memcpy(send_buffer
+ sizeof(msg_header
), &handshake
, sizeof(handshake
));
577 pthread_mutex_lock(&channel
->lock
);
579 ret
= lttcomm_send_creds_unix_sock(channel
->socket
, send_buffer
,
580 sizeof(send_buffer
));
585 /* Receive handshake info from the sessiond. */
586 ret
= receive_command_reply(channel
, &status
);
591 if (!channel
->version
.set
) {
596 if (channel
->version
.major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
602 pthread_mutex_unlock(&channel
->lock
);
607 enum lttng_notification_channel_status
send_condition_command(
608 struct lttng_notification_channel
*channel
,
609 enum lttng_notification_channel_message_type type
,
610 const struct lttng_condition
*condition
)
614 enum lttng_notification_channel_status status
=
615 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
616 struct lttng_dynamic_buffer buffer
;
617 struct lttng_notification_channel_message cmd_header
= {
618 .type
= (int8_t) type
,
621 lttng_dynamic_buffer_init(&buffer
);
624 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
628 assert(type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
||
629 type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
);
631 pthread_mutex_lock(&channel
->lock
);
632 socket
= channel
->socket
;
633 if (!lttng_condition_validate(condition
)) {
634 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
638 ret
= lttng_dynamic_buffer_append(&buffer
, &cmd_header
,
641 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
645 ret
= lttng_condition_serialize(condition
, &buffer
);
647 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
651 /* Update payload length. */
652 ((struct lttng_notification_channel_message
*) buffer
.data
)->size
=
653 (uint32_t) (buffer
.size
- sizeof(cmd_header
));
655 ret
= lttcomm_send_unix_sock(socket
, buffer
.data
, buffer
.size
);
657 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
661 ret
= receive_command_reply(channel
, &status
);
663 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
667 pthread_mutex_unlock(&channel
->lock
);
669 lttng_dynamic_buffer_reset(&buffer
);
673 enum lttng_notification_channel_status
lttng_notification_channel_subscribe(
674 struct lttng_notification_channel
*channel
,
675 const struct lttng_condition
*condition
)
677 return send_condition_command(channel
,
678 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
,
682 enum lttng_notification_channel_status
lttng_notification_channel_unsubscribe(
683 struct lttng_notification_channel
*channel
,
684 const struct lttng_condition
*condition
)
686 return send_condition_command(channel
,
687 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
,
691 void lttng_notification_channel_destroy(
692 struct lttng_notification_channel
*channel
)
698 if (channel
->socket
>= 0) {
699 (void) lttcomm_close_unix_sock(channel
->socket
);
701 pthread_mutex_destroy(&channel
->lock
);
702 lttng_dynamic_buffer_reset(&channel
->reception_buffer
);