author | viric@llimona |
Mon, 02 Apr 2007 17:53:52 +0200 | |
changeset 74 | bb1b20d79acf |
parent 66 | f70a27afd92e |
child 92 | 05004c52ecff |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
1 | 7 |
#include <assert.h> |
8 |
#include <sys/types.h> |
|
9 |
#include <sys/socket.h> |
|
44 | 10 |
#include <sys/select.h> |
1 | 11 |
#include <sys/un.h> |
12 |
#include <errno.h> |
|
3 | 13 |
#include <string.h> |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
14 |
#include <stdlib.h> |
44 | 15 |
#include <unistd.h> |
1 | 16 |
|
17 |
#include <stdio.h> |
|
18 |
||
19 |
#include "msg.h" |
|
3 | 20 |
#include "main.h" |
1 | 21 |
|
22 |
enum |
|
23 |
{ |
|
64 | 24 |
MAXCONN=1000 |
1 | 25 |
}; |
26 |
||
27 |
enum Break |
|
28 |
{ |
|
2 | 29 |
BREAK, |
30 |
NOBREAK |
|
1 | 31 |
}; |
32 |
||
3 | 33 |
/* Prototypes */ |
34 |
static void server_loop(int ls); |
|
35 |
static enum Break |
|
36 |
client_read(int index); |
|
37 |
static void end_server(int ls); |
|
8 | 38 |
static void s_newjob_ok(int index); |
39 |
static void s_runjob(int index); |
|
3 | 40 |
|
41 |
struct Client_conn |
|
42 |
{ |
|
43 |
int socket; |
|
44 |
int hasjob; |
|
45 |
int jobid; |
|
46 |
}; |
|
47 |
||
48 |
/* Globals */ |
|
49 |
static struct Client_conn client_cs[MAXCONN]; |
|
50 |
static int nconnections; |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
51 |
static char *path; |
1 | 52 |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
53 |
void server_main(int notify_fd, char *_path) |
1 | 54 |
{ |
44 | 55 |
int ls; |
2 | 56 |
struct sockaddr_un addr; |
57 |
int res; |
|
1 | 58 |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
59 |
path = _path; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
60 |
|
3 | 61 |
nconnections = 0; |
62 |
||
2 | 63 |
ls = socket(PF_UNIX, SOCK_STREAM, 0); |
64 |
assert(ls != -1); |
|
1 | 65 |
|
2 | 66 |
addr.sun_family = AF_UNIX; |
67 |
strcpy(addr.sun_path, path); |
|
1 | 68 |
|
2 | 69 |
res = bind(ls, (struct sockaddr *) &addr, sizeof(addr)); |
70 |
if (res == -1) |
|
71 |
{ |
|
72 |
perror("Error binding."); |
|
73 |
return; |
|
74 |
} |
|
1 | 75 |
|
2 | 76 |
res = listen(ls, 0); |
77 |
if (res == -1) |
|
78 |
{ |
|
79 |
perror("Error listening."); |
|
80 |
return; |
|
81 |
} |
|
1 | 82 |
|
25 | 83 |
notify_parent(notify_fd); |
84 |
||
2 | 85 |
server_loop(ls); |
1 | 86 |
} |
87 |
||
8 | 88 |
static int get_conn_of_jobid(int jobid) |
89 |
{ |
|
90 |
int i; |
|
91 |
for(i=0; i< nconnections; ++i) |
|
92 |
if (client_cs[i].hasjob && client_cs[i].jobid == jobid) |
|
93 |
return i; |
|
94 |
return -1; |
|
95 |
} |
|
96 |
||
3 | 97 |
static void server_loop(int ls) |
1 | 98 |
{ |
2 | 99 |
fd_set readset; |
100 |
int i; |
|
101 |
int maxfd; |
|
102 |
int keep_loop = 1; |
|
8 | 103 |
int newjob; |
1 | 104 |
|
2 | 105 |
while (keep_loop) |
106 |
{ |
|
107 |
FD_ZERO(&readset); |
|
108 |
FD_SET(ls,&readset); |
|
109 |
maxfd = ls; |
|
110 |
for(i=0; i< nconnections; ++i) |
|
111 |
{ |
|
3 | 112 |
FD_SET(client_cs[i].socket, &readset); |
113 |
if (client_cs[i].socket > maxfd) |
|
114 |
maxfd = client_cs[i].socket; |
|
2 | 115 |
} |
116 |
select(maxfd + 1, &readset, NULL, NULL, NULL); |
|
117 |
if (FD_ISSET(ls,&readset)) |
|
118 |
{ |
|
119 |
int cs; |
|
120 |
cs = accept(ls, NULL, NULL); |
|
121 |
assert(cs != -1); |
|
3 | 122 |
client_cs[nconnections++].socket = cs; |
2 | 123 |
} |
124 |
for(i=0; i< nconnections; ++i) |
|
3 | 125 |
if (FD_ISSET(client_cs[i].socket, &readset)) |
2 | 126 |
{ |
127 |
enum Break b; |
|
3 | 128 |
b = client_read(i); |
2 | 129 |
/* Check if we should break */ |
130 |
if (b == BREAK) |
|
131 |
keep_loop = 0; |
|
132 |
} |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
133 |
/* This will return firstjob->jobid or -1 */ |
8 | 134 |
newjob = next_run_job(); |
135 |
if (newjob != -1) |
|
136 |
{ |
|
137 |
int conn; |
|
138 |
conn = get_conn_of_jobid(newjob); |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
139 |
/* 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
|
140 |
s_mark_job_running(); |
8 | 141 |
s_runjob(conn); |
142 |
} |
|
2 | 143 |
} |
1 | 144 |
|
2 | 145 |
end_server(ls); |
1 | 146 |
} |
147 |
||
3 | 148 |
static void end_server(int ls) |
1 | 149 |
{ |
2 | 150 |
close(ls); |
151 |
unlink(path); |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
26
diff
changeset
|
152 |
/* 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
|
153 |
* 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
|
154 |
free(path); |
1 | 155 |
} |
156 |
||
3 | 157 |
static void remove_connection(int index) |
1 | 158 |
{ |
2 | 159 |
int i; |
3 | 160 |
|
161 |
if(client_cs[index].hasjob) |
|
2 | 162 |
{ |
3 | 163 |
s_removejob(client_cs[index].jobid); |
2 | 164 |
} |
3 | 165 |
|
166 |
for(i=index; i<(nconnections-1); ++i) |
|
167 |
{ |
|
168 |
memcpy(&client_cs[i], &client_cs[i+1], sizeof(client_cs[0])); |
|
169 |
} |
|
170 |
nconnections--; |
|
1 | 171 |
} |
172 |
||
3 | 173 |
|
174 |
static enum Break |
|
175 |
client_read(int index) |
|
1 | 176 |
{ |
3 | 177 |
struct msg m; |
2 | 178 |
int s; |
179 |
int res; |
|
1 | 180 |
|
3 | 181 |
s = client_cs[index].socket; |
1 | 182 |
|
2 | 183 |
/* Read the message */ |
9 | 184 |
res = recv(s, &m, sizeof(m),0); |
8 | 185 |
if (res == -1) |
186 |
{ |
|
9 | 187 |
fprintf(stderr, "Error reading from client %i, socket %i\n", |
8 | 188 |
index, s); |
189 |
perror("client read"); |
|
190 |
exit(-1); |
|
191 |
} |
|
2 | 192 |
if (res == 0) |
193 |
{ |
|
194 |
close(s); |
|
3 | 195 |
remove_connection(index); |
45 | 196 |
/* It will not fail, even if the index is not a notification */ |
197 |
s_remove_notification(index); |
|
2 | 198 |
return NOBREAK; |
199 |
} |
|
1 | 200 |
|
3 | 201 |
client_cs[index].hasjob = 0; |
202 |
||
2 | 203 |
/* Process message */ |
31 | 204 |
if (m.type == KILL_SERVER) |
2 | 205 |
return BREAK; /* break in the parent*/ |
1 | 206 |
|
3 | 207 |
if (m.type == NEWJOB) |
208 |
{ |
|
18 | 209 |
client_cs[index].jobid = s_newjob(s, &m); |
3 | 210 |
client_cs[index].hasjob = 1; |
4 | 211 |
s_newjob_ok(index); |
3 | 212 |
} |
213 |
||
22 | 214 |
if (m.type == RUNJOB_OK) |
215 |
{ |
|
216 |
char *buffer = 0; |
|
32 | 217 |
if (m.u.output.store_output) |
22 | 218 |
{ |
219 |
/* Receive the output filename */ |
|
32 | 220 |
buffer = (char *) malloc(m.u.output.ofilename_size); |
22 | 221 |
res = recv_bytes(client_cs[index].socket, buffer, |
32 | 222 |
m.u.output.ofilename_size); |
223 |
assert(res == m.u.output.ofilename_size); |
|
22 | 224 |
} |
35 | 225 |
s_process_runjob_ok(client_cs[index].jobid, buffer, |
226 |
m.u.output.pid); |
|
22 | 227 |
} |
228 |
||
3 | 229 |
if (m.type == LIST) |
5 | 230 |
{ |
231 |
s_list(client_cs[index].socket); |
|
9 | 232 |
/* We must actively close, meaning End of Lines */ |
5 | 233 |
close(client_cs[index].socket); |
234 |
remove_connection(index); |
|
235 |
} |
|
3 | 236 |
|
8 | 237 |
if (m.type == ENDJOB) |
238 |
{ |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
239 |
job_finished(m.u.errorlevel); |
42 | 240 |
check_notify_list(client_cs[index].jobid); |
8 | 241 |
} |
242 |
||
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
243 |
if (m.type == CLEAR_FINISHED) |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
244 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
245 |
s_clear_finished(); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
246 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
19
diff
changeset
|
247 |
|
32 | 248 |
if (m.type == ASK_OUTPUT) |
249 |
{ |
|
250 |
s_send_output(client_cs[index].socket, m.u.jobid); |
|
251 |
} |
|
252 |
||
41 | 253 |
if (m.type == REMOVEJOB) |
254 |
{ |
|
255 |
s_remove_job(client_cs[index].socket, m.u.jobid); |
|
256 |
} |
|
257 |
||
42 | 258 |
if (m.type == WAITJOB) |
259 |
{ |
|
260 |
s_wait_job(client_cs[index].socket, m.u.jobid); |
|
261 |
} |
|
262 |
||
53 | 263 |
if (m.type == URGENT) |
264 |
{ |
|
265 |
s_move_urgent(client_cs[index].socket, m.u.jobid); |
|
266 |
} |
|
267 |
||
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
268 |
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
|
269 |
{ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
270 |
s_swap_jobs(client_cs[index].socket, m.u.swap.jobid1, |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
271 |
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
|
272 |
} |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
273 |
|
63 | 274 |
if (m.type == GET_STATE) |
275 |
{ |
|
276 |
s_send_state(client_cs[index].socket, m.u.jobid); |
|
277 |
} |
|
278 |
||
2 | 279 |
return NOBREAK; /* normal */ |
1 | 280 |
} |
4 | 281 |
|
8 | 282 |
static void s_runjob(int index) |
283 |
{ |
|
284 |
int s; |
|
285 |
struct msg m; |
|
286 |
int res; |
|
287 |
||
288 |
assert(client_cs[index].hasjob); |
|
289 |
||
290 |
s = client_cs[index].socket; |
|
291 |
||
292 |
m.type = RUNJOB; |
|
293 |
||
9 | 294 |
res = send(s, &m, sizeof(m), 0); |
8 | 295 |
if(res == -1) |
9 | 296 |
perror("send"); |
8 | 297 |
} |
298 |
||
299 |
static void s_newjob_ok(int index) |
|
4 | 300 |
{ |
301 |
int s; |
|
302 |
struct msg m; |
|
303 |
int res; |
|
304 |
||
305 |
assert(client_cs[index].hasjob); |
|
306 |
||
307 |
s = client_cs[index].socket; |
|
308 |
||
309 |
m.type = NEWJOB_OK; |
|
310 |
m.u.jobid = client_cs[index].jobid; |
|
311 |
||
9 | 312 |
res = send(s, &m, sizeof(m), 0); |
4 | 313 |
if(res == -1) |
9 | 314 |
perror("send"); |
4 | 315 |
} |