From f83bcd90ceb5ce659ffe9d7747d6f3366a09748a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 4 Oct 2018 17:50:27 -0400 Subject: [PATCH] Allow get_next_notification to return when interrupted MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Applications (and scripts) which consume a given set of notifications indefinitely may fail to exit if a SIGTERM handler is registered. lttng_notification_channel_get_next_notification() blocks indefinitely on recvmsg() until a new notification is available. The wrapper that is used to do so automatically restarts the recvmsg() if it is interrupted, thus not allowing clients a change to cleanly exit. This change causes the notification channel to wait for a message to be available using select() before starting the actual reception of the data and return LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUTPED if a signal occurs during the wait. If data is available, it is assumed that the message is well-formed and can promptly be received in its entirety. The goal of this change is to give a monitoring application a chance to leave the get_next_notification() function and check if it should exit. Signed-off-by: Jérémie Galarneau --- include/lttng/notification/channel.h | 38 +++++++++++++++++----------- src/lib/lttng-ctl/channel.c | 23 +++++++++++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/include/lttng/notification/channel.h b/include/lttng/notification/channel.h index 7708cfd56..878d37ee1 100644 --- a/include/lttng/notification/channel.h +++ b/include/lttng/notification/channel.h @@ -31,6 +31,7 @@ struct lttng_notification_channel; enum lttng_notification_channel_status { LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED = 1, + LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED = 2, LTTNG_NOTIFICATION_CHANNEL_STATUS_OK = 0, LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR = -1, LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED = -2, @@ -81,10 +82,14 @@ extern struct lttng_notification_channel *lttng_notification_channel_create( * Notifications can be dropped if a client consumes the notifications sent * through the notification channel too slowly. * - * Returns LTTNG_NOTIFICATION_CHANNEL_STATUS_OK and a notification on success, - * LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was - * provided, or LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED if - * notifications were dropped. + * Returns + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_OK and a notification on success, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was + * provided, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED if notifications + * were dropped, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED if a signal was received + * that caused the reception to fail. */ extern enum lttng_notification_channel_status lttng_notification_channel_get_next_notification( @@ -102,9 +107,10 @@ lttng_notification_channel_get_next_notification( * lttng_notification_channel_get_next_notification() can be called and * is guaranteed to not block. * - * Returns LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success or - * LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was - * provided. + * Returns + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was + * provided. */ extern enum lttng_notification_channel_status lttng_notification_channel_has_pending_notification( @@ -120,10 +126,11 @@ lttng_notification_channel_has_pending_notification( * An error will be reported if the client tries to subscribe to the same * condition multiple times without unsubscribing. * - * Returns LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success, - * LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was - * provided, or LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED if the - * client was already subscribed to the condition through this channel. + * Returns + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was + * provided, or LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED if the + * client was already subscribed to the condition through this channel. */ extern enum lttng_notification_channel_status lttng_notification_channel_subscribe( @@ -139,10 +146,11 @@ lttng_notification_channel_subscribe( * An error will be reported if the client tries to unsubscribe to from a * conditions' notifications to which it was not previously subscribed. * - * Returns LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success, - * LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was - * provided, or LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION if the - * client was not already subscribed to the condition through this channel. + * Returns + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_OK on success, + * - LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID if an invalid parameter was + * provided, or LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION if the + * client was not already subscribed to the condition through this channel. */ extern enum lttng_notification_channel_status lttng_notification_channel_unsubscribe( diff --git a/src/lib/lttng-ctl/channel.c b/src/lib/lttng-ctl/channel.c index 5214fe73a..5487d051b 100644 --- a/src/lib/lttng-ctl/channel.c +++ b/src/lib/lttng-ctl/channel.c @@ -211,6 +211,7 @@ lttng_notification_channel_get_next_notification( struct lttng_notification *notification = NULL; enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; + fd_set read_fds; if (!channel || !_notification) { status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID; @@ -239,6 +240,28 @@ lttng_notification_channel_get_next_notification( goto end_unlock; } + /* + * Block on select() instead of the message reception itself as the + * recvmsg() wrappers always restard on EINTR. We choose to wait + * using select() in order to: + * 1) Return if a signal occurs, + * 2) Not deal with partially received messages. + * + * The drawback to this approach is that we assume that messages + * are complete/well formed. If a message is shorter than its + * announced length, receive_message() will block on recvmsg() + * and never return (even if a signal is received). + */ + FD_ZERO(&read_fds); + FD_SET(channel->socket, &read_fds); + ret = select(channel->socket + 1, &read_fds, NULL, NULL, NULL); + if (ret == -1) { + status = errno == EINTR ? + LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED : + LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; + goto end_unlock; + } + ret = receive_message(channel); if (ret) { status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR; -- 2.34.1