author | viric@llimona |
Tue, 27 Mar 2007 23:29:56 +0200 | |
changeset 58 | 3c492266923e |
parent 55 | 678ca291d545 |
child 62 | 2b29d4666f6c |
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 <assert.h> |
9 | 8 |
#include <stdio.h> |
44 | 9 |
#include <string.h> |
10 |
#include <stdlib.h> |
|
11 |
#include <sys/types.h> |
|
12 |
#include <sys/socket.h> |
|
1 | 13 |
#include "msg.h" |
3 | 14 |
#include "main.h" |
1 | 15 |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
16 |
static void c_end_of_job(int errorlevel); |
8 | 17 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
18 |
void c_new_job(const char *command) |
3 | 19 |
{ |
20 |
struct msg m; |
|
21 |
||
22 |
m.type = NEWJOB; |
|
23 |
||
8 | 24 |
/* global */ |
18 | 25 |
m.u.newjob.command_size = strlen(command) + 1; /* add null */ |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
26 |
m.u.newjob.store_output = command_line.store_output; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
27 |
m.u.newjob.should_keep_finished = command_line.should_keep_finished; |
3 | 28 |
|
18 | 29 |
/* Send the message */ |
30 |
send_msg(server_socket, &m); |
|
31 |
||
32 |
/* Send the command */ |
|
33 |
send_bytes(server_socket, command, m.u.newjob.command_size); |
|
3 | 34 |
} |
35 |
||
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
35
diff
changeset
|
36 |
int c_wait_server_commands(const char *my_command) |
3 | 37 |
{ |
38 |
struct msg m; |
|
39 |
int res; |
|
40 |
||
41 |
while (1) |
|
42 |
{ |
|
9 | 43 |
res = recv(server_socket, &m, sizeof(m), 0); |
3 | 44 |
if(res == -1) |
9 | 45 |
{ |
3 | 46 |
perror("read"); |
9 | 47 |
exit(-1); |
48 |
} |
|
3 | 49 |
|
50 |
if (res == 0) |
|
51 |
break; |
|
9 | 52 |
if (res != sizeof(m)) |
53 |
{ |
|
54 |
fprintf(stderr, "c: recv() message size wrong: %i instead of %i\n", |
|
55 |
res, (int) sizeof(m)); |
|
56 |
} |
|
3 | 57 |
assert(res == sizeof(m)); |
58 |
if (m.type == NEWJOB_OK) |
|
59 |
; |
|
8 | 60 |
if (m.type == RUNJOB) |
5 | 61 |
{ |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
62 |
int errorlevel; |
22 | 63 |
/* This will send RUNJOB_OK */ |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
64 |
errorlevel = run_job(my_command); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
65 |
c_end_of_job(errorlevel); |
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
35
diff
changeset
|
66 |
return errorlevel; |
5 | 67 |
} |
68 |
} |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
35
diff
changeset
|
69 |
return -1; |
5 | 70 |
} |
71 |
||
72 |
void c_wait_server_lines() |
|
73 |
{ |
|
74 |
struct msg m; |
|
75 |
int res; |
|
76 |
||
77 |
while (1) |
|
78 |
{ |
|
21 | 79 |
res = recv_msg(server_socket, &m); |
5 | 80 |
if(res == -1) |
22 | 81 |
{ |
5 | 82 |
perror("read"); |
22 | 83 |
exit(-1); |
84 |
} |
|
5 | 85 |
|
86 |
if (res == 0) |
|
87 |
break; |
|
88 |
assert(res == sizeof(m)); |
|
89 |
if (m.type == LIST_LINE) |
|
90 |
{ |
|
21 | 91 |
char * buffer; |
92 |
buffer = (char *) malloc(m.u.line_size); |
|
93 |
recv_bytes(server_socket, buffer, m.u.line_size); |
|
94 |
printf("%s", buffer); |
|
95 |
free(buffer); |
|
5 | 96 |
} |
3 | 97 |
} |
98 |
} |
|
99 |
||
8 | 100 |
void c_list_jobs() |
3 | 101 |
{ |
102 |
struct msg m; |
|
103 |
int res; |
|
104 |
||
105 |
m.type = LIST; |
|
106 |
||
9 | 107 |
res = send(server_socket, &m, sizeof(m), 0); |
3 | 108 |
if(res == -1) |
9 | 109 |
perror("send"); |
3 | 110 |
} |
111 |
||
35 | 112 |
void c_send_runjob_ok(const char *ofname, int pid) |
22 | 113 |
{ |
114 |
struct msg m; |
|
115 |
||
116 |
/* Prepare the message */ |
|
117 |
m.type = RUNJOB_OK; |
|
32 | 118 |
m.u.output.store_output = command_line.store_output; |
35 | 119 |
m.u.output.pid = pid; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
120 |
if (command_line.store_output) |
32 | 121 |
m.u.output.ofilename_size = strlen(ofname) + 1; |
22 | 122 |
else |
32 | 123 |
m.u.output.ofilename_size = 0; |
22 | 124 |
|
125 |
send_msg(server_socket, &m); |
|
126 |
||
127 |
/* Send the filename */ |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
128 |
if (command_line.store_output) |
32 | 129 |
send_bytes(server_socket, ofname, m.u.output.ofilename_size); |
22 | 130 |
} |
131 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
132 |
static void c_end_of_job(int errorlevel) |
8 | 133 |
{ |
134 |
struct msg m; |
|
135 |
int res; |
|
136 |
||
137 |
m.type = ENDJOB; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
138 |
m.u.errorlevel = errorlevel; |
8 | 139 |
|
9 | 140 |
res = send(server_socket, &m, sizeof(m),0); |
8 | 141 |
if(res == -1) |
9 | 142 |
perror("send"); |
8 | 143 |
} |
144 |
||
44 | 145 |
void c_shutdown_server() |
1 | 146 |
{ |
2 | 147 |
struct msg m; |
1 | 148 |
|
31 | 149 |
m.type = KILL_SERVER; |
32 | 150 |
send_msg(server_socket, &m); |
1 | 151 |
} |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
152 |
|
44 | 153 |
void c_clear_finished() |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
154 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
155 |
struct msg m; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
156 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
157 |
m.type = CLEAR_FINISHED; |
32 | 158 |
send_msg(server_socket, &m); |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
159 |
} |
32 | 160 |
|
35 | 161 |
static char * get_output_file(int *pid) |
32 | 162 |
{ |
163 |
struct msg m; |
|
164 |
int res; |
|
165 |
char *string = 0; |
|
166 |
||
167 |
/* Send the request */ |
|
168 |
m.type = ASK_OUTPUT; |
|
169 |
m.u.jobid = command_line.jobid; |
|
170 |
send_msg(server_socket, &m); |
|
171 |
||
172 |
/* Receive the answer */ |
|
173 |
res = recv_msg(server_socket, &m); |
|
174 |
assert(res == sizeof(m)); |
|
175 |
switch(m.type) |
|
176 |
{ |
|
177 |
case ANSWER_OUTPUT: |
|
178 |
if (m.u.output.store_output) |
|
179 |
{ |
|
180 |
/* Receive the output file name */ |
|
181 |
string = (char *) malloc(m.u.output.ofilename_size); |
|
182 |
recv_bytes(server_socket, string, m.u.output.ofilename_size); |
|
35 | 183 |
*pid = m.u.output.pid; |
33 | 184 |
return string; |
32 | 185 |
} |
35 | 186 |
*pid = m.u.output.pid; |
187 |
return 0; |
|
32 | 188 |
/* WILL NOT GO FURTHER */ |
189 |
case LIST_LINE: /* Only ONE line accepted */ |
|
190 |
string = (char *) malloc(m.u.line_size); |
|
191 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
192 |
assert(res == m.u.line_size); |
|
193 |
fprintf(stderr, "Error in the request: %s", |
|
194 |
string); |
|
195 |
exit(-1); |
|
196 |
/* WILL NOT GO FURTHER */ |
|
44 | 197 |
default: |
198 |
fprintf(stderr, "Wrong internal message\n"); |
|
199 |
exit(-1); |
|
32 | 200 |
} |
33 | 201 |
/* This will never be reached */ |
202 |
return 0; |
|
32 | 203 |
} |
33 | 204 |
|
205 |
void c_tail() |
|
206 |
{ |
|
207 |
char *str; |
|
35 | 208 |
int pid; |
209 |
str = get_output_file(&pid); |
|
210 |
if (str == 0) |
|
211 |
{ |
|
212 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
213 |
exit(-1); |
|
214 |
} |
|
33 | 215 |
c_run_tail(str); |
216 |
} |
|
217 |
||
218 |
void c_cat() |
|
219 |
{ |
|
220 |
char *str; |
|
35 | 221 |
int pid; |
222 |
str = get_output_file(&pid); |
|
223 |
if (str == 0) |
|
224 |
{ |
|
225 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
226 |
exit(-1); |
|
227 |
} |
|
33 | 228 |
c_run_cat(str); |
229 |
} |
|
230 |
||
231 |
void c_show_output_file() |
|
232 |
{ |
|
233 |
char *str; |
|
35 | 234 |
int pid; |
235 |
/* This will exit if there is any error */ |
|
236 |
str = get_output_file(&pid); |
|
237 |
if (str == 0) |
|
238 |
{ |
|
239 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
240 |
exit(-1); |
|
241 |
} |
|
33 | 242 |
printf("%s\n", str); |
35 | 243 |
free(str); |
33 | 244 |
} |
35 | 245 |
|
246 |
void c_show_pid() |
|
247 |
{ |
|
248 |
char *str; |
|
249 |
int pid; |
|
250 |
/* This will exit if there is any error */ |
|
251 |
str = get_output_file(&pid); |
|
252 |
printf("%i\n", pid); |
|
253 |
} |
|
41 | 254 |
|
255 |
void c_remove_job() |
|
256 |
{ |
|
257 |
struct msg m; |
|
258 |
int res; |
|
259 |
char *string = 0; |
|
260 |
||
261 |
/* Send the request */ |
|
262 |
m.type = REMOVEJOB; |
|
263 |
m.u.jobid = command_line.jobid; |
|
264 |
send_msg(server_socket, &m); |
|
265 |
||
266 |
/* Receive the answer */ |
|
267 |
res = recv_msg(server_socket, &m); |
|
268 |
assert(res == sizeof(m)); |
|
269 |
switch(m.type) |
|
270 |
{ |
|
271 |
case REMOVEJOB_OK: |
|
272 |
return; |
|
273 |
/* WILL NOT GO FURTHER */ |
|
274 |
case LIST_LINE: /* Only ONE line accepted */ |
|
275 |
string = (char *) malloc(m.u.line_size); |
|
276 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
277 |
assert(res == m.u.line_size); |
|
278 |
fprintf(stderr, "Error in the request: %s", |
|
279 |
string); |
|
280 |
exit(-1); |
|
281 |
/* WILL NOT GO FURTHER */ |
|
44 | 282 |
default: |
283 |
fprintf(stderr, "Wrong internal message\n"); |
|
284 |
exit(-1); |
|
41 | 285 |
} |
286 |
/* This will never be reached */ |
|
287 |
} |
|
42 | 288 |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
289 |
/* Returns the errorlevel */ |
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
290 |
int c_wait_job() |
42 | 291 |
{ |
292 |
struct msg m; |
|
293 |
int res; |
|
294 |
char *string = 0; |
|
295 |
||
296 |
/* Send the request */ |
|
297 |
m.type = WAITJOB; |
|
298 |
m.u.jobid = command_line.jobid; |
|
299 |
send_msg(server_socket, &m); |
|
300 |
||
301 |
/* Receive the answer */ |
|
302 |
res = recv_msg(server_socket, &m); |
|
303 |
assert(res == sizeof(m)); |
|
304 |
switch(m.type) |
|
305 |
{ |
|
306 |
case WAITJOB_OK: |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
307 |
return m.u.errorlevel; |
42 | 308 |
/* WILL NOT GO FURTHER */ |
309 |
case LIST_LINE: /* Only ONE line accepted */ |
|
310 |
string = (char *) malloc(m.u.line_size); |
|
311 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
312 |
assert(res == m.u.line_size); |
|
313 |
fprintf(stderr, "Error in the request: %s", |
|
314 |
string); |
|
315 |
exit(-1); |
|
316 |
/* WILL NOT GO FURTHER */ |
|
44 | 317 |
default: |
318 |
fprintf(stderr, "Wrong internal message\n"); |
|
319 |
exit(-1); |
|
42 | 320 |
} |
321 |
/* This will never be reached */ |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
322 |
return -1; |
42 | 323 |
} |
53 | 324 |
|
325 |
void c_move_urgent() |
|
326 |
{ |
|
327 |
struct msg m; |
|
328 |
int res; |
|
329 |
char *string = 0; |
|
330 |
||
331 |
/* Send the request */ |
|
332 |
m.type = URGENT; |
|
333 |
m.u.jobid = command_line.jobid; |
|
334 |
send_msg(server_socket, &m); |
|
335 |
||
336 |
/* Receive the answer */ |
|
337 |
res = recv_msg(server_socket, &m); |
|
338 |
assert(res == sizeof(m)); |
|
339 |
switch(m.type) |
|
340 |
{ |
|
341 |
case URGENT_OK: |
|
342 |
return; |
|
343 |
/* WILL NOT GO FURTHER */ |
|
344 |
case LIST_LINE: /* Only ONE line accepted */ |
|
345 |
string = (char *) malloc(m.u.line_size); |
|
346 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
347 |
assert(res == m.u.line_size); |
|
348 |
fprintf(stderr, "Error in the request: %s", |
|
349 |
string); |
|
350 |
exit(-1); |
|
351 |
/* WILL NOT GO FURTHER */ |
|
352 |
default: |
|
353 |
fprintf(stderr, "Wrong internal message\n"); |
|
354 |
exit(-1); |
|
355 |
} |
|
356 |
/* This will never be reached */ |
|
357 |
return; |
|
358 |
} |