author | viric@mandarina |
Mon, 05 May 2008 23:14:22 +0200 | |
changeset 216 | b4476e12a717 |
parent 178 | 5b64d66a8d89 |
child 218 | f084c8de313f |
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 <sys/types.h> |
8 |
#include <sys/socket.h> |
|
44 | 9 |
#include <sys/select.h> |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
10 |
#ifdef linux |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
11 |
#include <sys/time.h> |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
12 |
#endif |
94 | 13 |
#include <sys/resource.h> |
1 | 14 |
#include <sys/un.h> |
15 |
#include <errno.h> |
|
3 | 16 |
#include <string.h> |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
17 |
#include <stdlib.h> |
44 | 18 |
#include <unistd.h> |
94 | 19 |
#include <limits.h> |
99
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
20 |
#include <signal.h> |
120 | 21 |
#include <fcntl.h> |
1 | 22 |
|
23 |
#include <stdio.h> |
|
24 |
||
3 | 25 |
#include "main.h" |
1 | 26 |
|
27 |
enum |
|
28 |
{ |
|
64 | 29 |
MAXCONN=1000 |
1 | 30 |
}; |
31 |
||
32 |
enum Break |
|
33 |
{ |
|
2 | 34 |
BREAK, |
35 |
NOBREAK |
|
1 | 36 |
}; |
37 |
||
3 | 38 |
/* Prototypes */ |
39 |
static void server_loop(int ls); |
|
40 |
static enum Break |
|
41 |
client_read(int index); |
|
42 |
static void end_server(int ls); |
|
8 | 43 |
static void s_newjob_ok(int index); |
44 |
static void s_runjob(int index); |
|
3 | 45 |
|
46 |
struct Client_conn |
|
47 |
{ |
|
48 |
int socket; |
|
49 |
int hasjob; |
|
50 |
int jobid; |
|
51 |
}; |
|
52 |
||
53 |
/* Globals */ |
|
54 |
static struct Client_conn client_cs[MAXCONN]; |
|
55 |
static int nconnections; |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
56 |
static char *path; |
94 | 57 |
static int max_descriptors; |
58 |
||
155 | 59 |
/* For dependencies between tasks */ |
60 |
static int last_errorlevel = 0; |
|
61 |
||
99
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
62 |
static void sigterm_handler(int n) |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
63 |
{ |
120 | 64 |
const char *dumpfilename; |
65 |
int fd; |
|
66 |
||
67 |
/* Dump the job list if we should to */ |
|
68 |
dumpfilename = getenv("TS_SAVELIST"); |
|
69 |
if (dumpfilename != NULL) |
|
70 |
{ |
|
71 |
fd = open(dumpfilename, O_WRONLY | O_APPEND | O_CREAT, 0600); |
|
72 |
if (fd != -1) |
|
73 |
{ |
|
74 |
joblist_dump(fd); |
|
75 |
close(fd); |
|
76 |
} else |
|
77 |
warning("The TS_SAVELIST file \"%s\" cannot be opened", |
|
78 |
dumpfilename); |
|
79 |
} |
|
80 |
||
99
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
81 |
/* path will be initialized for sure, before installing the handler */ |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
82 |
unlink(path); |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
83 |
exit(1); |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
84 |
} |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
85 |
|
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
86 |
static void install_sigterm_handler() |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
87 |
{ |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
88 |
struct sigaction act; |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
89 |
|
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
90 |
act.sa_handler = sigterm_handler; |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
91 |
/* Reset the mask */ |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
92 |
memset(&act.sa_mask,0,sizeof(act.sa_mask)); |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
93 |
act.sa_flags = 0; |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
94 |
|
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
95 |
sigaction(SIGTERM, &act, NULL); |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
96 |
} |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
97 |
|
94 | 98 |
static int get_max_descriptors() |
99 |
{ |
|
100 |
const int MARGIN = 5; /* stdin, stderr, listen socket, and whatever */ |
|
101 |
int max; |
|
102 |
struct rlimit rlim; |
|
103 |
int res; |
|
104 |
||
105 |
max = MAXCONN; |
|
106 |
if (max > FD_SETSIZE) |
|
107 |
max = FD_SETSIZE; |
|
108 |
||
109 |
/* I'd like to use OPEN_MAX or NR_OPEN, but I don't know if any |
|
110 |
* of them is POSIX compliant */ |
|
111 |
||
112 |
res = getrlimit(RLIMIT_NOFILE, &rlim); |
|
113 |
if (res != 0) |
|
114 |
warning("getrlimit for open files"); |
|
115 |
else |
|
116 |
{ |
|
117 |
if (max > rlim.rlim_cur) |
|
118 |
max = rlim.rlim_cur; |
|
119 |
} |
|
120 |
||
121 |
if (max - MARGIN < 1) |
|
122 |
error("Too few opened descriptors available"); |
|
123 |
||
124 |
return max - MARGIN; |
|
125 |
} |
|
1 | 126 |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
127 |
void server_main(int notify_fd, char *_path) |
1 | 128 |
{ |
44 | 129 |
int ls; |
2 | 130 |
struct sockaddr_un addr; |
131 |
int res; |
|
1 | 132 |
|
104 | 133 |
process_type = SERVER; |
94 | 134 |
max_descriptors = get_max_descriptors(); |
135 |
||
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
136 |
path = _path; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
137 |
|
3 | 138 |
nconnections = 0; |
139 |
||
129
312083fb8899
Change PF_UNIX to AF_UNIX. According to socket(2), this is better.
viric@llimona
parents:
120
diff
changeset
|
140 |
ls = socket(AF_UNIX, SOCK_STREAM, 0); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
141 |
if(ls == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
142 |
error("cannot create the listen socket in the server"); |
1 | 143 |
|
2 | 144 |
addr.sun_family = AF_UNIX; |
145 |
strcpy(addr.sun_path, path); |
|
1 | 146 |
|
2 | 147 |
res = bind(ls, (struct sockaddr *) &addr, sizeof(addr)); |
148 |
if (res == -1) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
149 |
error("Error binding."); |
1 | 150 |
|
2 | 151 |
res = listen(ls, 0); |
152 |
if (res == -1) |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
153 |
error("Error listening."); |
1 | 154 |
|
99
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
155 |
install_sigterm_handler(); |
88d96be4e0e9
Never leave the socket if the server dies by SIGTERM.
viric@llimona
parents:
94
diff
changeset
|
156 |
|
25 | 157 |
notify_parent(notify_fd); |
158 |
||
2 | 159 |
server_loop(ls); |
1 | 160 |
} |
161 |
||
8 | 162 |
static int get_conn_of_jobid(int jobid) |
163 |
{ |
|
164 |
int i; |
|
165 |
for(i=0; i< nconnections; ++i) |
|
166 |
if (client_cs[i].hasjob && client_cs[i].jobid == jobid) |
|
167 |
return i; |
|
168 |
return -1; |
|
169 |
} |
|
170 |
||
3 | 171 |
static void server_loop(int ls) |
1 | 172 |
{ |
2 | 173 |
fd_set readset; |
174 |
int i; |
|
175 |
int maxfd; |
|
176 |
int keep_loop = 1; |
|
8 | 177 |
int newjob; |
1 | 178 |
|
2 | 179 |
while (keep_loop) |
180 |
{ |
|
181 |
FD_ZERO(&readset); |
|
94 | 182 |
maxfd = 0; |
183 |
/* If we can accept more connections, go on. |
|
184 |
* Otherwise, the system block them (no accept will be done). */ |
|
185 |
if (nconnections < max_descriptors) |
|
186 |
{ |
|
187 |
FD_SET(ls,&readset); |
|
188 |
maxfd = ls; |
|
189 |
} |
|
2 | 190 |
for(i=0; i< nconnections; ++i) |
191 |
{ |
|
3 | 192 |
FD_SET(client_cs[i].socket, &readset); |
193 |
if (client_cs[i].socket > maxfd) |
|
194 |
maxfd = client_cs[i].socket; |
|
2 | 195 |
} |
196 |
select(maxfd + 1, &readset, NULL, NULL, NULL); |
|
197 |
if (FD_ISSET(ls,&readset)) |
|
198 |
{ |
|
199 |
int cs; |
|
200 |
cs = accept(ls, NULL, NULL); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
201 |
if (cs == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
202 |
error("Accepting from %i", ls); |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
203 |
client_cs[nconnections].hasjob = 0; |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
204 |
client_cs[nconnections].socket = cs; |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
205 |
++nconnections; |
2 | 206 |
} |
207 |
for(i=0; i< nconnections; ++i) |
|
3 | 208 |
if (FD_ISSET(client_cs[i].socket, &readset)) |
2 | 209 |
{ |
210 |
enum Break b; |
|
3 | 211 |
b = client_read(i); |
2 | 212 |
/* Check if we should break */ |
213 |
if (b == BREAK) |
|
214 |
keep_loop = 0; |
|
215 |
} |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
216 |
/* This will return firstjob->jobid or -1 */ |
8 | 217 |
newjob = next_run_job(); |
218 |
if (newjob != -1) |
|
219 |
{ |
|
220 |
int conn; |
|
221 |
conn = get_conn_of_jobid(newjob); |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
222 |
/* This next marks the firstjob state to RUNNING */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
223 |
s_mark_job_running(); |
8 | 224 |
s_runjob(conn); |
225 |
} |
|
2 | 226 |
} |
1 | 227 |
|
2 | 228 |
end_server(ls); |
1 | 229 |
} |
230 |
||
3 | 231 |
static void end_server(int ls) |
1 | 232 |
{ |
2 | 233 |
close(ls); |
234 |
unlink(path); |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
235 |
/* This comes from the parent, in the fork after server_main. |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
236 |
* This is the last use of path in this process.*/ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
237 |
free(path); |
1 | 238 |
} |
239 |
||
3 | 240 |
static void remove_connection(int index) |
1 | 241 |
{ |
2 | 242 |
int i; |
3 | 243 |
|
244 |
if(client_cs[index].hasjob) |
|
2 | 245 |
{ |
3 | 246 |
s_removejob(client_cs[index].jobid); |
2 | 247 |
} |
3 | 248 |
|
249 |
for(i=index; i<(nconnections-1); ++i) |
|
250 |
{ |
|
251 |
memcpy(&client_cs[i], &client_cs[i+1], sizeof(client_cs[0])); |
|
252 |
} |
|
253 |
nconnections--; |
|
1 | 254 |
} |
255 |
||
3 | 256 |
|
257 |
static enum Break |
|
258 |
client_read(int index) |
|
1 | 259 |
{ |
3 | 260 |
struct msg m; |
2 | 261 |
int s; |
262 |
int res; |
|
1 | 263 |
|
3 | 264 |
s = client_cs[index].socket; |
1 | 265 |
|
2 | 266 |
/* Read the message */ |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
267 |
res = recv_msg(s, &m); |
8 | 268 |
if (res == -1) |
269 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
270 |
warning("client recv failed"); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
271 |
close(s); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
272 |
remove_connection(index); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
273 |
/* It will not fail, even if the index is not a notification */ |
176
306890eeeffa
Fixing a bug on s_remove_job, related to -w and dying with signals
viric@llimona
parents:
156
diff
changeset
|
274 |
s_remove_notification(s); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
275 |
return NOBREAK; |
8 | 276 |
} |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
277 |
else if (res == 0) |
2 | 278 |
{ |
279 |
close(s); |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
280 |
/* TODO: if the client dies while its job is running, someone may prefer |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
281 |
* to note the job as finished with an error. Now we simply remove |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
282 |
* it from the queue. */ |
3 | 283 |
remove_connection(index); |
45 | 284 |
/* It will not fail, even if the index is not a notification */ |
176
306890eeeffa
Fixing a bug on s_remove_job, related to -w and dying with signals
viric@llimona
parents:
156
diff
changeset
|
285 |
s_remove_notification(s); |
2 | 286 |
return NOBREAK; |
287 |
} |
|
1 | 288 |
|
2 | 289 |
/* Process message */ |
31 | 290 |
if (m.type == KILL_SERVER) |
2 | 291 |
return BREAK; /* break in the parent*/ |
1 | 292 |
|
3 | 293 |
if (m.type == NEWJOB) |
294 |
{ |
|
18 | 295 |
client_cs[index].jobid = s_newjob(s, &m); |
3 | 296 |
client_cs[index].hasjob = 1; |
4 | 297 |
s_newjob_ok(index); |
3 | 298 |
} |
299 |
||
22 | 300 |
if (m.type == RUNJOB_OK) |
301 |
{ |
|
302 |
char *buffer = 0; |
|
32 | 303 |
if (m.u.output.store_output) |
22 | 304 |
{ |
305 |
/* Receive the output filename */ |
|
32 | 306 |
buffer = (char *) malloc(m.u.output.ofilename_size); |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
307 |
res = recv_bytes(s, buffer, |
32 | 308 |
m.u.output.ofilename_size); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
309 |
if (res != m.u.output.ofilename_size) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
310 |
error("Reading the ofilename"); |
22 | 311 |
} |
35 | 312 |
s_process_runjob_ok(client_cs[index].jobid, buffer, |
313 |
m.u.output.pid); |
|
22 | 314 |
} |
315 |
||
3 | 316 |
if (m.type == LIST) |
5 | 317 |
{ |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
318 |
s_list(s); |
9 | 319 |
/* We must actively close, meaning End of Lines */ |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
320 |
close(s); |
5 | 321 |
remove_connection(index); |
322 |
} |
|
3 | 323 |
|
146 | 324 |
if (m.type == INFO) |
325 |
{ |
|
326 |
s_job_info(s, m.u.jobid); |
|
327 |
close(s); |
|
328 |
remove_connection(index); |
|
329 |
} |
|
330 |
||
8 | 331 |
if (m.type == ENDJOB) |
332 |
{ |
|
114 | 333 |
job_finished(&m.u.result); |
156 | 334 |
/* For the dependencies */ |
335 |
last_errorlevel = m.u.result.errorlevel; |
|
42 | 336 |
check_notify_list(client_cs[index].jobid); |
136
cfc266aa8eb0
Fixed introduced bug on EOC from the client, because the server died.
viric@llimona
parents:
134
diff
changeset
|
337 |
/* We don't want this connection to do anything |
cfc266aa8eb0
Fixed introduced bug on EOC from the client, because the server died.
viric@llimona
parents:
134
diff
changeset
|
338 |
* more related to the jobid, secially on remove_connection |
cfc266aa8eb0
Fixed introduced bug on EOC from the client, because the server died.
viric@llimona
parents:
134
diff
changeset
|
339 |
* when we receive the EOC. */ |
cfc266aa8eb0
Fixed introduced bug on EOC from the client, because the server died.
viric@llimona
parents:
134
diff
changeset
|
340 |
client_cs[index].hasjob = 0; |
8 | 341 |
} |
342 |
||
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
343 |
if (m.type == CLEAR_FINISHED) |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
344 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
345 |
s_clear_finished(); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
346 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
347 |
|
32 | 348 |
if (m.type == ASK_OUTPUT) |
349 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
350 |
s_send_output(s, m.u.jobid); |
32 | 351 |
} |
352 |
||
41 | 353 |
if (m.type == REMOVEJOB) |
354 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
355 |
int went_ok; |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
356 |
went_ok = s_remove_job(s, m.u.jobid); |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
357 |
if (went_ok) |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
358 |
{ |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
359 |
int i; |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
360 |
for(i = 0; i < nconnections; ++i) |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
361 |
{ |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
362 |
if (client_cs[i].hasjob && client_cs[i].jobid == m.u.jobid) |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
363 |
{ |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
364 |
close(client_cs[i].socket); |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
365 |
|
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
366 |
/* So remove_connection doesn't call s_removejob again */ |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
367 |
client_cs[i].hasjob = 0; |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
368 |
|
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
369 |
/* We don't try to remove any notification related to |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
370 |
* 'i', because it will be for sure a ts client for a job */ |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
371 |
remove_connection(i); |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
372 |
} |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
373 |
} |
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
374 |
} |
41 | 375 |
} |
376 |
||
42 | 377 |
if (m.type == WAITJOB) |
378 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
379 |
s_wait_job(s, m.u.jobid); |
42 | 380 |
} |
381 |
||
53 | 382 |
if (m.type == URGENT) |
383 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
384 |
s_move_urgent(s, m.u.jobid); |
53 | 385 |
} |
386 |
||
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
387 |
if (m.type == SWAP_JOBS) |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
388 |
{ |
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
389 |
s_swap_jobs(s, m.u.swap.jobid1, |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
390 |
m.u.swap.jobid2); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
391 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
392 |
|
63 | 393 |
if (m.type == GET_STATE) |
394 |
{ |
|
134
5a6b8bb1f7a8
Fixing two bugs noted in buglist.bug (ts clients not dying on removejob, queue
viric@llimona
parents:
129
diff
changeset
|
395 |
s_send_state(s, m.u.jobid); |
63 | 396 |
} |
397 |
||
2 | 398 |
return NOBREAK; /* normal */ |
1 | 399 |
} |
4 | 400 |
|
8 | 401 |
static void s_runjob(int index) |
402 |
{ |
|
403 |
int s; |
|
404 |
struct msg m; |
|
405 |
||
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
406 |
if (!client_cs[index].hasjob) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
407 |
error("Run job of the client %i which doesn't have any job", index); |
8 | 408 |
|
409 |
s = client_cs[index].socket; |
|
410 |
||
411 |
m.type = RUNJOB; |
|
155 | 412 |
m.u.last_errorlevel = last_errorlevel; |
8 | 413 |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
414 |
send_msg(s, &m); |
8 | 415 |
} |
416 |
||
417 |
static void s_newjob_ok(int index) |
|
4 | 418 |
{ |
419 |
int s; |
|
420 |
struct msg m; |
|
421 |
||
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
422 |
if (!client_cs[index].hasjob) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
423 |
error("Run job of the client %i which doesn't have any job", index); |
4 | 424 |
|
425 |
s = client_cs[index].socket; |
|
426 |
||
427 |
m.type = NEWJOB_OK; |
|
428 |
m.u.jobid = client_cs[index].jobid; |
|
429 |
||
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
430 |
send_msg(s, &m); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
431 |
} |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
432 |
|
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
433 |
static void dump_conn_struct(FILE *out, const struct Client_conn *p) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
434 |
{ |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
435 |
fprintf(out, " new_conn\n"); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
436 |
fprintf(out, " socket %i\n", p->socket); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
437 |
fprintf(out, " hasjob \"%i\"\n", p->hasjob); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
438 |
fprintf(out, " jobid %i\n", p->jobid); |
4 | 439 |
} |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
440 |
|
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
441 |
void dump_conns_struct(FILE *out) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
442 |
{ |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
443 |
int i; |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
444 |
|
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
445 |
fprintf(out, "New_conns"); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
446 |
|
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
447 |
for(i=0; i < nconnections; ++i) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
448 |
{ |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
449 |
dump_conn_struct(out, &client_cs[i]); |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
450 |
} |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
451 |
} |