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