Fix: don't do custom lookup to relayd stream ht
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index 009621a6f7b1c4701a0857848346f29c66b8511a..66273107c354c5b541b372cb8725a6ad732c5ea4 100644 (file)
@@ -254,7 +254,7 @@ int notify_thread_pipe(int wpipe)
        do {
                ret = write(wpipe, "!", 1);
        } while (ret < 0 && errno == EINTR);
-       if (ret < 0) {
+       if (ret < 0 || ret != 1) {
                PERROR("write poll pipe");
        }
 
@@ -669,7 +669,7 @@ void *relay_thread_dispatcher(void *data)
                                                sizeof(struct relay_command));
                        } while (ret < 0 && errno == EINTR);
                        free(relay_cmd);
-                       if (ret < 0) {
+                       if (ret < 0 || ret != sizeof(struct relay_command)) {
                                PERROR("write cmd pipe");
                                goto error;
                        }
@@ -866,6 +866,34 @@ char *create_output_path(char *path_name)
        }
 }
 
+/*
+ * Get stream from stream id.
+ * Need to be called with RCU read-side lock held.
+ */
+static
+struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
+               struct lttng_ht *streams_ht)
+{
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+       struct relay_stream *ret;
+
+       lttng_ht_lookup(streams_ht,
+                       (void *)((unsigned long) stream_id),
+                       &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node == NULL) {
+               DBG("Relay stream %" PRIu64 " not found", stream_id);
+               ret = NULL;
+               goto end;
+       }
+
+       ret = caa_container_of(node, struct relay_stream, stream_n);
+
+end:
+       return ret;
+}
+
 static
 void deferred_free_stream(struct rcu_head *head)
 {
@@ -958,6 +986,7 @@ error:
        send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
        if (send_ret < 0) {
                ERR("Relayd sending session id");
+               ret = send_ret;
        }
 
        return ret;
@@ -983,11 +1012,15 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
                goto end_no_session;
        }
 
-       /* FIXME : use data_size for something ? */
        ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
                        sizeof(struct lttcomm_relayd_add_stream), 0);
        if (ret < sizeof(struct lttcomm_relayd_add_stream)) {
-               ERR("Relay didn't receive valid add_stream struct size : %d", ret);
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid add_stream struct size : %d", ret);
+               }
                ret = -1;
                goto end_no_session;
        }
@@ -1050,6 +1083,7 @@ end:
                        sizeof(struct lttcomm_relayd_status_stream), 0);
        if (send_ret < 0) {
                ERR("Relay sending stream id");
+               ret = send_ret;
        }
        rcu_read_unlock();
 
@@ -1069,7 +1103,6 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
        struct lttcomm_relayd_generic_reply reply;
        struct relay_stream *stream;
        int ret, send_ret;
-       struct lttng_ht_node_ulong *node;
        struct lttng_ht_iter iter;
 
        DBG("Close stream received");
@@ -1083,23 +1116,19 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
        ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
                        sizeof(struct lttcomm_relayd_close_stream), 0);
        if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
-               ERR("Relay didn't receive valid add_stream struct size : %d", ret);
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid add_stream struct size : %d", ret);
+               }
                ret = -1;
                goto end_no_session;
        }
 
        rcu_read_lock();
