execute.c
author viric <viriketo@gmail.com>
Wed, 19 Oct 2016 22:09:02 +0200
changeset 351 69b9bbe171c6
parent 348 308315d04787
child 372 e9ac0653e4a2
permissions -rw-r--r--
Removed tag list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     1
/*
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     2
    Task Spooler - a task queue system for the unix user
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 246
diff changeset
     3
    Copyright (C) 2007-2009  Lluís Batlle i Rossell
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     4
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     5
    Please find the license in the provided COPYING file.
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     6
*/
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
     7
#include <unistd.h>
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
     8
#include <stdio.h>
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
     9
#include <signal.h>
44
4dcf05746ece Better include files.
viric@llimona
parents: 35
diff changeset
    10
#include <string.h>
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    11
#include <sys/types.h>
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    12
#include <sys/wait.h>
44
4dcf05746ece Better include files.
viric@llimona
parents: 35
diff changeset
    13
#include <stdlib.h>
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
    14
#include <signal.h>
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    15
#include <time.h>
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    16
#include <sys/times.h>
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    17
#include <sys/time.h>
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
    18
#include <sys/types.h>
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
    19
#include <fcntl.h>
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
    20
#include <assert.h>
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    21
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    22
#include "main.h"
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    23
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    24
/* from signals.c */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    25
extern int signals_child_pid; /* 0, not set. otherwise, set. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    26
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    27
/* Returns errorlevel */
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    28
static void run_parent(int fd_read_filename, int pid, struct Result *result)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    29
{
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    30
    int status;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    31
    char *ofname = 0;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    32
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    33
    int res;
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    34
    char *command;
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    35
    struct timeval starttv;
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    36
    struct timeval endtv;
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    37
    struct tms cpu_times;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    38
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    39
    /* Read the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    40
    /* This is linked with the write() in this same file, in run_child() */
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 28
diff changeset
    41
    if (command_line.store_output) {
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    42
        res = read(fd_read_filename, &namesize, sizeof(namesize));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    43
        if (res == -1)
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    44
            error("read the filename from %i", fd_read_filename);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    45
        if (res != sizeof(namesize))
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    46
            error("Reading the size of the name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    47
        ofname = (char *) malloc(namesize);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    48
        res = read(fd_read_filename, ofname, namesize);
107
c0a1c1e98b6a Fixed a bug in execute, calling error(). Wrong release again.
viric@llimona
parents: 99
diff changeset
    49
        if (res != namesize)
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
    50
            error("Reading the out file name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    51
    }
116
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    52
    res = read(fd_read_filename, &starttv, sizeof(starttv));
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    53
    if (res != sizeof(starttv))
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    54
        error("Reading the the struct timeval");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    55
    close(fd_read_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    56
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    57
    /* All went fine - prepare the SIGINT and send runjob_ok */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    58
    signals_child_pid = pid;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    59
    unblock_sigint_and_install_handler();
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    60
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 30
diff changeset
    61
    c_send_runjob_ok(ofname, pid);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    62
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    63
    wait(&status);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    64
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    65
    /* Set the errorlevel */
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    66
    if (WIFEXITED(status))
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    67
    {
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    68
        /* We force the proper cast */
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    69
        signed char tmp;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    70
        tmp = WEXITSTATUS(status);
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    71
        result->errorlevel = tmp;
246
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    72
        result->died_by_signal = 0;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    73
    }
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    74
    else if (WIFSIGNALED(status))
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    75
    {
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    76
        signed char tmp;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    77
        tmp = WTERMSIG(status);
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    78
        result->signal = tmp;
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    79
        result->errorlevel = -1;
246
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    80
        result->died_by_signal = 1;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    81
    }
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    82
    else
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    83
    {
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    84
        result->died_by_signal = 0;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    85
        result->errorlevel = -1;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    86
    }
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    87
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    88
    command = build_command_string();
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    89
    if (command_line.send_output_by_mail)
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    90
    {
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    91
        send_mail(command_line.jobid, result->errorlevel, ofname, command);
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    92
    }
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    93
    hook_on_finish(command_line.jobid, result->errorlevel, ofname, command);
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    94
    free(command);
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    95
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    96
    free(ofname);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    97
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    98
    /* Calculate times */
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    99
    gettimeofday(&endtv, NULL);
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
   100
    result->real_ms = endtv.tv_sec - starttv.tv_sec +
114
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   101
        ((float) (endtv.tv_usec - starttv.tv_usec) / 1000000.);
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   102
    times(&cpu_times);
114
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   103
    /* The times are given in clock ticks. The number of clock ticks per second
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   104
     * is obtained in POSIX using sysconf(). */
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   105
    result->user_ms = (float) cpu_times.tms_cutime /
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   106
        (float) sysconf(_SC_CLK_TCK);
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   107
    result->system_ms = (float) cpu_times.tms_cstime /
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   108
        (float) sysconf(_SC_CLK_TCK);
44
4dcf05746ece Better include files.
viric@llimona
parents: 35
diff changeset
   109
}
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   110
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   111
void create_closed_read_on(int dest)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   112
{
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   113
    int p[2];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   114
    /* Closing input */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   115
    pipe(p);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   116
    close(p[1]); /* closing the write handle */
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   117
    dup2(p[0], dest); /* the pipe reading goes to dest */
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   118
    if(p[0] != dest)
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   119
        close(p[0]);
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   120
}
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   121
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   122
/* This will close fd_out and fd_in in the parent */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   123
static void run_gzip(int fd_out, int fd_in)
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   124
{
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   125
    int pid;
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   126
    pid = fork();
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   127
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   128
    switch(pid)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   129
    {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   130
        case 0: /* child */
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   131
            restore_sigmask();
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   132
            dup2(fd_in,0); /* stdout */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   133
            dup2(fd_out,1); /* stdout */
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   134
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   135
            close(fd_out);
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   136
            /* Without stderr */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   137
            close(2);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   138
            execlp("gzip", "gzip", NULL);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   139
            exit(-1);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   140
            /* Won't return */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   141
       case -1:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   142
            exit(-1); /* Fork error */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   143
       default:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   144
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   145
            close(fd_out);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   146
    }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   147
}
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   148
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   149
static void run_child(int fd_send_filename)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   150
{
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   151
    char outfname[] = "/ts-out.XXXXXX";
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   152
    char errfname[sizeof outfname + 2]; /* .e */
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   153
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   154
    int outfd;
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   155
    int err;
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   156
    struct timeval starttv;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   157
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 28
diff changeset
   158
    if (command_line.store_output)
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   159
    {
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   160
        /* Prepare path */
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   161
        const char *tmpdir = getenv("TMPDIR");
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   162
        int lname;
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   163
        char *outfname_full;
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   164
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   165
        if (tmpdir == NULL)
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   166
            tmpdir = "/tmp";
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   167
        lname = strlen(tmpdir) + strlen(outfname) + 1 /* \0 */;
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   168
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   169
        outfname_full = (char *)malloc(lname);
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   170
        strcpy(outfname_full, tmpdir);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   171
        strcat(outfname_full, outfname);
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   172
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   173
        if (command_line.gzip)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   174
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   175
            int p[2];
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   176
            /* We assume that all handles are closed*/
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   177
            err = pipe(p);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   178
            assert(err == 0);
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   179
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   180
            /* gzip output goes to the filename */
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   181
            /* This will be the handle other than 0,1,2 */
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   182
            /* mkstemp doesn't admit adding ".gz" to the pattern */
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   183
            outfd = mkstemp(outfname_full); /* stdout */
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   184
            assert(outfd != -1);
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   185
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   186
            /* Program stdout and stderr */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   187
            /* which go to pipe write handle */
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   188
            err = dup2(p[1], 1);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   189
            assert(err != -1);
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   190
            if (command_line.stderr_apart)
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   191
            {
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   192
                int errfd;
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   193
                strncpy(errfname, outfname_full, sizeof errfname);
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   194
                strncat(errfname, ".e", 2);
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   195
                errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   196
                assert(err == 0);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   197
                err = dup2(errfd, 2);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   198
                assert(err == 0);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   199
                err = close(errfd);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   200
                assert(err == 0);
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   201
            }
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   202
            else
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   203
            {
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   204
                err = dup2(p[1], 2);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   205
                assert(err != -1);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   206
            }
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   207
            err = close(p[1]);
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   208
            assert(err == 0);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   209
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   210
            /* run gzip.
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   211
             * This wants p[0] in 0, so gzip will read
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   212
             * from it */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   213
            run_gzip(outfd, p[0]);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   214
        }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   215
        else
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   216
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   217
            /* Prepare the filename */
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   218
            outfd = mkstemp(outfname_full); /* stdout */
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   219
            dup2(outfd, 1); /* stdout */
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   220
            if (command_line.stderr_apart)
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   221
            {
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   222
                int errfd;
347
79b87e385bc5 Make ts respect TMPDIR for output files. It was using hardcoded /tmp.
viric <viriketo@gmail.com>
parents: 329
diff changeset
   223
                strncpy(errfname, outfname_full, sizeof errfname);
305
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   224
                strncat(errfname, ".e", 2);
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   225
                errfd = open(errfname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   226
                dup2(errfd, 2);
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   227
                close(errfd);
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   228
            }
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   229
            else
365f3598d010 Adding the functionality of keeping stderr apart. For Jan Šmydke.
viric <viriketo@gmail.com>
parents: 295
diff changeset
   230
                dup2(outfd, 2);
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   231
            close(outfd);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   232
        }
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   233
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   234
        /* Send the filename */
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   235
        namesize = strlen(outfname_full)+1;
329
b01bb36b4290 Fixing some code based on gcc warnings.
viric <viriketo@gmail.com>
parents: 305
diff changeset
   236
        write(fd_send_filename, (char *)&namesize, sizeof(namesize));
348
308315d04787 Fixing output file name passing and sending.
viric <viriketo@gmail.com>
parents: 347
diff changeset
   237
        write(fd_send_filename, outfname_full, namesize);
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   238
    }
116
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   239
    /* Times */
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   240
    gettimeofday(&starttv, NULL);
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   241
    write(fd_send_filename, &starttv, sizeof(starttv));
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   242
    close(fd_send_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   243
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   244
    /* Closing input */
70
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   245
    if (command_line.should_go_background)
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   246
        create_closed_read_on(0);
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   247
168
02b699d307fd Comment on setsid for the job execution.
lbatlle@npdl268.bpo.hp.com
parents: 167
diff changeset
   248
    /* We create a new session, so we can kill process groups as:
02b699d307fd Comment on setsid for the job execution.
lbatlle@npdl268.bpo.hp.com
parents: 167
diff changeset
   249
         kill -- -`ts -p` */
167
8b757ba536d2 Session created on exec child.
lbatlle@npdl268.bpo.hp.com
parents: 138
diff changeset
   250
    setsid();
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   251
    execvp(command_line.command.array[0], command_line.command.array);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   252
}
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   253
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   254
int run_job(struct Result *res)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   255
{
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   256
    int pid;
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   257
    int errorlevel;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   258
    int p[2];
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   259
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   260
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   261
    /* For the parent */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   262
    /*program_signal(); Still not needed*/
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   263
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
   264
    block_sigint();
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
   265
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   266
    /* Prepare the output filename sending */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   267
    pipe(p);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   268
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   269
    pid = fork();
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   270
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   271
    switch(pid)
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   272
    {
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   273
        case 0:
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   274
            restore_sigmask();
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   275
            close(server_socket);
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   276
            close(p[0]);
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   277
            run_child(p[1]);
69
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   278
            /* Not reachable, if the 'exec' of the command
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   279
             * works. Thus, command exists, etc. */
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   280
            fprintf(stderr, "ts could not run the command\n");
69
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   281
            exit(-1);
295
a63f43a17fd7 Adding the option '-B' for Mark, that will make ts quit if it cannot enqueue a
viric <viriketo@gmail.com>
parents: 267
diff changeset
   282
            /* To avoid a compiler warning */
a63f43a17fd7 Adding the option '-B' for Mark, that will make ts quit if it cannot enqueue a
viric <viriketo@gmail.com>
parents: 267
diff changeset
   283
            errorlevel = 0;
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   284
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   285
        case -1:
295
a63f43a17fd7 Adding the option '-B' for Mark, that will make ts quit if it cannot enqueue a
viric <viriketo@gmail.com>
parents: 267
diff changeset
   286
            /* To avoid a compiler warning */
a63f43a17fd7 Adding the option '-B' for Mark, that will make ts quit if it cannot enqueue a
viric <viriketo@gmail.com>
parents: 267
diff changeset
   287
            errorlevel = 0;
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   288
            error("forking");
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   289
        default:
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   290
            close(p[1]);
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   291
            run_parent(p[0], pid, res);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   292
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   293
    }
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   294
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   295
    return errorlevel;
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   296
}
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   297
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   298
#if 0
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   299
Not needed
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   300
static void sigchld_handler(int val)
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   301
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   302
}
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   303
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   304
static void program_signal()
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   305
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   306
  struct sigaction act;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   307
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   308
  act.sa_handler = sigchld_handler;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   309
  /* Reset the mask */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   310
  memset(&act.sa_mask,0,sizeof(act.sa_mask));
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   311
  act.sa_flags = SA_NOCLDSTOP;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   312
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   313
  sigaction(SIGCHLD, &act, NULL);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   314
}
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   315
#endif