Fix: undefined operation on last_relay_viewer_session_id
[lttng-tools.git] / src / bin / lttng-relayd / live.c
index a720ed732f0f6746fc399c9ad203635086acfd0a..b1d8591fe80bc5efcef02767c142faa7797545b7 100644 (file)
@@ -43,6 +43,7 @@
 #include <common/common.h>
 #include <common/compat/poll.h>
 #include <common/compat/socket.h>
+#include <common/compat/endian.h>
 #include <common/defaults.h>
 #include <common/futex.h>
 #include <common/index/index.h>
@@ -514,6 +515,11 @@ restart:
                        revents = LTTNG_POLL_GETEV(&events, i);
                        pollfd = LTTNG_POLL_GETFD(&events, i);
 
+                       if (!revents) {
+                               /* No activity for this FD (poll implementation). */
+                               continue;
+                       }
+
                        /* Thread quit pipe has been closed. Killing thread. */
                        ret = check_thread_quit_pipe(pollfd, revents);
                        if (ret) {
@@ -727,7 +733,12 @@ int viewer_connect(struct relay_connection *conn)
        reply.major = htobe32(reply.major);
        reply.minor = htobe32(reply.minor);
        if (conn->type == RELAY_VIEWER_COMMAND) {
-               reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
+               /*
+                * Increment outside of htobe64 macro, because can be used more than once
+                * within the macro, and thus the operation may be undefined.
+                */
+               last_relay_viewer_session_id++;
+               reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
        }
 
        health_code_update();
@@ -931,6 +942,8 @@ int viewer_get_new_streams(struct relay_connection *conn)
 
        health_code_update();
 
+       memset(&response, 0, sizeof(response));
+
        rcu_read_lock();
        session = session_find_by_id(conn->sessions_ht, session_id);
        if (!session) {
@@ -1032,6 +1045,8 @@ int viewer_attach_session(struct relay_connection *conn)
 
        health_code_update();
 
+       memset(&response, 0, sizeof(response));
+
        if (!conn->viewer_session) {
                DBG("Client trying to attach before creating a live viewer session");
                response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
@@ -1201,6 +1216,7 @@ static int check_index_status(struct relay_viewer_stream *vstream,
                                 */
                                index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
                                index->timestamp_end = htobe64(rstream->beacon_ts_end);
+                               index->stream_id = htobe64(rstream->ctf_stream_id);
                                goto index_ready;
                        } else if (rstream->total_index_received <= vstream->last_sent_index
                                        && !vstream->close_write_flag) {
@@ -1314,7 +1330,7 @@ int viewer_get_next_index(struct relay_connection *conn)
        ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
        pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
        if (ret < 0) {
-               goto end;
+               goto end_unlock;
        } else if (ret == 1) {
                /*
                 * This means the viewer index data structure has been populated by the
@@ -1618,6 +1634,8 @@ int viewer_get_metadata(struct relay_connection *conn)
        }
        health_code_update();
 
+       memset(&reply, 0, sizeof(reply));
+
        rcu_read_lock();
        stream = viewer_stream_find_by_id(be64toh(request.stream_id));
        if (!stream || !stream->metadata_flag) {
@@ -1718,6 +1736,7 @@ int viewer_create_session(struct relay_connection *conn)
 
        DBG("Viewer create session received");
 
+       memset(&resp, 0, sizeof(resp));
        resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
        conn->viewer_session = zmalloc(sizeof(*conn->viewer_session));
        if (!conn->viewer_session) {
@@ -1749,6 +1768,7 @@ void live_relay_unknown_command(struct relay_connection *conn)
 {
        struct lttcomm_relayd_generic_reply reply;
 
+       memset(&reply, 0, sizeof(reply));
        reply.ret_code = htobe32(LTTNG_ERR_UNK);
        (void) send_response(conn->sock, &reply, sizeof(reply));
 }
@@ -1938,6 +1958,11 @@ restart:
 
                        health_code_update();
 
+                       if (!revents) {
+                               /* No activity for this FD (poll implementation). */
+                               continue;
+                       }
+
                        /* Thread quit pipe has been closed. Killing thread. */
                        ret = check_thread_quit_pipe(pollfd, revents);
                        if (ret) {
This page took 0.028334 seconds and 4 git commands to generate.