msg_waitall for now
[lttngtop.git] / src / lttng-live-comm.c
index 15a3dd2f62ff1971190de83efb398b44c5ba43c1..08351320d52678e594e76a0d13ead5b6263f075d 100644 (file)
@@ -128,7 +128,7 @@ int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -138,17 +138,22 @@ int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
                ret_len = send(ctx->control_sock, &connect, sizeof(connect), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending version\n");
+               perror("[error] Error sending version");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(connect));
 
        do {
-               ret_len = recv(ctx->control_sock, &connect, sizeof(connect), 0);
+               ret_len = recv(ctx->control_sock, &connect, sizeof(connect), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving version\n");
+               perror("[error] Error receiving version");
                ret = ret_len;
                goto error;
        }
@@ -250,17 +255,22 @@ int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(cmd));
 
        do {
-               ret_len = recv(ctx->control_sock, &list, sizeof(list), 0);
+               ret_len = recv(ctx->control_sock, &list, sizeof(list), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving session list\n");
+               perror("[error] Error receiving session list");
                ret = ret_len;
                goto error;
        }
@@ -269,10 +279,15 @@ int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
        sessions_count = be32toh(list.sessions_count);
        for (i = 0; i < sessions_count; i++) {
                do {
-                       ret_len = recv(ctx->control_sock, &lsession, sizeof(lsession), 0);
+                       ret_len = recv(ctx->control_sock, &lsession, sizeof(lsession), MSG_WAITALL);
                } while (ret_len < 0 && errno == EINTR);
+               if (ret_len == 0) {
+                       fprintf(stderr, "[error] Remote side has closed connection\n");
+                       ret = -1;
+                       goto error;
+               }
                if (ret_len < 0) {
-                       fprintf(stderr, "[error] Error receiving session\n");
+                       perror("[error] Error receiving session");
                        ret = ret_len;
                        goto error;
                }
@@ -352,14 +367,14 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
        memset(&rq, 0, sizeof(rq));
        rq.session_id = htobe64(id);
        // TODO: add cmd line parameter to select seek beginning
-       // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
-       rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
+       rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
+       //rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
 
        do {
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -369,17 +384,22 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
                ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending attach request\n");
+               perror("[error] Error sending attach request");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(rq));
 
        do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
+               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving attach response\n");
+               perror("[error] Error receiving attach response");
                ret = ret_len;
                goto error;
        }
@@ -392,7 +412,7 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
                ret = -LTTNG_VIEWER_ATTACH_UNK;
                goto end;
        case LTTNG_VIEWER_ATTACH_ALREADY:
-               fprintf(stderr, "[error] Already a viewer attached\n");
+               fprintf(stderr, "[error] There is already a viewer attached to this session\n");
                ret = -1;
                goto end;
        case LTTNG_VIEWER_ATTACH_NOT_LIVE:
@@ -430,10 +450,15 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
                        ctx->session->stream_count);
        for (i = 0; i < be32toh(rp.streams_count); i++) {
                do {
-                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
+                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), MSG_WAITALL);
                } while (ret_len < 0 && errno == EINTR);
+               if (ret_len == 0) {
+                       fprintf(stderr, "[error] Remote side has closed connection\n");
+                       ret = -1;
+                       goto error;
+               }
                if (ret_len < 0) {
-                       fprintf(stderr, "[error] Error receiving stream\n");
+                       perror("[error] Error receiving stream");
                        ret = ret_len;
                        goto error;
                }
@@ -559,7 +584,7 @@ int get_data_packet(struct lttng_live_ctx *ctx,
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -569,17 +594,22 @@ int get_data_packet(struct lttng_live_ctx *ctx,
                ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending get_data_packet request\n");
+               perror("[error] Error sending get_data_packet request");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(rq));
 
        do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
+               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving data response\n");
+               perror("[error] Error receiving data response");
                ret = ret_len;
                goto error;
        }
