forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_request.cpp
1525 lines (1391 loc) · 50.7 KB
/
handle_request.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 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/>.
// Handle a scheduling server RPC
#include "config.h"
#ifdef _USING_FCGI_
#include "boinc_fcgi.h"
#else
#include <cstdio>
#endif
#include <cassert>
#include <cstdlib>
#include <vector>
#include <string>
#include <cstring>
#include <ctime>
#include <cmath>
#include <unistd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include "backend_lib.h"
#include "boinc_db.h"
#include "error_numbers.h"
#include "filesys.h"
#include "parse.h"
#include "str_replace.h"
#include "str_util.h"
#include "util.h"
#include "sched_vda.h"
#include "credit.h"
#include "sched_files.h"
#include "sched_main.h"
#include "sched_types.h"
#include "sched_util.h"
#include "handle_request.h"
#include "sched_msgs.h"
#include "sched_resend.h"
#include "sched_send.h"
#include "sched_config.h"
#include "sched_locality.h"
#include "sched_result.h"
#include "sched_customize.h"
#include "time_stats_log.h"
// are the 2 hosts obviously different computers?
//
static bool obviously_different(HOST& h1, HOST& h2) {
if (h1.p_ncpus != h2.p_ncpus) return true;
if (strcmp(h1.p_vendor, h2.p_vendor)) return true;
if (strcmp(h1.p_model, h2.p_model)) return true;
if (strcmp(h1.os_name, h2.os_name)) return true;
if (strcmp(h1.os_version, h2.os_version)) return true;
return false;
}
// find the user's most recently-created host with given various characteristics
//
static bool find_host_by_other(DB_USER& user, HOST req_host, DB_HOST& host) {
char buf[2048];
char dn[512], ip[512], os[512], pm[512];
// don't dig through hosts of these users
// prevents flooding the DB with slow queries from users with many hosts
//
for (unsigned int i=0; i < config.dont_search_host_for_userid.size(); i++) {
if (user.id == config.dont_search_host_for_userid[i]) {
return false;
}
}
// Only check if all the fields are populated
//
if (strlen(req_host.domain_name) && strlen(req_host.last_ip_addr) && strlen(req_host.os_name) && strlen(req_host.p_model)) {
safe_strcpy(dn, req_host.domain_name);
escape_string(dn, sizeof(dn));
safe_strcpy(ip, req_host.last_ip_addr);
escape_string(ip, sizeof(ip));
safe_strcpy(os, req_host.os_name);
escape_string(os, sizeof(os));
safe_strcpy(pm, req_host.p_model);
escape_string(pm, sizeof(pm));
sprintf(buf,
"where userid=%lu and id>%lu and domain_name='%s' and last_ip_addr = '%s' and os_name = '%s' and p_model = '%s'"
" and m_nbytes = %lf order by id desc", user.id, req_host.id, dn, ip, os, pm, req_host.m_nbytes
);
if (!host.enumerate(buf)) {
host.end_enumerate();
return true;
}
}
return false;
}
static void send_error_message(const char* msg, int delay) {
g_reply->insert_message(msg, "low");
g_reply->set_delay(delay);
g_reply->nucleus_only = true;
}
// Try to lock a file with name based on host ID,
// to prevent 2 schedulers from running at same time for same host.
// Return:
// 0 if successful
// In this case store file descriptor in reply struct so we can unlock later
// In other cases store -1 in reply struct
// PID (>0) if another process has lock
// -1 if error (e.g. can't create file)
//
int lock_sched() {
char filename[256];
char pid_string[16];
int fd, pid, count;
g_reply->lockfile_fd=-1;
sprintf(filename, "%s/CGI_%07lu",
config.sched_lockfile_dir, g_reply->host.id
);
fd = open(filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd < 0) return -1;
// if we can't get an advisory write lock on the file,
// return the PID of the process that DOES hold the lock.
// (or -1 if failure)
//
pid = mylockf(fd);
if (pid) {
close(fd);
return pid;
}
// write PID into the CGI_<HOSTID> file and flush to disk
//
count = sprintf(pid_string, "%d\n", getpid());
ssize_t n = write(fd, pid_string, count);
if (n < 0) {
close(fd);
return -1;
}
fsync(fd);
g_reply->lockfile_fd = fd;
return 0;
}
// unlock and delete per-host lockfile
//
void unlock_sched() {
char filename[256];
if (g_reply->lockfile_fd < 0) return;
sprintf(filename, "%s/CGI_%07lu", config.sched_lockfile_dir, g_reply->host.id);
unlink(filename);
close(g_reply->lockfile_fd);
}
// find the user's most recently-created host with given host CPID
//
static bool find_host_by_cpid(DB_USER& user, char* host_cpid, DB_HOST& host) {
char buf[1024], buf2[256];
sprintf(buf, "%s%s", host_cpid, user.email_addr);
md5_block((const unsigned char*)buf, strlen(buf), buf2);
sprintf(buf,
"where userid=%lu and host_cpid='%s' order by id desc", user.id, buf2
);
if (!host.enumerate(buf)) {
host.end_enumerate();
return true;
}
return false;
}
// Called when there's evidence that the host has detached.
// Mark in-progress results for the given host
// as server state OVER, outcome CLIENT_DETACHED.
// This serves two purposes:
// 1) make sure we don't resend these results to the host
// (they may be the reason the user detached)
// 2) trigger the generation of new results for these WUs
//
static void mark_results_over(DB_HOST& host) {
char buf[256], buf2[256];
DB_RESULT result;
sprintf(buf, "where hostid=%lu and server_state=%d",
host.id,
RESULT_SERVER_STATE_IN_PROGRESS
);
while (!result.enumerate(buf)) {
sprintf(buf2,
"server_state=%d, outcome=%d, received_time = %ld",
RESULT_SERVER_STATE_OVER,
RESULT_OUTCOME_CLIENT_DETACHED,
time(0)
);
result.update_field(buf2);
// and trigger WU transition
//
DB_WORKUNIT wu;
wu.id = result.workunitid;
sprintf(buf2, "transition_time=%d", (int)time(0));
wu.update_field(buf2);
log_messages.printf(MSG_CRITICAL,
"[HOST#%lu] [RESULT#%lu] [WU#%lu] changed CPID: marking in-progress result %s as client error!\n",
host.id, result.id, result.workunitid, result.name
);
}
}
// Based on the info in the request message,
// look up the host and its user, and make sure the authenticator matches.
// Some special cases:
// 1) If no host ID is supplied, or if RPC seqno mismatch,
// create a new host record
// 2) If the host record specified by g_request->hostid is a "zombie"
// (i.e. it was merged with another host via the web site)
// then follow links to find the proper host
//
// POSTCONDITION:
// If this function returns zero, then:
// - reply.host contains a valid host record (possibly new)
// - reply.user contains a valid user record
// - if user belongs to a team, reply.team contains team record
//
int authenticate_user() {
int retval;
char buf[1024];
DB_HOST host;
DB_USER user;
DB_TEAM team;
if (g_request->hostid) {
retval = host.lookup_id(g_request->hostid);
while (!retval && host.userid==0) {
// if host record is zombie, follow link to new host
//
retval = host.lookup_id(host.rpc_seqno);
if (!retval) {
g_reply->hostid = host.id;
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] forwarding to new host ID %lu\n",
g_request->hostid, host.id
);
}
}
if (retval) {
g_reply->insert_message("Can't find host record", "low");
log_messages.printf(MSG_NORMAL,
"[HOST#%lu?] can't find host\n",
g_request->hostid
);
g_request->hostid = 0;
goto lookup_user_and_make_new_host;
}
g_reply->host = host;
// look up user based on the ID in host record,
// and see if the authenticator matches (regular or weak)
//
g_request->using_weak_auth = false;
sprintf(buf, "where id=%lu", host.userid);
retval = user.lookup(buf);
if (!retval && !strcmp(user.authenticator, g_request->authenticator)) {
// req auth matches user auth - go on
} else {
if (!retval) {
// user for host.userid exists - check weak auth
//
get_weak_auth(user, buf);
if (!strcmp(buf, g_request->authenticator)) {
g_request->using_weak_auth = true;
log_messages.printf(MSG_DEBUG,
"[HOST#%lu] accepting weak authenticator\n",
host.id
);
}
}
if (!g_request->using_weak_auth) {
// weak auth failed - look up user based on authenticator
//
strlcpy(
user.authenticator, g_request->authenticator, sizeof(user.authenticator)
);
escape_string(user.authenticator, sizeof(user.authenticator));
sprintf(buf, "where authenticator='%s'", user.authenticator);
retval = user.lookup(buf);
if (retval) {
g_reply->insert_message(
_("Invalid or missing account key. To fix, remove and add this project."),
"notice"
);
g_reply->set_delay(DELAY_MISSING_KEY);
g_reply->nucleus_only = true;
log_messages.printf(MSG_CRITICAL,
"[HOST#%lu] [USER#%lu] Bad authenticator '%s'\n",
host.id, user.id, g_request->authenticator
);
return ERR_AUTHENTICATOR;
}
}
}
g_reply->user = user;
if (host.userid != user.id) {
// If the request's host ID isn't consistent with the authenticator,
// create a new host record.
//
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] [USER#%lu] inconsistent host ID; creating new host\n",
host.id, user.id
);
goto make_new_host;
}
// If the seqno from the host is less than what we expect,
// the user must have copied the state file to a different host.
// Make a new host record.
//
if (!batch && g_request->rpc_seqno < g_reply->host.rpc_seqno) {
g_request->hostid = 0;
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] [USER#%lu] RPC seqno %d less than expected %d; creating new host\n",
g_reply->host.id, user.id, g_request->rpc_seqno, g_reply->host.rpc_seqno
);
goto make_new_host;
}
} else {
// Here no hostid was given, or the ID was bad.
// Look up the user, then create a new host record
//
lookup_user_and_make_new_host:
// if authenticator contains _, it's a weak auth
//
if (strchr(g_request->authenticator, '_')) {
int userid = atoi(g_request->authenticator);
retval = user.lookup_id(userid);
if (!retval) {
get_weak_auth(user, buf);
if (strcmp(buf, g_request->authenticator)) {
retval = ERR_AUTHENTICATOR;
}
}
} else {
strlcpy(
user.authenticator, g_request->authenticator,
sizeof(user.authenticator)
);
escape_string(user.authenticator, sizeof(user.authenticator));
sprintf(buf, "where authenticator='%s'", user.authenticator);
retval = user.lookup(buf);
}
if (retval) {
g_reply->insert_message(
"Invalid or missing account key. To fix, remove and add this project .",
"low"
);
g_reply->set_delay(DELAY_MISSING_KEY);
log_messages.printf(MSG_CRITICAL,
"[HOST#<none>] Bad authenticator '%s': %s\n",
g_request->authenticator, boincerror(retval)
);
return ERR_AUTHENTICATOR;
}
g_reply->user = user;
// If host CPID is present,
// scan backwards through this user's hosts,
// looking for one with the same host CPID.
// If we find one, it means the user detached and reattached.
// Use the existing host record,
// and mark in-progress results as over.
//
if (strlen(g_request->host.host_cpid)) {
if (find_host_by_cpid(user, g_request->host.host_cpid, host)) {
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] [USER#%lu] No host ID in request, but host with matching CPID found.\n",
host.id, host.userid
);
if (obviously_different(host, g_request->host)) {
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] [USER#%lu] But that host doesn't match request.\n",
host.id, host.userid
);
} else {
if ((g_request->allow_multiple_clients != 1)
&& (g_request->other_results.size() == 0)
) {
mark_results_over(host);
}
goto got_host;
}
}
}
make_new_host:
// One final attempt to locate an existing host record:
// scan backwards through this user's hosts,
// looking for one with the same host name,
// IP address, processor and amount of RAM.
// If found, use the existing host record,
// and mark in-progress results as over.
//
// NOTE: If the client was run with --allow_multiple_clients, skip this.
//
if ((g_request->allow_multiple_clients != 1)
&& find_host_by_other(user, g_request->host, host)
) {
log_messages.printf(MSG_NORMAL,
"[HOST#%lu] [USER#%lu] Found similar existing host for this user - assigned.\n",
host.id, host.userid
);
if (g_request->other_results.size() == 0) {
// mark host's jobs as abandoned
// if client has no jobs in progress
//
mark_results_over(host);
}
goto got_host;
}
// either of the above cases,
// or host ID didn't match user ID,
// or RPC seqno was too low.
//
// Create a new host.
// g_reply->user is filled in and valid at this point
//
host = g_request->host;
host.id = 0;
host.create_time = time(0);
host.userid = g_reply->user.id;
host.rpc_seqno = 0;
host.expavg_time = time(0);
safe_strcpy(host.venue, g_reply->user.venue);
host.fix_nans();
retval = host.insert();
if (retval) {
g_reply->insert_message(
"Couldn't create host record in database", "low"
);
boinc_db.print_error("host.insert()");
log_messages.printf(MSG_CRITICAL, "host.insert() failed\n");
return retval;
}
host.id = boinc_db.insert_id();
got_host:
g_reply->host = host;
g_reply->hostid = g_reply->host.id;
// this tells client to updates its host ID
g_request->rpc_seqno = 0;
// this value eventually gets written to host DB record;
// for new hosts it must be zero.
// This kludge forces this.
}
// have user record in g_reply->user at this point
//
if (g_reply->user.teamid) {
retval = team.lookup_id(g_reply->user.teamid);
if (!retval) g_reply->team = team;
}
// compute email hash
//
md5_block(
(unsigned char*)g_reply->user.email_addr,
strlen(g_reply->user.email_addr),
g_reply->email_hash
);
// if new user CPID, update user record
//
if (!g_request->using_weak_auth && strlen(g_request->cross_project_id)) {
if (strcmp(g_request->cross_project_id, g_reply->user.cross_project_id)) {
user.id = g_reply->user.id;
escape_string(g_request->cross_project_id, sizeof(g_request->cross_project_id));
sprintf(buf, "cross_project_id='%s'", g_request->cross_project_id);
unescape_string(g_request->cross_project_id, sizeof(g_request->cross_project_id));
user.update_field(buf);
}
}
return 0;
}
inline static const char* get_remote_addr() {
// Server is behind a load balancer or proxy
const char* p = getenv("HTTP_X_FORWARDED_FOR");
if (p) {
return p;
}
const char * r = getenv("REMOTE_ADDR");
return r ? r : "?.?.?.?";
}
// modify host struct based on request.
// Copy all fields that are determined by the client.
//
static int modify_host_struct(HOST& host) {
host.timezone = g_request->host.timezone;
strlcpy(host.domain_name, g_request->host.domain_name, sizeof(host.domain_name));
char buf[1024], buf2[1024];
sprintf(buf, "[BOINC|%d.%d.%d",
g_request->core_client_major_version,
g_request->core_client_minor_version,
g_request->core_client_release
);
if (strlen(g_request->client_brand)) {
strcat(buf, "|");
strcat(buf, g_request->client_brand);
}
strcat(buf, "]");
g_request->coprocs.summary_string(buf2, sizeof(buf2));
strlcpy(host.serialnum, buf, sizeof(host.serialnum));
strlcat(host.serialnum, buf2, sizeof(host.serialnum));
if (strlen(g_request->host.virtualbox_version)) {
sprintf(buf2, "[vbox|%s]", g_request->host.virtualbox_version);
strlcat(host.serialnum, buf2, sizeof(host.serialnum));
}
if (strcmp(host.last_ip_addr, g_request->host.last_ip_addr)) {
strlcpy(
host.last_ip_addr, g_request->host.last_ip_addr,
sizeof(host.last_ip_addr)
);
host.nsame_ip_addr = 0;
} else {
host.nsame_ip_addr++;
}
host.on_frac = g_request->host.on_frac;
host.connected_frac = g_request->host.connected_frac;
host.active_frac = g_request->host.active_frac;
host.gpu_active_frac = g_request->host.gpu_active_frac;
host.cpu_and_network_available_frac = g_request->host.cpu_and_network_available_frac;
host.client_start_time = g_request->host.client_start_time;
host.previous_uptime = g_request->host.previous_uptime;
host.duration_correction_factor = g_request->host.duration_correction_factor;
host.p_ncpus = g_request->host.p_ncpus;
strlcpy(host.p_vendor, g_request->host.p_vendor, sizeof(host.p_vendor));
// unlikely this will change
strlcpy(host.p_model, g_request->host.p_model, sizeof(host.p_model));
host.p_fpops = g_request->host.p_fpops;
host.p_iops = g_request->host.p_iops;
host.p_membw = g_request->host.p_membw;
strlcpy(host.os_name, g_request->host.os_name, sizeof(host.os_name));
strlcpy(host.os_version, g_request->host.os_version, sizeof(host.os_version));
host.m_nbytes = g_request->host.m_nbytes;
host.m_cache = g_request->host.m_cache;
host.m_swap = g_request->host.m_swap;
host.d_total = g_request->host.d_total;
host.d_free = g_request->host.d_free;
host.d_boinc_used_total = g_request->host.d_boinc_used_total;
host.d_boinc_used_project = g_request->host.d_boinc_used_project;
host.n_bwup = g_request->host.n_bwup;
host.n_bwdown = g_request->host.n_bwdown;
if (strlen(g_request->host.host_cpid)) {
safe_strcpy(host.host_cpid, g_request->host.host_cpid);
}
strlcpy(host.product_name, g_request->host.product_name, sizeof(host.product_name));
host.fix_nans();
return 0;
}
// update the DB record to the values in "xhost"
// "initial_host" stores the current DB values;
// update only those fields that have changed
//
static int update_host_record(HOST& initial_host, HOST& xhost, USER& user) {
DB_HOST host;
int retval;
char buf[1024];
host = xhost;
// hash the CPID reported by the host with the user's email address.
// This prevents one user from spoofing another one's host.
//
if (strlen(host.host_cpid)) {
sprintf(buf, "%s%s", host.host_cpid, user.email_addr);
md5_block((const unsigned char*)buf, strlen(buf), host.host_cpid);
}
const char* p = get_remote_addr();
if (p) {
strlcpy(host.external_ip_addr, p, sizeof(host.external_ip_addr));
}
retval = host.update_diff_sched(initial_host);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"host.update() failed: %s\n", boincerror(retval)
);
}
return 0;
}
inline const char* reason_str(int n) {
switch (n) {
case ABORT_REASON_NOT_FOUND: return "result not in request";
case ABORT_REASON_WU_CANCELLED: return "WU cancelled";
case ABORT_REASON_ASSIMILATED: return "WU assimilated";
case ABORT_REASON_TIMED_OUT: return "result timed out";
}
return "Unknown";
}
// Figure out which of the results the host currently has
// should be aborted outright, or aborted if not started yet
//
int send_result_abort() {
int aborts_sent = 0;
int retval = 0;
DB_IN_PROGRESS_RESULT result;
std::string result_names;
unsigned int i;
if (g_request->other_results.size() == 0) {
return 0;
}
// build list of result names
//
for (i=0; i<g_request->other_results.size(); i++) {
OTHER_RESULT& orp=g_request->other_results[i];
orp.abort = true;
// if the host has a result not in the DB, abort it
orp.abort_if_not_started = false;
orp.reason = ABORT_REASON_NOT_FOUND;
if (i > 0) result_names.append(", ");
result_names.append("'");
char buf[1024];
safe_strcpy(buf, orp.name);
escape_string(buf, sizeof(buf));
result_names.append(buf);
result_names.append("'");
}
// look up selected fields from the results and their WUs,
// and decide if they should be aborted
//
while (!(retval = result.enumerate(g_reply->host.id, result_names.c_str()))) {
for (i=0; i<g_request->other_results.size(); i++) {
OTHER_RESULT& orp = g_request->other_results[i];
if (!strcmp(orp.name, result.result_name)) {
if (result.error_mask&WU_ERROR_CANCELLED ) {
// if the WU has been canceled, abort the result
//
orp.abort = true;
orp.abort_if_not_started = false;
orp.reason = ABORT_REASON_WU_CANCELLED;
} else if (result.assimilate_state == ASSIMILATE_DONE) {
// if the WU has been assimilated, abort if not started
//
orp.abort = false;
orp.abort_if_not_started = true;
orp.reason = ABORT_REASON_ASSIMILATED;
} else if (result.server_state == RESULT_SERVER_STATE_OVER
&& result.outcome == RESULT_OUTCOME_NO_REPLY
) {
// if timed out, abort if not started
//
orp.abort = false;
orp.abort_if_not_started = true;
orp.reason = ABORT_REASON_TIMED_OUT;
} else {
// all is good with the result - let it process
orp.abort = false;
orp.abort_if_not_started = false;
}
break;
}
}
}
// If enumeration returned an error, don't send any aborts
//
if (retval && (retval != ERR_DB_NOT_FOUND)) {
return retval;
}
// loop through the results and send the appropriate message (if any)
//
for (i=0; i<g_request->other_results.size(); i++) {
OTHER_RESULT& orp = g_request->other_results[i];
if (orp.abort) {
g_reply->result_aborts.push_back(orp.name);
log_messages.printf(MSG_NORMAL,
"[HOST#%lu]: Send result_abort for result %s; reason: %s\n",
g_reply->host.id, orp.name, reason_str(orp.reason)
);
// send user message
char buf[256];
sprintf(buf, "Result %s is no longer usable", orp.name);
g_reply->insert_message(buf, "low");
} else if (orp.abort_if_not_started) {
g_reply->result_abort_if_not_starteds.push_back(orp.name);
log_messages.printf(MSG_NORMAL,
"[HOST#%lu]: Send result_abort_if_unstarted for result %s; reason %d\n",
g_reply->host.id, orp.name, orp.reason
);
}
}
return aborts_sent;
}
// 1) Decide which global prefs to use for sched decisions: either
// - <working_global_prefs> from request msg
// - <global_prefs> from request message
// - prefs from user DB record
// and parse them into g_request->global_prefs.
// 2) update prefs in user record if needed
// 2) send global prefs in reply msg if needed
//
int handle_global_prefs() {
char buf[BLOB_SIZE+256];
g_reply->send_global_prefs = false;
bool have_working_prefs = (strlen(g_request->working_global_prefs_xml)>0);
bool have_master_prefs = (strlen(g_request->global_prefs_xml)>0);
// absent if the host has host-specific prefs
bool have_db_prefs = (strlen(g_reply->user.global_prefs)>0);
bool same_account = !strcmp(
g_request->global_prefs_source_email_hash, g_reply->email_hash
);
double master_mod_time=0, db_mod_time=0, working_mod_time=0;
if (have_master_prefs) {
parse_double(g_request->global_prefs_xml, "<mod_time>", master_mod_time);
if (master_mod_time > dtime()) master_mod_time = dtime();
}
if (have_working_prefs) {
parse_double(g_request->working_global_prefs_xml, "<mod_time>", working_mod_time);
if (working_mod_time > dtime()) working_mod_time = dtime();
}
if (have_db_prefs) {
parse_double(g_reply->user.global_prefs, "<mod_time>", db_mod_time);
if (db_mod_time > dtime()) db_mod_time = dtime();
}
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL,
"[prefs] have_master:%d have_working: %d have_db: %d\n",
have_master_prefs, have_working_prefs, have_db_prefs
);
}
// decide which prefs to use for sched decisions,
// and parse them into g_request->global_prefs
//
if (have_working_prefs) {
g_request->global_prefs.parse(g_request->working_global_prefs_xml, "");
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL, "[prefs] using working prefs\n");
}
} else {
if (have_master_prefs) {
if (have_db_prefs && db_mod_time > master_mod_time) {
g_request->global_prefs.parse(g_reply->user.global_prefs, g_reply->host.venue);
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL,
"[prefs] using db prefs - more recent\n"
);
}
} else {
g_request->global_prefs.parse(g_request->global_prefs_xml, g_reply->host.venue);
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL,
"[prefs] using master prefs\n"
);
}
}
} else {
if (have_db_prefs) {
g_request->global_prefs.parse(g_reply->user.global_prefs, g_reply->host.venue);
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL, "[prefs] using db prefs\n");
}
} else {
g_request->global_prefs.defaults();
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL, "[prefs] using default prefs\n");
}
}
}
}
// decide whether to update DB
//
if (!g_request->using_weak_auth && have_master_prefs) {
bool update_user_record = false;
if (have_db_prefs) {
if (master_mod_time > db_mod_time && same_account) {
update_user_record = true;
}
} else {
if (same_account) update_user_record = true;
}
if (update_user_record) {
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL, "[prefs] updating db prefs\n");
}
safe_strcpy(g_reply->user.global_prefs, g_request->global_prefs_xml);
DB_USER user;
user.id = g_reply->user.id;
escape_string(g_request->global_prefs_xml, sizeof(g_request->global_prefs_xml));
sprintf(buf, "global_prefs='%s'", g_request->global_prefs_xml);
unescape_string(g_request->global_prefs_xml, sizeof(g_request->global_prefs_xml));
int retval = user.update_field(buf);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"user.update_field() failed: %s\n", boincerror(retval)
);
}
}
}
// decide whether to send DB prefs in reply msg
//
if (config.debug_prefs) {
log_messages.printf(MSG_NORMAL,
"[prefs] have DB prefs: %d; dbmod %f; global mod %f; working mod %f\n",
have_db_prefs, db_mod_time, g_request->global_prefs.mod_time, working_mod_time
);
}
if (have_db_prefs && db_mod_time > master_mod_time && db_mod_time > working_mod_time) {
if (config.debug_prefs) {
log_messages.printf(MSG_DEBUG,
"[prefs] sending DB prefs in reply\n"
);
}
g_reply->send_global_prefs = true;
}
return 0;
}
// if the client has an old code sign public key,
// send it the new one, with a signature based on the old one.
// If they don't have a code sign key, send them one.
// Return false if they have a key we don't recognize
// (in which case we won't send them work).
//
bool send_code_sign_key(char* code_sign_key) {
char* oldkey, *signature;
int i, retval;
char path[MAXPATHLEN];
if (!strlen(g_request->code_sign_key)) {
safe_strcpy(g_reply->code_sign_key, code_sign_key);
return true;
}
if (!strcmp(g_request->code_sign_key, code_sign_key)) {
return true;
}
log_messages.printf(MSG_NORMAL, "received old code sign key\n");
// look for a signature file for the client's key.
// These are in pairs of files (N = 0, 1, ...)
// old_key_N: contains an old key
// signature_N: contains a signature for new key,
// based on the old key
// signature_stripped_N: signature for new key w/ trailing \n removed
// (needed for 7.0+ clients, which strip trailing whitespace)
//
// A project can have several of these if it wants,
// e.g. if it changes keys a lot.
//
for (i=0; ; i++) {
sprintf(path, "%s/old_key_%d", config.key_dir, i);
retval = read_file_malloc(path, oldkey);
if (retval) {
// we've scanned all the signature files and
// didn't find one that worked.
// User must reattach.
//
log_messages.printf(MSG_CRITICAL,
"scanned old_key_i files, can find client's key\n"
);
break;
}
strip_whitespace(oldkey);
if (!strcmp(oldkey, g_request->code_sign_key)) {
// We've found the client's key.
// Get the signature for the new key.
//
if (g_request->core_client_major_version < 7) {
sprintf(path, "%s/signature_%d", config.key_dir, i);
} else {
sprintf(path, "%s/signature_stripped_%d", config.key_dir, i);
}
retval = read_file_malloc(path, signature);
if (retval) {
// project is missing the signature file.
// Tell the user to reattach.
//
log_messages.printf(MSG_CRITICAL,
"Missing signature file for old key %d\n", i
);
free(oldkey);
break;
} else {
log_messages.printf(MSG_NORMAL,
"sending new code sign key and signature\n"
);
safe_strcpy(g_reply->code_sign_key, code_sign_key);
safe_strcpy(g_reply->code_sign_key_signature, signature);
free(signature);
free(oldkey);
return true;
}
}
free(oldkey);
}
g_reply->insert_message(
_("The project has changed its security key. Please remove and add this project."),
"notice"
);
return false;
}
// If <min_core_client_version_announced> is set,
// and the core client version is less than this version,
// send a warning to users to upgrade before deadline
// <min_core_client_upgrade_deadline>
//
void warn_user_if_core_client_upgrade_scheduled() {
if (g_request->core_client_version < config.min_core_client_version_announced) {
// time remaining in hours, before upgrade required
int remaining = config.min_core_client_upgrade_deadline-time(0);
remaining /= 3600;
if (remaining > 0) {
char msg[512];
int days = remaining / 24;
int hours = remaining % 24;
sprintf(msg,
"In %d days and %d hours, this project will require a minimum "
"BOINC version of %d.%d.%d. You are currently using "
"version %d.%d.%d; please upgrade before this time.",
days, hours,
config.min_core_client_version_announced / 10000,
(config.min_core_client_version_announced / 100)%100,
config.min_core_client_version_announced % 100,
g_request->core_client_major_version,
g_request->core_client_minor_version,
g_request->core_client_release
);
// make this low priority until three days are left. Then
// bump to high.
//
if (days<3) {
g_reply->insert_message(msg, "notice");
} else {
g_reply->insert_message(msg, "low");
}
log_messages.printf(MSG_DEBUG,
"Sending warning: upgrade client %d.%d.%d within %d days %d hours\n",
g_request->core_client_major_version,
g_request->core_client_minor_version,
g_request->core_client_release,
days, hours
);
}
}
return;