X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Fchannel.c;h=16474464d880a9ffaa6c0fbad6c068dbeb781407;hp=75a911f2af3ec7080085c648792d23393f82ad1c;hb=1d757b1cd3b4669b52e2d9ceafb03eafd42490ff;hpb=a58c490f0bff52a73717d31d04d1472629180de2 diff --git a/src/lib/lttng-ctl/channel.c b/src/lib/lttng-ctl/channel.c index 75a911f2a..16474464d 100644 --- a/src/lib/lttng-ctl/channel.c +++ b/src/lib/lttng-ctl/channel.c @@ -26,13 +26,15 @@ #include #include #include "lttng-ctl-helper.h" +#include +#include static int handshake(struct lttng_notification_channel *channel); /* * Populates the reception buffer with the next complete message. - * The caller must acquire the client's lock. + * The caller must acquire the channel's lock. */ static int receive_message(struct lttng_notification_channel *channel) @@ -40,9 +42,9 @@ int receive_message(struct lttng_notification_channel *channel) ssize_t ret; struct lttng_notification_channel_message msg; - ret = lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0); - if (ret) { - goto error; + if (lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0)) { + ret = -1; + goto end; } ret = lttcomm_recv_unix_sock(channel->socket, &msg, sizeof(msg)); @@ -81,7 +83,9 @@ int receive_message(struct lttng_notification_channel *channel) end: return ret; error: - lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0); + if (lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0)) { + ret = -1; + } goto end; } @@ -213,6 +217,8 @@ lttng_notification_channel_get_next_notification( goto end; } + pthread_mutex_lock(&channel->lock); + if (channel->pending_notifications.count) { struct pending_notification *pending_notification; @@ -230,11 +236,9 @@ lttng_notification_channel_get_next_notification( cds_list_del(&pending_notification->node); channel->pending_notifications.count--; free(pending_notification); - goto end; + goto end_unlock; } - pthread_mutex_lock(&channel->lock); - ret = receive_message(channel); if (ret) { status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; @@ -354,6 +358,101 @@ error: goto end; } +enum lttng_notification_channel_status +lttng_notification_channel_has_pending_notification( + struct lttng_notification_channel *channel, + bool *_notification_pending) +{ + int ret; + enum lttng_notification_channel_status status = + LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; + fd_set read_fds; + struct timeval timeout; + + FD_ZERO(&read_fds); + memset(&timeout, 0, sizeof(timeout)); + + if (!channel || !_notification_pending) { + status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID; + goto end; + } + + pthread_mutex_lock(&channel->lock); + + if (channel->pending_notifications.count) { + *_notification_pending = true; + goto end_unlock; + } + + if (channel->socket < 0) { + status = LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED; + goto end_unlock; + } + + /* + * Check, without blocking, if data is available on the channel's + * socket. If there is data available, it is safe to read (blocking) + * on the socket for a message from the session daemon. + * + * Since all commands wait for the session daemon's reply before + * releasing the channel's lock, the protocol only allows for + * notifications and "notification dropped" messages to come + * through. If we receive a different message type, it is + * considered a protocol error. + * + * Note that this function is not guaranteed not to block. This + * will block until our peer (the session daemon) has sent a complete + * message if we see data available on the socket. If the peer does + * not respect the protocol, this may block indefinitely. + */ + FD_SET(channel->socket, &read_fds); + do { + ret = select(channel->socket + 1, &read_fds, NULL, NULL, &timeout); + } while (ret < 0 && errno == EINTR); + + if (ret == 0) { + /* No data available. */ + *_notification_pending = false; + goto end_unlock; + } else if (ret < 0) { + status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; + goto end_unlock; + } + + /* Data available on socket. */ + ret = receive_message(channel); + if (ret) { + status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; + goto end_unlock; + } + + switch (get_current_message_type(channel)) { + case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION: + ret = enqueue_notification_from_current_message(channel); + if (ret) { + goto end; + } + *_notification_pending = true; + break; + case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED: + ret = enqueue_dropped_notification(channel); + if (ret) { + goto end; + } + *_notification_pending = true; + break; + default: + /* Protocol error. */ + status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; + goto end_unlock; + } + +end_unlock: + pthread_mutex_unlock(&channel->lock); +end: + return status; +} + static int receive_command_reply(struct lttng_notification_channel *channel, enum lttng_notification_channel_status *status) @@ -442,7 +541,7 @@ int handshake(struct lttng_notification_channel *channel) pthread_mutex_lock(&channel->lock); - ret = lttcomm_send_unix_sock(channel->socket, send_buffer, + ret = lttcomm_send_creds_unix_sock(channel->socket, send_buffer, sizeof(send_buffer)); if (ret < 0) { goto end_unlock;