@@ -645,8 +675,7 @@ int get_data_packet(struct lttng_live_ctx *ctx,
                        /* unmap old base */
                        ret = munmap_align(pos->base_mma);
                        if (ret) {
-                               fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
-                                       strerror(errno));
+                               perror("[error] Unable to unmap old base");
                                ret = -1;
                                goto error;
                        }
@@ -656,8 +685,7 @@ int get_data_packet(struct lttng_live_ctx *ctx,
                                PROT_READ | PROT_WRITE,
                                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
                if (pos->base_mma == MAP_FAILED) {
-                       fprintf(stderr, "[error] mmap error %s.\n",
-                               strerror(errno));
+                       perror("[error] mmap error");
                        pos->base_mma = NULL;
                        ret = -1;
                        goto error;
@@ -673,8 +701,13 @@ int get_data_packet(struct lttng_live_ctx *ctx,
                        mmap_align_addr(pos->base_mma), len,
                        MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving trace packet\n");
+               perror("[error] Error receiving trace packet");
                ret = ret_len;
                goto error;
        }
@@ -713,7 +746,7 @@ int get_new_metadata(struct lttng_live_ctx *ctx,
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -723,17 +756,22 @@ int get_new_metadata(struct lttng_live_ctx *ctx,
                ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending get_metadata request\n");
+               perror("[error] Error sending get_metadata request");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(rq));
 
        do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
+               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving metadata response\n");
+               perror("[error] Error receiving metadata response");
                ret = ret_len;
                goto error;
        }
@@ -773,8 +811,14 @@ int get_new_metadata(struct lttng_live_ctx *ctx,
        do {
                ret_len = recv(ctx->control_sock, data, len, MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               free(data);
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving trace packet\n");
+               perror("[error] Error receiving trace packet");
                ret = ret_len;
                free(data);
                goto error;
@@ -829,7 +873,7 @@ retry:
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -839,17 +883,22 @@ retry:
                ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending get_next_index request\n");
+               perror("[error] Error sending get_next_index request");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(rq));
 
        do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
+               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving index response\n");
+               perror("[error] Error receiving index response");
                ret = ret_len;
                goto error;
        }
@@ -1066,17 +1115,22 @@ int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(cmd));
 
        do {
-               ret_len = recv(ctx->control_sock, &resp, sizeof(resp), 0);
+               ret_len = recv(ctx->control_sock, &resp, sizeof(resp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving create session reply\n");
+               perror("[error] Error receiving create session reply");
                ret = ret_len;
                goto error;
        }
@@ -1211,7 +1265,7 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
                ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending cmd\n");
+               perror("[error] Error sending cmd");
                ret = ret_len;
                goto error;
        }
@@ -1221,17 +1275,22 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
                ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
        } while (ret_len < 0 && errno == EINTR);
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error sending get_new_streams request\n");
+               perror("[error] Error sending get_new_streams request");
                ret = ret_len;
                goto error;
        }
        assert(ret_len == sizeof(rq));
 
        do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
+               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), MSG_WAITALL);
        } while (ret_len < 0 && errno == EINTR);
+       if (ret_len == 0) {
+               fprintf(stderr, "[error] Remote side has closed connection\n");
+               ret = -1;
+               goto error;
+       }
        if (ret_len < 0) {
-               fprintf(stderr, "[error] Error receiving get_new_streams response\n");
+               perror("[error] Error receiving get_new_streams response");
                ret = ret_len;
                goto error;
        }
@@ -1274,10 +1333,15 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
                        ctx->session->stream_count);
        for (i = 0; i < stream_count; i++) {
                do {
-                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
+                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), MSG_WAITALL);
                } while (ret_len < 0 && errno == EINTR);
+               if (ret_len == 0) {
+                       fprintf(stderr, "[error] Remote side has closed connection\n");
+                       ret = -1;
+                       goto error;
+               }
                if (ret_len < 0) {
-                       fprintf(stderr, "[error] Error receiving stream\n");
+                       perror("[error] Error receiving stream");
                        ret = ret_len;
                        goto error;
                }
This page took 0.029557 seconds and 4 git commands to generate.