execute.c
changeset 348 308315d04787
parent 347 79b87e385bc5
child 372 e9ac0653e4a2
equal deleted inserted replaced
347:79b87e385bc5 348:308315d04787
    15 #include <time.h>
    15 #include <time.h>
    16 #include <sys/times.h>
    16 #include <sys/times.h>
    17 #include <sys/time.h>
    17 #include <sys/time.h>
    18 #include <sys/types.h>
    18 #include <sys/types.h>
    19 #include <fcntl.h>
    19 #include <fcntl.h>
       
    20 #include <assert.h>
    20 
    21 
    21 #include "main.h"
    22 #include "main.h"
    22 
    23 
    23 /* from signals.c */
    24 /* from signals.c */
    24 extern int signals_child_pid; /* 0, not set. otherwise, set. */
    25 extern int signals_child_pid; /* 0, not set. otherwise, set. */
    44         if (res != sizeof(namesize))
    45         if (res != sizeof(namesize))
    45             error("Reading the size of the name");
    46             error("Reading the size of the name");
    46         ofname = (char *) malloc(namesize);
    47         ofname = (char *) malloc(namesize);
    47         res = read(fd_read_filename, ofname, namesize);
    48         res = read(fd_read_filename, ofname, namesize);
    48         if (res != namesize)
    49         if (res != namesize)
    49             error("Reading the the out file name");
    50             error("Reading the out file name");
    50     }
    51     }
    51     res = read(fd_read_filename, &starttv, sizeof(starttv));
    52     res = read(fd_read_filename, &starttv, sizeof(starttv));
    52     if (res != sizeof(starttv))
    53     if (res != sizeof(starttv))
    53         error("Reading the the struct timeval");
    54         error("Reading the the struct timeval");
    54     close(fd_read_filename);
    55     close(fd_read_filename);
   149 {
   150 {
   150     char outfname[] = "/ts-out.XXXXXX";
   151     char outfname[] = "/ts-out.XXXXXX";
   151     char errfname[sizeof outfname + 2]; /* .e */
   152     char errfname[sizeof outfname + 2]; /* .e */
   152     int namesize;
   153     int namesize;
   153     int outfd;
   154     int outfd;
       
   155     int err;
   154     struct timeval starttv;
   156     struct timeval starttv;
   155 
   157 
   156     if (command_line.store_output)
   158     if (command_line.store_output)
   157     {
   159     {
   158         /* Prepare path */
   160         /* Prepare path */
   160         int lname;
   162         int lname;
   161         char *outfname_full;
   163         char *outfname_full;
   162 
   164 
   163         if (tmpdir == NULL)
   165         if (tmpdir == NULL)
   164             tmpdir = "/tmp";
   166             tmpdir = "/tmp";
   165         lname = strlen(tmpdir) + strlen(outfname) + 3 /* .gz*/ + 1 /* \0 */;
   167         lname = strlen(tmpdir) + strlen(outfname) + 1 /* \0 */;
   166 
   168 
   167         outfname_full = (char *)malloc(lname);
   169         outfname_full = (char *)malloc(lname);
   168         strncpy(outfname_full, tmpdir, lname);
   170         strcpy(outfname_full, tmpdir);
   169         strncat(outfname_full, outfname, lname);
   171         strcat(outfname_full, outfname);
   170 
   172 
   171         if (command_line.gzip)
   173         if (command_line.gzip)
   172         {
   174         {
   173             int p[2];
   175             int p[2];
   174             /* We assume that all handles are closed*/
   176             /* We assume that all handles are closed*/
   175             pipe(p);
   177             err = pipe(p);
   176 
   178             assert(err == 0);
   177             strncat(outfname_full, ".gz", lname);
       
   178 
   179 
   179             /* gzip output goes to the filename */
   180             /* gzip output goes to the filename */
   180             /* This will be the handle other than 0,1,2 */
   181             /* This will be the handle other than 0,1,2 */
       
   182             /* mkstemp doesn't admit adding ".gz" to the pattern */
   181             outfd = mkstemp(outfname_full); /* stdout */
   183             outfd = mkstemp(outfname_full); /* stdout */
       
   184             assert(outfd != -1);
   182 
   185 
   183             /* Program stdout and stderr */
   186             /* Program stdout and stderr */
   184             /* which go to pipe write handle */
   187             /* which go to pipe write handle */
   185             dup2(p[1], 1);
   188             err = dup2(p[1], 1);
       
   189             assert(err != -1);
   186             if (command_line.stderr_apart)
   190             if (command_line.stderr_apart)
   187             {
   191             {
   188                 int errfd;
   192                 int errfd;
   189                 strncpy(errfname, outfname_full, sizeof errfname);
   193                 strncpy(errfname, outfname_full, sizeof errfname);
   190                 strncat(errfname, ".e", 2);
   194                 strncat(errfname, ".e", 2);
   191                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   195                 errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   192                 dup2(errfd, 2);
   196                 assert(err == 0);
   193                 close(errfd);
   197                 err = dup2(errfd, 2);
       
   198                 assert(err == 0);
       
   199                 err = close(errfd);
       
   200                 assert(err == 0);
   194             }
   201             }
   195             else
   202             else
   196                 dup2(p[1], 2);
   203             {
   197             close(p[1]);
   204                 err = dup2(p[1], 2);
       
   205                 assert(err != -1);
       
   206             }
       
   207             err = close(p[1]);
       
   208             assert(err == 0);
   198 
   209 
   199             /* run gzip.
   210             /* run gzip.
   200              * This wants p[0] in 0, so gzip will read
   211              * This wants p[0] in 0, so gzip will read
   201              * from it */
   212              * from it */
   202             run_gzip(outfd, p[0]);
   213             run_gzip(outfd, p[0]);
   219                 dup2(outfd, 2);
   230                 dup2(outfd, 2);
   220             close(outfd);
   231             close(outfd);
   221         }
   232         }
   222 
   233 
   223         /* Send the filename */
   234         /* Send the filename */
   224         namesize = strlen(outfname_full);
   235         namesize = strlen(outfname_full)+1;
   225         write(fd_send_filename, (char *)&namesize, sizeof(namesize));
   236         write(fd_send_filename, (char *)&namesize, sizeof(namesize));
   226         write(fd_send_filename, outfname_full, sizeof(outfname));
   237         write(fd_send_filename, outfname_full, namesize);
   227     }
   238     }
   228     /* Times */
   239     /* Times */
   229     gettimeofday(&starttv, NULL);
   240     gettimeofday(&starttv, NULL);
   230     write(fd_send_filename, &starttv, sizeof(starttv));
   241     write(fd_send_filename, &starttv, sizeof(starttv));
   231     close(fd_send_filename);
   242     close(fd_send_filename);