Allow get_next_notification to return when interrupted
[lttng-tools.git] / src / lib / lttng-ctl / channel.c
index 16474464d880a9ffaa6c0fbad6c068dbeb781407..5487d051b5013fd60b080491ce865966bc6ffeab 100644 (file)
@@ -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;
@@ -430,14 +453,14 @@ lttng_notification_channel_has_pending_notification(
        case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION:
                ret = enqueue_notification_from_current_message(channel);
                if (ret) {
-                       goto end;
+                       goto end_unlock;
                }
                *_notification_pending = true;
                break;
        case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED:
                ret = enqueue_dropped_notification(channel);
                if (ret) {
-                       goto end;
+                       goto end_unlock;
                }
                *_notification_pending = true;
                break;
@@ -575,14 +598,16 @@ enum lttng_notification_channel_status send_condition_command(
                const struct lttng_condition *condition)
 {
        int socket;
-       ssize_t command_size, ret;
+       ssize_t ret;
        enum lttng_notification_channel_status status =
                        LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
-       char *command_buffer = NULL;
-       struct lttng_notification_channel_message cmd_message = {
-               .type = type,
+       struct lttng_dynamic_buffer buffer;
+       struct lttng_notification_channel_message cmd_header = {
+               .type = (int8_t) type,
        };
 
+       lttng_dynamic_buffer_init(&buffer);
+
        if (!channel) {
                status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
                goto end;
@@ -598,28 +623,24 @@ enum lttng_notification_channel_status send_condition_command(
                goto end_unlock;
        }
 
-       ret = lttng_condition_serialize(condition, NULL);
-       if (ret < 0) {
-               status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
-               goto end_unlock;
-       }
-       assert(ret < UINT32_MAX);
-       cmd_message.size = (uint32_t) ret;
-       command_size = ret + sizeof(
-                       struct lttng_notification_channel_message);
-       command_buffer = zmalloc(command_size);
-       if (!command_buffer) {
+       ret = lttng_dynamic_buffer_append(&buffer, &cmd_header,
+                       sizeof(cmd_header));
+       if (ret) {
+               status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
                goto end_unlock;
        }
 
-       memcpy(command_buffer, &cmd_message, sizeof(cmd_message));
-       ret = lttng_condition_serialize(condition,
-                       command_buffer + sizeof(cmd_message));
-       if (ret < 0) {
+       ret = lttng_condition_serialize(condition, &buffer);
+       if (ret) {
+               status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
                goto end_unlock;
        }
 
-       ret = lttcomm_send_unix_sock(socket, command_buffer, command_size);
+       /* Update payload length. */
+       ((struct lttng_notification_channel_message *) buffer.data)->size =
+                       (uint32_t) (buffer.size - sizeof(cmd_header));
+
+       ret = lttcomm_send_unix_sock(socket, buffer.data, buffer.size);
        if (ret < 0) {
                status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
                goto end_unlock;
@@ -633,7 +654,7 @@ enum lttng_notification_channel_status send_condition_command(
 end_unlock:
        pthread_mutex_unlock(&channel->lock);
 end:
-       free(command_buffer);
+       lttng_dynamic_buffer_reset(&buffer);
        return status;
 }
 
This page took 0.025518 seconds and 5 git commands to generate.