author | viric@mandarina |
Mon, 05 May 2008 23:14:22 +0200 | |
changeset 216 | b4476e12a717 |
parent 203 | 664044b1de73 |
child 219 | c24a1f5c1715 |
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 |
|
203
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
180
diff
changeset
|
334 |
return tail_file(str, 10 /* Last lines to show */); |
33 | 335 |
} |
336 |
||
203
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
180
diff
changeset
|
337 |
int c_cat() |
33 | 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 |
} |
|
203
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
180
diff
changeset
|
347 |
c_wait_job_send(); |
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
180
diff
changeset
|
348 |
|
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
180
diff
changeset
|
349 |
return tail_file(str, -1 /* All the lines */); |
33 | 350 |
} |
351 |
||
352 |
void c_show_output_file() |
|
353 |
{ |
|
354 |
char *str; |
|
35 | 355 |
int pid; |
356 |
/* This will exit if there is any error */ |
|
357 |
str = get_output_file(&pid); |
|
358 |
if (str == 0) |
|
359 |
{ |
|
146 | 360 |
fprintf(stderr, "The output is not stored.\n"); |
35 | 361 |
exit(-1); |
362 |
} |
|
33 | 363 |
printf("%s\n", str); |
35 | 364 |
free(str); |
33 | 365 |
} |
35 | 366 |
|
367 |
void c_show_pid() |
|
368 |
{ |
|
369 |
char *str; |
|
370 |
int pid; |
|
371 |
/* This will exit if there is any error */ |
|
372 |
str = get_output_file(&pid); |
|
373 |
printf("%i\n", pid); |
|
374 |
} |
|
41 | 375 |
|
376 |
void c_remove_job() |
|
377 |
{ |
|
378 |
struct msg m; |
|
379 |
int res; |
|
380 |
char *string = 0; |
|
381 |
||
382 |
/* Send the request */ |
|
383 |
m.type = REMOVEJOB; |
|
384 |
m.u.jobid = command_line.jobid; |
|
385 |
send_msg(server_socket, &m); |
|
386 |
||
387 |
/* Receive the answer */ |
|
388 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
389 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
390 |
error("Error in remove_job"); |
41 | 391 |
switch(m.type) |
392 |
{ |
|
393 |
case REMOVEJOB_OK: |
|
394 |
return; |
|
395 |
/* WILL NOT GO FURTHER */ |
|
396 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 397 |
string = (char *) malloc(m.u.size); |
398 |
res = recv_bytes(server_socket, string, m.u.size); |
|
41 | 399 |
fprintf(stderr, "Error in the request: %s", |
400 |
string); |
|
401 |
exit(-1); |
|
402 |
/* WILL NOT GO FURTHER */ |
|
44 | 403 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
404 |
warning("Wrong internal message in remove_job"); |
41 | 405 |
} |
406 |
/* This will never be reached */ |
|
407 |
} |
|
42 | 408 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
409 |
int c_wait_job_recv() |
42 | 410 |
{ |
411 |
struct msg m; |
|
412 |
int res; |
|
413 |
char *string = 0; |
|
414 |
||
415 |
/* Receive the answer */ |
|
416 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
417 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
418 |
error("Error in wait_job"); |
42 | 419 |
switch(m.type) |
420 |
{ |
|
421 |
case WAITJOB_OK: |
|
110 | 422 |
return m.u.result.errorlevel; |
42 | 423 |
/* WILL NOT GO FURTHER */ |
424 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 425 |
string = (char *) malloc(m.u.size); |
426 |
res = recv_bytes(server_socket, string, m.u.size); |
|
427 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
428 |
error("Error in wait_job - line size"); |
42 | 429 |
fprintf(stderr, "Error in the request: %s", |
430 |
string); |
|
431 |
exit(-1); |
|
432 |
/* WILL NOT GO FURTHER */ |
|
44 | 433 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
434 |
warning("Wrong internal message in c_wait_job"); |
42 | 435 |
} |
436 |
/* This will never be reached */ |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
437 |
return -1; |
42 | 438 |
} |
53 | 439 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
440 |
void c_wait_job_send() |
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 |
struct msg m; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
443 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
444 |
/* Send the request */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
445 |
m.type = WAITJOB; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
446 |
m.u.jobid = command_line.jobid; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
447 |
send_msg(server_socket, &m); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
448 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
449 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
450 |
/* Returns the errorlevel */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
451 |
int c_wait_job() |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
452 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
453 |
c_wait_job_send(); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
454 |
return c_wait_job_recv(); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
455 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
156
diff
changeset
|
456 |
|
53 | 457 |
void c_move_urgent() |
458 |
{ |
|
459 |
struct msg m; |
|
460 |
int res; |
|
461 |
char *string = 0; |
|
462 |
||
463 |
/* Send the request */ |
|
464 |
m.type = URGENT; |
|
465 |
m.u.jobid = command_line.jobid; |
|
466 |
send_msg(server_socket, &m); |
|
467 |
||
468 |
/* Receive the answer */ |
|
469 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
470 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
471 |
error("Error in move_urgent"); |
53 | 472 |
switch(m.type) |
473 |
{ |
|
474 |
case URGENT_OK: |
|
475 |
return; |
|
476 |
/* WILL NOT GO FURTHER */ |
|
477 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 478 |
string = (char *) malloc(m.u.size); |
479 |
res = recv_bytes(server_socket, string, m.u.size); |
|
480 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
481 |
error("Error in move_urgent - line size"); |
53 | 482 |
fprintf(stderr, "Error in the request: %s", |
483 |
string); |
|
484 |
exit(-1); |
|
485 |
/* WILL NOT GO FURTHER */ |
|
486 |
default: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
487 |
warning("Wrong internal message in move_urgent"); |
53 | 488 |
} |
489 |
/* This will never be reached */ |
|
490 |
return; |
|
491 |
} |
|
63 | 492 |
|
493 |
void c_get_state() |
|
494 |
{ |
|
495 |
struct msg m; |
|
496 |
int res; |
|
497 |
char *string = 0; |
|
498 |
||
499 |
/* Send the request */ |
|
500 |
m.type = GET_STATE; |
|
501 |
m.u.jobid = command_line.jobid; |
|
502 |
send_msg(server_socket, &m); |
|
503 |
||
504 |
/* Receive the answer */ |
|
505 |
res = recv_msg(server_socket, &m); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
506 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
507 |
error("Error in get_state - line size"); |
63 | 508 |
switch(m.type) |
509 |
{ |
|
510 |
case ANSWER_STATE: |
|
511 |
printf("%s\n", jstate2string(m.u.state)); |
|
512 |
return; |
|
513 |
/* WILL NOT GO FURTHER */ |
|
514 |
case LIST_LINE: /* Only ONE line accepted */ |
|
146 | 515 |
string = (char *) malloc(m.u.size); |
516 |
res = recv_bytes(server_socket, string, m.u.size); |
|
517 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
518 |
error("Error in get_state - line size"); |
63 | 519 |
fprintf(stderr, "Error in the request: %s", |
520 |
string); |
|
521 |
exit(-1); |
|
522 |
/* WILL NOT GO FURTHER */ |
|
523 |
default: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
524 |
warning("Wrong internal message in get_state"); |
63 | 525 |
} |
526 |
/* This will never be reached */ |
|
527 |
return; |
|
528 |
} |
|
66
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 |
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
|
531 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
532 |
struct msg m; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
533 |
int res; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
534 |
char *string = 0; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
535 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
536 |
/* Send the request */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
537 |
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
|
538 |
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
|
539 |
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
|
540 |
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
|
541 |
|
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
542 |
/* Receive the answer */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
543 |
res = recv_msg(server_socket, &m); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
544 |
if(res != sizeof(m)) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
545 |
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
|
546 |
switch(m.type) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
547 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
548 |
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
|
549 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
550 |
/* 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
|
551 |
case LIST_LINE: /* Only ONE line accepted */ |
146 | 552 |
string = (char *) malloc(m.u.size); |
553 |
res = recv_bytes(server_socket, string, m.u.size); |
|
554 |
if(res != m.u.size) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
555 |
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
|
556 |
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
|
557 |
string); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
558 |
exit(-1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
559 |
/* 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
|
560 |
default: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
71
diff
changeset
|
561 |
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
|
562 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
563 |
/* 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
|
564 |
return; |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
63
diff
changeset
|
565 |
} |