-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.c
executable file
·600 lines (506 loc) · 15.7 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2013 Lluís Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifdef linux
#include <sys/time.h>
#endif
#include <sys/resource.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <fcntl.h>
#include <libgen.h>
#include <sys/wait.h>
#include <stdio.h>
#include "main.h"
enum Break {
BREAK,
NOBREAK,
CLOSE
};
char* logdir = 0;
/* Prototypes */
static void server_loop(int ls);
static enum Break client_read(int index);
static void end_server(int ls);
static void s_newjob_ok(int index);
static void s_newjob_nok(int index);
static void s_runjob(int jobid, int index);
static void clean_after_client_disappeared(int socket, int index);
struct Client_conn {
int socket;
int hasjob;
int jobid;
};
/* Globals */
static struct Client_conn *client_cs;
static int nconnections;
static char *path;
static int max_descriptors;
/* in jobs.c */
extern int max_jobs;
static void s_send_version(int s) {
struct Msg m = default_msg();
m.type = VERSION;
m.u.version = PROTOCOL_VERSION;
send_msg(s, &m);
}
static void sigterm_handler(int n) {
const char *dumpfilename;
int fd;
/* Dump the job list if we should to */
dumpfilename = getenv("TS_SAVELIST");
if (dumpfilename != NULL) {
fd = open(dumpfilename, O_WRONLY | O_APPEND | O_CREAT, 0600);
if (fd != -1) {
joblist_dump(fd);
close(fd);
} else
warning("The TS_SAVELIST file \"%s\" cannot be opened",
dumpfilename);
}
/* path will be initialized for sure, before installing the handler */
unlink(path);
exit(1);
}
static void set_default_maxslots() {
char *str;
str = getenv("TS_SLOTS");
if (str != NULL) {
int slots;
slots = abs(atoi(str));
s_set_max_slots(slots);
}
}
static void initialize_log_dir() {
char *tmpdir = getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR");
logdir = malloc(strlen(tmpdir) + 1);
strcpy(logdir, tmpdir);
}
static void install_sigterm_handler() {
struct sigaction act;
act.sa_handler = sigterm_handler;
/* Reset the mask */
memset(&act.sa_mask, 0, sizeof(act.sa_mask));
act.sa_flags = 0;
sigaction(SIGTERM, &act, NULL);
}
static int get_max_descriptors() {
const int MARGIN = 5; /* stdin, stderr, listen socket, and whatever */
int max = 1000; /* initial value used only if getrlimit fails to return a value */
struct rlimit rlim;
int res;
const char *str;
res = getrlimit(RLIMIT_NOFILE, &rlim);
if (res != 0)
warning("getrlimit for open files");
else
max = rlim.rlim_cur - MARGIN;
str = getenv("TS_MAXCONN");
if (str != NULL) {
int user_maxconn;
user_maxconn = abs(atoi(str));
if (max > user_maxconn)
max = user_maxconn;
}
if (max > FD_SETSIZE)
max = FD_SETSIZE - MARGIN;
if (max < 1)
error("Too few opened descriptors available");
return max;
}
void server_main(int notify_fd, char *_path) {
int ls;
struct sockaddr_un addr;
int res;
char *dirpath;
process_type = SERVER;
max_descriptors = get_max_descriptors();
/* allocate dynamic memory for client_cs, thus removing any arbitrary limit */
client_cs = malloc(max_descriptors * sizeof(struct Client_conn));
/* Arbitrary limit, that will block the enqueuing, but should allow space
* for usual ts queries */
max_jobs = max_descriptors - 5;
path = _path;
/* Move the server to the socket directory */
dirpath = malloc(strlen(path) + 1);
strcpy(dirpath, path);
chdir(dirname(dirpath));
free(dirpath);
nconnections = 0;
ls = socket(AF_UNIX, SOCK_STREAM, 0);
if (ls == -1)
error("cannot create the listen socket in the server");
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
res = bind(ls, (struct sockaddr *) &addr, sizeof(addr));
if (res == -1)
error("Error binding.");
res = listen(ls, 0);
if (res == -1)
error("Error listening.");
install_sigterm_handler();
set_default_maxslots();
initialize_log_dir();
notify_parent(notify_fd);
#ifndef CPU
initGPU();
#endif
server_loop(ls);
}
static int get_conn_of_jobid(int jobid) {
int i;
for (i = 0; i < nconnections; ++i)
if (client_cs[i].hasjob && client_cs[i].jobid == jobid)
return i;
return -1;
}
static void server_loop(int ls) {
fd_set readset;
int i;
int maxfd;
int keep_loop = 1;
int newjob;
int res;
while (keep_loop) {
struct timeval tv;
/* Wait up to 30 secs before checking whether a job can run.
* This is needed for GPU jobs because if GPUs are occupied and
* released outside of `ts`, `ts` will not notice until new commands
* from users come. */
tv.tv_sec = 30;
tv.tv_usec = 0;
FD_ZERO(&readset);
maxfd = 0;
/* If we can accept more connections, go on.
* Otherwise, the system block them (no accept will be done). */
if (nconnections < max_descriptors) {
FD_SET(ls, &readset);
maxfd = ls;
}
for (i = 0; i < nconnections; ++i) {
FD_SET(client_cs[i].socket, &readset);
if (client_cs[i].socket > maxfd)
maxfd = client_cs[i].socket;
}
/* timeout mode if there are queued GPU jobs only */
if (s_count_allocating_jobs() > 0)
res = select(maxfd + 1, &readset, NULL, NULL, &tv);
else
res = select(maxfd + 1, &readset, NULL, NULL, NULL);
if (res != -1) {
if (FD_ISSET(ls, &readset)) {
int cs;
cs = accept(ls, NULL, NULL);
if (cs == -1)
error("Accepting from %i", ls);
client_cs[nconnections].hasjob = 0;
client_cs[nconnections].socket = cs;
++nconnections;
}
for (i = 0; i < nconnections; ++i) {
if (FD_ISSET(client_cs[i].socket, &readset)) {
enum Break b;
b = client_read(i);
/* Check if we should break */
if (b == CLOSE) {
warning("Closing");
/* On unknown message, we close the client,
or it may hang waiting for an answer */
clean_after_client_disappeared(client_cs[i].socket, i);
} else if (b == BREAK)
keep_loop = 0;
}
}
}
/* This will return firstjob->jobid or -1 */
newjob = next_run_job();
if (newjob != -1) {
int conn, awaken_job;
conn = get_conn_of_jobid(newjob);
/* This next marks the firstjob state to RUNNING */
s_mark_job_running(newjob);
s_runjob(newjob, conn);
while ((awaken_job = wake_hold_client()) != -1) {
int wake_conn = get_conn_of_jobid(awaken_job);
if (wake_conn == -1)
error("The job awaken does not have a connection open");
s_newjob_ok(wake_conn);
}
}
}
end_server(ls);
}
static void end_server(int ls) {
close(ls);
unlink(path);
/* This comes from the parent, in the fork after server_main.
* This is the last use of path in this process.*/
free(path);
free(client_cs);
free(logdir);
#ifndef CPU
cleanupGpu();
#endif
}
static void remove_connection(int index) {
int i;
if (client_cs[index].hasjob) {
s_removejob(client_cs[index].jobid);
}
for (i = index; i < (nconnections - 1); ++i) {
memcpy(&client_cs[i], &client_cs[i + 1], sizeof(client_cs[0]));
}
nconnections--;
}
static void
clean_after_client_disappeared(int socket, int index) {
/* Act as if the job ended. */
int jobid = client_cs[index].jobid;
if (client_cs[index].hasjob) {
struct Result r;
r.errorlevel = -1;
r.died_by_signal = 1;
r.signal = SIGKILL;
r.user_ms = 0;
r.system_ms = 0;
r.real_ms = 0;
r.skipped = 0;
warning("JobID %i quit while running.", jobid);
job_finished(&r, jobid);
/* For the dependencies */
check_notify_list(jobid);
/* We don't want this connection to do anything
* more related to the jobid, secially on remove_connection
* when we receive the EOC. */
client_cs[index].hasjob = 0;
} else
/* If it doesn't have a running job,
* it may well be a notification */
s_remove_notification(socket);
close(socket);
remove_connection(index);
}
static enum Break
client_read(int index) {
struct Msg m = default_msg();
int s;
int res;
s = client_cs[index].socket;
/* Read the message */
res = recv_msg(s, &m);
if (res == -1) {
warning("client recv failed");
clean_after_client_disappeared(s, index);
return NOBREAK;
} else if (res == 0) {
clean_after_client_disappeared(s, index);
return NOBREAK;
}
/* Process message */
switch (m.type) {
case KILL_SERVER:
return BREAK; /* break in the parent*/
break;
case NEWJOB:
client_cs[index].jobid = s_newjob(s, &m);
client_cs[index].hasjob = 1;
if (!job_is_holding_client(client_cs[index].jobid))
s_newjob_ok(index);
else if (!m.u.newjob.wait_enqueuing) {
s_newjob_nok(index);
clean_after_client_disappeared(s, index);
}
break;
case RUNJOB_OK: {
char *buffer = 0;
if (m.u.output.store_output) {
/* Receive the output filename */
buffer = (char *) malloc(m.u.output.ofilename_size);
res = recv_bytes(s, buffer,
m.u.output.ofilename_size);
if (res != m.u.output.ofilename_size)
error("Reading the ofilename");
}
s_process_runjob_ok(client_cs[index].jobid, buffer,
m.u.output.pid);
}
break;
case KILL_ALL:
s_kill_all_jobs(s);
break;
case LIST:
term_width = m.u.term_width;
s_list(s, m.u.list_format);
/* We must actively close, meaning End of Lines */
close(s);
remove_connection(index);
break;
#ifndef CPU
case LIST_GPU:
s_list_gpu(s);
close(s);
remove_connection(index);
break;
#endif
case INFO:
s_job_info(s, m.u.jobid);
close(s);
remove_connection(index);
break;
case LAST_ID:
s_send_last_id(s);
break;
case GET_LABEL:
s_send_label(s, m.u.jobid);
break;
case GET_CMD:
s_send_cmd(s, m.u.jobid);
break;
case ENDJOB:
job_finished(&m.u.result, client_cs[index].jobid);
/* For the dependencies */
check_notify_list(client_cs[index].jobid);
/* We don't want this connection to do anything
* more related to the jobid, secially on remove_connection
* when we receive the EOC. */
client_cs[index].hasjob = 0;
break;
case CLEAR_FINISHED:
s_clear_finished();
break;
case ASK_OUTPUT:
s_send_output(s, m.u.jobid);
break;
case REMOVEJOB: {
int went_ok;
/* Will update the jobid. If it's -1, will set the jobid found */
went_ok = s_remove_job(s, &m.u.jobid);
if (went_ok) {
int i;
for (i = 0; i < nconnections; ++i) {
if (client_cs[i].hasjob && client_cs[i].jobid == m.u.jobid) {
close(client_cs[i].socket);
/* So remove_connection doesn't call s_removejob again */
client_cs[i].hasjob = 0;
/* We don't try to remove any notification related to
* 'i', because it will be for sure a ts client for a job */
remove_connection(i);
}
}
}
}
break;
case WAITJOB:
s_wait_job(s, m.u.jobid);
break;
case WAIT_RUNNING_JOB:
s_wait_running_job(s, m.u.jobid);
break;
case COUNT_RUNNING:
s_count_running_jobs(s);
break;
case URGENT:
s_move_urgent(s, m.u.jobid);
break;
case SET_MAX_SLOTS:
s_set_max_slots(m.u.max_slots);
break;
case GET_MAX_SLOTS:
s_get_max_slots(s);
break;
case SWAP_JOBS:
s_swap_jobs(s, m.u.swap.jobid1,
m.u.swap.jobid2);
break;
case GET_STATE:
s_send_state(s, m.u.jobid);
break;
case GET_ENV:
s_get_env(s, m.u.size);
break;
case SET_ENV:
s_set_env(s, m.u.size);
break;
case UNSET_ENV:
s_unset_env(s, m.u.size);
break;
#ifndef CPU
case SET_FREE_PERC:
s_set_free_percentage(m.u.size);
break;
case GET_FREE_PERC:
s_get_free_percentage(s);
break;
#endif
case GET_VERSION:
s_send_version(s);
break;
case GET_LOGDIR:
s_get_logdir(s);
break;
case SET_LOGDIR: {
char *path = malloc(m.u.size);
recv_bytes(s, path, m.u.size);
s_set_logdir(path);
}
close(s);
remove_connection(index);
break;
default:
/* Command not supported */
/* On unknown message, we close the client,
or it may hang waiting for an answer */
warning("Unknown message: %i", m.type);
return CLOSE;
}
return NOBREAK; /* normal */
}
static void s_runjob(int jobid, int index) {
int s;
if (!client_cs[index].hasjob)
error("Run job of the client %i which doesn't have any job", index);
s = client_cs[index].socket;
s_send_runjob(s, jobid);
}
static void s_newjob_ok(int index) {
int s;
struct Msg m = default_msg();
if (!client_cs[index].hasjob)
error("Run job of the client %i which doesn't have any job", index);
s = client_cs[index].socket;
m.type = NEWJOB_OK;
m.u.jobid = client_cs[index].jobid;
send_msg(s, &m);
}
static void s_newjob_nok(int index) {
int s;
struct Msg m = default_msg();
if (!client_cs[index].hasjob)
error("Run job of the client %i which doesn't have any job", index);
s = client_cs[index].socket;
m.type = NEWJOB_NOK;
send_msg(s, &m);
}
static void dump_conn_struct(FILE *out, const struct Client_conn *p) {
fprintf(out, " new_conn\n");
fprintf(out, " socket %i\n", p->socket);
fprintf(out, " hasjob \"%i\"\n", p->hasjob);
fprintf(out, " jobid %i\n", p->jobid);
}
void dump_conns_struct(FILE *out) {
int i;
fprintf(out, "New_conns");
for (i = 0; i < nconnections; ++i) {
dump_conn_struct(out, &client_cs[i]);
}
}