author | viric@llimona |
Sat, 09 Feb 2008 11:56:03 +0100 | |
changeset 196 | bca29e2a1a86 |
parent 180 | a35159b7009a |
child 203 | 664044b1de73 |
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 |
*/ |
|
9 | 7 |
#include <stdio.h> |
44 | 8 |
#include <string.h> |
147 | 9 |
#include <unistd.h> |
44 | 10 |
#include <stdlib.h> |
11 |
#include <sys/types.h> |
|
12 |
#include <sys/socket.h> |
|
146 | 13 |
#include <sys/time.h> |
3 | 14 |
#include "main.h" |
1 | 15 |
|
110 | 16 |
static void c_end_of_job(const struct Result *res); |
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); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
40 |
if(commandstring == NULL) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
41 |
error("Error in malloc for commandstring"); |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
42 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
43 |
/* Build the command */ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
44 |
strcpy(commandstring, array[0]); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
45 |
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
|
46 |
{ |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
47 |
strcat(commandstring, " "); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
48 |
strcat(commandstring, array[i]); |
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 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
51 |
return commandstring; |
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 |
|
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
54 |
void c_new_job() |
3 | 55 |
{ |
56 |
struct msg m; |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
57 |
char *new_command; |
147 | 58 |
char *myenv; |
3 | 59 |
|
60 |
m.type = NEWJOB; |
|
61 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
62 |
new_command = build_command_string(); |
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
63 |
|
147 | 64 |
myenv = get_environment(); |
65 |
||
8 | 66 |
/* global */ |
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
67 |
m.u.newjob.command_size = strlen(new_command) + 1; /* add null */ |
147 | 68 |
if (myenv) |
69 |
m.u.newjob.env_size = strlen(myenv) + 1; /* add null */ |
|
70 |
else |
|
71 |
m.u.newjob.env_size = 0; |
|
150 | 72 |
if (command_line.label) |
73 |
m.u.newjob.label_size = strlen(command_line.label) + 1; /* add null */ |
|
74 |
else |
|
75 |
m.u.newjob.label_size = 0; |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
76 |
m.u.newjob.store_output = command_line.store_output; |
155 | 77 |
m.u.newjob.depend = command_line.depend; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
78 |
m.u.newjob.should_keep_finished = command_line.should_keep_finished; |
147 | 79 |
m.u.newjob.command_size = strlen(new_command) + 1; /* add null */ |
3 | 80 |
|
18 | 81 |
/* Send the message */ |
82 |
send_msg(server_socket, &m); |
|
83 |
||
84 |
/* Send the command */ |
|
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
85 |
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
|
86 |
|
150 | 87 |
/* Send the label */ |
88 |
send_bytes(server_socket, command_line.label, m.u.newjob.label_size); |
|
89 |
||
147 | 90 |
/* Send the environment */ |
91 |
send_bytes(server_socket, myenv, m.u.newjob.env_size); |
|
92 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
93 |
free(new_command); |
147 | 94 |
free(myenv); |
3 | 95 |
} |
96 |
||
62 | 97 |
int c_wait_newjob_ok() |
98 |
{ |
|
99 |
struct msg m; |
|
100 |
int res; |
|
101 |
||
102 |
res = recv_msg(server_socket, &m); |
|
103 |
if(res == -1) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
104 |
error("Error in wait_newjob_ok"); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
105 |
if(m.type != NEWJOB_OK) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
106 |
error("Error getting the newjob_ok"); |
62 | 107 |
|
108 |
return m.u.jobid; |
|
109 |
} |
|
110 |
||
68
3ae9b38d7d30
Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents:
63
diff
changeset
|
111 |
int c_wait_server_commands() |
3 | 112 |
{ |
113 |
struct msg m; |
|
114 |
int res; |
|
115 |
||
116 |
while (1) |
|
117 |
{ |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
118 |
res = recv_msg(server_socket, &m); |
3 | 119 |
if(res == -1) |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
120 |
error("Error in wait_server_commands"); |
3 | 121 |
|
122 |
if (res == 0) |
|
123 |
break; |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
124 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
125 |
error("Error in wait_server_commands"); |
8 | 126 |
if (m.type == RUNJOB) |
5 | 127 |
{ |
110 | 128 |
struct Result res; |
156 | 129 |
res.skipped = 0; |
155 | 130 |
/* These will send RUNJOB_OK */ |
156 | 131 |
if (command_line.depend && m.u.last_errorlevel != 0) |
132 |
{ |
|
133 |
res.errorlevel = -1; |
|
134 |
res.user_ms = 0.; |
|
135 |
res.system_ms = 0.; |
|
136 |
res.real_ms = 0.; |
|
137 |
res.skipped = 1; |
|
138 |
c_send_runjob_ok(0, -1); |
|
139 |
} |
|
140 |
else |
|
141 |
run_job(&res); |
|
110 | 142 |
c_end_of_job(&res); |
143 |
return res.errorlevel; |
|
5 | 144 |
} |
145 |
} |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
35
diff
changeset
|
146 |
return -1; |
5 | 147 |
} |
148 |
||
149 |
void c_wait_server_lines() |
|
150 |
{ |
|
151 |
struct msg m; |
|
152 |
int res; |
|
153 |
||
154 |
while (1) |
|
155 |
{ |
|
21 | 156 |
res = recv_msg(server_socket, &m); |
5 | 157 |
if(res == -1) |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
158 |
error("Error in wait_server_lines"); |
5 | 159 |
|
160 |
if (res == 0) |
|
161 |
break; |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
162 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
163 |
error("Error in wait_server_lines 2"); |
5 | 164 |
if (m.type == LIST_LINE) |
165 |
{ |
|
21 | 166 |
char * buffer; |
146 | 167 |
buffer = (char *) malloc(m.u.size); |
168 |
recv_bytes(server_socket, buffer, m.u.size); |
|
21 | 169 |
printf("%s", buffer); |
170 |
free(buffer); |
|
5 | 171 |
} |
3 | 172 |
} |
173 |
} |
|
174 |
||
8 | 175 |
void c_list_jobs() |
3 | 176 |
{ |
177 |
struct msg m; |
|
178 |
||
179 |
m.type = LIST; |
|
180 |
||
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
181 |
send_msg(server_socket, &m); |
3 | 182 |
} |
183 |
||
146 | 184 |
void c_show_info() |
185 |
{ |
|
186 |
struct msg m; |
|
187 |
int res; |
|
188 |
||
189 |
m.type = INFO; |
|
190 |
m.u.jobid = command_line.jobid; |
|
191 |
||
192 |
send_msg(server_socket, &m); |
|
193 |
||
194 |
while (1) |
|
195 |
{ |
|
196 |
res = recv_msg(server_socket, &m); |
|
197 |
if(res == -1) |
|
198 |
error("Error in wait_server_lines"); |
|
199 |
||
200 |
if (res == 0) |
|
201 |
break; |
|
202 |
if(res != sizeof(m)) |
|
203 |
error("Error in wait_server_lines 2"); |
|
204 |
if (m.type == INFO_DATA) |
|
205 |
{ |
|
206 |
char * buffer; |
|
207 |
enum { DSIZE = 1000 }; |
|
208 |
||
147 | 209 |
/* We're going to output data using the stdout fd */ |
210 |
fflush(stdout); |
|
146 | 211 |
buffer = (char *) malloc(DSIZE); |
212 |
do |
|
213 |
{ |
|
214 |
res = recv(server_socket, buffer, DSIZE, 0); |
|
215 |
if (res > 0) |
|
147 | 216 |
write(1, buffer, res); |
146 | 217 |
} while(res > 0); |
218 |
free(buffer); |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
||
35 | 223 |
void c_send_runjob_ok(const char *ofname, int pid) |
22 | 224 |
{ |
225 |
struct msg m; |
|
226 |
||
227 |
/* Prepare the message */ |
|
228 |
m.type = RUNJOB_OK; |
|
155 | 229 |
if (ofname) /* ofname == 0, skipped execution */ |
230 |
m.u.output.store_output = command_line.store_output; |
|
231 |
else |
|
232 |
m.u.output.store_output = 0; |
|
35 | 233 |
m.u.output.pid = pid; |
155 | 234 |
if (m.u.output.store_output) |
32 | 235 |
m.u.output.ofilename_size = strlen(ofname) + 1; |
22 | 236 |
else |
32 | 237 |
m.u.output.ofilename_size = 0; |
22 | 238 |
|
239 |
send_msg(server_socket, &m); |
|
240 |
||
241 |
/* Send the filename */ |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
27
diff
changeset
|
242 |
if (command_line.store_output) |
32 | 243 |
send_bytes(server_socket, ofname, m.u.output.ofilename_size); |
22 | 244 |
} |
245 |
||
110 | 246 |
static void c_end_of_job(const struct Result *res) |
8 | 247 |
{ |
248 |
struct msg m; |
|
249 |
||
250 |
m.type = ENDJOB; |
|
110 | 251 |
m.u.result = *res; /* struct copy */ |
8 | 252 |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
253 |
send_msg(server_socket, &m); |
8 | 254 |
} |
255 |
||
44 | 256 |
void c_shutdown_server() |
1 | 257 |
{ |
2 | 258 |
struct msg m; |
1 | 259 |
|
31 | 260 |
m.type = KILL_SERVER; |
32 | 261 |
send_msg(server_socket, &m); |
1 | 262 |
} |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
263 |
|
44 | 264 |
void c_clear_finished() |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
265 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
266 |
struct msg m; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
267 |
|
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
268 |
m.type = CLEAR_FINISHED; |
32 | 269 |
send_msg(server_socket, &m); |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
270 |
} |
32 | 271 |
|
35 | 272 |
static char * get_output_file(int *pid) |
32 | 273 |
{ |
274 |
struct msg m; |
|
275 |
int res; |
|
276 |
char *string = 0; |
|
277 |
||
278 |
/* Send the request */ |
|
279 |
m.type = ASK_OUTPUT; |
|
280 |
m.u.jobid = command_line.jobid; |
|
281 |
send_msg(server_socket, &m); |
|
282 |
||
283 |
/* Receive the answer */ |
|
284 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
285 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
286 |
error("Error in get_output_file"); |
32 | 287 |
switch(m.type) |
288 |
{ |
|
289 |
case ANSWER_OUTPUT: |
|
290 |
if (m.u.output.store_output) |
|
291 |
{ |
|
292 |
/* Receive the output file name */ |
|
146 | 293 |
string = 0; |
294 |
if (m.u.output.ofilename_size > 0) |
|
295 |
{ |
|
296 |
string = (char *) malloc(m.u.output.ofilename_size); |
|
297 |
recv_bytes(server_socket, string, m.u.output.ofilename_size); |
|
298 |
} |
|
35 | 299 |
*pid = m.u.output.pid; |
33 | 300 |
return string; |
32 | 301 |
} |
35 | 302 |
*pid = m.u.output.pid; |
303 |
return 0; |
|
32 | 304 |
/* WILL NOT GO FURTHER */ |
305 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 306 |
string = (char *) malloc(m.u.size); |
307 |
res = recv_bytes(server_socket, string, m.u.size); |
|
308 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
309 |
error("Error in get_output_file line size"); |
32 | 310 |
fprintf(stderr, "Error in the request: %s", |
311 |
string); |
|
312 |
exit(-1); |
|
313 |
/* WILL NOT GO FURTHER */ |
|
44 | 314 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
315 |
warning("Wrong internal message in get_output_file line size"); |
32 | 316 |
} |
33 | 317 |
/* This will never be reached */ |
318 |
return 0; |
|
32 | 319 |
} |
33 | 320 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
321 |
int c_tail() |
33 | 322 |
{ |
323 |
char *str; |
|
35 | 324 |
int pid; |
325 |
str = get_output_file(&pid); |
|
326 |
if (str == 0) |
|
327 |
{ |
|
328 |
fprintf(stderr, "The output is not stored. Cannot tail.\n"); |
|
329 |
exit(-1); |
|
330 |
} |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
331 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
332 |
c_wait_job_send(); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
333 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
334 |
return tail_file(str); |
33 | 335 |
} |
336 |
||
337 |
void c_cat() |
|
338 |
{ |
|
339 |
char *str; |
|
35 | 340 |
int pid; |
341 |
str = get_output_file(&pid); |
|
342 |
if (str == 0) |
|
343 |
{ |
|
153 | 344 |
fprintf(stderr, "The output is not stored. Cannot cat.\n"); |
35 | 345 |
exit(-1); |
346 |
} |
|
33 | 347 |
c_run_cat(str); |
348 |
} |
|
349 |
||
350 |
void c_show_output_file() |
|
351 |
{ |
|
352 |
char *str; |
|
35 | 353 |
int pid; |
354 |
/* This will exit if there is any error */ |
|
355 |
str = get_output_file(&pid); |
|
356 |
if (str == 0) |
|
357 |
{ |
|
146 | 358 |
fprintf(stderr, "The output is not stored.\n"); |
35 | 359 |
exit(-1); |
360 |
} |
|
33 | 361 |
printf("%s\n", str); |
35 | 362 |
free(str); |
33 | 363 |
} |
35 | 364 |
|
365 |
void c_show_pid() |
|
366 |
{ |
|
367 |
char *str; |
|
368 |
int pid; |
|
369 |
/* This will exit if there is any error */ |
|
370 |
str = get_output_file(&pid); |
|
371 |
printf("%i\n", pid); |
|
372 |
} |
|
41 | 373 |
|
374 |
void c_remove_job() |
|
375 |
{ |
|
376 |
struct msg m; |
|
377 |
int res; |
|
378 |
char *string = 0; |
|
379 |
||
380 |
/* Send the request */ |
|
381 |
m.type = REMOVEJOB; |
|
382 |
m.u.jobid = command_line.jobid; |
|
383 |
send_msg(server_socket, &m); |
|
384 |
||
385 |
/* Receive the answer */ |
|
386 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
387 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
388 |
error("Error in remove_job"); |
41 | 389 |
switch(m.type) |
390 |
{ |
|
391 |
case REMOVEJOB_OK: |
|
392 |
return; |
|
393 |
/* WILL NOT GO FURTHER */ |
|
394 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 395 |
string = (char *) malloc(m.u.size); |
396 |
res = recv_bytes(server_socket, string, m.u.size); |
|
41 | 397 |
fprintf(stderr, "Error in the request: %s", |
398 |
string); |
|
399 |
exit(-1); |
|
400 |
/* WILL NOT GO FURTHER */ |
|
44 | 401 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
402 |
warning("Wrong internal message in remove_job"); |
41 | 403 |
} |
404 |
/* This will never be reached */ |
|
405 |
} |
|
42 | 406 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
407 |
int c_wait_job_recv() |
42 | 408 |
{ |
409 |
struct msg m; |
|
410 |
int res; |
|
411 |
char *string = 0; |
|
412 |
||
413 |
/* Receive the answer */ |
|
414 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
415 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
416 |
error("Error in wait_job"); |
42 | 417 |
switch(m.type) |
418 |
{ |
|
419 |
case WAITJOB_OK: |
|
110 | 420 |
return m.u.result.errorlevel; |
42 | 421 |
/* WILL NOT GO FURTHER */ |
422 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 423 |
string = (char *) malloc(m.u.size); |
424 |
res = recv_bytes(server_socket, string, m.u.size); |
|
425 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
426 |
error("Error in wait_job - line size"); |
42 | 427 |
fprintf(stderr, "Error in the request: %s", |
428 |
string); |
|
429 |
exit(-1); |
|
430 |
/* WILL NOT GO FURTHER */ |
|
44 | 431 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
432 |
warning("Wrong internal message in c_wait_job"); |
42 | 433 |
} |
434 |
/* This will never be reached */ |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
435 |
return -1; |
42 | 436 |
} |
53 | 437 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
438 |
void c_wait_job_send() |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
439 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
440 |
struct msg m; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
441 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
442 |
/* Send the request */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
443 |
m.type = WAITJOB; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
444 |
m.u.jobid = command_line.jobid; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
445 |
send_msg(server_socket, &m); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
446 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
447 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
448 |
/* Returns the errorlevel */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
449 |
int c_wait_job() |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
450 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
451 |
c_wait_job_send(); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
452 |
return c_wait_job_recv(); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
453 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
454 |
|
53 | 455 |
void c_move_urgent() |
456 |
{ |
|
457 |
struct msg m; |
|
458 |
int res; |
|
459 |
char *string = 0; |
|
460 |
||
461 |
/* Send the request */ |
|
462 |
m.type = URGENT; |
|
463 |
m.u.jobid = command_line.jobid; |
|
464 |
send_msg(server_socket, &m); |
|
465 |
||
466 |
/* Receive the answer */ |
|
467 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
468 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
469 |
error("Error in move_urgent"); |
53 | 470 |
switch(m.type) |
471 |
{ |
|
472 |
case URGENT_OK: |
|
473 |
return; |
|
474 |
/* WILL NOT GO FURTHER */ |
|
475 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 476 |
string = (char *) malloc(m.u.size); |
477 |
res = recv_bytes(server_socket, string, m.u.size); |
|
478 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
479 |
error("Error in move_urgent - line size"); |
53 | 480 |
fprintf(stderr, "Error in the request: %s", |
481 |
string); |
|
482 |
exit(-1); |
|
483 |
/* WILL NOT GO FURTHER */ |
|
484 |
default: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
485 |
warning("Wrong internal message in move_urgent"); |
53 | 486 |
} |
487 |
/* This will never be reached */ |
|
488 |
return; |
|
489 |
} |
|
63 | 490 |
|
491 |
void c_get_state() |
|
492 |
{ |
|
493 |
struct msg m; |
|
494 |
int res; |
|
495 |
char *string = 0; |
|
496 |
||
497 |
/* Send the request */ |
|
498 |
m.type = GET_STATE; |
|
499 |
m.u.jobid = command_line.jobid; |
|
500 |
send_msg(server_socket, &m); |
|
501 |
||
502 |
/* Receive the answer */ |
|
503 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
504 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
505 |
error("Error in get_state - line size"); |
63 | 506 |
switch(m.type) |
507 |
{ |
|
508 |
case ANSWER_STATE: |
|
509 |
printf("%s\n", jstate2string(m.u.state)); |
|
510 |
return; |
|
511 |
/* WILL NOT GO FURTHER */ |
|
512 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 513 |
string = (char *) malloc(m.u.size); |
514 |
res = recv_bytes(server_socket, string, m.u.size); |
|
515 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
516 |
error("Error in get_state - line size"); |
63 | 517 |
fprintf(stderr, "Error in the request: %s", |
518 |
string); |
|
519 |
exit(-1); |
|
520 |
/* WILL NOT GO FURTHER */ |
|
521 |
default: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
522 |
warning("Wrong internal message in get_state"); |
63 | 523 |
} |
524 |
/* This will never be reached */ |
|
525 |
return; |
|
526 |
} |
|
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
527 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
528 |
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
|
529 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
530 |
struct msg m; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
531 |
int res; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
532 |
char *string = 0; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
533 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
534 |
/* Send the request */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
535 |
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
|
536 |
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
|
537 |
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
|
538 |
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
|
539 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
540 |
/* Receive the answer */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
541 |
res = recv_msg(server_socket, &m); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
542 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
543 |
error("Error in swap_jobs"); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
544 |
switch(m.type) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
545 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
546 |
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
|
547 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
548 |
/* 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
|
549 |
case LIST_LINE: /* Only ONE line accepted */ |
146 | 550 |
string = (char *) malloc(m.u.size); |
551 |
res = recv_bytes(server_socket, string, m.u.size); |
|
552 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
553 |
error("Error in swap_jobs - line size"); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
554 |
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
|
555 |
string); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
556 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
557 |
/* 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
|
558 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
559 |
warning("Wrong internal message in swap_jobs"); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
560 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
561 |
/* 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
|
562 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
563 |
} |