Fix: cppcheck linter cleanups
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index 2a7152b892192f7e683bf881b2b106af0a2da36c..4de6613a0eff3e8847d31cc65c90228cc467adf8 100644 (file)
@@ -455,6 +455,13 @@ int close_stream_check(struct relay_stream *stream)
 {
 
        if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) {
+               /*
+                * We are about to close the stream so set the data pending flag to 1
+                * which will make the end data pending command skip the stream which
+                * is now closed and ready. Note that after proceeding to a file close,
+                * the written file is ready for reading.
+                */
+               stream->data_pending_check_done = 1;
                return 1;
        }
        return 0;
@@ -507,8 +514,6 @@ void *relay_thread_listener(void *data)
        while (1) {
                DBG("Listener accepting connections");
 
-               nb_fd = LTTNG_POLL_GETNB(&events);
-
 restart:
                ret = lttng_poll_wait(&events, -1);
                if (ret < 0) {
@@ -521,6 +526,8 @@ restart:
                        goto error;
                }
 
+               nb_fd = ret;
+
                DBG("Relay new connection received");
                for (i = 0; i < nb_fd; i++) {
                        /* Fetch once the poll data */
@@ -885,8 +892,6 @@ void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht
 
        DBG("Relay deleting session %" PRIu64, cmd->session->id);
 
-       lttcomm_destroy_sock(cmd->session->sock);
-
        rcu_read_lock();
        cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) {
                node = lttng_ht_iter_get_node_ulong(&iter);
@@ -910,6 +915,54 @@ void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht
        free(cmd->session);
 }
 
+/*
+ * Handle the RELAYD_CREATE_SESSION command.
+ *
+ * On success, send back the session id or else return a negative value.
+ */
+static
+int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
+               struct relay_command *cmd)
+{
+       int ret = 0, send_ret;
+       struct relay_session *session;
+       struct lttcomm_relayd_status_session reply;
+
+       assert(recv_hdr);
+       assert(cmd);
+
+       memset(&reply, 0, sizeof(reply));
+
+       session = zmalloc(sizeof(struct relay_session));
+       if (session == NULL) {
+               PERROR("relay session zmalloc");
+               ret = -1;
+               goto error;
+       }
+
+       session->id = ++last_relay_session_id;
+       session->sock = cmd->sock;
+       cmd->session = session;
+
+       reply.session_id = htobe64(session->id);
+
+       DBG("Created session %" PRIu64, session->id);
+
+error:
+       if (ret < 0) {
+               reply.ret_code = htobe32(LTTNG_ERR_FATAL);
+       } else {
+               reply.ret_code = htobe32(LTTNG_OK);
+       }
+
+       send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
+       if (send_ret < 0) {
+               ERR("Relayd sending session id");
+       }
+
+       return ret;
+}
+
 /*
  * relay_add_stream: allocate a new stream for a session
  */
@@ -924,7 +977,7 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
        char *path = NULL, *root_path = NULL;
        int ret, send_ret;
 
