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 <sys/select.h>
33 int handshake(struct lttng_notification_channel
*channel
);
36 * Populates the reception buffer with the next complete message.
37 * The caller must acquire the channel's lock.
40 int receive_message(struct lttng_notification_channel
*channel
)
43 struct lttng_notification_channel_message msg
;
45 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
50 ret
= lttcomm_recv_unix_sock(channel
->socket
, &msg
, sizeof(msg
));
56 if (msg
.size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
61 /* Add message header at buffer's start. */
62 ret
= lttng_dynamic_buffer_append(&channel
->reception_buffer
, &msg
,
68 /* Reserve space for the payload. */
69 ret
= lttng_dynamic_buffer_set_size(&channel
->reception_buffer
,
70 channel
->reception_buffer
.size
+ msg
.size
);
75 /* Receive message payload. */
76 ret
= lttcomm_recv_unix_sock(channel
->socket
,
77 channel
->reception_buffer
.data
+ sizeof(msg
), msg
.size
);
78 if (ret
< (ssize_t
) msg
.size
) {
86 if (lttng_dynamic_buffer_set_size(&channel
->reception_buffer
, 0)) {
93 enum lttng_notification_channel_message_type
get_current_message_type(
94 struct lttng_notification_channel
*channel
)
96 struct lttng_notification_channel_message
*msg
;
98 assert(channel
->reception_buffer
.size
>= sizeof(*msg
));
100 msg
= (struct lttng_notification_channel_message
*)
101 channel
->reception_buffer
.data
;
102 return (enum lttng_notification_channel_message_type
) msg
->type
;
106 struct lttng_notification
*create_notification_from_current_message(
107 struct lttng_notification_channel
*channel
)
110 struct lttng_notification
*notification
= NULL
;
111 struct lttng_buffer_view view
;
113 if (channel
->reception_buffer
.size
<=
114 sizeof(struct lttng_notification_channel_message
)) {
118 view
= lttng_buffer_view_from_dynamic_buffer(&channel
->reception_buffer
,
119 sizeof(struct lttng_notification_channel_message
), -1);
121 ret
= lttng_notification_create_from_buffer(&view
, ¬ification
);
122 if (ret
!= channel
->reception_buffer
.size
-
123 sizeof(struct lttng_notification_channel_message
)) {
124 lttng_notification_destroy(notification
);
132 struct lttng_notification_channel
*lttng_notification_channel_create(
133 struct lttng_endpoint
*endpoint
)
136 bool is_in_tracing_group
= false, is_root
= false;
137 char *sock_path
= NULL
;
138 struct lttng_notification_channel
*channel
= NULL
;
141 endpoint
!= lttng_session_daemon_notification_endpoint
) {
145 sock_path
= zmalloc(LTTNG_PATH_MAX
);
150 channel
= zmalloc(sizeof(struct lttng_notification_channel
));
154 channel
->socket
= -1;
155 pthread_mutex_init(&channel
->lock
, NULL
);
156 lttng_dynamic_buffer_init(&channel
->reception_buffer
);
157 CDS_INIT_LIST_HEAD(&channel
->pending_notifications
.list
);
159 is_root
= (getuid() == 0);
161 is_in_tracing_group
= lttng_check_tracing_group();
164 if (is_root
|| is_in_tracing_group
) {
165 lttng_ctl_copy_string(sock_path
,
166 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
,
168 ret
= lttcomm_connect_unix_sock(sock_path
);
175 /* Fallback to local session daemon. */
176 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
177 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
178 utils_get_home_dir());
179 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
183 ret
= lttcomm_connect_unix_sock(sock_path
);
190 channel
->socket
= fd
;
192 ret
= handshake(channel
);
200 lttng_notification_channel_destroy(channel
);
205 enum lttng_notification_channel_status
206 lttng_notification_channel_get_next_notification(
207 struct lttng_notification_channel
*channel
,
208 struct lttng_notification
**_notification
)
211 struct lttng_notification
*notification
= NULL
;
212 enum lttng_notification_channel_status status
=
213 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
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
);
242 ret
= receive_message(channel
);
244 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
248 switch (get_current_message_type(channel
)) {
249 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
250 notification
= create_notification_from_current_message(
253 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
257 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
258 /* No payload to consume. */
259 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
;
262 /* Protocol error. */
263 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
268 pthread_mutex_unlock(&channel
->lock
);
271 *_notification
= notification
;
277 int enqueue_dropped_notification(
278 struct lttng_notification_channel
*channel
)
281 struct pending_notification
*pending_notification
;
282 struct cds_list_head
*last_element
=
283 channel
->pending_notifications
.list
.prev
;
285 pending_notification
= caa_container_of(last_element
,
286 struct pending_notification
, node
);
287 if (!pending_notification
->notification
) {
289 * The last enqueued notification indicates dropped
290 * notifications; there is nothing to do as we group
291 * dropped notifications together.
296 if (channel
->pending_notifications
.count
>=
297 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
&&
298 pending_notification
->notification
) {
300 * Discard the last enqueued notification to indicate
301 * that notifications were dropped at this point.
303 lttng_notification_destroy(
304 pending_notification
->notification
);
305 pending_notification
->notification
= NULL
;
309 pending_notification
= zmalloc(sizeof(*pending_notification
));
310 if (!pending_notification
) {
314 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
315 cds_list_add(&pending_notification
->node
,
316 &channel
->pending_notifications
.list
);
317 channel
->pending_notifications
.count
++;
323 int enqueue_notification_from_current_message(
324 struct lttng_notification_channel
*channel
)
327 struct lttng_notification
*notification
;
328 struct pending_notification
*pending_notification
;
330 if (channel
->pending_notifications
.count
>=
331 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT
) {
332 /* Drop the notification. */
333 ret
= enqueue_dropped_notification(channel
);
337 pending_notification
= zmalloc(sizeof(*pending_notification
));
338 if (!pending_notification
) {
342 CDS_INIT_LIST_HEAD(&pending_notification
->node
);
344 notification
= create_notification_from_current_message(channel
);
350 pending_notification
->notification
= notification
;
351 cds_list_add(&pending_notification
->node
,
352 &channel
->pending_notifications
.list
);
353 channel
->pending_notifications
.count
++;
357 free(pending_notification
);
361 enum lttng_notification_channel_status
362 lttng_notification_channel_has_pending_notification(
363 struct lttng_notification_channel
*channel
,
364 bool *_notification_pending
)
367 enum lttng_notification_channel_status status
=
368 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
370 struct timeval timeout
;
373 memset(&timeout
, 0, sizeof(timeout
));
375 if (!channel
|| !_notification_pending
) {
376 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
380 pthread_mutex_lock(&channel
->lock
);
382 if (channel
->pending_notifications
.count
) {
383 *_notification_pending
= true;
387 if (channel
->socket
< 0) {
388 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
;
393 * Check, without blocking, if data is available on the channel's
394 * socket. If there is data available, it is safe to read (blocking)
395 * on the socket for a message from the session daemon.
397 * Since all commands wait for the session daemon's reply before
398 * releasing the channel's lock, the protocol only allows for
399 * notifications and "notification dropped" messages to come
400 * through. If we receive a different message type, it is
401 * considered a protocol error.
403 * Note that this function is not guaranteed not to block. This
404 * will block until our peer (the session daemon) has sent a complete
405 * message if we see data available on the socket. If the peer does
406 * not respect the protocol, this may block indefinitely.
408 FD_SET(channel
->socket
, &read_fds
);
410 ret
= select(channel
->socket
+ 1, &read_fds
, NULL
, NULL
, &timeout
);
411 } while (ret
< 0 && errno
== EINTR
);
414 /* No data available. */
415 *_notification_pending
= false;
417 } else if (ret
< 0) {
418 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
422 /* Data available on socket. */
423 ret
= receive_message(channel
);
425 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
429 switch (get_current_message_type(channel
)) {
430 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
431 ret
= enqueue_notification_from_current_message(channel
);
435 *_notification_pending
= true;
437 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
438 ret
= enqueue_dropped_notification(channel
);
442 *_notification_pending
= true;
445 /* Protocol error. */
446 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
451 pthread_mutex_unlock(&channel
->lock
);
457 int receive_command_reply(struct lttng_notification_channel
*channel
,
458 enum lttng_notification_channel_status
*status
)
461 struct lttng_notification_channel_command_reply
*reply
;
464 enum lttng_notification_channel_message_type msg_type
;
466 ret
= receive_message(channel
);
471 msg_type
= get_current_message_type(channel
);
473 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
:
475 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
:
476 ret
= enqueue_notification_from_current_message(
482 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
:
483 ret
= enqueue_dropped_notification(channel
);
488 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
490 struct lttng_notification_channel_command_handshake
*handshake
;
492 handshake
= (struct lttng_notification_channel_command_handshake
*)
493 (channel
->reception_buffer
.data
+
494 sizeof(struct lttng_notification_channel_message
));
495 channel
->version
.major
= handshake
->major
;
496 channel
->version
.minor
= handshake
->minor
;
497 channel
->version
.set
= true;
507 if (channel
->reception_buffer
.size
<
508 (sizeof(struct lttng_notification_channel_message
) +
510 /* Invalid message received. */
515 reply
= (struct lttng_notification_channel_command_reply
*)
516 (channel
->reception_buffer
.data
+
517 sizeof(struct lttng_notification_channel_message
));
518 *status
= (enum lttng_notification_channel_status
) reply
->status
;
524 int handshake(struct lttng_notification_channel
*channel
)
527 enum lttng_notification_channel_status status
=
528 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
529 struct lttng_notification_channel_command_handshake handshake
= {
530 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
531 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
533 struct lttng_notification_channel_message msg_header
= {
534 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
535 .size
= sizeof(handshake
),
537 char send_buffer
[sizeof(msg_header
) + sizeof(handshake
)];
539 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
540 memcpy(send_buffer
+ sizeof(msg_header
), &handshake
, sizeof(handshake
));
542 pthread_mutex_lock(&channel
->lock
);
544 ret
= lttcomm_send_creds_unix_sock(channel
->socket
, send_buffer
,
545 sizeof(send_buffer
));
550 /* Receive handshake info from the sessiond. */
551 ret
= receive_command_reply(channel
, &status
);
556 if (!channel
->version
.set
) {
561 if (channel
->version
.major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
567 pthread_mutex_unlock(&channel
->lock
);
572 enum lttng_notification_channel_status
send_condition_command(
573 struct lttng_notification_channel
*channel
,
574 enum lttng_notification_channel_message_type type
,
575 const struct lttng_condition
*condition
)
579 enum lttng_notification_channel_status status
=
580 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
581 struct lttng_dynamic_buffer buffer
;
582 struct lttng_notification_channel_message cmd_header
= {
583 .type
= (int8_t) type
,
586 lttng_dynamic_buffer_init(&buffer
);
589 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
593 assert(type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
||
594 type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
);
596 pthread_mutex_lock(&channel
->lock
);
597 socket
= channel
->socket
;
598 if (!lttng_condition_validate(condition
)) {
599 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
603 ret
= lttng_dynamic_buffer_append(&buffer
, &cmd_header
,
606 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
610 ret
= lttng_condition_serialize(condition
, &buffer
);
612 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
;
616 /* Update payload length. */
617 ((struct lttng_notification_channel_message
*) buffer
.data
)->size
=
618 (uint32_t) (buffer
.size
- sizeof(cmd_header
));
620 ret
= lttcomm_send_unix_sock(socket
, buffer
.data
, buffer
.size
);
622 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
626 ret
= receive_command_reply(channel
, &status
);
628 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR
;
632 pthread_mutex_unlock(&channel
->lock
);
634 lttng_dynamic_buffer_reset(&buffer
);
638 enum lttng_notification_channel_status
lttng_notification_channel_subscribe(
639 struct lttng_notification_channel
*channel
,
640 const struct lttng_condition
*condition
)
642 return send_condition_command(channel
,
643 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
,
647 enum lttng_notification_channel_status
lttng_notification_channel_unsubscribe(
648 struct lttng_notification_channel
*channel
,
649 const struct lttng_condition
*condition
)
651 return send_condition_command(channel
,
652 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
,
656 void lttng_notification_channel_destroy(
657 struct lttng_notification_channel
*channel
)
663 if (channel
->socket
>= 0) {
664 (void) lttcomm_close_unix_sock(channel
->socket
);
666 pthread_mutex_destroy(&channel
->lock
);
667 lttng_dynamic_buffer_reset(&channel
->reception_buffer
);