author | viric@llimona |
Fri, 13 Apr 2007 20:52:28 +0200 | |
changeset 127 | c620eacd7630 |
parent 117 | db479ef293d3 |
child 138 | 00461b7bdf4b |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
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 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
21 |
/* Returns errorlevel */ |
112 | 22 |
static void run_parent(int fd_read_filename, int pid, struct Result *result) |
8 | 23 |
{ |
24 |
int status; |
|
22 | 25 |
char *ofname = 0; |
26 |
int namesize; |
|
27 |
int res; |
|
73 | 28 |
char *command; |
110 | 29 |
struct timeval starttv; |
30 |
struct timeval endtv; |
|
31 |
struct tms cpu_times; |
|
22 | 32 |
|
33 |
/* Read the filename */ |
|
34 |
/* 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
|
35 |
if (command_line.store_output) { |
22 | 36 |
res = read(fd_read_filename, &namesize, sizeof(namesize)); |
37 |
if (res == -1) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
38 |
error("read the filename from %i", fd_read_filename); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
39 |
if (res != sizeof(namesize)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
40 |
error("Reading the size of the name"); |
22 | 41 |
ofname = (char *) malloc(namesize); |
42 |
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
|
43 |
if (res != namesize) |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
44 |
error("Reading the the out file name"); |
22 | 45 |
} |
116 | 46 |
res = read(fd_read_filename, &starttv, sizeof(starttv)); |
47 |
if (res != sizeof(starttv)) |
|
48 |
error("Reading the the struct timeval"); |
|
22 | 49 |
close(fd_read_filename); |
50 |
||
35 | 51 |
c_send_runjob_ok(ofname, pid); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
52 |
|
8 | 53 |
wait(&status); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
54 |
|
115
367ada1cb69b
Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents:
114
diff
changeset
|
55 |
/* Set the errorlevel */ |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
56 |
if (WIFEXITED(status)) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
57 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
58 |
/* We force the proper cast */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
59 |
signed char tmp; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
60 |
tmp = WEXITSTATUS(status); |
115
367ada1cb69b
Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents:
114
diff
changeset
|
61 |
result->errorlevel = tmp; |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
62 |
} else |
112 | 63 |
result->errorlevel = -1; |
71 | 64 |
|
73 | 65 |
command = build_command_string(); |
71 | 66 |
if (command_line.send_output_by_mail) |
67 |
{ |
|
115
367ada1cb69b
Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents:
114
diff
changeset
|
68 |
send_mail(command_line.jobid, result->errorlevel, ofname, command); |
71 | 69 |
} |
115
367ada1cb69b
Fixed the errorlevel/times on signal received on the child.
viric@mandarina
parents:
114
diff
changeset
|
70 |
hook_on_finish(command_line.jobid, result->errorlevel, ofname, command); |
73 | 71 |
free(command); |
71 | 72 |
|
73 |
free(ofname); |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
74 |
|
110 | 75 |
/* Calculate times */ |
76 |
gettimeofday(&endtv, NULL); |
|
112 | 77 |
result->real_ms = endtv.tv_sec - starttv.tv_sec + |
114 | 78 |
((float) (endtv.tv_usec - starttv.tv_usec) / 1000000.); |
110 | 79 |
times(&cpu_times); |
114 | 80 |
/* The times are given in clock ticks. The number of clock ticks per second |
81 |
* is obtained in POSIX using sysconf(). */ |
|
82 |
result->user_ms = (float) cpu_times.tms_cutime / |
|
83 |
(float) sysconf(_SC_CLK_TCK); |
|
84 |
result->system_ms = (float) cpu_times.tms_cstime / |
|
85 |
(float) sysconf(_SC_CLK_TCK); |
|
44 | 86 |
} |
8 | 87 |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
88 |
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
|
89 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
90 |
int p[2]; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
91 |
/* Closing input */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
92 |
pipe(p); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
98 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
99 |
/* 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
|
100 |
static void run_gzip(int fd_out, int fd_in) |
65 | 101 |
{ |
102 |
int pid; |
|
103 |
pid = fork(); |
|
104 |
||
105 |
switch(pid) |
|
106 |
{ |
|
107 |
case 0: /* child */ |
|
95 | 108 |
restore_sigmask(); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
109 |
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
|
110 |
dup2(fd_out,1); /* stdout */ |
65 | 111 |
close(fd_in); |
112 |
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
|
113 |
/* Without stderr */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
114 |
close(2); |
65 | 115 |
execlp("gzip", "gzip", NULL); |
116 |
exit(-1); |
|
117 |
/* Won't return */ |
|
118 |
case -1: |
|
119 |
exit(-1); /* Fork error */ |
|
120 |
default: |
|
121 |
close(fd_in); |
|
122 |
close(fd_out); |
|
123 |
} |
|
124 |
} |
|
125 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
126 |
static void run_child(int fd_send_filename) |
8 | 127 |
{ |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
22
diff
changeset
|
128 |
char outfname[] = "/tmp/ts-out.XXXXXX"; |
22 | 129 |
int namesize; |
130 |
int outfd; |
|
110 | 131 |
struct timeval starttv; |
22 | 132 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
28
diff
changeset
|
133 |
if (command_line.store_output) |
22 | 134 |
{ |
135 |
int res; |
|
136 |
||
65 | 137 |
if (command_line.gzip) |
138 |
{ |
|
139 |
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
|
140 |
/* We assume that all handles are closed*/ |
65 | 141 |
pipe(p); |
142 |
||
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
143 |
/* 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
|
144 |
/* 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
|
145 |
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
|
146 |
dup2(p[1], 2); |
65 | 147 |
close(p[1]); |
148 |
||
149 |
/* 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
|
150 |
/* This will be the handle other than 0,1,2 */ |
65 | 151 |
outfd = mkstemp(outfname); /* stdout */ |
152 |
||
153 |
/* run gzip. |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
154 |
* 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
|
155 |
* from it */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
156 |
run_gzip(outfd, p[0]); |
65 | 157 |
} |
158 |
else |
|
159 |
{ |
|
160 |
/* Prepare the filename */ |
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
close(outfd); |
65 | 165 |
} |
22 | 166 |
|
167 |
/* Send the filename */ |
|
168 |
namesize = sizeof(outfname); |
|
169 |
res = write(fd_send_filename, (char *)&namesize, sizeof(namesize)); |
|
170 |
write(fd_send_filename, outfname, sizeof(outfname)); |
|
171 |
} |
|
116 | 172 |
/* Times */ |
173 |
gettimeofday(&starttv, NULL); |
|
174 |
write(fd_send_filename, &starttv, sizeof(starttv)); |
|
22 | 175 |
close(fd_send_filename); |
176 |
||
9 | 177 |
/* Closing input */ |
70
8c244f28224e
Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents:
69
diff
changeset
|
178 |
if (command_line.should_go_background) |
8c244f28224e
Fixed "-f". Now it doesn't close stdin in this case.
viric@llimona
parents:
69
diff
changeset
|
179 |
create_closed_read_on(0); |
9 | 180 |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
181 |
execvp(command_line.command.array[0], command_line.command.array); |
8 | 182 |
} |
183 |
||
110 | 184 |
int run_job(struct Result *res) |
8 | 185 |
{ |
186 |
int pid; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
187 |
int errorlevel; |
22 | 188 |
int p[2]; |
8 | 189 |
|
190 |
||
9 | 191 |
/* For the parent */ |
192 |
/*program_signal(); Still not needed*/ |
|
193 |
||
22 | 194 |
/* Prepare the output filename sending */ |
195 |
pipe(p); |
|
196 |
||
197 |
pid = fork(); |
|
198 |
||
8 | 199 |
switch(pid) |
200 |
{ |
|
201 |
case 0: |
|
95 | 202 |
restore_sigmask(); |
9 | 203 |
close(server_socket); |
22 | 204 |
close(p[0]); |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
205 |
run_child(p[1]); |
69 | 206 |
/* Not reachable, if the 'exec' of the command |
207 |
* works. Thus, command exists, etc. */ |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
208 |
fprintf(stderr, "ts could not run the command\n"); |
69 | 209 |
exit(-1); |
8 | 210 |
break; |
211 |
case -1: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
212 |
error("forking"); |
8 | 213 |
default: |
22 | 214 |
close(p[1]); |
110 | 215 |
run_parent(p[0], pid, res); |
8 | 216 |
break; |
217 |
} |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
218 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
219 |
return errorlevel; |
8 | 220 |
} |
9 | 221 |
|
95 | 222 |
#if 0 |
223 |
Not needed |
|
9 | 224 |
static void sigchld_handler(int val) |
225 |
{ |
|
226 |
} |
|
227 |
||
228 |
static void program_signal() |
|
229 |
{ |
|
230 |
struct sigaction act; |
|
231 |
||
232 |
act.sa_handler = sigchld_handler; |
|
233 |
/* Reset the mask */ |
|
234 |
memset(&act.sa_mask,0,sizeof(act.sa_mask)); |
|
235 |
act.sa_flags = SA_NOCLDSTOP; |
|
236 |
||
237 |
sigaction(SIGCHLD, &act, NULL); |
|
238 |
} |
|
95 | 239 |
#endif |