forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
547 lines (470 loc) · 15.2 KB
/
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2015 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/>.
// command-line version of the BOINC client
// This file contains no GUI-related code.
#include "cpp.h"
#ifdef WIN32
#include "boinc_win.h"
#include "sysmon_win.h"
#include "win_util.h"
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#else
#include "config.h"
#if HAVE_SYS_SOCKET_H
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include <sys/stat.h>
#include <syslog.h>
#include <cstdlib>
#include <unistd.h>
#include <csignal>
#ifdef ANDROID
#include "android/log.h"
#endif
#endif
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#if (defined(SANDBOX) && defined(_DEBUG))
#include "SetupSecurity.h"
#endif
#endif
#include "diagnostics.h"
#include "error_numbers.h"
#include "str_util.h"
#include "str_replace.h"
#include "util.h"
#include "prefs.h"
#include "filesys.h"
#include "network.h"
#include "idlemon.h"
#include "client_state.h"
#include "file_names.h"
#include "log_flags.h"
#include "client_msgs.h"
#include "http_curl.h"
#include "sandbox.h"
#include "main.h"
// Log informational messages to system specific places
//
void log_message_startup(const char* msg) {
char evt_msg[2048];
char* time_string = time_to_string(dtime());
snprintf(evt_msg, sizeof(evt_msg),
"%s %s\n",
time_string, msg
);
if (!gstate.executing_as_daemon) {
fprintf(stdout, "%s", evt_msg);
} else {
#ifdef _WIN32
LogEventInfoMessage(evt_msg);
#elif defined(__EMX__)
#elif defined (__APPLE__)
#elif defined (ANDROID)
__android_log_print(ANDROID_LOG_INFO, "BOINC", evt_msg);
#else
syslog(LOG_DAEMON|LOG_INFO, "%s", evt_msg);
#endif
}
}
// Log error messages to system specific places
//
void log_message_error(const char* msg) {
char evt_msg[2048];
char* time_string = time_to_string(dtime());
#ifdef _WIN32
char buf[1024];
snprintf(evt_msg, sizeof(evt_msg),
"%s %s\n"
"GLE: %s\n",
time_string, msg,
windows_format_error_string(GetLastError(), buf, sizeof(buf))
);
#else
snprintf(evt_msg, sizeof(evt_msg),
"%s %s\n",
time_string, msg
);
#endif
if (!gstate.executing_as_daemon) {
fprintf(stderr, "%s", evt_msg);
} else {
#ifdef _WIN32
LogEventErrorMessage(evt_msg);
#elif defined(__EMX__)
#elif defined (__APPLE__)
#elif defined (ANDROID)
__android_log_print(ANDROID_LOG_ERROR, "BOINC", evt_msg);
#else
syslog(LOG_DAEMON|LOG_ERR, "%s", evt_msg);
#endif
}
}
void log_message_error(const char* msg, int error_code) {
char evt_msg[2048];
char* time_string = time_to_string(dtime());
snprintf(evt_msg, sizeof(evt_msg),
"%s %s\n"
"Error Code: %d\n",
time_string, msg, error_code
);
if (!gstate.executing_as_daemon) {
fprintf(stderr, "%s", evt_msg);
} else {
#ifdef _WIN32
LogEventErrorMessage(evt_msg);
#elif defined(__EMX__)
#elif defined (__APPLE__)
#elif defined (ANDROID)
__android_log_print(ANDROID_LOG_ERROR, "BOINC", evt_msg);
#else
syslog(LOG_DAEMON|LOG_ERR, "%s", evt_msg);
#endif
}
}
#ifndef _WIN32
static void signal_handler(int signum) {
msg_printf(NULL, MSG_INFO, "Received signal %d", signum);
switch(signum) {
case SIGHUP:
case SIGINT:
case SIGQUIT:
case SIGTERM:
#ifdef SIGPWR
case SIGPWR:
#endif
gstate.requested_exit = true;
#ifdef __EMX__
// close socket
shutdown(gstate.gui_rpcs.lsock, 2);
#endif
break;
default:
msg_printf(NULL, MSG_INTERNAL_ERROR, "Signal not handled");
}
}
#endif
static void init_core_client(int argc, char** argv) {
setbuf(stdout, 0);
setbuf(stderr, 0);
cc_config.defaults();
gstate.parse_cmdline(argc, argv);
gstate.now = dtime();
#ifdef _WIN32
if (!cc_config.allow_multiple_clients) {
chdir_to_data_dir();
}
#endif
#ifndef _WIN32
if (g_use_sandbox)
// Set file creation mask to be writable by both user and group and
// world-executable but neither world-readable nor world-writable
// Our umask will be inherited by all our child processes
//
umask (6);
#endif
// Initialize the BOINC Diagnostics Framework
int flags =
#ifdef _DEBUG
BOINC_DIAG_MEMORYLEAKCHECKENABLED |
#endif
BOINC_DIAG_DUMPCALLSTACKENABLED |
BOINC_DIAG_HEAPCHECKENABLED |
BOINC_DIAG_TRACETOSTDOUT;
if (gstate.redirect_io || gstate.executing_as_daemon || gstate.detach_console) {
flags |=
BOINC_DIAG_REDIRECTSTDERR |
BOINC_DIAG_REDIRECTSTDOUT;
}
diagnostics_init(flags, "stdoutdae", "stderrdae");
#ifdef _WIN32
// Specify which allocation will cause a debugger to break. Use a previous
// memory leak detection report which looks like this:
// {650} normal block at 0x000000000070A6F0, 24 bytes long.
// Data: < N P p > 80 1E 4E 00 00 00 00 00 50 AE 70 00 00 00 00 00
//_CrtSetBreakAlloc(650);
//_CrtSetBreakAlloc(651);
//_CrtSetBreakAlloc(652);
//_CrtSetBreakAlloc(653);
//_CrtSetBreakAlloc(654);
#endif
read_config_file(true);
// Win32 - detach from console if requested
#ifdef _WIN32
if (gstate.detach_console) {
FreeConsole();
}
#endif
// Unix: install signal handlers
#ifndef _WIN32
// Handle quit signals gracefully
boinc_set_signal_handler(SIGHUP, (handler_t)signal_handler);
boinc_set_signal_handler(SIGINT, (handler_t)signal_handler);
boinc_set_signal_handler(SIGQUIT, (handler_t)signal_handler);
boinc_set_signal_handler(SIGTERM, (handler_t)signal_handler);
#ifdef SIGPWR
boinc_set_signal_handler(SIGPWR, (handler_t)signal_handler);
#endif
#endif
}
// Some dual-GPU laptops (e.g., Macbook Pro) don't power down
// the more powerful GPU until all applications which used them exit.
// To save battery life, the client launches a second instance
// of the client as a child process to detect and get info
// about the GPUs.
// The child process writes the info to a temp file which our main
// client then reads.
//
static void do_gpu_detection(int argc, char** argv) {
vector<string> warnings;
boinc_install_signal_handlers();
gstate.parse_cmdline(argc, argv);
gstate.now = dtime();
int flags =
BOINC_DIAG_DUMPCALLSTACKENABLED |
BOINC_DIAG_HEAPCHECKENABLED |
BOINC_DIAG_TRACETOSTDOUT |
BOINC_DIAG_REDIRECTSTDERR |
BOINC_DIAG_REDIRECTSTDOUT;
diagnostics_init(flags, "stdoutgpudetect", "stderrgpudetect");
read_config_file(true);
coprocs.detect_gpus(warnings);
coprocs.write_coproc_info_file(warnings);
warnings.clear();
}
static int initialize() {
int retval;
if (!cc_config.allow_multiple_clients) {
retval = wait_client_mutex(".", 10);
if (retval) {
log_message_error("Another instance of BOINC is running.");
return ERR_EXEC;
}
}
// Initialize WinSock
#if defined(_WIN32) && defined(USE_WINSOCK)
if (WinsockInitialize() != 0) {
log_message_error("Failed to initialize the Windows Sockets interface.");
return ERR_IO;
}
#endif
curl_init();
#ifdef _WIN32
if(!startup_idle_monitor()) {
log_message_error(
"Failed to initialize the BOINC idle monitor interface."
"BOINC will not be able to determine if the user is idle or not...\n"
);
}
#endif
return 0;
}
static int finalize() {
static bool finalized = false;
if (finalized) return 0;
finalized = true;
gstate.quit_activities();
#ifdef _WIN32
shutdown_idle_monitor();
#ifdef USE_WINSOCK
if (WinsockCleanup()) {
log_message_error("WinSockCleanup() failed");
return ERR_IO;
}
#endif
cleanup_system_monitor();
#endif
curl_cleanup();
#ifdef _DEBUG
gstate.free_mem();
#endif
diagnostics_finish();
gstate.cleanup_completed = true;
return 0;
}
int boinc_main_loop() {
int retval;
retval = initialize();
if (retval) return retval;
#ifdef __APPLE__
// If we run too soon during system boot we can cause a kernel panic
if (gstate.executing_as_daemon) {
if (TickCount() < (120*60)) { // If system has been up for less than 2 minutes
boinc_sleep(30.);
}
}
#endif
retval = gstate.init();
if (retval) {
log_message_error("gstate.init() failed", retval);
return retval;
}
log_message_startup("Initialization completed");
while (1) {
if (!gstate.poll_slow_events()) {
gstate.do_io_or_sleep(POLL_INTERVAL);
}
fflush(stderr);
fflush(stdout);
if (gstate.time_to_exit()) {
msg_printf(NULL, MSG_INFO, "Time to exit");
break;
}
if (gstate.requested_exit) {
if (cc_config.abort_jobs_on_exit) {
if (!gstate.in_abort_sequence) {
msg_printf(NULL, MSG_INFO,
"Exit requested; starting abort sequence"
);
gstate.start_abort_sequence();
}
} else {
msg_printf(NULL, MSG_INFO, "Exiting");
break;
}
}
if (gstate.in_abort_sequence) {
if (gstate.abort_sequence_done()) {
msg_printf(NULL, MSG_INFO, "Abort sequence done; exiting");
break;
}
}
}
return finalize();
}
int main(int argc, char** argv) {
int retval = 0;
coprocs.set_path_to_client(argv[0]); // Used to launch a child process for --detect_gpus
for (int index = 1; index < argc; index++) {
if (strcmp(argv[index], "-daemon") == 0 || strcmp(argv[index], "--daemon") == 0) {
gstate.executing_as_daemon = true;
log_message_startup("BOINC is initializing...");
#if !defined(_WIN32) && !defined(__EMX__) && !defined(__APPLE__)
// from <unistd.h>:
// Detach from the controlling terminal and run in the background
// as system daemon.
// Don't change working directory to root ("/"), but redirect
// standard input, standard output and standard error to /dev/null.
//
retval = daemon(1, 0);
break;
#endif
}
if (!strcmp(argv[index], "--detect_gpus")) {
do_gpu_detection(argc, argv);
return 0;
}
if (!strcmp(argv[index], "--run_test_app")) {
read_config_file(true);
run_test_app();
}
#ifdef _WIN32
// This bit of silliness is required to properly detach when run from within a command
// prompt under Win32. The root cause of the problem is that CMD.EXE does not return
// control to the user until the spawned program exits, detaching from the console is
// not enough. So we need to do the following. If the -detach flag is given, trap it
// prior to the main setup in init_core_client. Reinvoke the program, changing the
// -detach into -detach_phase_two, and then exit. At this point, cmd.exe thinks all is
// well, and returns control to the user. Meanwhile the second invocation will grok the
// -detach_phase_two flag, and detach itself from the console, finally getting us to
// where we want to be.
// FIXME FIXME. Duplicate instances of -detach may cause this to be
// executed unnecessarily. At worst, I think it leads to a few extra
// processes being created and destroyed.
if (strcmp(argv[index], "-detach") == 0 || strcmp(argv[index], "--detach") == 0 ||
strcmp(argv[index], "-detach_console") == 0 || strcmp(argv[index], "--detach_console") == 0
) {
int i, len;
char *commandLine;
STARTUPINFO si;
PROCESS_INFORMATION pi;
argv[index] = "-detach_phase_two";
// start with space for two '"'s
len = 2;
for (i = 0; i < argc; i++) {
len += (int)strlen(argv[i]) + 1;
}
if ((commandLine = (char *) malloc(len)) == NULL) {
// Drop back ten and punt. Can't do the detach thing, so we just carry on.
// At least the program will start.
break;
}
commandLine[0] = '"';
// OK, we can safely use strcpy and strcat, since we know that we allocated enough
strlcpy(&commandLine[1], argv[0], len);
strlcat(commandLine, "\"", len);
for (i = 1; i < argc; i++) {
strlcat(commandLine, " ", len);
strlcat(commandLine, argv[i], len);
}
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
// If process creation succeeds, we exit, if it fails punt and continue
// as usual. We won't detach properly, but the program will run.
if (CreateProcess(NULL, commandLine, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
exit(0);
}
break;
}
#endif
}
init_core_client(argc, argv);
#ifdef _WIN32
retval = initialize_system_monitor(argc, argv);
if (retval) return retval;
if ( (argc > 1) && (strcmp(argv[1], "-daemon") == 0 || strcmp(argv[1], "--daemon") == 0) ) {
retval = initialize_service_dispatcher(argc, argv);
} else {
retval = boinc_main_loop();
}
#else
#ifdef SANDBOX
// Make sure owners, groups and permissions are correct
// for the current setting of g_use_sandbox
//
#if defined(_DEBUG) && defined(__APPLE__)
// GDB can't attach to applications which are running as a different user
// or group, so fix up data with current user and group during debugging
//
if (check_security(g_use_sandbox, false, NULL, 0)) {
SetBOINCDataOwnersGroupsAndPermissions();
}
#endif // _DEBUG && __APPLE__
int securityErr = check_security(g_use_sandbox, false, NULL, 0);
if (securityErr) {
printf(
"File ownership or permissions are set in a way that\n"
"does not allow sandboxed execution of BOINC applications.\n"
"To use BOINC anyway, use the -insecure command line option.\n"
"To change ownership/permission, reinstall BOINC"
#ifdef __APPLE__
" or run\n the shell script Mac_SA_Secure.sh"
#else
" or run\n the shell script secure.sh"
#endif
". (Error code %d)\n", securityErr
);
return ERR_USER_PERMISSION;
}
#endif // SANDBOX
retval = boinc_main_loop();
#endif
return retval;
}