execute.c
author viric <viriketo@gmail.com>
Tue, 11 Oct 2011 19:50:10 +0200
changeset 303 2edd42e77392
parent 295 a63f43a17fd7
child 305 365f3598d010
permissions -rw-r--r--
Updating the version to 0.7.1, for a release.
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>
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    18
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    19
#include "main.h"
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    20
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    21
/* from signals.c */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    22
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
    23
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    24
/* Returns errorlevel */
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    25
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
    26
{
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    27
    int status;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    28
    char *ofname = 0;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    29
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    30
    int res;
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    31
    char *command;
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    32
    struct timeval starttv;
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    33
    struct timeval endtv;
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    34
    struct tms cpu_times;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    35
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    36
    /* Read the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    37
    /* 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
    38
    if (command_line.store_output) {
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    39
        res = read(fd_read_filename, &namesize, sizeof(namesize));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    40
        if (res == -1)
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    41
            error("read the filename from %i", fd_read_filename);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    42
        if (res != sizeof(namesize))
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    43
            error("Reading the size of the name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    44
        ofname = (char *) malloc(namesize);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    45
        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
    46
        if (res != namesize)
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    47
            error("Reading the the out file name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    48
    }
116
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    49
    res = read(fd_read_filename, &starttv, sizeof(starttv));
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    50
    if (res != sizeof(starttv))
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
    51
        error("Reading the the struct timeval");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    52
    close(fd_read_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    53
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    54
    /* 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
    55
    signals_child_pid = pid;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    56
    unblock_sigint_and_install_handler();
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
    57
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 30
diff changeset
    58
    c_send_runjob_ok(ofname, pid);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    59
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    60
    wait(&status);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    61
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    62
    /* Set the errorlevel */
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    63
    if (WIFEXITED(status))
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    64
    {
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    65
        /* We force the proper cast */
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    66
        signed char tmp;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    67
        tmp = WEXITSTATUS(status);
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    68
        result->errorlevel = tmp;
246
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    69
        result->died_by_signal = 0;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    70
    }
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    71
    else if (WIFSIGNALED(status))
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    72
    {
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    73
        signed char tmp;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    74
        tmp = WTERMSIG(status);
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    75
        result->signal = tmp;
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    76
        result->errorlevel = -1;
246
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    77
        result->died_by_signal = 1;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    78
    }
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    79
    else
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    80
    {
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    81
        result->died_by_signal = 0;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    82
        result->errorlevel = -1;
239b28c0cca9 Adding information on the exit status (signal/exit code).
viric@mandarina
parents: 231
diff changeset
    83
    }
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    84
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    85
    command = build_command_string();
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    86
    if (command_line.send_output_by_mail)
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    87
    {
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    88
        send_mail(command_line.jobid, result->errorlevel, ofname, command);
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    89
    }
115
367ada1cb69b Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents: 114
diff changeset
    90
    hook_on_finish(command_line.jobid, result->errorlevel, ofname, command);
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    91
    free(command);
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    92
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    93
    free(ofname);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    94
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    95
    /* Calculate times */
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    96
    gettimeofday(&endtv, NULL);
112
303834d55e99 More code on times()
viric@mandarina
parents: 110
diff changeset
    97
    result->real_ms = endtv.tv_sec - starttv.tv_sec +
114
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
    98
        ((float) (endtv.tv_usec - starttv.tv_usec) / 1000000.);
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
    99
    times(&cpu_times);
114
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   100
    /* The times are given in clock ticks. The number of clock ticks per second
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   101
     * is obtained in POSIX using sysconf(). */
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   102
    result->user_ms = (float) cpu_times.tms_cutime /
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   103
        (float) sysconf(_SC_CLK_TCK);
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   104
    result->system_ms = (float) cpu_times.tms_cstime /
bd123730295d times() reporting finished.
viric@mandarina
parents: 112
diff changeset
   105
        (float) sysconf(_SC_CLK_TCK);
44
4dcf05746ece Better include files.
viric@llimona
parents: 35
diff changeset
   106
}
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   107
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   108
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
   109
{
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   110
    int p[2];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   111
    /* Closing input */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   112
    pipe(p);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   113
    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
   114
    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
   115
    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
   116
        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
   117
}
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   118
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   119
/* 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
   120
static void run_gzip(int fd_out, int fd_in)
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   121
{
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   122
    int pid;
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   123
    pid = fork();
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   124
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   125
    switch(pid)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   126
    {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   127
        case 0: /* child */
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   128
            restore_sigmask();
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   129
            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
   130
            dup2(fd_out,1); /* stdout */
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   131
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   132
            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
   133
            /* Without stderr */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   134
            close(2);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   135
            execlp("gzip", "gzip", NULL);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   136
            exit(-1);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   137
            /* Won't return */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   138
       case -1:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   139
            exit(-1); /* Fork error */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   140
       default:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   141
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   142
            close(fd_out);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   143
    }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   144
}
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   145
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   146
static void run_child(int fd_send_filename)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   147
{
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 22
diff changeset
   148
    char outfname[] = "/tmp/ts-out.XXXXXX";
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   149
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   150
    int outfd;
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   151
    struct timeval starttv;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   152
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 28
diff changeset
   153
    if (command_line.store_output)
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   154
    {
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   155
        int res;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   156
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   157
        if (command_line.gzip)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   158
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   159
            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
   160
            /* We assume that all handles are closed*/
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   161
            pipe(p);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   162
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   163
            /* 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
   164
            /* which go to pipe write handle */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   165
            dup2(p[1], 1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   166
            dup2(p[1], 2);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   167
            close(p[1]);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   168
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   169
            /* gzip output goes to the filename */
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   170
            /* This will be the handle other than 0,1,2 */
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   171
            outfd = mkstemp(outfname); /* stdout */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   172
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   173
            /* run gzip.
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   174
             * 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
   175
             * from it */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   176
            run_gzip(outfd, p[0]);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   177
        }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   178
        else
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   179
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   180
            /* Prepare the filename */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   181
            outfd = mkstemp(outfname); /* stdout */
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   182
            dup2(outfd, 1); /* stdout */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   183
            dup2(outfd, 2); /* stderr */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   184
            close(outfd);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   185
        }
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   186
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   187
        /* Send the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   188
        namesize = sizeof(outfname);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   189
        res = write(fd_send_filename, (char *)&namesize, sizeof(namesize));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   190
        write(fd_send_filename, outfname, sizeof(outfname));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   191
    }
116
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   192
    /* Times */
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   193
    gettimeofday(&starttv, NULL);
5fe47d692522 Fixing an error measuring times
viric@llimona
parents: 115
diff changeset
   194
    write(fd_send_filename, &starttv, sizeof(starttv));
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   195
    close(fd_send_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   196
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   197
    /* Closing input */
70
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   198
    if (command_line.should_go_background)
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   199
        create_closed_read_on(0);
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   200
168
02b699d307fd Comment on setsid for the job execution.
lbatlle@npdl268.bpo.hp.com
parents: 167
diff changeset
   201
    /* 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
   202
         kill -- -`ts -p` */
167
8b757ba536d2 Session created on exec child.
lbatlle@npdl268.bpo.hp.com
parents: 138
diff changeset
   203
    setsid();
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   204
    execvp(command_line.command.array[0], command_line.command.array);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   205
}
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   206
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   207
int run_job(struct Result *res)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   208
{
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   209
    int pid;
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   210
    int errorlevel;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   211
    int p[2];
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   212
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   213
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   214
    /* For the parent */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   215
    /*program_signal(); Still not needed*/
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   216
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
   217
    block_sigint();
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 117
diff changeset
   218
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   219
    /* Prepare the output filename sending */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   220
    pipe(p);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   221
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   222
    pid = fork();
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   223
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   224
    switch(pid)
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   225
    {
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   226
        case 0:
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   227
            restore_sigmask();
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   228
            close(server_socket);
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   229
            close(p[0]);
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   230
            run_child(p[1]);
69
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   231
            /* Not reachable, if the 'exec' of the command
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   232
             * works. Thus, command exists, etc. */
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   233
            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
   234
            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
   235
            /* 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
   236
            errorlevel = 0;
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   237
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   238
        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
   239
            /* 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
   240
            errorlevel = 0;
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   241
            error("forking");
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   242
        default:
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   243
            close(p[1]);
110
2726c92a5cb3 Half-code for 'times'
viric@mandarina
parents: 107
diff changeset
   244
            run_parent(p[0], pid, res);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   245
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   246
    }
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   247
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   248
    return errorlevel;
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   249
}
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   250
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   251
#if 0
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   252
Not needed
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   253
static void sigchld_handler(int val)
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   254
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   255
}
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   256
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   257
static void program_signal()
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   258
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   259
  struct sigaction act;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   260
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   261
  act.sa_handler = sigchld_handler;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   262
  /* Reset the mask */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   263
  memset(&act.sa_mask,0,sizeof(act.sa_mask));
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   264
  act.sa_flags = SA_NOCLDSTOP;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   265
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   266
  sigaction(SIGCHLD, &act, NULL);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   267
}
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   268
#endif