Re-indent code for fork_monitor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
diff --git a/main.c b/main.c
index be0d8b3..64d23a7 100644
--- a/main.c
+++ b/main.c
@@ -4295,72 +4295,73 @@ out:
return ret;
}
-#if !defined(WIN32)
-static void fork_monitor()
-{
- // Make a pipe: [readFD, writeFD]
- int pfd[2];
- int r = pipe(pfd);
- if (r<0) {
- perror("pipe - failed to create pipe for --monitor");
- exit(1);
- }
-
- // Make stderr write end of pipe
- fflush(stderr);
- r = dup2(pfd[1], 2);
- if (r<0) {
- perror("dup2 - failed to alias stderr to write end of pipe for --monitor");
- exit(1);
- }
- r = close(pfd[1]);
- if (r<0) {
- perror("close - failed to close write end of pipe for --monitor");
- exit(1);
- }
-
- // Don't allow a dying monitor to kill the main process
- sighandler_t sr = signal(SIGPIPE, SIG_IGN);
- if (SIG_ERR==sr) {
- perror("signal - failed to edit signal mask for --monitor");
- exit(1);
- }
-
- // Fork a child process
- r = fork();
- if (r<0) {
- perror("fork - failed to fork child process for --monitor");
- exit(1);
- }
+#if defined(unix)
+ static void fork_monitor()
+ {
+ // Make a pipe: [readFD, writeFD]
+ int pfd[2];
+ int r = pipe(pfd);
+ if (r<0) {
+ perror("pipe - failed to create pipe for --monitor");
+ exit(1);
+ }
- // In child, launch command
- if (0==r) {
- // Make stdin read end of pipe
- r = dup2(pfd[0], 0);
+ // Make stderr write end of pipe
+ fflush(stderr);
+ r = dup2(pfd[1], 2);
if (r<0) {
- perror("dup2 - in child, failed to alias read end of pipe to stdin for --monitor");
+ perror("dup2 - failed to alias stderr to write end of pipe for --monitor");
exit(1);
}
- close(pfd[0]);
+ r = close(pfd[1]);
if (r<0) {
- perror("close - in child, failed to close read end of pipe for --monitor");
+ perror("close - failed to close write end of pipe for --monitor");
exit(1);
}
- // Launch user specified command
- execl("/bin/bash", "/bin/bash", "-c", opt_stderr_cmd, (char*)NULL);
- perror("execl - in child failed to exec user specified command for --monitor");
- exit(1);
- }
+ // Don't allow a dying monitor to kill the main process
+ sighandler_t sr0 = signal(SIGPIPE, SIG_IGN);
+ sighandler_t sr1 = signal(SIGPIPE, SIG_IGN);
+ if (SIG_ERR==sr0 || SIG_ERR==sr1) {
+ perror("signal - failed to edit signal mask for --monitor");
+ exit(1);
+ }
- // In parent, clean up unused fds
- r = close(pfd[0]);
- if (r<0) {
- perror("close - failed to close read end of pipe for --monitor");
- exit(1);
+ // Fork a child process
+ r = fork();
+ if (r<0) {
+ perror("fork - failed to fork child process for --monitor");
+ exit(1);
+ }
+
+ // Child: launch monitor command
+ if (0==r) {
+ // Make stdin read end of pipe
+ r = dup2(pfd[0], 0);
+ if (r<0) {
+ perror("dup2 - in child, failed to alias read end of pipe to stdin for --monitor");
+ exit(1);
+ }
+ close(pfd[0]);
+ if (r<0) {
+ perror("close - in child, failed to close read end of pipe for --monitor");
+ exit(1);
+ }
+
+ // Launch user specified command
+ execl("/bin/bash", "/bin/bash", "-c", opt_stderr_cmd, (char*)NULL);
+ perror("execl - in child failed to exec user specified command for --monitor");
+ exit(1);
+ }
+
+ // Parent: clean up unused fds and bail
+ r = close(pfd[0]);
+ if (r<0) {
+ perror("close - failed to close read end of pipe for --monitor");
+ exit(1);
+ }
}
-}
-#endif // !WIN32
+#endif // defined(unix)
int main (int argc, char *argv[])
{