common: improve redirect_output() error handling
authorBrian Norris <banorris@uci.edu>
Thu, 4 Apr 2013 19:55:33 +0000 (12:55 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 4 Apr 2013 20:03:14 +0000 (13:03 -0700)
Check the return code and errno results of all system/library calls.
Also delete the extraneous 'fd' variable and use the appropriate
STDOUT_FILENO macro instead of fileno(stdout).

common.cc

index a43064d527a03340d0f62d260b13b7638cf131f5..338058d9f3a8b990278605fe0b6c6c1c38e0d082 100644 (file)
--- a/common.cc
+++ b/common.cc
@@ -88,10 +88,12 @@ static int fd_user_out; /**< @brief File descriptor from which to read user prog
  */
 void redirect_output()
 {
  */
 void redirect_output()
 {
-       int fd;
-
        /* Save stdout for later use */
        /* Save stdout for later use */
-       model_out = dup(fileno(stdout));
+       model_out = dup(STDOUT_FILENO);
+       if (model_out < 0) {
+               perror("dup");
+               exit(EXIT_FAILURE);
+       }
 
        /* Redirect program output to a pipe */
        int pipefd[2];
 
        /* Redirect program output to a pipe */
        int pipefd[2];
@@ -99,11 +101,17 @@ void redirect_output()
                perror("pipe");
                exit(EXIT_FAILURE);
        }
                perror("pipe");
                exit(EXIT_FAILURE);
        }
-       fd = dup2(pipefd[1], fileno(stdout)); // STDOUT_FILENO
+       if (dup2(pipefd[1], STDOUT_FILENO) < 0) {
+               perror("dup2");
+               exit(EXIT_FAILURE);
+       }
        close(pipefd[1]);
 
        /* Save the "read" side of the pipe for use later */
        close(pipefd[1]);
 
        /* Save the "read" side of the pipe for use later */
-       fcntl(pipefd[0], F_SETFL, O_NONBLOCK);
+       if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) < 0) {
+               perror("fcntl");
+               exit(EXIT_FAILURE);
+       }
        fd_user_out = pipefd[0];
 }
 
        fd_user_out = pipefd[0];
 }