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