-       if (!session || session->version_check_done == 0) {
+       if (!session || cmd->version_check_done == 0) {
                ERR("Trying to add a stream before version check");
                ret = -1;
                goto end_no_session;
@@ -1021,7 +1074,7 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
 
        DBG("Close stream received");
 
-       if (!session || session->version_check_done == 0) {
+       if (!session || cmd->version_check_done == 0) {
                ERR("Trying to close a stream before version check");
                ret = -1;
                goto end_no_session;
@@ -1220,14 +1273,16 @@ int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
 
        if (data_buffer_size < data_size) {
                /* In case the realloc fails, we can free the memory */
-               char *tmp_data_ptr = data_buffer;
-               data_buffer = realloc(data_buffer, data_size);
-               if (!data_buffer) {
+               char *tmp_data_ptr;
+
+               tmp_data_ptr = realloc(data_buffer, data_size);
+               if (!tmp_data_ptr) {
                        ERR("Allocating data buffer");
-                       free(tmp_data_ptr);
+                       free(data_buffer);
                        ret = -1;
                        goto end;
                }
+               data_buffer = tmp_data_ptr;
                data_buffer_size = data_size;
        }
        memset(data_buffer, 0, data_size);
@@ -1281,22 +1336,10 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
 {
        int ret;
        struct lttcomm_relayd_version reply, msg;
-       struct relay_session *session;
 
-       if (cmd->session == NULL) {
-               session = zmalloc(sizeof(struct relay_session));
-               if (session == NULL) {
-                       PERROR("relay session zmalloc");
-                       ret = -1;
-                       goto end;
-               }
-               session->id = ++last_relay_session_id;
-               DBG("Created session %" PRIu64, session->id);
-               cmd->session = session;
-       } else {
-               session = cmd->session;
-       }
-       session->version_check_done = 1;
+       assert(cmd);
+
+       cmd->version_check_done = 1;
 
        /* Get version from the other side. */
        ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
@@ -1313,7 +1356,7 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
         * structure considering that the other side will adapt.
         */
 
-       ret = sscanf(VERSION, "%u.%u", &reply.major, &reply.minor);
+       ret = sscanf(VERSION, "%10u.%10u", &reply.major, &reply.minor);
        if (ret < 2) {
                ERR("Error in scanning version");
                ret = -1;
@@ -1351,7 +1394,7 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
 
        DBG("Data pending command received");
 
-       if (!session || session->version_check_done == 0) {
+       if (!session || cmd->version_check_done == 0) {
                ERR("Trying to check for data before version check");
                ret = -1;
                goto end_no_session;
@@ -1392,6 +1435,9 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
                ret = 1;
        }
 
+       /* Pending check is now done. */
+       stream->data_pending_check_done = 1;
+
 end_unlock:
        rcu_read_unlock();
 
@@ -1430,6 +1476,141 @@ int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
        return ret;
 }
 
+/*
+ * Initialize a data pending command. This means that a client is about to ask
+ * for data pending for each stream he/she holds. Simply iterate over all
+ * streams of a session and set the data_pending_check_done flag.
+ *
+ * This command returns to the client a LTTNG_OK code.
+ */
+static
+int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
+               struct relay_command *cmd, struct lttng_ht *streams_ht)
+{
+       int ret;
+       struct lttng_ht_iter iter;
+       struct lttcomm_relayd_begin_data_pending msg;
+       struct lttcomm_relayd_generic_reply reply;
+       struct relay_stream *stream;
+       uint64_t session_id;
+
+       assert(recv_hdr);
+       assert(cmd);
+       assert(streams_ht);
+
+       DBG("Init streams for data pending");
+
+       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)) {
+               ERR("Relay didn't receive valid begin data_pending struct size: %d",
+                               ret);
+               ret = -1;
+               goto end_no_session;
+       }
+
+       session_id = be64toh(msg.session_id);
+
+       /*
+        * Iterate over all streams to set the begin data pending flag. For now, the
+        * streams are indexed by stream handle so we have to iterate over all
+        * streams to find the one associated with the right session_id.
+        */
+       rcu_read_lock();
+       cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
+               if (stream->session->id == session_id) {
+                       stream->data_pending_check_done = 0;
+                       DBG("Set begin data pending flag to stream %" PRIu64,
+                                       stream->stream_handle);
+               }
+       }
+       rcu_read_unlock();
+
+       /* All good, send back reply. */
+       reply.ret_code = htobe32(LTTNG_OK);
+
+       ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
+       if (ret < 0) {
+               ERR("Relay begin data pending send reply failed");
+       }
+
+end_no_session:
+       return ret;
+}
+
+/*
+ * End data pending command. This will check, for a given session id, if each
+ * stream associated with it has its data_pending_check_done flag set. If not,
+ * this means that the client lost track of the stream but the data is still
+ * being streamed on our side. In this case, we inform the client that data is
+ * inflight.
+ *
+ * Return to the client if there is data in flight or not with a ret_code.
+ */
+static
+int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
+               struct relay_command *cmd, struct lttng_ht *streams_ht)
+{
+       int ret;
+       struct lttng_ht_iter iter;
+       struct lttcomm_relayd_end_data_pending msg;
+       struct lttcomm_relayd_generic_reply reply;
+       struct relay_stream *stream;
+       uint64_t session_id;
+       uint32_t is_data_inflight = 0;
+
+       assert(recv_hdr);
+       assert(cmd);
+       assert(streams_ht);
+
+       DBG("End data pending command");
+
+       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)) {
+               ERR("Relay didn't receive valid end data_pending struct size: %d",
+                               ret);
+               ret = -1;
+               goto end_no_session;
+       }
+
+       session_id = be64toh(msg.session_id);
+
+       /* Iterate over all streams to see if the begin data pending flag is set. */
+       rcu_read_lock();
+       cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
+               if (stream->session->id == session_id &&
+                               !stream->data_pending_check_done) {
+                       is_data_inflight = 1;
+                       DBG("Data is still in flight for stream %" PRIu64,
+                                       stream->stream_handle);
+                       break;
+               }
+       }
+       rcu_read_unlock();
+
+       /* All good, send back reply. */
+       reply.ret_code = htobe32(is_data_inflight);
+
+       ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
+       if (ret < 0) {
+               ERR("Relay end data pending send reply failed");
+       }
+
+end_no_session:
+       return ret;
+}
+
 /*
  * relay_process_control: Process the commands received on the control socket
  */
