<div dir="ltr">Merged in master, 2.6 and 2.5.<div><br></div><div>Thanks!</div><div>Jérémie</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 28, 2015 at 5:23 PM, Mathieu Desnoyers <span dir="ltr"><<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Merge fixes from Babeltrace lttng-live plugin, especially about<br>
incorrect use of send() and recv().<br>
<br>
Can be triggered with 32 virtual processors visible on the system with<br>
the root_regression test suite.<br>
<br>
Signed-off-by: Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com">mathieu.desnoyers@efficios.com</a>><br>
---<br>
tests/regression/tools/live/live_test.c | 303 +++++++++++++++++---------------<br>
1 file changed, 158 insertions(+), 145 deletions(-)<br>
<br>
diff --git a/tests/regression/tools/live/live_test.c b/tests/regression/tools/live/live_test.c<br>
index e30c391..b447733 100644<br>
--- a/tests/regression/tools/live/live_test.c<br>
+++ b/tests/regression/tools/live/live_test.c<br>
@@ -80,6 +80,38 @@ struct live_session {<br>
};<br>
<br>
static<br>
+ssize_t lttng_live_recv(int fd, void *buf, size_t len)<br>
+{<br>
+ ssize_t ret;<br>
+ size_t copied = 0, to_copy = len;<br>
+<br>
+ do {<br>
+ ret = recv(fd, buf + copied, to_copy, 0);<br>
+ if (ret > 0) {<br>
+ assert(ret <= to_copy);<br>
+ copied += ret;<br>
+ to_copy -= ret;<br>
+ }<br>
+ } while ((ret > 0 && to_copy > 0)<br>
+ || (ret < 0 && errno == EINTR));<br>
+ if (ret > 0)<br>
+ ret = copied;<br>
+ /* ret = 0 means orderly shutdown, ret < 0 is error. */<br>
+ return ret;<br>
+}<br>
+<br>
+static<br>
+ssize_t lttng_live_send(int fd, const void *buf, size_t len)<br>
+{<br>
+ ssize_t ret;<br>
+<br>
+ do {<br>
+ ret = send(fd, buf, len, MSG_NOSIGNAL);<br>
+ } while (ret < 0 && errno == EINTR);<br>
+ return ret;<br>
+}<br>
+<br>
+static<br>
int connect_viewer(char *hostname)<br>
{<br>
struct hostent *host;<br>
@@ -125,7 +157,7 @@ int establish_connection(void)<br>
{<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_connect connect;<br>
- int ret;<br>
+ ssize_t ret_len;<br>
<br>
cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);<br>
cmd.data_size = sizeof(connect);<br>
@@ -136,32 +168,30 @@ int establish_connection(void)<br>
connect.minor = htobe32(VERSION_MINOR);<br>
connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);<br>
<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = send(control_sock, &connect, sizeof(connect), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &connect, sizeof(connect));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending version\n");<br>
goto error;<br>
}<br>
<br>
- do {<br>
- ret = recv(control_sock, &connect, sizeof(connect), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving version\n");<br>
goto error;<br>
}<br>
- ret = 0;<br>
+ return 0;<br>
<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
/*<br>
@@ -172,34 +202,33 @@ int list_sessions(int *session_id)<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_list_sessions list;<br>
struct lttng_viewer_session lsession;<br>
- int i, ret;<br>
+ int i;<br>
+ ssize_t ret_len;<br>
int first_session = 0;<br>
<br>
cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);<br>
cmd.data_size = 0;<br>
cmd.cmd_version = 0;<br>
<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
<br>
- do {<br>
- ret = recv(control_sock, &list, sizeof(list), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &list, sizeof(list));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving session list\n");<br>
goto error;<br>
}<br>
<br>
for (i = 0; i < be32toh(list.sessions_count); i++) {<br>
- do {<br>
- ret = recv(control_sock, &lsession, sizeof(lsession), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving session\n");<br>
goto error;<br>
}<br>
@@ -209,52 +238,48 @@ int list_sessions(int *session_id)<br>
}<br>
}<br>
<br>
- ret = be32toh(list.sessions_count);<br>
+ return be32toh(list.sessions_count);<br>
<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
-int create_viewer_session()<br>
+int create_viewer_session(void)<br>
{<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_create_session_response resp;<br>
- int ret;<br>
ssize_t ret_len;<br>
<br>
cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);<br>
cmd.data_size = 0;<br>
cmd.cmd_version = 0;<br>
<br>
- do {<br>
- ret_len = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret_len < 0 && errno == EINTR);<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
if (ret_len < 0) {<br>
fprintf(stderr, "[error] Error sending cmd\n");<br>
- ret = ret_len;<br>
goto error;<br>
}<br>
assert(ret_len == sizeof(cmd));<br>
<br>
- do {<br>
- ret_len = recv(control_sock, &resp, sizeof(resp), 0);<br>
- } while (ret_len < 0 && errno == EINTR);<br>
+ ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
if (ret_len < 0) {<br>
fprintf(stderr, "[error] Error receiving create session reply\n");<br>
- ret = ret_len;<br>
goto error;<br>
}<br>
assert(ret_len == sizeof(resp));<br>
<br>
if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {<br>
fprintf(stderr, "[error] Error creating viewer session\n");<br>
- ret = -1;<br>
goto error;<br>
}<br>
- ret = 0;<br>
+ return 0;<br>
<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
int attach_session(int id)<br>
@@ -263,11 +288,11 @@ int attach_session(int id)<br>
struct lttng_viewer_attach_session_request rq;<br>
struct lttng_viewer_attach_session_response rp;<br>
struct lttng_viewer_stream stream;<br>
- int ret, i;<br>
+ int i;<br>
+ ssize_t ret_len;<br>
<br>
session = zmalloc(sizeof(struct live_session));<br>
if (!session) {<br>
- ret = -1;<br>
goto error;<br>
}<br>
<br>
@@ -279,46 +304,44 @@ int attach_session(int id)<br>
rq.session_id = htobe64(id);<br>
rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);<br>
<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = send(control_sock, &rq, sizeof(rq), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending attach request\n");<br>
goto error;<br>
}<br>
<br>
- do {<br>
- ret = recv(control_sock, &rp, sizeof(rp), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving attach response\n");<br>
goto error;<br>
}<br>
if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
}<br>
<br>
session->stream_count = be32toh(rp.streams_count);<br>
session->streams = zmalloc(session->stream_count *<br>
sizeof(struct viewer_stream));<br>
if (!session->streams) {<br>
- ret = -1;<br>
goto error;<br>
}<br>
<br>
for (i = 0; i < be32toh(rp.streams_count); i++) {<br>
- do {<br>
- ret = recv(control_sock, &stream, sizeof(stream), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving stream\n");<br>
goto error;<br>
}<br>
@@ -330,7 +353,6 @@ int attach_session(int id)<br>
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);<br>
if (session->streams[i].mmap_base == MAP_FAILED) {<br>
fprintf(stderr, "mmap error\n");<br>
- ret = -1;<br>
goto error;<br>
}<br>
<br>
@@ -338,11 +360,10 @@ int attach_session(int id)<br>
session->streams[i].metadata_flag = 1;<br>
}<br>
}<br>
- ret = session->stream_count;<br>
+ return session->stream_count;<br>
<br>
-end:<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
int get_metadata(void)<br>
@@ -350,6 +371,7 @@ int get_metadata(void)<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_get_metadata rq;<br>
struct lttng_viewer_metadata_packet rp;<br>
+ ssize_t ret_len;<br>
int ret;<br>
uint64_t i;<br>
char *data = NULL;<br>
@@ -369,30 +391,27 @@ int get_metadata(void)<br>
<br>
if (metadata_stream_id < 0) {<br>
fprintf(stderr, "No metadata stream found\n");<br>
- ret = -1;<br>
goto error;<br>
}<br>
<br>
rq.stream_id = htobe64(session->streams[metadata_stream_id].id);<br>
<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = send(control_sock, &rq, sizeof(rq), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending get_metadata request\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = recv(control_sock, &rp, sizeof(rp), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving metadata response\n");<br>
goto error;<br>
}<br>
@@ -401,21 +420,19 @@ int get_metadata(void)<br>
break;<br>
case LTTNG_VIEWER_NO_NEW_METADATA:<br>
fprintf(stderr, "NO NEW\n");<br>
- ret = -1;<br>
+ ret = 0;<br>
goto end;<br>
case LTTNG_VIEWER_METADATA_ERR:<br>
fprintf(stderr, "ERR\n");<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
default:<br>
fprintf(stderr, "UNKNOWN\n");<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
}<br>
<br>
len = be64toh(rp.len);<br>
if (len <= 0) {<br>
- goto end;<br>
+ goto error;<br>
}<br>
<br>
data = zmalloc(len);<br>
@@ -423,20 +440,24 @@ int get_metadata(void)<br>
PERROR("relay data zmalloc");<br>
goto error;<br>
}<br>
- do {<br>
- ret = recv(control_sock, data, len, MSG_WAITALL);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, data, len);<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error_free_data;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving trace packet\n");<br>
- free(data);<br>
- goto error;<br>
+ goto error_free_data;<br>
}<br>
free(data);<br>
-<br>
- ret = (int) len;<br>
+ ret = len;<br>
end:<br>
-error:<br>
return ret;<br>
+<br>
+error_free_data:<br>
+ free(data);<br>
+error:<br>
+ return -1;<br>
}<br>
<br>
int get_next_index(void)<br>
@@ -444,7 +465,7 @@ int get_next_index(void)<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_get_next_index rq;<br>
struct lttng_viewer_index rp;<br>
- int ret;<br>
+ ssize_t ret_len;<br>
int id;<br>
<br>
cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);<br>
@@ -455,27 +476,26 @@ int get_next_index(void)<br>
if (session->streams[id].metadata_flag) {<br>
continue;<br>
}<br>
+ memset(&rq, 0, sizeof(rq));<br>
rq.stream_id = htobe64(session->streams[id].id);<br>
<br>
retry:<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = send(control_sock, &rq, sizeof(rq), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending get_next_index request\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = recv(control_sock, &rp, sizeof(rp), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving index response\n");<br>
goto error;<br>
}<br>
@@ -498,11 +518,9 @@ retry:<br>
break;<br>
case LTTNG_VIEWER_INDEX_ERR:<br>
fprintf(stderr, "(ERR)\n");<br>
- ret = -1;<br>
goto error;<br>
default:<br>
fprintf(stderr, "SHOULD NOT HAPPEN\n");<br>
- ret = -1;<br>
goto error;<br>
}<br>
if (!first_packet_stream_id) {<br>
@@ -511,10 +529,10 @@ retry:<br>
first_packet_stream_id = id;<br>
}<br>
}<br>
- ret = 0;<br>
+ return 0;<br>
<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
static<br>
@@ -524,35 +542,34 @@ int get_data_packet(int id, uint64_t offset,<br>
struct lttng_viewer_cmd cmd;<br>
struct lttng_viewer_get_packet rq;<br>
struct lttng_viewer_trace_packet rp;<br>
- int ret;<br>
+ ssize_t ret_len;<br>
<br>
cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);<br>
cmd.data_size = sizeof(rq);<br>
cmd.cmd_version = 0;<br>
<br>
+ memset(&rq, 0, sizeof(rq));<br>
rq.stream_id = htobe64(session->streams[id].id);<br>
/* Already in big endian. */<br>
rq.offset = offset;<br>
rq.len = htobe32(len);<br>
<br>
- do {<br>
- ret = send(control_sock, &cmd, sizeof(cmd), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending cmd\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = send(control_sock, &rq, sizeof(rq), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error sending get_data_packet request\n");<br>
goto error;<br>
}<br>
- do {<br>
- ret = recv(control_sock, &rp, sizeof(rp), 0);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving data response\n");<br>
goto error;<br>
}<br>
@@ -560,49 +577,45 @@ int get_data_packet(int id, uint64_t offset,<br>
<br>
switch (be32toh(rp.status)) {<br>
case LTTNG_VIEWER_GET_PACKET_OK:<br>
+ len = be32toh(rp.len);<br>
break;<br>
case LTTNG_VIEWER_GET_PACKET_RETRY:<br>
fprintf(stderr, "RETRY\n");<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
case LTTNG_VIEWER_GET_PACKET_ERR:<br>
if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {<br>
fprintf(stderr, "NEW_METADATA\n");<br>
- ret = 0;<br>
goto end;<br>
}<br>
fprintf(stderr, "ERR\n");<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
default:<br>
fprintf(stderr, "UNKNOWN\n");<br>
- ret = -1;<br>
- goto end;<br>
+ goto error;<br>
}<br>
<br>
- len = be32toh(rp.len);<br>
- if (len <= 0) {<br>
- goto end;<br>
+ if (len == 0) {<br>
+ goto error;<br>
}<br>
<br>
if (len > mmap_size) {<br>
fprintf(stderr, "mmap_size not big enough\n");<br>
- ret = -1;<br>
goto error;<br>
}<br>
<br>
- do {<br>
- ret = recv(control_sock, session->streams[id].mmap_base, len, MSG_WAITALL);<br>
- } while (ret < 0 && errno == EINTR);<br>
- if (ret < 0) {<br>
+ ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len);<br>
+ if (ret_len == 0) {<br>
+ fprintf(stderr, "[error] Remote side has closed connection\n");<br>
+ goto error;<br>
+ }<br>
+ if (ret_len < 0) {<br>
fprintf(stderr, "Error receiving trace packet\n");<br>
goto error;<br>
}<br>
- ret = len;<br>
-<br>
end:<br>
+ return 0;<br>
error:<br>
- return ret;<br>
+ return -1;<br>
}<br>
<br>
int main(int argc, char **argv)<br>
@@ -638,7 +651,7 @@ int main(int argc, char **argv)<br>
<br>
ret = get_data_packet(first_packet_stream_id, first_packet_offset,<br>
first_packet_len);<br>
- ok(ret == first_packet_len,<br>
+ ok(ret == 0,<br>
"Get one data packet for stream %d, offset %d, len %d",<br>
first_packet_stream_id, first_packet_offset,<br>
first_packet_len);<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.1.4<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Jérémie Galarneau<br>EfficiOS Inc.<br><a href="http://www.efficios.com" target="_blank">http://www.efficios.com</a></div>
</div>