author | viric@llimona |
Mon, 02 Apr 2007 17:53:52 +0200 | |
changeset 74 | bb1b20d79acf |
parent 71 | 531666e297d7 |
child 92 | 05004c52ecff |
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 |
|
71 | 18 |
char *build_command_string() |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
19 |
{ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
20 |
int size; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
21 |
int i; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
22 |
int num; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
23 |
char **array; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
24 |
char *commandstring; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
25 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
26 |
size = 0; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
27 |
num = command_line.command.num; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
28 |
array = command_line.command.array; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
29 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
30 |
/* Count bytes needed */ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
31 |
for (i = 0; i < num; ++i) |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
32 |
{ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
33 |
/* The '1' is for spaces, and at the last i, |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
34 |
* for the null character */ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
35 |
size = size + strlen(array[i]) + 1; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
36 |
} |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
37 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
38 |
/* Alloc */ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
39 |
commandstring = (char *) malloc(size); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
40 |
assert(commandstring != NULL); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
41 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
42 |
/* Build the command */ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
43 |
strcpy(commandstring, array[0]); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
44 |
for (i = 1; i < num; ++i) |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
45 |
{ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
46 |
strcat(commandstring, " "); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
47 |
strcat(commandstring, array[i]); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
48 |
} |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
49 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
50 |
return commandstring; |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
51 |
} |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
52 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
53 |
void c_new_job() |
3 | 54 |
{ |
55 |
struct msg m; |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
56 |
char *new_command; |
3 | 57 |
|
58 |
m.type = NEWJOB; |
|
59 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
60 |
new_command = build_command_string(); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
61 |
|
8 | 62 |
/* global */ |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
63 |
m.u.newjob.command_size = strlen(new_command) + 1; /* add null */ |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
64 |
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
|
65 |
m.u.newjob.should_keep_finished = command_line.should_keep_finished; |
3 | 66 |
|
18 | 67 |
/* Send the message */ |
68 |
send_msg(server_socket, &m); |
|
69 |
||
70 |
/* Send the command */ |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
71 |
send_bytes(server_socket, new_command, m.u.newjob.command_size); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
72 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
73 |
free(new_command); |
3 | 74 |
} |
75 |
||
62 | 76 |
int c_wait_newjob_ok() |
77 |
{ |
|
78 |
struct msg m; |
|
79 |
int res; |
|
80 |
||
81 |
res = recv_msg(server_socket, &m); |
|
82 |
if(res == -1) |
|
83 |
{ |
|
84 |
perror("read"); |
|
85 |
exit(-1); |
|
86 |
} |
|
87 |
assert(m.type = NEWJOB_OK); |
|
88 |
||
89 |
return m.u.jobid; |
|
90 |
} |
|
91 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
92 |
int c_wait_server_commands() |
3 | 93 |
{ |
94 |
struct msg m; |
|
95 |
int res; |
|
96 |
||
97 |
while (1) |
|
98 |
{ |
|
9 | 99 |
res = recv(server_socket, &m, sizeof(m), 0); |
3 | 100 |
if(res == -1) |
9 | 101 |
{ |
3 | 102 |
perror("read"); |
9 | 103 |
exit(-1); |
104 |
} |
|
3 | 105 |
|
106 |
if (res == 0) |
|
107 |
break; |
|
9 | 108 |
if (res != sizeof(m)) |
109 |
{ |
|
110 |
fprintf(stderr, "c: recv() message size wrong: %i instead of %i\n", |
|
111 |
res, (int) sizeof(m)); |
|
112 |
} |
|
3 | 113 |
assert(res == sizeof(m)); |
8 | 114 |
if (m.type == RUNJOB) |
5 | 115 |
{ |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
116 |
int errorlevel; |
22 | 117 |
/* This will send RUNJOB_OK */ |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
118 |
errorlevel = run_job(); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
119 |
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
|
120 |
return errorlevel; |
5 | 121 |
} |
122 |
} |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
35
diff
changeset
|
123 |
return -1; |
5 | 124 |
} |
125 |
||
126 |
void c_wait_server_lines() |
|
127 |
{ |
|
128 |
struct msg m; |
|
129 |
int res; |
|
130 |
||
131 |
while (1) |
|
132 |
{ |
|
21 | 133 |
res = recv_msg(server_socket, &m); |
5 | 134 |
if(res == -1) |
22 | 135 |
{ |
5 | 136 |
perror("read"); |
22 | 137 |
exit(-1); |
138 |
} |
|
5 | 139 |
|
140 |
if (res == 0) |
|
141 |
break; |
|
142 |
assert(res == sizeof(m)); |
|
143 |
if (m.type == LIST_LINE) |
|
144 |
{ |
|
21 | 145 |
char * buffer; |
146 |
buffer = (char *) malloc(m.u.line_size); |
|
147 |
recv_bytes(server_socket, buffer, m.u.line_size); |
|
148 |
printf("%s", buffer); |
|
149 |
free(buffer); |
|
5 | 150 |
} |
3 | 151 |
} |
152 |
} |
|
153 |
||
8 | 154 |
void c_list_jobs() |
3 | 155 |
{ |
156 |
struct msg m; |
|
157 |
int res; |
|
158 |
||
159 |
m.type = LIST; |
|
160 |
||
9 | 161 |
res = send(server_socket, &m, sizeof(m), 0); |
3 | 162 |
if(res == -1) |
9 | 163 |
perror("send"); |
3 | 164 |
} |
165 |
||
35 | 166 |
void c_send_runjob_ok(const char *ofname, int pid) |
22 | 167 |
{ |
168 |
struct msg m; |
|
169 |
||
170 |
/* Prepare the message */ |
|
171 |
m.type = RUNJOB_OK; |
|
32 | 172 |
m.u.output.store_output = command_line.store_output; |
35 | 173 |
m.u.output.pid = pid; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
174 |
if (command_line.store_output) |
32 | 175 |
m.u.output.ofilename_size = strlen(ofname) + 1; |
22 | 176 |
else |
32 | 177 |
m.u.output.ofilename_size = 0; |
22 | 178 |
|
179 |
send_msg(server_socket, &m); |
|
180 |
||
181 |
/* Send the filename */ |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
182 |
if (command_line.store_output) |
32 | 183 |
send_bytes(server_socket, ofname, m.u.output.ofilename_size); |
22 | 184 |
} |
185 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
186 |
static void c_end_of_job(int errorlevel) |
8 | 187 |
{ |
188 |
struct msg m; |
|
189 |
int res; |
|
190 |
||
191 |
m.type = ENDJOB; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
192 |
m.u.errorlevel = errorlevel; |
8 | 193 |
|
9 | 194 |
res = send(server_socket, &m, sizeof(m),0); |
8 | 195 |
if(res == -1) |
9 | 196 |
perror("send"); |
8 | 197 |
} |
198 |
||
44 | 199 |
void c_shutdown_server() |
1 | 200 |
{ |
2 | 201 |
struct msg m; |
1 | 202 |
|
31 | 203 |
m.type = KILL_SERVER; |
32 | 204 |
send_msg(server_socket, &m); |
1 | 205 |
} |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
206 |
|
44 | 207 |
void c_clear_finished() |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
208 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
209 |
struct msg m; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
210 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
211 |
m.type = CLEAR_FINISHED; |
32 | 212 |
send_msg(server_socket, &m); |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
213 |
} |
32 | 214 |
|
35 | 215 |
static char * get_output_file(int *pid) |
32 | 216 |
{ |
217 |
struct msg m; |
|
218 |
int res; |
|
219 |
char *string = 0; |
|
220 |
||
221 |
/* Send the request */ |
|
222 |
m.type = ASK_OUTPUT; |
|
223 |
m.u.jobid = command_line.jobid; |
|
224 |
send_msg(server_socket, &m); |
|
225 |
||
226 |
/* Receive the answer */ |
|
227 |
res = recv_msg(server_socket, &m); |
|
228 |
assert(res == sizeof(m)); |
|
229 |
switch(m.type) |
|
230 |
{ |
|
231 |
case ANSWER_OUTPUT: |
|
232 |
if (m.u.output.store_output) |
|
233 |
{ |
|
234 |
/* Receive the output file name */ |
|
235 |
string = (char *) malloc(m.u.output.ofilename_size); |
|
236 |
recv_bytes(server_socket, string, m.u.output.ofilename_size); |
|
35 | 237 |
*pid = m.u.output.pid; |
33 | 238 |
return string; |
32 | 239 |
} |
35 | 240 |
*pid = m.u.output.pid; |
241 |
return 0; |
|
32 | 242 |
/* WILL NOT GO FURTHER */ |
243 |
case LIST_LINE: /* Only ONE line accepted */ |
|
244 |
string = (char *) malloc(m.u.line_size); |
|
245 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
246 |
assert(res == m.u.line_size); |
|
247 |
fprintf(stderr, "Error in the request: %s", |
|
248 |
string); |
|
249 |
exit(-1); |
|
250 |
/* WILL NOT GO FURTHER */ |
|
44 | 251 |
default: |
252 |
fprintf(stderr, "Wrong internal message\n"); |
|
253 |
exit(-1); |
|
32 | 254 |
} |
33 | 255 |
/* This will never be reached */ |
256 |
return 0; |
|
32 | 257 |
} |
33 | 258 |
|
259 |
void c_tail() |
|
260 |
{ |
|
261 |
char *str; |
|
35 | 262 |
int pid; |
263 |
str = get_output_file(&pid); |
|
264 |
if (str == 0) |
|
265 |
{ |
|
266 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
267 |
exit(-1); |
|
268 |
} |
|
33 | 269 |
c_run_tail(str); |
270 |
} |
|
271 |
||
272 |
void c_cat() |
|
273 |
{ |
|
274 |
char *str; |
|
35 | 275 |
int pid; |
276 |
str = get_output_file(&pid); |
|
277 |
if (str == 0) |
|
278 |
{ |
|
279 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
280 |
exit(-1); |
|
281 |
} |
|
33 | 282 |
c_run_cat(str); |
283 |
} |
|
284 |
||
285 |
void c_show_output_file() |
|
286 |
{ |
|
287 |
char *str; |
|
35 | 288 |
int pid; |
289 |
/* This will exit if there is any error */ |
|
290 |
str = get_output_file(&pid); |
|
291 |
if (str == 0) |
|
292 |
{ |
|
293 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
294 |
exit(-1); |
|
295 |
} |
|
33 | 296 |
printf("%s\n", str); |
35 | 297 |
free(str); |
33 | 298 |
} |
35 | 299 |
|
300 |
void c_show_pid() |
|
301 |
{ |
|
302 |
char *str; |
|
303 |
int pid; |
|
304 |
/* This will exit if there is any error */ |
|
305 |
str = get_output_file(&pid); |
|
306 |
printf("%i\n", pid); |
|
307 |
} |
|
41 | 308 |
|
309 |
void c_remove_job() |
|
310 |
{ |
|
311 |
struct msg m; |
|
312 |
int res; |
|
313 |
char *string = 0; |
|
314 |
||
315 |
/* Send the request */ |
|
316 |
m.type = REMOVEJOB; |
|
317 |
m.u.jobid = command_line.jobid; |
|
318 |
send_msg(server_socket, &m); |
|
319 |
||
320 |
/* Receive the answer */ |
|
321 |
res = recv_msg(server_socket, &m); |
|
322 |
assert(res == sizeof(m)); |
|
323 |
switch(m.type) |
|
324 |
{ |
|
325 |
case REMOVEJOB_OK: |
|
326 |
return; |
|
327 |
/* WILL NOT GO FURTHER */ |
|
328 |
case LIST_LINE: /* Only ONE line accepted */ |
|
329 |
string = (char *) malloc(m.u.line_size); |
|
330 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
331 |
assert(res == m.u.line_size); |
|
332 |
fprintf(stderr, "Error in the request: %s", |
|
333 |
string); |
|
334 |
exit(-1); |
|
335 |
/* WILL NOT GO FURTHER */ |
|
44 | 336 |
default: |
337 |
fprintf(stderr, "Wrong internal message\n"); |
|
338 |
exit(-1); |
|
41 | 339 |
} |
340 |
/* This will never be reached */ |
|
341 |
} |
|
42 | 342 |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
343 |
/* Returns the errorlevel */ |
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
344 |
int c_wait_job() |
42 | 345 |
{ |
346 |
struct msg m; |
|
347 |
int res; |
|
348 |
char *string = 0; |
|
349 |
||
350 |
/* Send the request */ |
|
351 |
m.type = WAITJOB; |
|
352 |
m.u.jobid = command_line.jobid; |
|
353 |
send_msg(server_socket, &m); |
|
354 |
||
355 |
/* Receive the answer */ |
|
356 |
res = recv_msg(server_socket, &m); |
|
357 |
assert(res == sizeof(m)); |
|
358 |
switch(m.type) |
|
359 |
{ |
|
360 |
case WAITJOB_OK: |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
361 |
return m.u.errorlevel; |
42 | 362 |
/* WILL NOT GO FURTHER */ |
363 |
case LIST_LINE: /* Only ONE line accepted */ |
|
364 |
string = (char *) malloc(m.u.line_size); |
|
365 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
366 |
assert(res == m.u.line_size); |
|
367 |
fprintf(stderr, "Error in the request: %s", |
|
368 |
string); |
|
369 |
exit(-1); |
|
370 |
/* WILL NOT GO FURTHER */ |
|
44 | 371 |
default: |
372 |
fprintf(stderr, "Wrong internal message\n"); |
|
373 |
exit(-1); |
|
42 | 374 |
} |
375 |
/* This will never be reached */ |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
376 |
return -1; |
42 | 377 |
} |
53 | 378 |
|
379 |
void c_move_urgent() |
|
380 |
{ |
|
381 |
struct msg m; |
|
382 |
int res; |
|
383 |
char *string = 0; |
|
384 |
||
385 |
/* Send the request */ |
|
386 |
m.type = URGENT; |
|
387 |
m.u.jobid = command_line.jobid; |
|
388 |
send_msg(server_socket, &m); |
|
389 |
||
390 |
/* Receive the answer */ |
|
391 |
res = recv_msg(server_socket, &m); |
|
392 |
assert(res == sizeof(m)); |
|
393 |
switch(m.type) |
|
394 |
{ |
|
395 |
case URGENT_OK: |
|
396 |
return; |
|
397 |
/* WILL NOT GO FURTHER */ |
|
398 |
case LIST_LINE: /* Only ONE line accepted */ |
|
399 |
string = (char *) malloc(m.u.line_size); |
|
400 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
401 |
assert(res == m.u.line_size); |
|
402 |
fprintf(stderr, "Error in the request: %s", |
|
403 |
string); |
|
404 |
exit(-1); |
|
405 |
/* WILL NOT GO FURTHER */ |
|
406 |
default: |
|
407 |
fprintf(stderr, "Wrong internal message\n"); |
|
408 |
exit(-1); |
|
409 |
} |
|
410 |
/* This will never be reached */ |
|
411 |
return; |
|
412 |
} |
|
63 | 413 |
|
414 |
void c_get_state() |
|
415 |
{ |
|
416 |
struct msg m; |
|
417 |
int res; |
|
418 |
char *string = 0; |
|
419 |
||
420 |
/* Send the request */ |
|
421 |
m.type = GET_STATE; |
|
422 |
m.u.jobid = command_line.jobid; |
|
423 |
send_msg(server_socket, &m); |
|
424 |
||
425 |
/* Receive the answer */ |
|
426 |
res = recv_msg(server_socket, &m); |
|
427 |
assert(res == sizeof(m)); |
|
428 |
switch(m.type) |
|
429 |
{ |
|
430 |
case ANSWER_STATE: |
|
431 |
printf("%s\n", jstate2string(m.u.state)); |
|
432 |
return; |
|
433 |
/* WILL NOT GO FURTHER */ |
|
434 |
case LIST_LINE: /* Only ONE line accepted */ |
|
435 |
string = (char *) malloc(m.u.line_size); |
|
436 |
res = recv_bytes(server_socket, string, m.u.line_size); |
|
437 |
assert(res == m.u.line_size); |
|
438 |
fprintf(stderr, "Error in the request: %s", |
|
439 |
string); |
|
440 |
exit(-1); |
|
441 |
/* WILL NOT GO FURTHER */ |
|
442 |
default: |
|
443 |
fprintf(stderr, "Wrong internal message\n"); |
|
444 |
exit(-1); |
|
445 |
} |
|
446 |
/* This will never be reached */ |
|
447 |
return; |
|
448 |
} |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
449 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
450 |
void c_swap_jobs() |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
451 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
452 |
struct msg m; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
453 |
int res; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
454 |
char *string = 0; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
455 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
456 |
/* Send the request */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
457 |
m.type = SWAP_JOBS; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
458 |
m.u.swap.jobid1 = command_line.jobid; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
459 |
m.u.swap.jobid2 = command_line.jobid2; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
460 |
send_msg(server_socket, &m); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
461 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
462 |
/* Receive the answer */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
463 |
res = recv_msg(server_socket, &m); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
464 |
assert(res == sizeof(m)); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
465 |
switch(m.type) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
466 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
467 |
case SWAP_JOBS_OK: |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
468 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
469 |
/* WILL NOT GO FURTHER */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
470 |
case LIST_LINE: /* Only ONE line accepted */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
471 |
string = (char *) malloc(m.u.line_size); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
472 |
res = recv_bytes(server_socket, string, m.u.line_size); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
473 |
assert(res == m.u.line_size); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
474 |
fprintf(stderr, "Error in the request: %s", |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
475 |
string); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
476 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
477 |
/* WILL NOT GO FURTHER */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
478 |
default: |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
479 |
fprintf(stderr, "Wrong internal message\n"); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
480 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
481 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
482 |
/* This will never be reached */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
483 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
484 |
} |