author | viric@llimona |
Tue, 27 Mar 2007 23:29:56 +0200 | |
changeset 58 | 3c492266923e |
parent 55 | 678ca291d545 |
child 63 | 47be318f4cbc |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
8 | 7 |
#include <stdlib.h> |
44 | 8 |
#include <unistd.h> |
8 | 9 |
#include <stdio.h> |
3 | 10 |
#include <assert.h> |
44 | 11 |
#include <string.h> |
3 | 12 |
#include "msg.h" |
32 | 13 |
#include "main.h" |
3 | 14 |
|
8 | 15 |
static enum |
16 |
{ |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
17 |
FREE, /* No task is running */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
18 |
WAITING /* A task is running, and the server is waiting */ |
8 | 19 |
} state = FREE; |
20 |
||
3 | 21 |
struct Job |
22 |
{ |
|
42 | 23 |
struct Job *next; |
3 | 24 |
int jobid; |
18 | 25 |
char *command; |
3 | 26 |
enum Jobstate state; |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
27 |
int errorlevel; |
22 | 28 |
char *output_filename; |
27
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
29 |
int store_output; |
35 | 30 |
int pid; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
54
diff
changeset
|
31 |
int should_keep_finished; |
3 | 32 |
}; |
33 |
||
42 | 34 |
struct Notify |
35 |
{ |
|
36 |
int socket; |
|
37 |
int jobid; |
|
38 |
struct Notify *next; |
|
39 |
}; |
|
40 |
||
3 | 41 |
/* Globals */ |
42 |
static struct Job *firstjob = 0; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
43 |
static struct Job *first_finished_job = 0; |
44 | 44 |
static int jobids = 0; |
3 | 45 |
|
42 | 46 |
static struct Notify *first_notify = 0; |
47 |
||
5 | 48 |
static void send_list_line(int s, const char * str) |
49 |
{ |
|
50 |
struct msg m; |
|
51 |
||
21 | 52 |
/* Message */ |
5 | 53 |
m.type = LIST_LINE; |
21 | 54 |
m.u.line_size = strlen(str) + 1; |
5 | 55 |
|
21 | 56 |
send_msg(s, &m); |
57 |
||
58 |
/* Send the line */ |
|
59 |
send_bytes(s, str, m.u.line_size); |
|
5 | 60 |
} |
61 |
||
53 | 62 |
static void send_urgent_ok(int s) |
63 |
{ |
|
64 |
struct msg m; |
|
65 |
||
66 |
/* Message */ |
|
67 |
m.type = URGENT_OK; |
|
68 |
||
69 |
send_msg(s, &m); |
|
70 |
} |
|
71 |
||
72 |
static struct Job * find_previous_job(struct Job *final) |
|
73 |
{ |
|
74 |
struct Job *p; |
|
75 |
||
76 |
/* Show Queued or Running jobs */ |
|
77 |
p = firstjob; |
|
78 |
while(p != 0) |
|
79 |
{ |
|
80 |
if (p->next == final) |
|
81 |
return p; |
|
82 |
p = p->next; |
|
83 |
} |
|
84 |
||
85 |
return 0; |
|
86 |
} |
|
87 |
||
22 | 88 |
static struct Job * findjob(int jobid) |
89 |
{ |
|
90 |
struct Job *p; |
|
91 |
||
92 |
/* Show Queued or Running jobs */ |
|
93 |
p = firstjob; |
|
94 |
while(p != 0) |
|
95 |
{ |
|
96 |
if (p->jobid == jobid) |
|
97 |
return p; |
|
98 |
p = p->next; |
|
99 |
} |
|
100 |
||
101 |
return 0; |
|
102 |
} |
|
103 |
||
32 | 104 |
static struct Job * find_finished_job(int jobid) |
105 |
{ |
|
106 |
struct Job *p; |
|
107 |
||
108 |
/* Show Queued or Running jobs */ |
|
109 |
p = first_finished_job; |
|
110 |
while(p != 0) |
|
111 |
{ |
|
112 |
if (p->jobid == jobid) |
|
113 |
return p; |
|
114 |
p = p->next; |
|
115 |
} |
|
116 |
||
117 |
return 0; |
|
118 |
} |
|
119 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
120 |
void s_mark_job_running() |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
121 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
122 |
firstjob->state = RUNNING; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
123 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
124 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
125 |
static const char * jstate2string(enum Jobstate s) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
126 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
127 |
char * jobstate; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
128 |
switch(s) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
129 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
130 |
case QUEUED: |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
131 |
jobstate = "queued"; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
132 |
break; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
133 |
case RUNNING: |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
134 |
jobstate = "running"; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
135 |
break; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
136 |
case FINISHED: |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
137 |
jobstate = "finished"; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
138 |
break; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
139 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
140 |
return jobstate; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
141 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
142 |
|
3 | 143 |
void s_list(int s) |
144 |
{ |
|
145 |
struct Job *p; |
|
5 | 146 |
char buffer[LINE_LEN]; |
3 | 147 |
|
39 | 148 |
sprintf(buffer, "%-4s%-10s%-20s%-8s%-37s\n", |
149 |
"ID", |
|
150 |
"State", |
|
151 |
"Output", |
|
152 |
"E-Level", |
|
153 |
"Command"); |
|
5 | 154 |
send_list_line(s,buffer); |
3 | 155 |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
156 |
/* Show Queued or Running jobs */ |
3 | 157 |
p = firstjob; |
158 |
while(p != 0) |
|
159 |
{ |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
160 |
const char * jobstate; |
22 | 161 |
const char * output_filename; |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
162 |
jobstate = jstate2string(p->state); |
27
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
163 |
if (p->store_output) |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
164 |
{ |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
165 |
if (p->state == RUNNING) |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
166 |
{ |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
167 |
if (p->output_filename == 0) |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
168 |
/* This may happen due to concurrency |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
169 |
* problems */ |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
170 |
output_filename = "(...)"; |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
171 |
else |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
172 |
output_filename = p->output_filename; |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
173 |
} else |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
174 |
output_filename = "(file)"; |
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
175 |
} else |
22 | 176 |
output_filename = "stdout"; |
27
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
177 |
|
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
178 |
|
39 | 179 |
sprintf(buffer, "%-4i%-10s%-20s%-8s%-37s\n", |
5 | 180 |
p->jobid, |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
181 |
jobstate, |
22 | 182 |
output_filename, |
39 | 183 |
"", |
5 | 184 |
p->command); |
185 |
send_list_line(s,buffer); |
|
3 | 186 |
p = p->next; |
187 |
} |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
188 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
189 |
p = first_finished_job; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
190 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
191 |
if (p != 0) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
192 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
193 |
/* Show Finished jobs */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
194 |
while(p != 0) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
195 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
196 |
const char * jobstate; |
22 | 197 |
const char * output_filename; |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
198 |
jobstate = jstate2string(p->state); |
22 | 199 |
if (p->output_filename == 0) |
200 |
output_filename = "stdout"; |
|
201 |
else |
|
202 |
output_filename = p->output_filename; |
|
39 | 203 |
sprintf(buffer, "%-4i%-10s%-20s%-8i%-37s\n", |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
204 |
p->jobid, |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
205 |
jobstate, |
22 | 206 |
output_filename, |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
207 |
p->errorlevel, |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
208 |
p->command); |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
209 |
send_list_line(s,buffer); |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
210 |
p = p->next; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
211 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
212 |
} |
3 | 213 |
} |
214 |
||
215 |
static struct Job * newjobptr() |
|
216 |
{ |
|
217 |
struct Job *p; |
|
218 |
||
219 |
if (firstjob == 0) |
|
220 |
{ |
|
221 |
firstjob = (struct Job *) malloc(sizeof(*firstjob)); |
|
222 |
firstjob->next = 0; |
|
223 |
return firstjob; |
|
224 |
} |
|
225 |
||
226 |
p = firstjob; |
|
227 |
while(p->next != 0) |
|
228 |
p = p->next; |
|
229 |
||
230 |
p->next = (struct Job *) malloc(sizeof(*p)); |
|
231 |
p->next->next = 0; |
|
232 |
||
233 |
return p->next; |
|
234 |
} |
|
235 |
||
236 |
/* Returns job id */ |
|
18 | 237 |
int s_newjob(int s, struct msg *m) |
3 | 238 |
{ |
239 |
struct Job *p; |
|
18 | 240 |
int res; |
3 | 241 |
|
242 |
p = newjobptr(); |
|
243 |
||
244 |
p->jobid = jobids++; |
|
245 |
p->state = QUEUED; |
|
27
886bdb2f4632
Fixed the Output File information in the list.
viric@llimona
parents:
26
diff
changeset
|
246 |
p->store_output = m->u.newjob.store_output; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
54
diff
changeset
|
247 |
p->should_keep_finished = m->u.newjob.should_keep_finished; |
18 | 248 |
|
249 |
/* load the command */ |
|
250 |
p->command = malloc(m->u.newjob.command_size); |
|
251 |
/* !!! Check retval */ |
|
252 |
res = recv_bytes(s, p->command, m->u.newjob.command_size); |
|
253 |
assert(res != -1); |
|
3 | 254 |
|
255 |
return p->jobid; |
|
256 |
} |
|
257 |
||
258 |
void s_removejob(int jobid) |
|
259 |
{ |
|
260 |
struct Job *p; |
|
261 |
struct Job *newnext; |
|
262 |
||
263 |
if (firstjob->jobid == jobid) |
|
264 |
{ |
|
265 |
struct Job *newfirst; |
|
266 |
/* First job is to be removed */ |
|
267 |
newfirst = firstjob->next; |
|
18 | 268 |
free(firstjob->command); |
3 | 269 |
free(firstjob); |
270 |
firstjob = newfirst; |
|
271 |
return; |
|
272 |
} |
|
273 |
||
274 |
p = firstjob; |
|
275 |
/* Not first job */ |
|
276 |
while (p->next != 0) |
|
277 |
{ |
|
278 |
if (p->next->jobid == jobid) |
|
279 |
break; |
|
280 |
p = p->next; |
|
281 |
} |
|
282 |
assert(p->next != 0); |
|
283 |
||
284 |
newnext = p->next->next; |
|
285 |
||
18 | 286 |
free(p->next->command); |
3 | 287 |
free(p->next); |
288 |
p->next = newnext; |
|
289 |
} |
|
8 | 290 |
|
291 |
/* -1 if no one should be run. */ |
|
292 |
int next_run_job() |
|
293 |
{ |
|
294 |
if (state == WAITING) |
|
295 |
return -1; |
|
296 |
||
297 |
if (firstjob != 0) |
|
9 | 298 |
{ |
299 |
state = WAITING; |
|
8 | 300 |
return firstjob->jobid; |
9 | 301 |
} |
8 | 302 |
|
303 |
return -1; |
|
304 |
} |
|
305 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
306 |
/* Add the job to the finished queue. */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
307 |
static void new_finished_job(struct Job *j) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
308 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
309 |
struct Job *p; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
310 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
311 |
if (first_finished_job == 0) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
312 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
313 |
first_finished_job = j; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
314 |
first_finished_job->next = 0; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
315 |
return; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
316 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
317 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
318 |
p = first_finished_job; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
319 |
while(p->next != 0) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
320 |
p = p->next; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
321 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
322 |
p->next = j; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
323 |
p->next->next = 0; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
324 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
325 |
return; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
326 |
} |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
327 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
328 |
void job_finished(int errorlevel) |
8 | 329 |
{ |
330 |
struct Job *newfirst; |
|
331 |
||
332 |
assert(state == WAITING); |
|
333 |
assert(firstjob != 0); |
|
334 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
335 |
assert(firstjob->state == RUNNING); |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
336 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
337 |
/* Mark state */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
338 |
firstjob->state = FINISHED; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
339 |
firstjob->errorlevel = errorlevel; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
340 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
341 |
/* Add it to the finished queue */ |
26 | 342 |
newfirst = firstjob->next; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
54
diff
changeset
|
343 |
if (firstjob->should_keep_finished) |
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
54
diff
changeset
|
344 |
new_finished_job(firstjob); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
345 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
346 |
/* Remove it from the run queue */ |
26 | 347 |
firstjob = newfirst; |
8 | 348 |
|
349 |
state = FREE; |
|
350 |
} |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
351 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
352 |
void s_clear_finished() |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
353 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
354 |
struct Job *p; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
355 |
|
22 | 356 |
if (first_finished_job == 0) |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
357 |
return; |
22 | 358 |
|
359 |
p = first_finished_job; |
|
360 |
first_finished_job = 0; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
361 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
362 |
while (p->next != 0) |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
363 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
364 |
struct Job *tmp; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
365 |
tmp = p->next; |
22 | 366 |
free(p->command); |
367 |
free(p->output_filename); |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
368 |
free(p); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
369 |
p = tmp; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
370 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
371 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
372 |
free(p->next); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
373 |
} |
22 | 374 |
|
35 | 375 |
void s_process_runjob_ok(int jobid, char *oname, int pid) |
22 | 376 |
{ |
377 |
struct Job *p; |
|
378 |
p = findjob(jobid); |
|
379 |
assert(p != 0); |
|
380 |
assert(p->state == RUNNING); |
|
381 |
||
35 | 382 |
p->pid = pid; |
22 | 383 |
p->output_filename = oname; |
384 |
} |
|
32 | 385 |
|
386 |
void s_send_output(int s, int jobid) |
|
387 |
{ |
|
388 |
struct Job *p = 0; |
|
389 |
struct msg m; |
|
390 |
||
391 |
if (jobid == -1) |
|
392 |
{ |
|
393 |
/* This means that we want the tail of the running task, or that |
|
394 |
* of the last job run */ |
|
395 |
if (state == WAITING) |
|
396 |
{ |
|
397 |
p = firstjob; |
|
398 |
assert(p != 0); |
|
399 |
} |
|
400 |
else |
|
401 |
{ |
|
402 |
p = first_finished_job; |
|
403 |
if (p == 0) |
|
404 |
{ |
|
405 |
send_list_line(s, "No jobs.\n"); |
|
406 |
return; |
|
407 |
} |
|
408 |
while(p->next != 0) |
|
409 |
p = p->next; |
|
410 |
} |
|
411 |
} else |
|
412 |
{ |
|
413 |
if (state == WAITING && firstjob->jobid == jobid) |
|
414 |
p = firstjob; |
|
415 |
else |
|
416 |
p = find_finished_job(jobid); |
|
417 |
} |
|
418 |
||
419 |
if (p == 0) |
|
420 |
{ |
|
421 |
char tmp[50]; |
|
422 |
sprintf(tmp, "Job %i not finished or not running.\n", jobid); |
|
423 |
send_list_line(s, tmp); |
|
424 |
return; |
|
425 |
} |
|
426 |
||
427 |
m.type = ANSWER_OUTPUT; |
|
428 |
if (!p->store_output) |
|
429 |
{ |
|
430 |
send_list_line(s, "The job hasn't output stored.\n"); |
|
431 |
return; |
|
432 |
} |
|
433 |
m.u.output.store_output = p->store_output; |
|
35 | 434 |
m.u.output.pid = p->pid; |
32 | 435 |
m.u.output.ofilename_size = strlen(p->output_filename) + 1; |
436 |
send_msg(s, &m); |
|
437 |
send_bytes(s, p->output_filename, m.u.output.ofilename_size); |
|
438 |
} |
|
41 | 439 |
|
440 |
void s_remove_job(int s, int jobid) |
|
441 |
{ |
|
442 |
struct Job *p = 0; |
|
443 |
struct msg m; |
|
444 |
struct Job *before_p = 0; |
|
445 |
||
446 |
if (jobid == -1) |
|
447 |
{ |
|
448 |
/* Find the last job added */ |
|
449 |
p = firstjob; |
|
450 |
if (p != 0) |
|
451 |
{ |
|
452 |
while (p->next != 0) |
|
453 |
{ |
|
454 |
before_p = p; |
|
455 |
p = p->next; |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
else |
|
460 |
{ |
|
461 |
p = firstjob; |
|
462 |
if (p != 0) |
|
463 |
{ |
|
464 |
while (p->next != 0 && p->jobid != jobid) |
|
465 |
{ |
|
466 |
before_p = p; |
|
467 |
p = p->next; |
|
468 |
} |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
if (p == 0 || p->state != QUEUED || before_p == 0) |
|
473 |
{ |
|
474 |
char tmp[50]; |
|
475 |
if (jobid == -1) |
|
44 | 476 |
sprintf(tmp, "The last job cannot be removed.\n"); |
41 | 477 |
else |
478 |
sprintf(tmp, "The job %i cannot be removed.\n", jobid); |
|
479 |
send_list_line(s, tmp); |
|
480 |
return; |
|
481 |
} |
|
482 |
||
483 |
before_p->next = p->next; |
|
484 |
free(p); |
|
485 |
m.type = REMOVEJOB_OK; |
|
486 |
send_msg(s, &m); |
|
487 |
} |
|
42 | 488 |
|
489 |
static void add_to_notify_list(int s, int jobid) |
|
490 |
{ |
|
491 |
struct Notify *n; |
|
492 |
struct Notify *new; |
|
493 |
||
494 |
new = (struct Notify *) malloc(sizeof(*n)); |
|
495 |
||
496 |
new->socket = s; |
|
497 |
new->jobid = jobid; |
|
498 |
new->next = 0; |
|
499 |
||
500 |
n = first_notify; |
|
501 |
if (n == 0) |
|
502 |
{ |
|
503 |
first_notify = new; |
|
504 |
return; |
|
505 |
} |
|
506 |
||
507 |
while(n->next != 0) |
|
508 |
n = n->next; |
|
509 |
||
510 |
n->next = new; |
|
511 |
} |
|
512 |
||
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
513 |
static void send_waitjob_ok(int s, int errorlevel) |
42 | 514 |
{ |
515 |
struct msg m; |
|
516 |
||
517 |
m.type = WAITJOB_OK; |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
518 |
m.u.errorlevel = errorlevel; |
42 | 519 |
send_msg(s, &m); |
520 |
} |
|
521 |
||
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
522 |
static struct Job * |
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
523 |
get_job(int jobid) |
42 | 524 |
{ |
525 |
struct Job *j; |
|
526 |
||
527 |
j = findjob(jobid); |
|
528 |
if (j != NULL) |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
529 |
return j; |
42 | 530 |
|
531 |
j = find_finished_job(jobid); |
|
532 |
||
533 |
if (j != NULL) |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
534 |
return j; |
42 | 535 |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
536 |
return 0; |
42 | 537 |
} |
538 |
||
539 |
/* Don't complain, if the socket doesn't exist */ |
|
540 |
void s_remove_notification(int s) |
|
541 |
{ |
|
542 |
struct Notify *n; |
|
543 |
struct Notify *previous; |
|
544 |
n = first_notify; |
|
545 |
while (n != 0 && n->socket != s) |
|
546 |
n = n->next; |
|
547 |
if (n == 0) |
|
548 |
return; |
|
549 |
||
550 |
/* Remove the notification */ |
|
551 |
previous = first_notify; |
|
552 |
if (n == previous) |
|
553 |
{ |
|
554 |
free(first_notify); |
|
555 |
first_notify = 0; |
|
556 |
return; |
|
557 |
} |
|
558 |
||
559 |
/* if not the first... */ |
|
560 |
while(previous->next != n) |
|
561 |
previous = previous->next; |
|
562 |
||
563 |
previous->next = n->next; |
|
564 |
free(n); |
|
565 |
} |
|
566 |
||
567 |
/* This is called when a job finishes */ |
|
568 |
void check_notify_list(int jobid) |
|
569 |
{ |
|
570 |
struct Notify *n; |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
571 |
struct Job *j; |
42 | 572 |
|
573 |
n = first_notify; |
|
574 |
while (n != 0 && n->jobid != jobid) |
|
575 |
{ |
|
576 |
n = n->next; |
|
577 |
} |
|
578 |
||
579 |
if (n == 0) |
|
580 |
{ |
|
581 |
return; |
|
582 |
} |
|
583 |
||
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
584 |
j = get_job(jobid); |
42 | 585 |
/* If the job finishes, notify the waiter */ |
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
586 |
if (j->state == FINISHED) |
42 | 587 |
{ |
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
588 |
send_waitjob_ok(n->socket, j->errorlevel); |
42 | 589 |
s_remove_notification(n->socket); |
590 |
} |
|
591 |
} |
|
592 |
||
593 |
void s_wait_job(int s, int jobid) |
|
594 |
{ |
|
595 |
struct Job *p = 0; |
|
596 |
||
597 |
if (jobid == -1) |
|
598 |
{ |
|
599 |
/* Find the last job added */ |
|
600 |
p = firstjob; |
|
601 |
||
602 |
if (p != 0) |
|
603 |
while (p->next != 0) |
|
604 |
p = p->next; |
|
605 |
||
606 |
/* Look in finished jobs if needed */ |
|
607 |
if (p == 0) |
|
608 |
{ |
|
609 |
p = first_finished_job; |
|
610 |
if (p != 0) |
|
611 |
while (p->next != 0) |
|
612 |
p = p->next; |
|
613 |
} |
|
614 |
} |
|
615 |
else |
|
616 |
{ |
|
617 |
p = firstjob; |
|
618 |
while (p != 0 && p->jobid != jobid) |
|
619 |
p = p->next; |
|
620 |
||
621 |
/* Look in finished jobs if needed */ |
|
622 |
if (p == 0) |
|
623 |
{ |
|
624 |
p = first_finished_job; |
|
625 |
while (p != 0 && p->jobid != jobid) |
|
626 |
p = p->next; |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
if (p == 0) |
|
631 |
{ |
|
632 |
char tmp[50]; |
|
633 |
if (jobid == -1) |
|
44 | 634 |
sprintf(tmp, "The last job cannot be waited.\n"); |
42 | 635 |
else |
636 |
sprintf(tmp, "The job %i cannot be waited.\n", jobid); |
|
637 |
send_list_line(s, tmp); |
|
638 |
return; |
|
639 |
} |
|
640 |
||
641 |
if (p->state == FINISHED) |
|
642 |
{ |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
643 |
send_waitjob_ok(s, p->errorlevel); |
42 | 644 |
} |
645 |
else |
|
646 |
add_to_notify_list(s, p->jobid); |
|
647 |
} |
|
53 | 648 |
|
649 |
void s_move_urgent(int s, int jobid) |
|
650 |
{ |
|
651 |
struct Job *p = 0; |
|
652 |
struct Job *tmp1; |
|
653 |
||
654 |
if (jobid == -1) |
|
655 |
{ |
|
656 |
/* Find the last job added */ |
|
657 |
p = firstjob; |
|
658 |
||
659 |
if (p != 0) |
|
660 |
while (p->next != 0) |
|
661 |
p = p->next; |
|
662 |
} |
|
663 |
else |
|
664 |
{ |
|
665 |
p = firstjob; |
|
666 |
while (p != 0 && p->jobid != jobid) |
|
667 |
p = p->next; |
|
668 |
} |
|
669 |
||
670 |
if (p == 0 || firstjob->next == 0) |
|
671 |
{ |
|
672 |
char tmp[50]; |
|
673 |
if (jobid == -1) |
|
674 |
sprintf(tmp, "The last job cannot be urged.\n"); |
|
675 |
else |
|
676 |
sprintf(tmp, "The job %i cannot be urged.\n", jobid); |
|
677 |
send_list_line(s, tmp); |
|
678 |
return; |
|
679 |
} |
|
680 |
||
681 |
/* Interchange the pointers */ |
|
682 |
tmp1 = find_previous_job(p); |
|
683 |
tmp1->next = p->next; |
|
684 |
p->next = firstjob->next; |
|
685 |
firstjob->next = p; |
|
686 |
||
687 |
||
688 |
send_urgent_ok(s); |
|
689 |
} |