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