/
sched_main.cpp
685 lines (617 loc) · 20.4 KB
/
sched_main.cpp
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2023 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// The BOINC scheduler.
// Normally runs as a CGI or fast CGI program.
// You can also run it:
// - manually for debugging, with a single request
// - for simulation or performance testing, with a stream of requests
// (using --batch)
// TODO: what does the following mean?
// Also, You can call debug_sched() for whatever situation is of
// interest to you. It won't do anything unless you create
// (touch) the file 'debug_sched' in the project root directory.
//
#include "config.h"
#include <cassert>
#include <ctime>
#include "boinc_stdio.h"
#include <cstdlib>
#include <vector>
#include <string>
#include <cstring>
#include <unistd.h>
#include <csignal>
#include <cerrno>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "boinc_db.h"
#include "error_numbers.h"
#include "filesys.h"
#include "parse.h"
#include "shmem.h"
#include "str_util.h"
#include "svn_version.h"
#include "synch.h"
#include "util.h"
#include "handle_request.h"
#include "sched_config.h"
#include "sched_files.h"
#include "sched_keyword.h"
#include "sched_msgs.h"
#include "sched_types.h"
#include "sched_util.h"
#include "sched_main.h"
// Useful for debugging, if your cgi script keeps crashing. This
// makes it dump a core file that you can load into a debugger to see
// where the problem is.
#define DUMP_CORE_ON_SEGV 0
#define DEBUG_LEVEL 999
#define MAX_FCGI_COUNT 20
GUI_URLS gui_urls;
PROJECT_FILES project_files;
key_t sema_key;
int g_pid;
static bool db_opened=false;
SCHED_SHMEM* ssp = 0;
bool batch = false;
bool mark_jobs_done = false;
bool all_apps_use_hr;
static void usage(char* p) {
boinc::fprintf(stderr,
"Usage: %s [OPTION]...\n\n"
"Options:\n"
" --batch stdin contains a sequence of request messages.\n"
" Do them all, and ignore rpc_seqno.\n"
" --mark_jobs_done When send a job, also mark it as done.\n"
" (for performance testing)\n"
" --debug_log Write messages to the file 'debug_log'\n"
" --simulator X Start with simulated time X\n"
" (only if compiled with GCL_SIMULATOR)\n"
" -h | --help Show this help text\n"
" -v | --version Show version information\n",
p
);
}
void debug_sched(const char *trigger) {
char tmpfilename[256];
FILE *fp;
if (!boinc_file_exists(config.project_path("%s", trigger))) {
return;
}
sprintf(tmpfilename,
"sched_reply_%06ld_%06d", g_request->hostid, g_request->rpc_seqno
);
// use _XXXXXX if you want random filenames rather than
// deterministic mkstemp(tmpfilename);
fp=boinc::fopen(tmpfilename, "w");
if (!fp) {
log_messages.printf(MSG_CRITICAL,
"Found %s, but can't open %s\n", trigger, tmpfilename
);
return;
}
log_messages.printf(MSG_DEBUG,
"Found %s, so writing %s\n", trigger, tmpfilename
);
g_reply->write(fp, *g_request);
boinc::fclose(fp);
sprintf(tmpfilename,
"sched_request_%06ld_%06d", g_request->hostid, g_request->rpc_seqno
);
fp=boinc::fopen(tmpfilename, "w");
if (!fp) {
log_messages.printf(MSG_CRITICAL,
"Found %s, but can't open %s\n", trigger, tmpfilename
);
return;
}
log_messages.printf(MSG_DEBUG,
"Found %s, so writing %s\n", trigger, tmpfilename
);
g_request->write(fp);
boinc::fclose(fp);
return;
}
// call this only if we're not going to call handle_request()
//
static void send_message(const char* msg, int delay) {
boinc::fprintf(stdout,
"Content-type: text/plain\n\n"
"<scheduler_reply>\n"
" <message priority=\"low\">%s</message>\n"
" <request_delay>%d</request_delay>\n"
" <project_is_down/>\n"
"%s</scheduler_reply>\n",
msg, delay,
config.ended?" <ended>1</ended>\n":""
);
}
int open_database() {
int retval;
if (db_opened) {
retval = boinc_db.ping();
if (retval) {
log_messages.printf(MSG_CRITICAL,
"lost connection to database - trying to reconnect\n"
);
} else {
return 0;
}
}
retval = boinc_db.open(
config.db_name, config.db_host, config.db_user, config.db_passwd
);
if (retval) {
log_messages.printf(MSG_CRITICAL, "can't open database: %s\n",
boinc_db.error_string()
);
return retval;
}
db_opened = true;
return 0;
}
// If the scheduler 'hangs' (e.g. because DB is slow),
// Apache will send it a SIGTERM.
// Record this in the log file and close the DB conn.
//
void sigterm_handler(int /*signo*/) {
if (db_opened) {
boinc_db.close();
}
log_messages.printf(MSG_CRITICAL,
"Caught SIGTERM (sent by Apache); exiting\n"
);
unlock_sched();
boinc::fflush((FILE*)NULL);
exit(1);
return;
}
static void log_request_headers(int& length) {
char *cl=getenv("CONTENT_LENGTH");
char *ri=getenv("REMOTE_ADDR");
char *rm=getenv("REQUEST_METHOD");
char *ct=getenv("CONTENT_TYPE");
char *ha=getenv("HTTP_ACCEPT");
char *hu=getenv("HTTP_USER_AGENT");
if (config.debug_request_details) {
log_messages.printf(MSG_NORMAL,
"(req details) REQUEST_METHOD=%s CONTENT_TYPE=%s HTTP_ACCEPT=%s HTTP_USER_AGENT=%s\n",
rm?rm:"" , ct?ct:"", ha?ha:"", hu?hu:""
);
}
if (!cl) {
log_messages.printf(MSG_CRITICAL,
"CONTENT_LENGTH environment variable not set\n"
);
} else {
length=atoi(cl);
if (config.debug_request_details) {
log_messages.printf(MSG_NORMAL,
"CONTENT_LENGTH=%d from %s\n", length, ri?ri:"[Unknown]"
);
}
}
}
#if DUMP_CORE_ON_SEGV
void set_core_dump_size_limit() {
struct rlimit limit;
if (getrlimit(RLIMIT_CORE, &limit)) {
log_messages.printf(MSG_CRITICAL,
"Unable to read resource limit for core dump size.\n"
);
} else {
char short_string[256], *short_message=short_string;
short_message += sprintf(short_message,"Default resource limit for core dump size curr=");
if (limit.rlim_cur == RLIM_INFINITY) {
short_message += sprintf(short_message,"Inf max=");
} else {
short_message += sprintf(short_message,"%d max=", (int)limit.rlim_cur);
}
if (limit.rlim_max == RLIM_INFINITY) {
short_message += sprintf(short_message,"Inf\n");
} else {
short_message += sprintf(short_message,"%d\n", (int)limit.rlim_max);
}
log_messages.printf(MSG_DEBUG, "%s", short_string);
// now set limit to the maximum allowed value
limit.rlim_cur=limit.rlim_max;
if (setrlimit(RLIMIT_CORE, &limit)) {
log_messages.printf(MSG_CRITICAL,
"Unable to set current resource limit for core dump size to max value.\n"
);
} else {
log_messages.printf(MSG_DEBUG,
"Set limit for core dump size to max value.\n"
);
}
}
}
#endif
void attach_to_feeder_shmem() {
char path[MAXPATHLEN];
strlcpy(path, config.project_dir, sizeof(path));
get_key(path, 'a', sema_key);
int i, retval;
void* p;
retval = attach_shmem(config.shmem_key, &p);
if (retval || p==0) {
log_messages.printf(MSG_CRITICAL,
"Can't attach shmem: %d (feeder not running?)\n",
retval
);
log_messages.printf(MSG_CRITICAL,
"uid %d euid %d gid %d eguid%d\n",
getuid(), geteuid(), getgid(), getegid()
);
send_message(
"Server error: feeder not running", config.maintenance_delay
);
exit(0);
} else {
ssp = (SCHED_SHMEM*)p;
retval = ssp->verify();
if (retval) {
log_messages.printf(MSG_CRITICAL,
"shmem has wrong struct sizes - recompile\n"
);
send_message("Server error: recompile needed", config.maintenance_delay);
exit(0);
}
for (i=0; i<10; i++) {
if (ssp->ready) break;
log_messages.printf(MSG_DEBUG,
"waiting for ready flag\n"
);
sleep(1);
}
if (!ssp->ready) {
log_messages.printf(MSG_CRITICAL,
"feeder doesn't seem to be running\n"
);
send_message(
"Server error: feeder not running", config.maintenance_delay
);
exit(0);
}
}
all_apps_use_hr = true;
for (i=0; i<ssp->napps; i++) {
if (!ssp->apps[i].homogeneous_redundancy) {
all_apps_use_hr = false;
break;
}
}
}
inline static const char* get_remote_addr() {
const char * r = getenv("REMOTE_ADDR");
return r ? r : "?.?.?.?";
}
#if 0 // performance test for XML parsing (use a large request)
int main(int, char**) {
SCHEDULER_REQUEST sreq;
FILE* f = fopen("req", "r");
MIOFILE mf;
XML_PARSER xp(&mf);
mf.init_file(f);
for (int i=0; i<10; i++) {
sreq.parse(xp);
fseek(f, 0, SEEK_SET);
}
}
#else
#if !defined(PLAN_CLASS_TEST)
int main(int argc, char** argv) {
FILE* fin, *fout;
int i, retval;
char req_path[MAXPATHLEN], reply_path[MAXPATHLEN];
char log_path[MAXPATHLEN], path[MAXPATHLEN];
unsigned int counter=0;
char* code_sign_key;
int length = -1;
log_messages.pid = getpid();
bool debug_log = false;
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "--batch")) {
batch = true;
continue;
} else if (!strcmp(argv[i], "--mark_jobs_done")) {
mark_jobs_done = true;
} else if (!strcmp(argv[i], "--debug_log")) {
debug_log = true;
#ifdef GCL_SIMULATOR
} else if (!strcmp(argv[i], "--simulator")) {
if(!argv[++i]) {
log_messages.printf(MSG_CRITICAL, "%s requires an argument\n\n", argv[--i]);
usage(argv[0]);
exit(1);
}
simtime = atof(argv[i]);
#endif
} else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
usage(argv[0]);
exit(0);
} else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
printf("%s\n", SVN_VERSION);
exit(0);
} else if (strlen(argv[i])){
log_messages.printf(MSG_CRITICAL, "unknown command line argument: %s\n\n", argv[i]);
usage(argv[0]);
exit(1);
}
}
// install a signal handler that catches SIGTERMS sent by Apache if the CGI
// times out.
//
signal(SIGTERM, sigterm_handler);
if (debug_log) {
if (!boinc::freopen("debug_log", "w", stderr)) {
boinc::fprintf(stderr, "Can't redirect stderr\n");
exit(1);
}
} else {
char *stderr_buffer;
if (get_log_path(path, "scheduler.log") == ERR_MKDIR) {
boinc::fprintf(stderr, "Can't create log directory '%s' (errno: %d)\n", path, errno);
}
#ifndef _USING_FCGI_
char buf[256];
if (!freopen(path, "a", stderr)) {
fprintf(stderr, "Can't redirect stderr\n");
sprintf(buf, "Server can't open log file (%s)", path);
send_message(buf, config.maintenance_delay);
exit(1);
}
#else
FILE* f = boinc::fopen(path, "a");
if (f) {
log_messages.redirect(f);
} else {
char buf[256];
boinc::fprintf(stderr, "Can't redirect FCGI log messages\n");
sprintf(buf, "Server can't open log file for FCGI (%s)", path);
send_message(buf, config.maintenance_delay);
exit(1);
}
#endif
// install a larger buffer for stderr. This ensures that
// log information from different scheduler requests running
// in parallel aren't intermingled in the log file.
//
if (config.scheduler_log_buffer) {
stderr_buffer = (char*)malloc(config.scheduler_log_buffer);
if (!stderr_buffer) {
log_messages.printf(MSG_CRITICAL,
"Unable to allocate stderr buffer\n"
);
} else {
#ifdef _USING_FCGI_
retval = setvbuf(
f->stdio_stream, stderr_buffer, _IOFBF,
config.scheduler_log_buffer
);
#else
retval = setvbuf(
stderr, stderr_buffer, _IOFBF, config.scheduler_log_buffer
);
#endif
if (retval) {
log_messages.printf(MSG_CRITICAL,
"Unable to change stderr buffering\n"
);
}
}
}
}
srand(time(0)+getpid());
log_messages.set_debug_level(DEBUG_LEVEL);
#if DUMP_CORE_ON_SEGV
set_core_dump_size_limit();
#endif
retval = config.parse_file();
if (retval) {
log_messages.printf(MSG_CRITICAL,
"Can't parse config.xml: %s\n", boincerror(retval)
);
send_message("Server can't parse configuration file", config.maintenance_delay);
exit(0);
}
log_messages.set_debug_level(config.sched_debug_level);
if (config.sched_debug_level == 4) g_print_queries = true;
gui_urls.init();
project_files.init();
init_file_delete_regex();
sprintf(path, "%s/code_sign_public", config.key_dir);
retval = read_file_malloc(path, code_sign_key);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"Can't read code sign key file (%s)\n", path
);
send_message("Server can't find key file", config.maintenance_delay);
exit(0);
}
strip_whitespace(code_sign_key);
g_pid = getpid();
#ifdef _USING_FCGI_
//while(FCGI_Accept() >= 0 && counter < MAX_FCGI_COUNT) {
while(FCGI_Accept() >= 0) {
counter++;
log_messages.set_indent_level(0);
#endif
if (config.debug_request_headers) {
log_request_headers(length);
}
if (!debug_log && check_stop_sched()) {
send_message(
"Project is temporarily shut down for maintenance",
config.maintenance_delay
);
goto done;
}
if (!ssp) {
attach_to_feeder_shmem();
}
if (!ssp) {
send_message("Server error: can't attach shared memory", config.maintenance_delay);
goto done;
}
if (config.keyword_sched) {
keyword_sched_init();
}
if (strlen(config.debug_req_reply_dir)) {
struct stat statbuf;
// the code below is convoluted because,
// instead of going from stdin to stdout directly,
// we go via a pair of disk files
// (this makes it easy to save the input,
// and to know the length of the output).
// NOTE: to use this, you must create group-writeable dirs
// boinc_req and boinc_reply in the project dir
//
sprintf(req_path, "%s/%d_%u_sched_request.xml", config.debug_req_reply_dir, g_pid, counter);
sprintf(reply_path, "%s/%d_%u_sched_reply.xml", config.debug_req_reply_dir, g_pid, counter);
// keep an own 'log' per PID in case general logging fails
// this allows to associate at least the scheduler request with the client
// IP address (as shown in httpd error log) in case of a crash
sprintf(log_path, "%s/%d_%u_sched.log", config.debug_req_reply_dir, g_pid, counter);
fout = boinc::fopen(log_path, "a");
if (!fout) {
log_messages.printf(MSG_CRITICAL,
"can't write client log file %s\n", log_path
);
exit(1);
}
boinc::fprintf(fout, "PID: %d Client IP: %s\n", g_pid, get_remote_addr());
boinc::fclose(fout);
log_messages.printf(MSG_DEBUG,
"keeping sched_request in %s, sched_reply in %s, custom log in %s\n",
req_path, reply_path, log_path
);
fout = boinc::fopen(req_path, "w");
if (!fout) {
log_messages.printf(MSG_CRITICAL,
"can't write request file\n"
);
exit(1);
}
copy_stream(stdin, fout);
boinc::fclose(fout);
stat(req_path, &statbuf);
if (length>=0 && (statbuf.st_size != length)) {
log_messages.printf(MSG_CRITICAL,
"Request length %d != CONTENT_LENGTH %d\n",
(int)statbuf.st_size, length
);
}
fin = boinc::fopen(req_path, "r");
if (!fin) {
log_messages.printf(MSG_CRITICAL,
"can't read request file\n"
);
exit(1);
}
fout = boinc::fopen(reply_path, "w");
if (!fout) {
log_messages.printf(MSG_CRITICAL,
"can't write reply file\n"
);
exit(1);
}
handle_request(fin, fout, code_sign_key);
boinc::fclose(fin);
boinc::fclose(fout);
fin = boinc::fopen(reply_path, "r");
if (!fin) {
log_messages.printf(MSG_CRITICAL,
"can't read reply file\n"
);
exit(1);
}
copy_stream(fin, stdout);
boinc::fclose(fin);
// if not contacted from a client, don't keep the log files
/* not sure what lead to the assumption of a client setting
CONTENT_LENGTH, but it's wrong at least on our current
project / Apache / Client configuration. Commented out.
if (getenv("CONTENT_LENGTH")) {
unlink(req_path);
unlink(reply_path);
}
*/
#ifndef _USING_FCGI_
} else if (batch) {
while (!feof(stdin)) {
handle_request(stdin, stdout, code_sign_key);
fflush(stdout);
}
#endif
} else {
handle_request(stdin, stdout, code_sign_key);
boinc::fflush(stderr);
}
done:
#ifdef _USING_FCGI_
if (config.debug_fcgi) {
log_messages.printf(MSG_NORMAL,
"FCGI: counter: %d\n", counter
);
}
log_messages.flush();
} // do()
if (counter == MAX_FCGI_COUNT) {
boinc::fprintf(stderr, "FCGI: counter passed MAX_FCGI_COUNT - exiting..\n");
} else {
boinc::fprintf(stderr, "FCGI: FCGI_Accept failed - exiting..\n");
}
// when exiting, write headers back to apache so it won't complain
// about "incomplete headers"
boinc::fprintf(stdout,"Content-type: text/plain\n\n");
#endif
if (db_opened) {
boinc_db.close();
}
}
#endif
#endif
// the following stuff is here because if you put it in sched_limit.cpp
// you get "ssp undefined" in programs other than cgi
void RSC_JOB_LIMIT::print_log(const char* rsc_name) {
log_messages.printf(MSG_NORMAL,
"[quota] %s: base %d scaled %d njobs %d\n",
rsc_name, base_limit, scaled_limit, njobs
);
}
void JOB_LIMIT::print_log() {
if (total.any_limit()) total.print_log("total");
if (proc_type_limits[0].any_limit()) proc_type_limits[0].print_log("CPU");
if (proc_type_limits[1].any_limit()) proc_type_limits[1].print_log("GPU");
}
void JOB_LIMITS::print_log() {
log_messages.printf(MSG_NORMAL, "[quota] Overall limits on jobs in progress:\n");
project_limits.print_log();
for (unsigned int i=0; i<app_limits.size(); i++) {
if (app_limits[i].any_limit()) {
APP* app = ssp->lookup_app_name(app_limits[i].app_name);
if (!app) continue;
log_messages.printf(MSG_NORMAL,
"[quota] Limits for %s:\n", app->name
);
app_limits[i].print_log();
}
}
}