-       lttng_ht_lookup(streams_ht,
-                       (void *)((unsigned long) be64toh(stream_info.stream_id)),
-                       &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
-       if (node == NULL) {
-               DBG("Relay stream %" PRIu64 " not found", be64toh(stream_info.stream_id));
-               ret = -1;
-               goto end_unlock;
-       }
-
-       stream = caa_container_of(node, struct relay_stream, stream_n);
+       stream = relay_stream_from_stream_id(be64toh(stream_info.stream_id),
+                       streams_ht);
        if (!stream) {
                ret = -1;
                goto end_unlock;
@@ -1115,6 +1144,7 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
                if (delret < 0) {
                        PERROR("close stream");
                }
+               iter.iter.node = &stream->stream_n.node;
                delret = lttng_ht_del(streams_ht, &iter);
                assert(!delret);
                call_rcu(&stream->rcu_node,
@@ -1134,6 +1164,7 @@ end_unlock:
                        sizeof(struct lttcomm_relayd_generic_reply), 0);
        if (send_ret < 0) {
                ERR("Relay sending stream id");
+               ret = send_ret;
        }
 
 end_no_session:
@@ -1184,34 +1215,6 @@ int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
        return ret;
 }
 
-/*
- * Get stream from stream id.
- * Need to be called with RCU read-side lock held.
- */
-static
-struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
-               struct lttng_ht *streams_ht)
-{
-       struct lttng_ht_node_ulong *node;
-       struct lttng_ht_iter iter;
-       struct relay_stream *ret;
-
-       lttng_ht_lookup(streams_ht,
-                       (void *)((unsigned long) stream_id),
-                       &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
-       if (node == NULL) {
-               DBG("Relay stream %" PRIu64 " not found", stream_id);
-               ret = NULL;
-               goto end;
-       }
-
-       ret = caa_container_of(node, struct relay_stream, stream_n);
-
-end:
-       return ret;
-}
-
 /*
  * Append padding to the file pointed by the file descriptor fd.
  */
@@ -1234,7 +1237,7 @@ static int write_padding_to_file(int fd, uint32_t size)
        do {
                ret = write(fd, zeros, size);
        } while (ret < 0 && errno == EINTR);
-       if (ret < 0) {
+       if (ret < 0 || ret != size) {
                PERROR("write padding to file");
        }
 
@@ -1289,8 +1292,13 @@ int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
        DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
        ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
        if (ret < 0 || ret != data_size) {
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive the whole metadata");
+               }
                ret = -1;
-               ERR("Relay didn't receive the whole metadata");
                goto end;
        }
        metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
@@ -1307,7 +1315,7 @@ int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
                ret = write(metadata_stream->fd, metadata_struct->payload,
                                payload_size);
        } while (ret < 0 && errno == EINTR);
-       if (ret < payload_size) {
+       if (ret < 0 || ret != payload_size) {
                ERR("Relay error writing metadata on file");
                ret = -1;
                goto end_unlock;
@@ -1344,8 +1352,13 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
        /* Get version from the other side. */
        ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
        if (ret < 0 || ret != sizeof(msg)) {
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay failed to receive the version values.");
+               }
                ret = -1;
-               ERR("Relay failed to receive the version values.");
                goto end;
        }
 
@@ -1388,8 +1401,6 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
        struct lttcomm_relayd_generic_reply reply;
        struct relay_stream *stream;
        int ret;
-       struct lttng_ht_node_ulong *node;
-       struct lttng_ht_iter iter;
        uint64_t last_net_seq_num, stream_id;
 
        DBG("Data pending command received");
@@ -1402,7 +1413,13 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
 
        ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
        if (ret < sizeof(msg)) {
-               ERR("Relay didn't receive valid data_pending struct size : %d", ret);
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid data_pending struct size : %d",
+                                       ret);
+               }
                ret = -1;
                goto end_no_session;
        }
@@ -1411,23 +1428,18 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
        last_net_seq_num = be64toh(msg.last_net_seq_num);
 
        rcu_read_lock();