@@ -1440,11 +1621,9 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
        int ret = 0;
 
        switch (be32toh(recv_hdr->cmd)) {
-               /*
        case RELAYD_CREATE_SESSION:
                ret = relay_create_session(recv_hdr, cmd);
                break;
-               */
        case RELAYD_ADD_STREAM:
                ret = relay_add_stream(recv_hdr, cmd, streams_ht);
                break;
@@ -1466,6 +1645,12 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
        case RELAYD_QUIESCENT_CONTROL:
                ret = relay_quiescent_control(recv_hdr, cmd);
                break;
+       case RELAYD_BEGIN_DATA_PENDING:
+               ret = relay_begin_data_pending(recv_hdr, cmd, streams_ht);
+               break;
+       case RELAYD_END_DATA_PENDING:
+               ret = relay_end_data_pending(recv_hdr, cmd, streams_ht);
+               break;
        case RELAYD_UPDATE_SYNC_INFO:
        default:
                ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
@@ -1510,14 +1695,16 @@ int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
 
        data_size = be32toh(data_hdr.data_size);
        if (data_buffer_size < data_size) {
-               char *tmp_data_ptr = data_buffer;
-               data_buffer = realloc(data_buffer, data_size);
-               if (!data_buffer) {
+               char *tmp_data_ptr;
+
+               tmp_data_ptr = realloc(data_buffer, data_size);
+               if (!tmp_data_ptr) {
                        ERR("Allocating data buffer");
-                       free(tmp_data_ptr);
+                       free(data_buffer);
                        ret = -1;
                        goto end_unlock;
                }
+               data_buffer = tmp_data_ptr;
                data_buffer_size = data_size;
        }
        memset(data_buffer, 0, data_size);
@@ -1691,11 +1878,6 @@ void *relay_thread_worker(void *data)
        }
 
        while (1) {
-               /* Zeroed the events structure */
-               lttng_poll_reset(&events);
-
-               nb_fd = LTTNG_POLL_GETNB(&events);
-
                /* Infinite blocking call, waiting for transmission */
        restart:
                DBG3("Relayd worker thread polling...");
@@ -1710,6 +1892,8 @@ void *relay_thread_worker(void *data)
                        goto error;
                }
 
+               nb_fd = ret;
+
                for (i = 0; i < nb_fd; i++) {
                        /* Fetch once the poll data */
                        revents = LTTNG_POLL_GETEV(&events, i);
@@ -1874,7 +2058,7 @@ int main(int argc, char **argv)
 
        /* Parse arguments */
        progname = argv[0];
-       if ((ret = parse_args(argc, argv) < 0)) {
+       if ((ret = parse_args(argc, argv)) < 0) {
                goto exit;
        }
 
This page took 0.027913 seconds and 4 git commands to generate.