From 1a9d614f0e6e536e2b65e537334148354f14823e Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Sun, 5 Jun 2016 17:42:44 +0200
Subject: [PATCH] take care of SIGPIPE received when writing a dead socket

write() on a socket with the other end closed throws a SIGPIPE.
Let's ignore this signal. write() will return an error anyway...
---
 common/utils/T/local_tracer.c   | 5 +++++
 common/utils/T/tracer/enb.c     | 4 ++++
 common/utils/T/tracer/textlog.c | 4 ++++
 common/utils/T/tracer/vcd.c     | 4 ++++
 4 files changed, 17 insertions(+)

diff --git a/common/utils/T/local_tracer.c b/common/utils/T/local_tracer.c
index 5d46d2d38d..df6d350a8b 100644
--- a/common/utils/T/local_tracer.c
+++ b/common/utils/T/local_tracer.c
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <pthread.h>
 #include <inttypes.h>
+#include <signal.h>
 
 #include "T_defs.h"
 #include "T_IDs.h"
@@ -195,6 +196,7 @@ again:
 
 dead:
   /* socket died, let's stop all traces and wait for another tracer */
+  /* TODO: be careful with those write, they might write less than wanted */
   buf[0] = 1;
   if (write(to, buf, 1) != 1) abort();
   len = T_NUMBER_OF_IDS;
@@ -312,6 +314,9 @@ void T_local_tracer_main(int remote_port, int wait_for_tracer,
   int dont_wait = wait_for_tracer ? 0 : 1;
   void *f;
 
+  /* write on a socket fails if the other end is closed and we get SIGPIPE */
+  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();
+
   init_shm();
   s = local_socket;
 
diff --git a/common/utils/T/tracer/enb.c b/common/utils/T/tracer/enb.c
index 9cb8d12699..d0a2f38104 100644
--- a/common/utils/T/tracer/enb.c
+++ b/common/utils/T/tracer/enb.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
+#include <signal.h>
 #include "database.h"
 #include "event.h"
 #include "handler.h"
@@ -246,6 +247,9 @@ int main(int n, char **v)
   enb_gui eg;
   enb_data enb_data;
 
+  /* write on a socket fails if the other end is closed and we get SIGPIPE */
+  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();
+
   on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
   on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();
 
diff --git a/common/utils/T/tracer/textlog.c b/common/utils/T/tracer/textlog.c
index 2118cc1bf9..4b977a3eaa 100644
--- a/common/utils/T/tracer/textlog.c
+++ b/common/utils/T/tracer/textlog.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
+#include <signal.h>
 #include "database.h"
 #include "event.h"
 #include "handler.h"
@@ -93,6 +94,9 @@ int main(int n, char **v)
   int gui_active = 1;
   textlog_data textlog_data;
 
+  /* write on a socket fails if the other end is closed and we get SIGPIPE */
+  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();
+
   on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
   on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();
 
diff --git a/common/utils/T/tracer/vcd.c b/common/utils/T/tracer/vcd.c
index 2dc34aead9..e2460282b0 100644
--- a/common/utils/T/tracer/vcd.c
+++ b/common/utils/T/tracer/vcd.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
+#include <signal.h>
 #include "database.h"
 #include "event.h"
 #include "handler.h"
@@ -129,6 +130,9 @@ int main(int n, char **v)
   gui *g;
   vcd_data vcd_data;
 
+  /* write on a socket fails if the other end is closed and we get SIGPIPE */
+  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();
+
   on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
   on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();
 
-- 
GitLab