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