#include <inttypes.h>
#include <fcntl.h>
#include <sys/mman.h>
+#include <poll.h>
#include <babeltrace/ctf/ctf-index.h>
#include "lttng-live.h"
#include "lttng-viewer-abi.h"
+#define ACTIVE_POLL_DELAY 100 /* ms */
+
/*
* Memory allocation zeroed
*/
struct lttng_live_viewer_stream *viewer_stream,
char **metadata_buf);
+static
+ssize_t lttng_live_recv(int fd, void *buf, size_t len)
+{
+ ssize_t ret;
+ size_t copied = 0, to_copy = len;
+
+ do {
+ ret = recv(fd, buf + copied, to_copy, 0);
+ if (ret > 0) {
+ assert(ret <= to_copy);
+ copied += ret;
+ to_copy -= ret;
+ }
+ } while ((ret > 0 && to_copy > 0)
+ || (ret < 0 && errno == EINTR));
+ if (ret > 0)
+ ret = copied;
+ /* ret = 0 means orderly shutdown, ret < 0 is error. */
+ return ret;
+}
+
+static
+ssize_t lttng_live_send(int fd, const void *buf, size_t len)
+{
+ ssize_t ret;
+
+ do {
+ ret = send(fd, buf, len, MSG_NOSIGNAL);
+ } while (ret < 0 && errno == EINTR);
+ return ret;
+}
+
int lttng_live_connect_viewer(struct lttng_live_ctx *ctx)
{
struct hostent *host;
connect.minor = htobe32(LTTNG_LIVE_MINOR);
connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &connect, sizeof(connect), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &connect, sizeof(connect));
if (ret_len < 0) {
perror("[error] Error sending version");
ret = ret_len;
}
assert(ret_len == sizeof(connect));
- do {
- ret_len = recv(ctx->control_sock, &connect, sizeof(connect), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &connect, sizeof(connect));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
cmd.data_size = 0;
cmd.cmd_version = 0;
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = recv(ctx->control_sock, &list, sizeof(list), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &list, sizeof(list));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
sessions_count = be32toh(list.sessions_count);
for (i = 0; i < sessions_count; i++) {
- do {
- ret_len = recv(ctx->control_sock, &lsession, sizeof(lsession), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &lsession, sizeof(lsession));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
// 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);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending attach request");
ret = ret_len;
}
assert(ret_len == sizeof(rq));
- do {
- ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
ctx->session->stream_count);
for (i = 0; i < be32toh(rp.streams_count); i++) {
- do {
- ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
rq.offset = offset;
rq.len = htobe32(len);
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending get_data_packet request");
ret = ret_len;
}
assert(ret_len == sizeof(rq));
- do {
- ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
stream->mmap_size);
}
- do {
- ret_len = recv(ctx->control_sock,
- mmap_align_addr(pos->base_mma), len,
- MSG_WAITALL);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock,
+ mmap_align_addr(pos->base_mma), len);
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
cmd.data_size = sizeof(rq);
cmd.cmd_version = 0;
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending get_metadata request");
ret = ret_len;
}
assert(ret_len == sizeof(rq));
- do {
- ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
ret = -1;
goto error;
}
- do {
- ret_len = recv(ctx->control_sock, data, len, MSG_WAITALL);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, data, len);
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
{
int ret = 0;
struct lttng_live_viewer_stream *metadata_stream;
- size_t size;
+ size_t size, len_read = 0;;
metadata_stream = viewer_stream->ctf_trace->metadata_stream;
if (!metadata_stream) {
* negative value on error.
*/
ret = get_one_metadata_packet(ctx, metadata_stream);
- } while (ret > 0);
+ if (ret > 0) {
+ len_read += ret;
+ }
+ if (!len_read) {
+ (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
+ }
+ } while (ret > 0 || !len_read);
if (fclose(metadata_stream->metadata_fp_write))
perror("fclose");
rq.stream_id = htobe64(viewer_stream->id);
retry:
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending get_next_index request");
ret = ret_len;
}
assert(ret_len == sizeof(rq));
- do {
- ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
break;
case LTTNG_VIEWER_INDEX_RETRY:
printf_verbose("get_next_index: retry\n");
- sleep(1);
+ (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
goto retry;
case LTTNG_VIEWER_INDEX_HUP:
printf_verbose("get_next_index: stream hung up\n");
cmd.data_size = 0;
cmd.cmd_version = 0;
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = recv(ctx->control_sock, &resp, sizeof(resp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
memset(&rq, 0, sizeof(rq));
rq.session_id = htobe64(id);
- do {
- ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
ret = ret_len;
}
assert(ret_len == sizeof(cmd));
- do {
- ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending get_new_streams request");
ret = ret_len;
}
assert(ret_len == sizeof(rq));
- do {
- ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
ctx->session->stream_count);
for (i = 0; i < stream_count; i++) {
- do {
- ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
- } while (ret_len < 0 && errno == EINTR);
+ ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
ret = -1;
ret = ask_new_streams(ctx);
if (ret < 0)
goto end_free;
+ if (!ctx->session->stream_count)
+ (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
}
g_hash_table_foreach(ctx->session->ctf_traces, add_traces,