author | viric@mandarina |
Mon, 24 Dec 2007 21:01:52 +0100 | |
changeset 160 | 8b40ee19e765 |
parent 159 | 76293ced27b5 |
child 166 | 8c19a66b5407 |
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 |
*/ |
|
1 | 7 |
#include <unistd.h> |
44 | 8 |
#include <stdlib.h> |
9 |
#include <string.h> |
|
95 | 10 |
#include <signal.h> |
1 | 11 |
|
12 |
#include <stdio.h> |
|
146 | 13 |
#include <sys/time.h> |
1 | 14 |
|
3 | 15 |
#include "main.h" |
16 |
||
1 | 17 |
extern char *optarg; |
18 |
extern int optind, opterr, optopt; |
|
19 |
||
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
20 |
/* Globals */ |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
21 |
struct Command_line command_line; |
1 | 22 |
int server_socket; |
23 |
||
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
24 |
/* Globals for the environment of getopt */ |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
25 |
static char getopt_env[] = "POSIXLY_CORRECT=YES"; |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
26 |
static char *old_getopt_env; |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
27 |
|
149 | 28 |
static char version[] = "Task Spooler v0.4.2 - a task queue system for the unix user.\n" |
49 | 29 |
"Copyright (C) 2007 Lluis Batlle i Rossell"; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
30 |
|
95 | 31 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
32 |
static void default_command_line() |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
33 |
{ |
36 | 34 |
command_line.request = c_LIST; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
35 |
command_line.need_server = 0; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
36 |
command_line.store_output = 1; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
37 |
command_line.should_go_background = 1; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
38 |
command_line.should_keep_finished = 1; |
65 | 39 |
command_line.gzip = 0; |
71 | 40 |
command_line.send_output_by_mail = 0; |
150 | 41 |
command_line.label = 0; |
155 | 42 |
command_line.depend = 0; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
43 |
} |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
44 |
|
18 | 45 |
void get_command(int index, int argc, char **argv) |
46 |
{ |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
47 |
command_line.command.array = &(argv[index]); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
48 |
command_line.command.num = argc - index; |
18 | 49 |
} |
3 | 50 |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
51 |
static int get_two_jobs(const char *str, int *j1, int *j2) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
52 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
53 |
char tmp[50]; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
54 |
char *tmp2; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
55 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
56 |
if(strlen(str) >= 50) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
57 |
return 0; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
58 |
strcpy(tmp, str); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
59 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
60 |
tmp2 = strchr(tmp , '-'); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
61 |
if (tmp2 == NULL) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
62 |
return 0; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
63 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
64 |
/* We change the '-' to '\0', so we have a delimiter, |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
65 |
* and we can access the two strings for the ids */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
66 |
*tmp2 = '\0'; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
67 |
/* Skip the '\0', and point tmp2 to the second id */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
68 |
++tmp2; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
69 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
70 |
*j1 = atoi(tmp); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
71 |
*j2 = atoi(tmp2); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
72 |
return 1; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
73 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
74 |
|
1 | 75 |
void parse_opts(int argc, char **argv) |
76 |
{ |
|
2 | 77 |
int c; |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
78 |
int res; |
1 | 79 |
|
2 | 80 |
/* Parse options */ |
81 |
while(1) { |
|
155 | 82 |
c = getopt(argc, argv, ":VhKgClnfmr:t:c:o:p:w:u:s:U:i:L:d"); |
1 | 83 |
|
2 | 84 |
if (c == -1) |
85 |
break; |
|
1 | 86 |
|
2 | 87 |
switch(c) |
88 |
{ |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
89 |
case 'K': |
31 | 90 |
command_line.request = c_KILL_SERVER; |
95 | 91 |
command_line.should_go_background = 0; |
2 | 92 |
break; |
3 | 93 |
case 'l': |
31 | 94 |
command_line.request = c_LIST; |
3 | 95 |
break; |
36 | 96 |
case 'h': |
97 |
command_line.request = c_SHOW_HELP; |
|
98 |
break; |
|
155 | 99 |
case 'd': |
100 |
command_line.depend = 1; |
|
101 |
break; |
|
37 | 102 |
case 'V': |
103 |
command_line.request = c_SHOW_VERSION; |
|
104 |
break; |
|
33 | 105 |
case 'C': |
106 |
command_line.request = c_CLEAR_FINISHED; |
|
107 |
break; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
108 |
case 'c': |
33 | 109 |
command_line.request = c_CAT; |
110 |
command_line.jobid = atoi(optarg); |
|
111 |
break; |
|
112 |
case 'o': |
|
113 |
command_line.request = c_SHOW_OUTPUT_FILE; |
|
114 |
command_line.jobid = atoi(optarg); |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
115 |
break; |
22 | 116 |
case 'n': |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
117 |
command_line.store_output = 0; |
22 | 118 |
break; |
150 | 119 |
case 'L': |
120 |
command_line.label = optarg; |
|
121 |
break; |
|
65 | 122 |
case 'g': |
123 |
command_line.gzip = 1; |
|
124 |
break; |
|
31 | 125 |
case 'f': |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
126 |
command_line.should_go_background = 0; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
127 |
break; |
71 | 128 |
case 'm': |
129 |
command_line.send_output_by_mail = 1; |
|
130 |
break; |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
131 |
case 't': |
31 | 132 |
command_line.request = c_TAIL; |
32 | 133 |
command_line.jobid = atoi(optarg); |
29
ca47653a4cbc
Add an option for electing not to go into background.
viric@llimona
parents:
27
diff
changeset
|
134 |
break; |
35 | 135 |
case 'p': |
136 |
command_line.request = c_SHOW_PID; |
|
137 |
command_line.jobid = atoi(optarg); |
|
138 |
break; |
|
146 | 139 |
case 'i': |
140 |
command_line.request = c_INFO; |
|
141 |
command_line.jobid = atoi(optarg); |
|
142 |
break; |
|
41 | 143 |
case 'r': |
144 |
command_line.request = c_REMOVEJOB; |
|
145 |
command_line.jobid = atoi(optarg); |
|
146 |
break; |
|
42 | 147 |
case 'w': |
148 |
command_line.request = c_WAITJOB; |
|
149 |
command_line.jobid = atoi(optarg); |
|
150 |
break; |
|
53 | 151 |
case 'u': |
152 |
command_line.request = c_URGENT; |
|
153 |
command_line.jobid = atoi(optarg); |
|
154 |
break; |
|
63 | 155 |
case 's': |
156 |
command_line.request = c_GET_STATE; |
|
157 |
command_line.jobid = atoi(optarg); |
|
158 |
break; |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
159 |
case 'U': |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
160 |
command_line.request = c_SWAP_JOBS; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
161 |
res = get_two_jobs(optarg, &command_line.jobid, |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
162 |
&command_line.jobid2); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
163 |
if (!res) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
164 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
165 |
fprintf(stderr, "Wrong <id-id> for -U.\n"); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
166 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
167 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
168 |
if (command_line.jobid == command_line.jobid2) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
169 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
170 |
fprintf(stderr, "Wrong <id-id> for -U. " |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
171 |
"Use different ids.\n"); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
172 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
173 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
174 |
break; |
31 | 175 |
case ':': |
176 |
switch(optopt) |
|
177 |
{ |
|
178 |
case 't': |
|
32 | 179 |
command_line.request = c_TAIL; |
31 | 180 |
command_line.jobid = -1; /* This means the 'last' job */ |
181 |
break; |
|
33 | 182 |
case 'c': |
183 |
command_line.request = c_CAT; |
|
184 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
185 |
break; |
|
186 |
case 'o': |
|
187 |
command_line.request = c_SHOW_OUTPUT_FILE; |
|
188 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
189 |
break; |
|
35 | 190 |
case 'p': |
191 |
command_line.request = c_SHOW_PID; |
|
192 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
193 |
break; |
|
146 | 194 |
case 'i': |
195 |
command_line.request = c_INFO; |
|
196 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
197 |
break; |
|
41 | 198 |
case 'r': |
199 |
command_line.request = c_REMOVEJOB; |
|
200 |
command_line.jobid = -1; /* This means the 'last' |
|
201 |
added job */ |
|
202 |
break; |
|
42 | 203 |
case 'w': |
204 |
command_line.request = c_WAITJOB; |
|
205 |
command_line.jobid = -1; /* This means the 'last' |
|
206 |
added job */ |
|
207 |
break; |
|
53 | 208 |
case 'u': |
209 |
command_line.request = c_URGENT; |
|
210 |
command_line.jobid = -1; /* This means the 'last' |
|
211 |
added job */ |
|
212 |
break; |
|
63 | 213 |
case 's': |
214 |
command_line.request = c_GET_STATE; |
|
215 |
command_line.jobid = -1; /* This means the 'last' |
|
216 |
added job */ |
|
217 |
break; |
|
31 | 218 |
default: |
150 | 219 |
fprintf(stderr, "Option %c missing argument.\n", |
220 |
optopt); |
|
31 | 221 |
exit(-1); |
222 |
} |
|
223 |
break; |
|
224 |
case '?': |
|
225 |
fprintf(stderr, "Wrong option %c.\n", optopt); |
|
226 |
exit(-1); |
|
2 | 227 |
} |
228 |
} |
|
1 | 229 |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
230 |
command_line.command.num = 0; |
18 | 231 |
|
37 | 232 |
/* if the request is still the default option... |
233 |
* (the default values should be centralized) */ |
|
234 |
if (optind < argc && command_line.request == c_LIST) |
|
31 | 235 |
{ |
236 |
command_line.request = c_QUEUE; |
|
18 | 237 |
get_command(optind, argc, argv); |
31 | 238 |
} |
3 | 239 |
|
37 | 240 |
if (command_line.request != c_SHOW_HELP && |
241 |
command_line.request != c_SHOW_VERSION) |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
242 |
command_line.need_server = 1; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
243 |
|
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
244 |
if ( ! command_line.store_output && ! command_line.should_go_background ) |
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
245 |
command_line.should_keep_finished = 0; |
71 | 246 |
|
247 |
if ( command_line.send_output_by_mail && ((! command_line.store_output) || |
|
248 |
command_line.gzip) ) |
|
249 |
{ |
|
250 |
fprintf(stderr, |
|
251 |
"For e-mail, you should store the output (not through gzip)\n"); |
|
252 |
exit(-1); |
|
253 |
} |
|
1 | 254 |
} |
255 |
||
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
256 |
static void fill_first_3_handles() |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
257 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
258 |
int tmp_pipe1[2]; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
259 |
int tmp_pipe2[2]; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
260 |
/* This will fill handles 0 and 1 */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
261 |
pipe(tmp_pipe1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
262 |
/* This will fill handles 2 and 3 */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
263 |
pipe(tmp_pipe2); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
264 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
265 |
close(tmp_pipe2[1]); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
266 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
267 |
|
44 | 268 |
static void go_background() |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
269 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
270 |
int pid; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
271 |
pid = fork(); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
272 |
|
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
273 |
switch(pid) |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
274 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
275 |
case -1: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
276 |
error("fork failed"); |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
277 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
278 |
case 0: |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
279 |
close(0); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
280 |
close(1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
281 |
close(2); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
282 |
/* This is a weird thing. But we will later want to |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
283 |
* allocate special files to the 0, 1 or 2 fds. It's |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
284 |
* almost impossible, if other important things got |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
285 |
* allocated here. */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
286 |
fill_first_3_handles(); |
133 | 287 |
setsid(); |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
288 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
289 |
default: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
290 |
exit(0); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
291 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
292 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
293 |
|
37 | 294 |
static void print_help(const char *cmd) |
36 | 295 |
{ |
160 | 296 |
printf("usage: %s [action] [-ngfmd] [-L <lab>] [cmd...]\n", cmd); |
73 | 297 |
printf("Env vars:\n"); |
298 |
printf(" TS_SOCKET the path to the unix socket used by the ts command\n"); |
|
299 |
printf(" TS_MAILTO where to mail the result (on -m). Local user by default\n"); |
|
300 |
printf(" TS_MAXFINISHED maximum finished jobs in the queue\n"); |
|
301 |
printf(" TS_ONFINISH binary called on job end (passing jobid, error, outfile, command)\n"); |
|
151 | 302 |
printf(" TS_ENV command called on job enqueue, which output determines the job information\n"); |
52 | 303 |
printf("Actions:\n"); |
36 | 304 |
printf(" -K kill the task spooler server\n"); |
305 |
printf(" -C clear the list of finished jobs\n"); |
|
306 |
printf(" -l show the job list (default action)\n"); |
|
132 | 307 |
printf(" -t [id] tail -f the output of the job. Last run if not specified.\n"); |
308 |
printf(" -c [id] cat the output of the job. Last run if not specified.\n"); |
|
309 |
printf(" -p [id] show the pid of the job. Last run if not specified.\n"); |
|
61 | 310 |
printf(" -o [id] show the output file. Of last job run, if not specified.\n"); |
151 | 311 |
printf(" -i [id] show job information. Of last job run, if not specified.\n"); |
78 | 312 |
printf(" -s [id] show the job state. Of the last added, if not specified.\n"); |
42 | 313 |
printf(" -r [id] remove a job. The last added, if not specified.\n"); |
314 |
printf(" -w [id] wait for a job. The last added, if not specified.\n"); |
|
53 | 315 |
printf(" -u [id] put that job first. The last added, if not specified.\n"); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
316 |
printf(" -U <id-id> swap two jobs in the queue.\n"); |
37 | 317 |
printf(" -h show this help\n"); |
318 |
printf(" -V show the program version\n"); |
|
52 | 319 |
printf("Options adding jobs:\n"); |
36 | 320 |
printf(" -n don't store the output of the command.\n"); |
65 | 321 |
printf(" -g gzip the stored output (if not -n).\n"); |
36 | 322 |
printf(" -f don't fork into background.\n"); |
71 | 323 |
printf(" -m send the output by e-mail (uses sendmail).\n"); |
158 | 324 |
printf(" -d the job will be run only if the job before ends well\n"); |
159 | 325 |
printf(" -L <lab> name this task with a label, to be distinguished on listing.\n"); |
36 | 326 |
} |
327 |
||
37 | 328 |
static void print_version() |
329 |
{ |
|
330 |
puts(version); |
|
44 | 331 |
} |
37 | 332 |
|
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
333 |
static void set_getopt_env() |
48 | 334 |
{ |
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
335 |
old_getopt_env = getenv("POSIXLY_CORRECT"); |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
336 |
putenv(getopt_env); |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
337 |
} |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
338 |
|
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
339 |
static void unset_getopt_env() |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
340 |
{ |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
341 |
if (old_getopt_env == NULL) |
87
18dc656315eb
Finally fixed the POSIXLY_CORRECT env problem
lbatlle@npdl268.bpo.hp.com
parents:
81
diff
changeset
|
342 |
{ |
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
343 |
/* Wipe the string from the environment */ |
87
18dc656315eb
Finally fixed the POSIXLY_CORRECT env problem
lbatlle@npdl268.bpo.hp.com
parents:
81
diff
changeset
|
344 |
putenv("POSIXLY_CORRECT"); |
18dc656315eb
Finally fixed the POSIXLY_CORRECT env problem
lbatlle@npdl268.bpo.hp.com
parents:
81
diff
changeset
|
345 |
} |
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
346 |
else |
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
347 |
sprintf(getopt_env, "POSIXLY_CORRECT=%s", old_getopt_env); |
48 | 348 |
} |
349 |
||
1 | 350 |
int main(int argc, char **argv) |
351 |
{ |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
352 |
int errorlevel = 0; |
41 | 353 |
|
104 | 354 |
process_type = CLIENT; |
355 |
||
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
356 |
set_getopt_env(); |
41 | 357 |
/* This is needed in a gnu system, so getopt works well */ |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
358 |
default_command_line(); |
2 | 359 |
parse_opts(argc, argv); |
76
14bc35eee745
Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents:
73
diff
changeset
|
360 |
unset_getopt_env(); |
1 | 361 |
|
95 | 362 |
/* This will be inherited by the server, if it's run */ |
363 |
ignore_sigpipe(); |
|
364 |
||
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
365 |
if (command_line.need_server) |
3 | 366 |
ensure_server_up(); |
367 |
||
31 | 368 |
switch(command_line.request) |
3 | 369 |
{ |
37 | 370 |
case c_SHOW_VERSION: |
371 |
print_version(argv[0]); |
|
372 |
break; |
|
36 | 373 |
case c_SHOW_HELP: |
374 |
print_help(argv[0]); |
|
37 | 375 |
break; |
31 | 376 |
case c_QUEUE: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
377 |
if (command_line.command.num <= 0) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
378 |
error("Tried to queue a void command. parameters: %i", |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
379 |
command_line.command.num); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
380 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
381 |
error("The command %i needs the server", command_line.request); |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
382 |
c_new_job(); |
71 | 383 |
command_line.jobid = c_wait_newjob_ok(); |
62 | 384 |
if (command_line.store_output) |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
385 |
{ |
71 | 386 |
printf("%i\n", command_line.jobid); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
387 |
fflush(stdout); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
388 |
} |
62 | 389 |
if (command_line.should_go_background) |
390 |
{ |
|
391 |
go_background(); |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
392 |
c_wait_server_commands(); |
62 | 393 |
} else |
394 |
{ |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
65
diff
changeset
|
395 |
errorlevel = c_wait_server_commands(); |
62 | 396 |
} |
31 | 397 |
break; |
398 |
case c_LIST: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
399 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
400 |
error("The command %i needs the server", command_line.request); |
8 | 401 |
c_list_jobs(); |
5 | 402 |
c_wait_server_lines(); |
31 | 403 |
break; |
404 |
case c_KILL_SERVER: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
405 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
406 |
error("The command %i needs the server", command_line.request); |
3 | 407 |
c_shutdown_server(); |
31 | 408 |
break; |
409 |
case c_CLEAR_FINISHED: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
410 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
411 |
error("The command %i needs the server", command_line.request); |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
412 |
c_clear_finished(); |
31 | 413 |
break; |
32 | 414 |
case c_TAIL: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
415 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
416 |
error("The command %i needs the server", command_line.request); |
32 | 417 |
c_tail(); |
418 |
/* This will not return! */ |
|
419 |
break; |
|
33 | 420 |
case c_CAT: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
421 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
422 |
error("The command %i needs the server", command_line.request); |
33 | 423 |
c_cat(); |
424 |
/* This will not return! */ |
|
425 |
break; |
|
426 |
case c_SHOW_OUTPUT_FILE: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
427 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
428 |
error("The command %i needs the server", command_line.request); |
33 | 429 |
c_show_output_file(); |
430 |
break; |
|
35 | 431 |
case c_SHOW_PID: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
432 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
433 |
error("The command %i needs the server", command_line.request); |
35 | 434 |
c_show_pid(); |
435 |
break; |
|
146 | 436 |
case c_INFO: |
437 |
if (!command_line.need_server) |
|
438 |
error("The command %i needs the server", command_line.request); |
|
439 |
c_show_info(); |
|
440 |
break; |
|
41 | 441 |
case c_REMOVEJOB: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
442 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
443 |
error("The command %i needs the server", command_line.request); |
41 | 444 |
c_remove_job(); |
445 |
break; |
|
42 | 446 |
case c_WAITJOB: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
447 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
448 |
error("The command %i needs the server", command_line.request); |
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
449 |
errorlevel = c_wait_job(); |
42 | 450 |
break; |
53 | 451 |
case c_URGENT: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
452 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
453 |
error("The command %i needs the server", command_line.request); |
53 | 454 |
c_move_urgent(); |
455 |
break; |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
456 |
case c_SWAP_JOBS: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
457 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
458 |
error("The command %i needs the server", command_line.request); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
459 |
c_swap_jobs(); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
65
diff
changeset
|
460 |
break; |
63 | 461 |
case c_GET_STATE: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
462 |
if (!command_line.need_server) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
87
diff
changeset
|
463 |
error("The command %i needs the server", command_line.request); |
63 | 464 |
/* This will also print the state into stdout */ |
465 |
c_get_state(); |
|
466 |
break; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
467 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
468 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
469 |
if (command_line.need_server) |
2 | 470 |
{ |
3 | 471 |
close(server_socket); |
2 | 472 |
} |
1 | 473 |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
474 |
return errorlevel; |
1 | 475 |
} |