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