]> granicus.if.org Git - sysstat/commitdiff
iostat: Fix missing "}" and "]" in JSON output when stopped by SIGINT
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 3 Mar 2019 09:21:24 +0000 (10:21 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 3 Mar 2019 09:21:24 +0000 (10:21 +0100)
Add a SIGINT handler to iostat so that JSON output can be ended properly
when the user presses Ctrl/C.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
iostat.c

index ff16863f6f9c0f0048fa97e29bd92ea6e005ba2f..543beaf48fb9986eff7853517b42e35aa2a554de 100644 (file)
--- a/iostat.c
+++ b/iostat.c
@@ -74,7 +74,8 @@ unsigned int dm_major;        /* Device-mapper major number */
 long interval = 0;
 char timestamp[TIMESTAMP_LEN];
 
-struct sigaction alrm_act;
+struct sigaction alrm_act, int_act;
+int sigint_caught = 0;
 
 /*
  ***************************************************************************
@@ -138,6 +139,19 @@ void alarm_handler(int sig)
        alarm(interval);
 }
 
+/*
+ ***************************************************************************
+ * SIGINT signal handler.
+ *
+ * IN:
+ * @sig        Signal number.
+ **************************************************************************
+ */
+void int_handler(int sig)
+{
+       sigint_caught = 1;
+}
+
 /*
  ***************************************************************************
  * Initialize stats common structures.
@@ -1823,6 +1837,17 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
                skip = 1;
        }
 
+       /* Set a handler for SIGALRM */
+       memset(&alrm_act, 0, sizeof(alrm_act));
+       alrm_act.sa_handler = alarm_handler;
+       sigaction(SIGALRM, &alrm_act, NULL);
+       alarm(interval);
+
+       /* Set a handler for SIGINT */
+       memset(&int_act, 0, sizeof(int_act));
+       int_act.sa_handler = int_handler;
+       sigaction(SIGINT, &int_act, NULL);
+
        /* Don't buffer data if redirected to a pipe */
        setbuf(stdout, NULL);
 
@@ -1889,6 +1914,12 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
                if (count) {
                        curr ^= 1;
                        pause();
+
+                       if (sigint_caught) {
+                               /* SIGINT signal caught => Terminate JSON output properly */
+                               count = 0;
+                               printf("\n");   /* Skip "^C" displayed on screen */
+                       }
                }
        }
        while (count);
@@ -2243,12 +2274,6 @@ int main(int argc, char **argv)
                printf("\n");
        }
 
-       /* Set a handler for SIGALRM */
-       memset(&alrm_act, 0, sizeof(alrm_act));
-       alrm_act.sa_handler = alarm_handler;
-       sigaction(SIGALRM, &alrm_act, NULL);
-       alarm(interval);
-
        /* Main loop */
        rw_io_stat_loop(count, &rectime);