author | viric@llimona |
Fri, 06 Apr 2007 01:20:40 +0200 | |
changeset 107 | c0a1c1e98b6a |
parent 99 | 88d96be4e0e9 |
child 110 | 2726c92a5cb3 |
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> |
8 | 15 |
|
9 | 16 |
#include "msg.h" |
8 | 17 |
#include "main.h" |
18 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
19 |
/* Returns errorlevel */ |
35 | 20 |
static int run_parent(int fd_read_filename, int pid) |
8 | 21 |
{ |
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 | 24 |
char *ofname = 0; |
25 |
int namesize; |
|
26 |
int res; |
|
73 | 27 |
char *command; |
22 | 28 |
|
29 |
/* Read the filename */ |
|
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 | 32 |
res = read(fd_read_filename, &namesize, sizeof(namesize)); |
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 | 37 |
ofname = (char *) malloc(namesize); |
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 | 41 |
} |
42 |
close(fd_read_filename); |
|
43 |
||
35 | 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 | 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 | 55 |
{ |
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 | 58 |
} |
59 |
||
73 | 60 |
command = build_command_string(); |
71 | 61 |
if (command_line.send_output_by_mail) |
62 |
{ |
|
63 |
send_mail(command_line.jobid, errorlevel, ofname, command); |
|
64 |
} |
|
73 | 65 |
hook_on_finish(command_line.jobid, errorlevel, ofname, command); |
66 |
free(command); |
|
71 | 67 |
|
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 | 71 |
} |
8 | 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 | 86 |
{ |
87 |
int pid; |
|
88 |
pid = fork(); |
|
89 |
||
90 |
switch(pid) |
|
91 |
{ |
|
92 |
case 0: /* child */ |
|
95 | 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 | 96 |
close(fd_in); |
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 | 100 |
execlp("gzip", "gzip", NULL); |
101 |
exit(-1); |
|
102 |
/* Won't return */ |
|
103 |
case -1: |
|
104 |
exit(-1); /* Fork error */ |
|
105 |
default: |
|
106 |
close(fd_in); |
|
107 |
close(fd_out); |
|
108 |
} |
|
109 |
} |
|
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 | 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 | 114 |
int namesize; |
115 |
int outfd; |
|
116 |
||
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
28
diff
changeset
|
117 |
if (command_line.store_output) |
22 | 118 |
{ |
119 |
int res; |
|
120 |
||
65 | 121 |
if (command_line.gzip) |
122 |
{ |
|
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 | 125 |
pipe(p); |
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 | 131 |
close(p[1]); |
132 |
||
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 | 135 |
outfd = mkstemp(outfname); /* stdout */ |
136 |
||
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 | 141 |
} |
142 |
else |
|
143 |
{ |
|
144 |
/* Prepare the filename */ |
|
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 | 149 |
} |
22 | 150 |
|
151 |
/* Send the filename */ |
|
152 |
namesize = sizeof(outfname); |
|
153 |
res = write(fd_send_filename, (char *)&namesize, sizeof(namesize)); |
|
154 |
write(fd_send_filename, outfname, sizeof(outfname)); |
|
155 |
} |
|
156 |
close(fd_send_filename); |
|
157 |
||
9 | 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 | 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 | 163 |
} |
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 | 166 |
{ |
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 | 169 |
int p[2]; |
8 | 170 |
|
171 |
||
9 | 172 |
/* For the parent */ |
173 |
/*program_signal(); Still not needed*/ |
|
174 |
||
22 | 175 |
/* Prepare the output filename sending */ |
176 |
pipe(p); |
|
177 |
||
178 |
pid = fork(); |
|
179 |
||
8 | 180 |
switch(pid) |
181 |
{ |
|
182 |
case 0: |
|
95 | 183 |
restore_sigmask(); |
9 | 184 |
close(server_socket); |
22 | 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 | 187 |
/* Not reachable, if the 'exec' of the command |
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 | 190 |
exit(-1); |
8 | 191 |
break; |
192 |
case -1: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
77
diff
changeset
|
193 |
error("forking"); |
8 | 194 |
default: |
22 | 195 |
close(p[1]); |
35 | 196 |
errorlevel = run_parent(p[0], pid); |
8 | 197 |
break; |
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 | 201 |
} |
9 | 202 |
|
95 | 203 |
#if 0 |
204 |
Not needed |
|
9 | 205 |
static void sigchld_handler(int val) |
206 |
{ |
|
207 |
} |
|
208 |
||
209 |
static void program_signal() |
|
210 |
{ |
|
211 |
struct sigaction act; |
|
212 |
||
213 |
act.sa_handler = sigchld_handler; |
|
214 |
/* Reset the mask */ |
|
215 |
memset(&act.sa_mask,0,sizeof(act.sa_mask)); |
|
216 |
act.sa_flags = SA_NOCLDSTOP; |
|
217 |
||
218 |
sigaction(SIGCHLD, &act, NULL); |
|
219 |
} |
|
95 | 220 |
#endif |