#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 sockaddr_in server_addr;
int ret;
- host = gethostbyname(ctx->relay_hostname);
- if (!host) {
+ if (lttng_live_should_quit()) {
ret = -1;
goto end;
}
+ host = gethostbyname(ctx->relay_hostname);
+ if (!host) {
+ fprintf(stderr, "[error] Cannot lookup hostname %s\n",
+ ctx->relay_hostname);
+ goto error;
+ }
+
if ((ctx->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
- ret = -1;
- goto end;
+ goto error;
}
server_addr.sin_family = AF_INET;
if (connect(ctx->control_sock, (struct sockaddr *) &server_addr,
sizeof(struct sockaddr)) == -1) {
perror("Connect");
- ret = -1;
- goto end;
+ goto error;
}
ret = 0;
end:
return ret;
+
+error:
+ fprintf(stderr, "[error] Connection failed\n");
+ return -1;
}
int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
int ret;
ssize_t ret_len;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
cmd.data_size = sizeof(connect);
cmd.cmd_version = 0;
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
ret_len = lttng_live_send(ctx->control_sock, &connect, sizeof(connect));
if (ret_len < 0) {
perror("[error] Error sending version");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(connect));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving version");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(connect));
be64toh(connect.viewer_session_id));
printf_verbose("Relayd version : %u.%u\n", be32toh(connect.major),
be32toh(connect.minor));
-
ret = 0;
+end:
+ return ret;
error:
- return ret;
+ fprintf(stderr, "[error] Unable to establish connection\n");
+ return -1;
}
static
uint64_t session_id;
GPtrArray *session_list = NULL;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
if (strlen(ctx->session_name) == 0) {
print_list = 1;
session_list = g_ptr_array_new();
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving session list");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(list));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving session");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(lsession));
print_session_list(session_list, path);
free_session_list(session_list);
}
-
ret = 0;
+end:
+ return ret;
error:
- return ret;
+ fprintf(stderr, "[error] Unable to list sessions\n");
+ return -1;
}
int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream *stream,
int ret, i;
ssize_t ret_len;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
cmd.data_size = sizeof(rq);
cmd.cmd_version = 0;
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
if (ret_len < 0) {
perror("[error] Error sending attach request");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(rq));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving attach response");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(rp));
goto end;
case LTTNG_VIEWER_ATTACH_ALREADY:
fprintf(stderr, "[error] There is already a viewer attached to this session\n");
- ret = -1;
- goto end;
+ goto error;
case LTTNG_VIEWER_ATTACH_NOT_LIVE:
fprintf(stderr, "[error] Not a live session\n");
- ret = -1;
- goto end;
+ goto error;
case LTTNG_VIEWER_ATTACH_SEEK_ERR:
fprintf(stderr, "[error] Wrong seek parameter\n");
- ret = -1;
- goto end;
+ goto error;
default:
fprintf(stderr, "[error] Unknown attach return code %u\n",
be32toh(rp.status));
- ret = -1;
- goto end;
+ goto error;
}
if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
- ret = -1;
- goto end;
+ goto error;
}
ctx->session->stream_count += be32toh(rp.streams_count);
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving stream");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(stream));
}
ret = 0;
-
end:
-error:
return ret;
+
+error:
+ return -1;
}
static
printf_verbose("Asking for new streams returns %d\n",
ret);
if (ret < 0) {
+ if (lttng_live_should_quit()) {
+ goto end;
+ }
if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
printf_verbose("Session %" PRIu64 " closed\n",
id);
ret = ctf_append_trace_metadata(
viewer_stream->ctf_trace->handle->td,
metadata->ctf_trace->metadata_fp);
- if (ret != 0) {
+ /* We accept empty metadata packets */
+ if (ret != 0 && ret != -ENOENT) {
fprintf(stderr, "[error] Appending metadata\n");
goto error;
}
+ ret = 0;
error:
return ret;
int ret;
retry:
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
cmd.data_size = sizeof(rq);
cmd.cmd_version = 0;
memset(&rq, 0, sizeof(rq));
rq.stream_id = htobe64(stream->id);
- /* Already in big endian. */
- rq.offset = offset;
+ rq.offset = htobe64(offset);
rq.len = htobe32(len);
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
assert(ret_len == sizeof(rq));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving data response");
- ret = ret_len;
goto error;
}
if (ret_len != sizeof(rp)) {
fprintf(stderr, "[error] get_data_packet: expected %" PRId64
- ", received %" PRId64 "\n", ret_len,
- sizeof(rp));
- ret = -1;
+ ", received %" PRId64 "\n", sizeof(rp),
+ ret_len);
goto error;
}
"\n", len);
break;
case LTTNG_VIEWER_GET_PACKET_RETRY:
+ /* Unimplemented by relay daemon */
printf_verbose("get_data_packet: retry\n");
- ret = -1;
- goto end;
+ goto error;
case LTTNG_VIEWER_GET_PACKET_ERR:
if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
printf_verbose("get_data_packet: new metadata needed\n");
ret = append_metadata(ctx, stream);
if (ret)
goto error;
- goto retry;
}
if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
+ printf_verbose("get_data_packet: new streams needed\n");
ret = ask_new_streams(ctx);
if (ret < 0)
goto error;
- g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
- ctx->bt_ctx);
+ g_hash_table_foreach(ctx->session->ctf_traces,
+ add_traces, ctx->bt_ctx);
+ }
+ if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
+ | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
+ goto retry;
}
fprintf(stderr, "[error] get_data_packet: error\n");
- ret = -1;
- goto end;
+ goto error;
case LTTNG_VIEWER_GET_PACKET_EOF:
ret = -2;
- goto error;
+ goto end;
default:
printf_verbose("get_data_packet: unknown\n");
- assert(0);
- ret = -1;
- goto end;
+ goto error;
}
- if (len <= 0) {
- ret = -1;
- goto end;
+ if (len == 0) {
+ goto error;
}
if (len > stream->mmap_size) {
ret = munmap_align(pos->base_mma);
if (ret) {
perror("[error] Unable to unmap old base");
- ret = -1;
goto error;
}
pos->base_mma = NULL;
if (pos->base_mma == MAP_FAILED) {
perror("[error] mmap error");
pos->base_mma = NULL;
- ret = -1;
goto error;
}
mmap_align_addr(pos->base_mma), len);
if (ret_len == 0) {
fprintf(stderr, "[error] Remote side has closed connection\n");
- ret = -1;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving trace packet");
- ret = ret_len;
goto error;
}
assert(ret_len == len);
-
+ ret = 0;
end:
-error:
return ret;
+
+error:
+ return -1;
}
static
char *data = NULL;
ssize_t ret_len;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
rq.stream_id = htobe64(metadata_stream->id);
cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
cmd.data_size = sizeof(rq);
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
assert(ret_len == sizeof(rq));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving metadata response");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(rp));
goto end;
case LTTNG_VIEWER_METADATA_ERR:
printf_verbose("get_metadata : ERR\n");
- ret = -1;
- goto end;
+ goto error;
default:
printf_verbose("get_metadata : UNKNOWN\n");
- ret = -1;
- goto end;
+ goto error;
}
len = be64toh(rp.len);
printf_verbose("Writing %" PRIu64" bytes to metadata\n", len);
if (len <= 0) {
- ret = -1;
- goto end;
+ goto error;
}
data = zmalloc(len);
if (!data) {
perror("relay data zmalloc");
- ret = -1;
goto error;
}
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;
- free(data);
- goto error;
+ goto error_free_data;
}
if (ret_len < 0) {
perror("[error] Error receiving trace packet");
- ret = ret_len;
- free(data);
- goto error;
+ goto error_free_data;
}
assert(ret_len == len);
} while (ret_len < 0 && errno == EINTR);
if (ret_len < 0) {
fprintf(stderr, "[error] Writing in the metadata fp\n");
- free(data);
- ret = ret_len;
- goto error;
+ goto error_free_data;
}
assert(ret_len == len);
metadata_stream->metadata_len += len;
+ free(data);
ret = len;
+end:
+ return ret;
+error_free_data:
free(data);
-
-end:
error:
- return ret;
+ return -1;
}
/*
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))
rq.stream_id = htobe64(viewer_stream->id);
retry:
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
assert(ret_len == sizeof(rq));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving index response");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(rp));
ret = ask_new_streams(ctx);
if (ret < 0)
goto error;
- g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
- ctx->bt_ctx);
+ g_hash_table_foreach(ctx->session->ctf_traces,
+ add_traces, ctx->bt_ctx);
}
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");
break;
case LTTNG_VIEWER_INDEX_ERR:
fprintf(stderr, "[error] get_next_index: error\n");
- ret = -1;
goto error;
default:
fprintf(stderr, "[error] get_next_index: unkwown value\n");
- ret = -1;
goto error;
}
-
ret = 0;
+end:
+ return ret;
error:
- return ret;
+ return -1;
}
static
ret = get_next_index(session->ctx, viewer_stream, cur_index);
if (ret < 0) {
pos->offset = EOF;
- fprintf(stderr, "[error] get_next_index failed\n");
+ if (!lttng_live_should_quit()) {
+ fprintf(stderr, "[error] get_next_index failed\n");
+ }
return;
}
printf_verbose("get_data_packet for stream %" PRIu64 "\n",
viewer_stream->id);
ret = get_data_packet(session->ctx, pos, viewer_stream,
- be64toh(cur_index->offset),
+ cur_index->offset,
cur_index->packet_size / CHAR_BIT);
if (ret == -2) {
goto retry;
} else if (ret < 0) {
pos->offset = EOF;
- fprintf(stderr, "[error] get_data_packet failed\n");
+ if (!lttng_live_should_quit()) {
+ fprintf(stderr, "[error] get_data_packet failed\n");
+ }
return;
}
int ret;
ssize_t ret_len;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
cmd.data_size = 0;
cmd.cmd_version = 0;
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving create session reply");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(resp));
if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
fprintf(stderr, "[error] Error creating viewer session\n");
- ret = -1;
goto error;
}
ret = 0;
+end:
+ return ret;
error:
- return ret;
+ return -1;
}
static
ssize_t ret_len;
uint32_t stream_count;
+ if (lttng_live_should_quit()) {
+ ret = -1;
+ goto end;
+ }
+
cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
cmd.data_size = sizeof(rq);
cmd.cmd_version = 0;
ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
if (ret_len < 0) {
perror("[error] Error sending cmd");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(cmd));
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;
goto error;
}
assert(ret_len == sizeof(rq));
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving get_new_streams response");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(rp));
goto end;
case LTTNG_VIEWER_NEW_STREAMS_ERR:
fprintf(stderr, "[error] get_new_streams error\n");
- ret = -1;
- goto end;
+ goto error;
default:
fprintf(stderr, "[error] Unknown return code %u\n",
be32toh(rp.status));
- ret = -1;
- goto end;
+ goto error;
}
stream_count = be32toh(rp.streams_count);
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;
goto error;
}
if (ret_len < 0) {
perror("[error] Error receiving stream");
- ret = ret_len;
goto error;
}
assert(ret_len == sizeof(stream));
}
ret = 0;
-
end:
-error:
return ret;
+
+error:
+ return -1;
}
-void lttng_live_read(struct lttng_live_ctx *ctx)
+int lttng_live_read(struct lttng_live_ctx *ctx)
{
- int ret, i;
+ int ret = -1;
+ int i;
struct bt_ctf_iter *iter;
const struct bt_ctf_event *event;
struct bt_iter_pos begin_pos;
sout = container_of(td_write, struct ctf_text_stream_pos,
trace_descriptor);
- if (!sout->parent.event_cb)
+ if (!sout->parent.event_cb) {
goto end_free;
+ }
ret = lttng_live_create_viewer_session(ctx);
if (ret < 0) {
for (;;) {
int flags;
+ if (lttng_live_should_quit()) {
+ ret = 0;
+ goto end_free;
+ }
+
while (!ctx->session->stream_count) {
- if (ctx->session_ids->len == 0)
+ if (lttng_live_should_quit()
+ || ctx->session_ids->len == 0) {
+ ret = 0;
goto end_free;
+ }
ret = ask_new_streams(ctx);
- if (ret < 0)
+ 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,
begin_pos.type = BT_SEEK_BEGIN;
iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
if (!iter) {
+ if (lttng_live_should_quit()) {
+ ret = 0;
+ goto end;
+ }
fprintf(stderr, "[error] Iterator creation error\n");
goto end;
}
for (;;) {
+ if (lttng_live_should_quit()) {
+ ret = 0;
+ goto end_free;
+ }
event = bt_ctf_iter_read_event_flags(iter, &flags);
if (!(flags & BT_ITER_FLAG_RETRY)) {
if (!event) {
end_free:
bt_context_put(ctx->bt_ctx);
end:
- return;
+ if (lttng_live_should_quit()) {
+ ret = 0;
+ }
+ return ret;
}