diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2023-02-13 16:25:26 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2023-02-13 16:25:26 -0500 |
commit | c15b87b9e8137e7e9fafa0e6c3962218d3dda81d (patch) | |
tree | a68bd5102fc80e8148a286e05f18d631706031c3 | |
parent | 57ad7f3850e862f420dd5c8299a403b5697f9f99 (diff) | |
download | mstat-c15b87b9e8137e7e9fafa0e6c3962218d3dda81d.tar.gz |
Bugfix: Transfer control of standard input to child process
* print a linefeed when an interrupt signal fires to avoid "^Cmessages"
* sleep before exiting to allow time for stdout/stderr to flush from the child process
* replace "data written" with "MSTAT file written"
* print absolute path to data file at exit
-rw-r--r-- | mstat.c | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -60,10 +60,13 @@ static void handle_interrupt(int sig) { case 0: case SIGTERM: case SIGINT: + puts(""); if (option.file) { fflush(option.file); fclose(option.file); - printf("data written: %s\n", option.filename); + // Let stdout/stderr catch up + usleep(100000); + printf("MSTAT file written: %s\n", realpath(option.filename, NULL)); } exit(0); default: @@ -213,7 +216,12 @@ int main(int argc, char *argv[]) { exit(1); } + int stdin_handle = -1; if (p == 0) { + // Give control of STDIN to the child + dup2(STDIN_FILENO, stdin_handle); + close(STDIN_FILENO); + // Execute the requested program (with arguments) if (execv(where, &argv[positional]) < 0) { perror("execv"); @@ -225,6 +233,11 @@ int main(int argc, char *argv[]) { exit(1); } } + + // Give control of STDIN back to the parent + dup2(stdin_handle, STDIN_FILENO); + close(stdin_handle); + option.pid = p; } |