execute.c
author viric@llimona
Fri, 06 Apr 2007 01:20:40 +0200
changeset 107 c0a1c1e98b6a
parent 99 88d96be4e0e9
child 110 2726c92a5cb3
permissions -rw-r--r--
Fixed a bug in execute, calling error(). Wrong release again.
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
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
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>
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    15
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    16
#include "msg.h"
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    17
#include "main.h"
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    18
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    19
/* Returns errorlevel */
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 30
diff changeset
    20
static int run_parent(int fd_read_filename, int pid)
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
    int status;
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    23
    int errorlevel;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    24
    char *ofname = 0;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    25
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    26
    int res;
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    27
    char *command;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    28
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    29
    /* Read the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    30
    /* 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
    31
    if (command_line.store_output) {
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    32
        res = read(fd_read_filename, &namesize, sizeof(namesize));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    33
        if (res == -1)
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    34
            error("read the filename from %i", fd_read_filename);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    35
        if (res != sizeof(namesize))
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    36
            error("Reading the size of the name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    37
        ofname = (char *) malloc(namesize);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    38
        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
    39
        if (res != namesize)
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
    40
            error("Reading the the out file name");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    41
    }
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    42
    close(fd_read_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
    43
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 30
diff changeset
    44
    c_send_runjob_ok(ofname, pid);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    45
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    46
    wait(&status);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    47
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    48
    if (WIFEXITED(status))
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    49
    {
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    50
        /* We force the proper cast */
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    51
        signed char tmp;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    52
        tmp = WEXITSTATUS(status);
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    53
        errorlevel = tmp;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    54
    } else
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    55
    {
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    56
        free(ofname);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    57
        return -1;
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    58
    }
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    59
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    60
    command = build_command_string();
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    61
    if (command_line.send_output_by_mail)
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    62
    {
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    63
        send_mail(command_line.jobid, errorlevel, ofname, command);
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    64
    }
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    65
    hook_on_finish(command_line.jobid, errorlevel, ofname, command);
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
    66
    free(command);
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    67
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 70
diff changeset
    68
    free(ofname);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    69
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    70
    return errorlevel;
44
4dcf05746ece Better include files.
viric@llimona
parents: 35
diff changeset
    71
}
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
    72
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    73
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
    74
{
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    75
    int p[2];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    76
    /* Closing input */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    77
    pipe(p);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    78
    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
    79
    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
    80
    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
    81
        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
    82
}
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    83
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    84
/* 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
    85
static void run_gzip(int fd_out, int fd_in)
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    86
{
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    87
    int pid;
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    88
    pid = fork();
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    89
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    90
    switch(pid)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    91
    {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    92
        case 0: /* child */
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
    93
            restore_sigmask();
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    94
            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
    95
            dup2(fd_out,1); /* stdout */
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    96
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
    97
            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
    98
            /* Without stderr */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    99
            close(2);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   100
            execlp("gzip", "gzip", NULL);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   101
            exit(-1);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   102
            /* Won't return */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   103
       case -1:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   104
            exit(-1); /* Fork error */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   105
       default:
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   106
            close(fd_in);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   107
            close(fd_out);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   108
    }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   109
}
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   110
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   111
static void run_child(int fd_send_filename)
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   112
{
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 22
diff changeset
   113
    char outfname[] = "/tmp/ts-out.XXXXXX";
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   114
    int namesize;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   115
    int outfd;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   116
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 28
diff changeset
   117
    if (command_line.store_output)
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   118
    {
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   119
        int res;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   120
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   121
        if (command_line.gzip)
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   122
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   123
            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
   124
            /* We assume that all handles are closed*/
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   125
            pipe(p);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   126
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   127
            /* 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
   128
            /* 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
   129
            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
   130
            dup2(p[1], 2);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   131
            close(p[1]);
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   132
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   133
            /* 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
   134
            /* This will be the handle other than 0,1,2 */
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   135
            outfd = mkstemp(outfname); /* stdout */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   136
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   137
            /* run gzip.
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   138
             * 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
   139
             * from it */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   140
            run_gzip(outfd, p[0]);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   141
        }
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   142
        else
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   143
        {
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   144
            /* Prepare the filename */
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   145
            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
   146
            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
   147
            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
   148
            close(outfd);
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 49
diff changeset
   149
        }
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   150
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   151
        /* Send the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   152
        namesize = sizeof(outfname);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   153
        res = write(fd_send_filename, (char *)&namesize, sizeof(namesize));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   154
        write(fd_send_filename, outfname, sizeof(outfname));
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   155
    }
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   156
    close(fd_send_filename);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   157
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   158
    /* Closing input */
70
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   159
    if (command_line.should_go_background)
8c244f28224e Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents: 69
diff changeset
   160
        create_closed_read_on(0);
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   161
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   162
    execvp(command_line.command.array[0], command_line.command.array);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   163
}
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   164
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   165
int run_job()
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   166
{
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   167
    int pid;
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   168
    int errorlevel;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   169
    int p[2];
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   170
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   171
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   172
    /* For the parent */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   173
    /*program_signal(); Still not needed*/
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   174
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   175
    /* Prepare the output filename sending */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   176
    pipe(p);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   177
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   178
    pid = fork();
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   179
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   180
    switch(pid)
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   181
    {
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   182
        case 0:
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   183
            restore_sigmask();
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   184
            close(server_socket);
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   185
            close(p[0]);
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   186
            run_child(p[1]);
69
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   187
            /* Not reachable, if the 'exec' of the command
dc0c393785eb Merged -U with the "not using bash anymore".
viric@llimona
parents: 68 66
diff changeset
   188
             * works. Thus, command exists, etc. */
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   189
            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
   190
            exit(-1);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   191
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   192
        case -1:
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 77
diff changeset
   193
            error("forking");
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   194
        default:
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 19
diff changeset
   195
            close(p[1]);
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 30
diff changeset
   196
            errorlevel = run_parent(p[0], pid);
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   197
            break;
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   198
    }
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   199
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   200
    return errorlevel;
8
03339adb7014 Some more code for execution.
viric@llimona
parents:
diff changeset
   201
}
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   202
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   203
#if 0
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   204
Not needed
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   205
static void sigchld_handler(int val)
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   206
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   207
}
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   208
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   209
static void program_signal()
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   210
{
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   211
  struct sigaction act;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   212
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   213
  act.sa_handler = sigchld_handler;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   214
  /* Reset the mask */
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   215
  memset(&act.sa_mask,0,sizeof(act.sa_mask));
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   216
  act.sa_flags = SA_NOCLDSTOP;
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   217
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   218
  sigaction(SIGCHLD, &act, NULL);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   219
}
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents: 92
diff changeset
   220
#endif