execute.c
changeset 347 79b87e385bc5
parent 329 b01bb36b4290
child 348 308315d04787
equal deleted inserted replaced
346:90545736507e 347:79b87e385bc5
   145     }
   145     }
   146 }
   146 }
   147 
   147 
   148 static void run_child(int fd_send_filename)
   148 static void run_child(int fd_send_filename)
   149 {
   149 {
   150     char outfname[] = "/tmp/ts-out.XXXXXX";
   150     char outfname[] = "/ts-out.XXXXXX";
   151     char errfname[sizeof outfname + 2]; /* .e */
   151     char errfname[sizeof outfname + 2]; /* .e */
   152     int namesize;
   152     int namesize;
   153     int outfd;
   153     int outfd;
   154     struct timeval starttv;
   154     struct timeval starttv;
   155 
   155 
   156     if (command_line.store_output)
   156     if (command_line.store_output)
   157     {
   157     {
       
   158         /* Prepare path */
       
   159         const char *tmpdir = getenv("TMPDIR");
       
   160         int lname;
       
   161         char *outfname_full;
       
   162 
       
   163         if (tmpdir == NULL)
       
   164             tmpdir = "/tmp";
       
   165         lname = strlen(tmpdir) + strlen(outfname) + 3 /* .gz*/ + 1 /* \0 */;
       
   166 
       
   167         outfname_full = (char *)malloc(lname);
       
   168         strncpy(outfname_full, tmpdir, lname);
       
   169         strncat(outfname_full, outfname, lname);
       
   170 
   158         if (command_line.gzip)
   171         if (command_line.gzip)
   159         {
   172         {
   160             int p[2];
   173             int p[2];
   161             /* We assume that all handles are closed*/
   174             /* We assume that all handles are closed*/
   162             pipe(p);
   175             pipe(p);
   163 
   176 
       
   177             strncat(outfname_full, ".gz", lname);
       
   178 
   164             /* gzip output goes to the filename */
   179             /* gzip output goes to the filename */
   165             /* This will be the handle other than 0,1,2 */
   180             /* This will be the handle other than 0,1,2 */
   166             outfd = mkstemp(outfname); /* stdout */
   181             outfd = mkstemp(outfname_full); /* stdout */
   167 
   182 
   168             /* Program stdout and stderr */
   183             /* Program stdout and stderr */
   169             /* which go to pipe write handle */
   184             /* which go to pipe write handle */
   170             dup2(p[1], 1);
   185             dup2(p[1], 1);
   171             if (command_line.stderr_apart)
   186             if (command_line.stderr_apart)
   172             {
   187             {
   173                 int errfd;
   188                 int errfd;
   174                 strncpy(errfname, outfname, sizeof errfname);
   189                 strncpy(errfname, outfname_full, sizeof errfname);
   175                 strncat(errfname, ".e", 2);
   190                 strncat(errfname, ".e", 2);
   176                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   191                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   177                 dup2(errfd, 2);
   192                 dup2(errfd, 2);
   178                 close(errfd);
   193                 close(errfd);
   179             }
   194             }
   187             run_gzip(outfd, p[0]);
   202             run_gzip(outfd, p[0]);
   188         }
   203         }
   189         else
   204         else
   190         {
   205         {
   191             /* Prepare the filename */
   206             /* Prepare the filename */
   192             outfd = mkstemp(outfname); /* stdout */
   207             outfd = mkstemp(outfname_full); /* stdout */
   193             dup2(outfd, 1); /* stdout */
   208             dup2(outfd, 1); /* stdout */
   194             if (command_line.stderr_apart)
   209             if (command_line.stderr_apart)
   195             {
   210             {
   196                 int errfd;
   211                 int errfd;
   197                 strncpy(errfname, outfname, sizeof errfname);
   212                 strncpy(errfname, outfname_full, sizeof errfname);
   198                 strncat(errfname, ".e", 2);
   213                 strncat(errfname, ".e", 2);
   199                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   214                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   200                 dup2(errfd, 2);
   215                 dup2(errfd, 2);
   201                 close(errfd);
   216                 close(errfd);
   202             }
   217             }
   204                 dup2(outfd, 2);
   219                 dup2(outfd, 2);
   205             close(outfd);
   220             close(outfd);
   206         }
   221         }
   207 
   222 
   208         /* Send the filename */
   223         /* Send the filename */
   209         namesize = sizeof(outfname);
   224         namesize = strlen(outfname_full);
   210         write(fd_send_filename, (char *)&namesize, sizeof(namesize));
   225         write(fd_send_filename, (char *)&namesize, sizeof(namesize));
   211         write(fd_send_filename, outfname, sizeof(outfname));
   226         write(fd_send_filename, outfname_full, sizeof(outfname));
   212     }
   227     }
   213     /* Times */
   228     /* Times */
   214     gettimeofday(&starttv, NULL);
   229     gettimeofday(&starttv, NULL);
   215     write(fd_send_filename, &starttv, sizeof(starttv));
   230     write(fd_send_filename, &starttv, sizeof(starttv));
   216     close(fd_send_filename);
   231     close(fd_send_filename);