From 1a6c20cf7631f26fda45784c7f2c4c2f0845f536 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 23 Jun 2016 14:48:25 +0200
Subject: [PATCH] bugfix: record.c did not manage ctrl+c and ctrl+z correctly

ctrl+c did not work when the program was not connected yet.
It also didn't work if no event was received.
---
 common/utils/T/tracer/record.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/common/utils/T/tracer/record.c b/common/utils/T/tracer/record.c
index 2dfe35f4da..c81b93e7d7 100644
--- a/common/utils/T/tracer/record.c
+++ b/common/utils/T/tracer/record.c
@@ -30,9 +30,13 @@ void usage(void)
 
 volatile int run = 1;
 
+static int socket = -1;
+
 void force_stop(int x)
 {
   printf("\ngently quit...\n");
+  close(socket);
+  socket = -1;
   run = 0;
 }
 
@@ -47,7 +51,6 @@ int main(int n, char **v)
   char **on_off_name;
   int *on_off_action;
   int on_off_n = 0;
-  int socket;
   int *is_on;
   int number_of_events;
   int i;
@@ -56,11 +59,6 @@ int main(int n, char **v)
   /* write on a socket fails if the other end is closed and we get SIGPIPE */
   if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();
 
-  /* exit on ctrl+c and ctrl+z */
-  if (signal(SIGQUIT, force_stop) == SIG_ERR) abort();
-  if (signal(SIGINT, force_stop) == SIG_ERR) abort();
-  if (signal(SIGTSTP, force_stop) == 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();
 
@@ -119,6 +117,11 @@ int main(int n, char **v)
     exit(1);
   }
 
+  /* exit on ctrl+c and ctrl+z */
+  if (signal(SIGQUIT, force_stop) == SIG_ERR) abort();
+  if (signal(SIGINT, force_stop) == SIG_ERR) abort();
+  if (signal(SIGTSTP, force_stop) == SIG_ERR) abort();
+
   /* read messages */
   while (run) {
     int type;
-- 
GitLab