-       lttng_ht_lookup(streams_ht, (void *)((unsigned long) stream_id), &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
-       if (node == NULL) {
-               DBG("Relay stream %" PRIu64 " not found", stream_id);
+       stream = relay_stream_from_stream_id(stream_id, streams_ht);
+       if (stream == NULL) {
                ret = -1;
                goto end_unlock;
        }
 
-       stream = caa_container_of(node, struct relay_stream, stream_n);
-       assert(stream);
-
        DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
                        " and last_seq %" PRIu64, stream_id, stream->prev_seq,
                        last_net_seq_num);
 
        /* Avoid wrapping issue */
-       if (((int64_t) (stream->prev_seq - last_net_seq_num)) <= 0) {
+       if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) {
                /* Data has in fact been written and is NOT pending */
                ret = 0;
        } else {
@@ -1460,19 +1472,56 @@ end_no_session:
  */
 static
 int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
-               struct relay_command *cmd)
+               struct relay_command *cmd, struct lttng_ht *streams_ht)
 {
        int ret;
+       uint64_t stream_id;
+       struct relay_stream *stream;
+       struct lttng_ht_iter iter;
+       struct lttcomm_relayd_quiescent_control msg;
        struct lttcomm_relayd_generic_reply reply;
 
        DBG("Checking quiescent state on control socket");
 
+       if (!cmd->session || cmd->version_check_done == 0) {
+               ERR("Trying to check for data before version check");
+               ret = -1;
+               goto end_no_session;
+       }
+
+       ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
+       if (ret < sizeof(msg)) {
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid begin data_pending struct size: %d",
+                                       ret);
+               }
+               ret = -1;
+               goto end_no_session;
+       }
+
+       stream_id = be64toh(msg.stream_id);
+
+       rcu_read_lock();
+       cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
+               if (stream->stream_handle == stream_id) {
+                       stream->data_pending_check_done = 1;
+                       DBG("Relay quiescent control pending flag set to %" PRIu64,
+                                       stream_id);
+                       break;
+               }
+       }
+       rcu_read_unlock();
+
        reply.ret_code = htobe32(LTTNG_OK);
        ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
        if (ret < 0) {
                ERR("Relay data quiescent control ret code failed");
        }
 
+end_no_session:
        return ret;
 }
 
@@ -1508,8 +1557,13 @@ int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
 
        ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
        if (ret < sizeof(msg)) {
-               ERR("Relay didn't receive valid begin data_pending struct size: %d",
-                               ret);
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid begin data_pending struct size: %d",
+                                       ret);
+               }
                ret = -1;
                goto end_no_session;
        }
@@ -1578,8 +1632,13 @@ int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
 
        ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
        if (ret < sizeof(msg)) {
-               ERR("Relay didn't receive valid end data_pending struct size: %d",
-                               ret);
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Relay didn't receive valid end data_pending struct size: %d",
+                                       ret);
+               }
                ret = -1;
                goto end_no_session;
        }
@@ -1643,7 +1702,7 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
                ret = relay_data_pending(recv_hdr, cmd, streams_ht);
                break;
        case RELAYD_QUIESCENT_CONTROL:
-               ret = relay_quiescent_control(recv_hdr, cmd);
+               ret = relay_quiescent_control(recv_hdr, cmd, streams_ht);
                break;
        case RELAYD_BEGIN_DATA_PENDING:
                ret = relay_begin_data_pending(recv_hdr, cmd, streams_ht);
@@ -1679,7 +1738,12 @@ int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
        ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
                        sizeof(struct lttcomm_relayd_data_hdr), 0);
        if (ret <= 0) {
-               ERR("Connections seems to be closed");
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               } else {
+                       ERR("Unable to receive data header on sock %d", cmd->sock->fd);
+               }
                ret = -1;
                goto end;
        }
@@ -1715,6 +1779,10 @@ int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
                data_size, stream_id, net_seq_num);
        ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
        if (ret <= 0) {
+               if (ret == 0) {
+                       /* Orderly shutdown. Not necessary to print an error. */
+                       DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
+               }
                ret = -1;
                goto end_unlock;
        }
@@ -1722,7 +1790,7 @@ int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
        do {
                ret = write(stream->fd, data_buffer, data_size);
        } while (ret < 0 && errno == EINTR);
-       if (ret < data_size) {
+       if (ret < 0 || ret != data_size) {
                ERR("Relay error writing data to file");
                ret = -1;
                goto end_unlock;
@@ -1786,7 +1854,9 @@ int relay_add_connection(int fd, struct lttng_poll_event *events,
                PERROR("Relay command zmalloc");
                goto error;
        }
-       ret = read(fd, relay_connection, sizeof(struct relay_command));
+       do {
+               ret = read(fd, relay_connection, sizeof(struct relay_command));
+       } while (ret < 0 && errno == EINTR);
        if (ret < 0 || ret < sizeof(struct relay_command)) {
                PERROR("read relay cmd pipe");
                goto error_read;
This page took 0.027923 seconds and 4 git commands to generate.