From 83350a807f8bb7403664af9a83541c5b215847e3 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Sun, 5 Jun 2016 17:39:06 +0200
Subject: [PATCH] reading a socket does not abort anymore

fullread has been changed, so get_event too, and the callers also
for the moment the callers crash, enb.c will be a bit more clever,
the others to see...
---
 common/utils/T/tracer/enb.c     |  1 +
 common/utils/T/tracer/event.c   | 11 +++++++----
 common/utils/T/tracer/textlog.c |  1 +
 common/utils/T/tracer/utils.c   |  2 +-
 common/utils/T/tracer/utils.h   |  1 +
 common/utils/T/tracer/vcd.c     |  1 +
 6 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/common/utils/T/tracer/enb.c b/common/utils/T/tracer/enb.c
index 1d97b428d3..9cb8d12699 100644
--- a/common/utils/T/tracer/enb.c
+++ b/common/utils/T/tracer/enb.c
@@ -386,6 +386,7 @@ int main(int n, char **v)
     char v[T_BUFFER_MAX];
     event e;
     e = get_event(enb_data.socket, v, database);
+    if (e.type == -1) abort();
     handle_event(h, e);
   }
 
diff --git a/common/utils/T/tracer/event.c b/common/utils/T/tracer/event.c
index e626b4a7d6..0917c010b3 100644
--- a/common/utils/T/tracer/event.c
+++ b/common/utils/T/tracer/event.c
@@ -22,14 +22,14 @@ event get_event(int socket, char *event_buffer, void *database)
    */
 
 again:
-  fullread(socket, &length, 4);
+  if (fullread(socket, &length, 4) == -1) goto read_error;
 #ifdef T_SEND_TIME
-  fullread(socket, &t, sizeof(struct timespec));
+  if (fullread(socket, &t, sizeof(struct timespec)) == -1) goto read_error;
   length -= sizeof(struct timespec);
 #endif
-  fullread(socket, &type, sizeof(int));
+  if (fullread(socket, &type, sizeof(int)) == -1) goto read_error;
   length -= sizeof(int);
-  fullread(socket, event_buffer, length);
+  if (fullread(socket, event_buffer, length) == -1) goto read_error;
 
   if (type == -1) append_received_config_chunk(event_buffer, length);
   if (type == -2) verify_config();
@@ -41,6 +41,9 @@ again:
 #else
   return new_event(type, length, event_buffer, database);
 #endif
+
+read_error:
+  return (event){type: -1};
 }
 
 #ifdef T_SEND_TIME
diff --git a/common/utils/T/tracer/textlog.c b/common/utils/T/tracer/textlog.c
index 362ff8103e..2118cc1bf9 100644
--- a/common/utils/T/tracer/textlog.c
+++ b/common/utils/T/tracer/textlog.c
@@ -182,6 +182,7 @@ int main(int n, char **v)
     char v[T_BUFFER_MAX];
     event e;
     e = get_event(textlog_data.socket, v, database);
+    if (e.type == -1) abort();
     handle_event(h, e);
   }
 
diff --git a/common/utils/T/tracer/utils.c b/common/utils/T/tracer/utils.c
index f285a71fb1..3ebc000c67 100644
--- a/common/utils/T/tracer/utils.c
+++ b/common/utils/T/tracer/utils.c
@@ -118,7 +118,7 @@ int fullread(int fd, void *_buf, int count)
   int l;
   while (count) {
     l = read(fd, buf, count);
-    if (l <= 0) { printf("read socket problem\n"); abort(); }
+    if (l <= 0) return -1;
     count -= l;
     buf += l;
     ret += l;
diff --git a/common/utils/T/tracer/utils.h b/common/utils/T/tracer/utils.h
index a5b1f1b9f6..e15a8d2fba 100644
--- a/common/utils/T/tracer/utils.h
+++ b/common/utils/T/tracer/utils.h
@@ -23,6 +23,7 @@ list *list_append(list *l, void *data);
 /* socket_send: return 0 if okay, -1 on error */
 int socket_send(int socket, void *buffer, int size);
 int get_connection(char *addr, int port);
+/* fullread: return length read if okay (that is: 'count'), -1 on error */
 int fullread(int fd, void *_buf, int count);
 int connect_to(char *addr, int port);
 
diff --git a/common/utils/T/tracer/vcd.c b/common/utils/T/tracer/vcd.c
index d4972d15fc..2dc34aead9 100644
--- a/common/utils/T/tracer/vcd.c
+++ b/common/utils/T/tracer/vcd.c
@@ -192,6 +192,7 @@ int main(int n, char **v)
     char v[T_BUFFER_MAX];
     event e;
     e = get_event(vcd_data.socket, v, database);
+    if (e.type == -1) abort();
     handle_event(h, e);
   }
 
-- 
GitLab