2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2017-01-06 09:53:47 +00:00
|
|
|
// Copyright (C) 2017 University of California
|
2004-11-12 18:11:31 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// 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.
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2012-06-26 20:30:56 +00:00
|
|
|
// command-line version of the BOINC client
|
2002-05-17 22:33:57 +00:00
|
|
|
|
2006-06-28 17:46:37 +00:00
|
|
|
// This file contains no GUI-related code.
|
2002-08-09 21:43:19 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#include "cpp.h"
|
|
|
|
|
2004-01-15 01:42:49 +00:00
|
|
|
#ifdef WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "sysmon_win.h"
|
2008-05-22 03:57:55 +00:00
|
|
|
#include "win_util.h"
|
2009-08-22 17:00:19 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2005-01-07 07:32:23 +00:00
|
|
|
|
2005-03-07 21:19:09 +00:00
|
|
|
#else
|
2004-03-17 04:56:46 +00:00
|
|
|
#include "config.h"
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_SOCKET_H
|
2005-05-13 08:17:20 +00:00
|
|
|
#include <sys/types.h>
|
2005-05-10 18:17:28 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2006-06-15 11:11:41 +00:00
|
|
|
#include <sys/stat.h>
|
2006-01-16 04:09:03 +00:00
|
|
|
#include <syslog.h>
|
2008-02-27 23:26:38 +00:00
|
|
|
#include <cstdlib>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <unistd.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <csignal>
|
2013-02-11 20:46:46 +00:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include "android/log.h"
|
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2003-04-08 18:36:03 +00:00
|
|
|
|
2015-08-09 03:15:59 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Carbon/Carbon.h>
|
2017-02-25 12:56:12 +00:00
|
|
|
#include "hostinfo.h"
|
2015-08-09 03:15:59 +00:00
|
|
|
#endif
|
2005-03-25 12:23:30 +00:00
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "diagnostics.h"
|
2002-06-21 06:52:47 +00:00
|
|
|
#include "error_numbers.h"
|
2007-02-21 16:26:51 +00:00
|
|
|
#include "str_util.h"
|
2009-06-16 20:54:44 +00:00
|
|
|
#include "str_replace.h"
|
2005-03-07 21:19:09 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "filesys.h"
|
2005-03-30 22:01:23 +00:00
|
|
|
#include "network.h"
|
2009-11-13 21:23:15 +00:00
|
|
|
#include "idlemon.h"
|
2009-12-05 00:51:05 +00:00
|
|
|
|
2005-03-07 21:19:09 +00:00
|
|
|
#include "client_state.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "file_names.h"
|
|
|
|
#include "log_flags.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
2006-06-28 17:46:37 +00:00
|
|
|
#include "http_curl.h"
|
2007-10-05 16:47:07 +00:00
|
|
|
#include "sandbox.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2006-06-28 17:46:37 +00:00
|
|
|
#include "main.h"
|
2003-05-13 18:55:07 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
// Log informational messages to system specific places
|
2006-07-11 23:48:56 +00:00
|
|
|
//
|
2010-03-25 23:48:58 +00:00
|
|
|
void log_message_startup(const char* msg) {
|
2009-08-03 20:55:00 +00:00
|
|
|
char evt_msg[2048];
|
2013-04-10 22:51:35 +00:00
|
|
|
char* time_string = time_to_string(dtime());
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
2013-04-10 22:51:35 +00:00
|
|
|
"%s %s\n",
|
|
|
|
time_string, msg
|
2009-08-03 20:55:00 +00:00
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
2013-04-25 08:39:03 +00:00
|
|
|
fprintf(stdout, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventInfoMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
2012-08-04 00:27:32 +00:00
|
|
|
#elif defined (ANDROID)
|
2013-02-11 22:06:49 +00:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "BOINC", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#else
|
2013-04-25 08:39:03 +00:00
|
|
|
syslog(LOG_DAEMON|LOG_INFO, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#endif
|
2009-03-24 16:57:28 +00:00
|
|
|
}
|
2004-05-05 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
// Log error messages to system specific places
|
|
|
|
//
|
2010-03-25 23:48:58 +00:00
|
|
|
void log_message_error(const char* msg) {
|
2014-01-28 10:01:47 +00:00
|
|
|
char evt_msg[2048];
|
2013-04-10 22:51:35 +00:00
|
|
|
char* time_string = time_to_string(dtime());
|
2009-08-03 20:55:00 +00:00
|
|
|
#ifdef _WIN32
|
2014-01-28 10:01:47 +00:00
|
|
|
char buf[1024];
|
2009-08-03 20:55:00 +00:00
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
2013-04-10 22:51:35 +00:00
|
|
|
"%s %s\n"
|
2009-08-03 20:55:00 +00:00
|
|
|
"GLE: %s\n",
|
2013-07-04 23:00:10 +00:00
|
|
|
time_string, msg,
|
2014-01-28 08:46:47 +00:00
|
|
|
windows_format_error_string(GetLastError(), buf, sizeof(buf))
|
2009-08-03 20:55:00 +00:00
|
|
|
);
|
|
|
|
#else
|
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
2013-04-11 08:18:57 +00:00
|
|
|
"%s %s\n",
|
|
|
|
time_string, msg
|
2009-08-03 20:55:00 +00:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
if (!gstate.executing_as_daemon) {
|
2013-04-25 08:39:03 +00:00
|
|
|
fprintf(stderr, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventErrorMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
2012-08-04 00:27:32 +00:00
|
|
|
#elif defined (ANDROID)
|
2013-02-11 22:06:49 +00:00
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "BOINC", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#else
|
2013-04-25 08:39:03 +00:00
|
|
|
syslog(LOG_DAEMON|LOG_ERR, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#endif
|
2005-10-24 13:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 23:48:58 +00:00
|
|
|
void log_message_error(const char* msg, int error_code) {
|
2009-08-03 20:55:00 +00:00
|
|
|
char evt_msg[2048];
|
2013-04-10 22:51:35 +00:00
|
|
|
char* time_string = time_to_string(dtime());
|
2009-08-03 20:55:00 +00:00
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
2013-04-10 22:51:35 +00:00
|
|
|
"%s %s\n"
|
2009-08-03 20:55:00 +00:00
|
|
|
"Error Code: %d\n",
|
2013-04-10 22:51:35 +00:00
|
|
|
time_string, msg, error_code
|
2009-08-03 20:55:00 +00:00
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
2013-04-25 08:39:03 +00:00
|
|
|
fprintf(stderr, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventErrorMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
2012-08-04 00:27:32 +00:00
|
|
|
#elif defined (ANDROID)
|
2013-02-11 22:06:49 +00:00
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "BOINC", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#else
|
2013-04-25 08:39:03 +00:00
|
|
|
syslog(LOG_DAEMON|LOG_ERR, "%s", evt_msg);
|
2009-08-03 20:55:00 +00:00
|
|
|
#endif
|
2005-10-24 13:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
#ifndef _WIN32
|
2004-05-05 18:33:42 +00:00
|
|
|
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:
|
2007-02-02 20:14:18 +00:00
|
|
|
#ifdef SIGPWR
|
|
|
|
case SIGPWR:
|
|
|
|
#endif
|
2004-05-06 00:24:47 +00:00
|
|
|
gstate.requested_exit = true;
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
// close socket
|
|
|
|
shutdown(gstate.gui_rpcs.lsock, 2);
|
|
|
|
#endif
|
2004-05-06 00:24:47 +00:00
|
|
|
break;
|
2004-05-05 18:33:42 +00:00
|
|
|
default:
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(NULL, MSG_INTERNAL_ERROR, "Signal not handled");
|
2004-05-05 18:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-06 21:27:32 +00:00
|
|
|
#endif
|
2003-06-04 18:51:16 +00:00
|
|
|
|
2008-05-12 21:27:14 +00:00
|
|
|
static void init_core_client(int argc, char** argv) {
|
|
|
|
setbuf(stdout, 0);
|
|
|
|
setbuf(stderr, 0);
|
|
|
|
|
2014-05-08 07:51:18 +00:00
|
|
|
cc_config.defaults();
|
2008-05-12 22:46:07 +00:00
|
|
|
gstate.parse_cmdline(argc, argv);
|
2010-07-20 04:19:22 +00:00
|
|
|
gstate.now = dtime();
|
2008-05-12 22:46:07 +00:00
|
|
|
|
2008-05-12 21:27:14 +00:00
|
|
|
#ifdef _WIN32
|
2014-05-08 07:51:18 +00:00
|
|
|
if (!cc_config.allow_multiple_clients) {
|
2008-05-12 21:27:14 +00:00
|
|
|
chdir_to_data_dir();
|
|
|
|
}
|
2005-03-25 12:23:30 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-01 12:36:19 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (g_use_sandbox)
|
2008-01-16 10:38:21 +00:00
|
|
|
// Set file creation mask to be writable by both user and group and
|
|
|
|
// world-executable but neither world-readable nor world-writable
|
2007-02-12 23:53:16 +00:00
|
|
|
// Our umask will be inherited by all our child processes
|
|
|
|
//
|
2008-01-16 10:38:21 +00:00
|
|
|
umask (6);
|
2006-08-01 12:36:19 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// Initialize the BOINC Diagnostics Framework
|
2007-02-12 23:53:16 +00:00
|
|
|
int flags =
|
2010-05-11 19:18:55 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
BOINC_DIAG_MEMORYLEAKCHECKENABLED |
|
|
|
|
#endif
|
2005-12-01 08:07:07 +00:00
|
|
|
BOINC_DIAG_DUMPCALLSTACKENABLED |
|
2005-02-01 00:54:06 +00:00
|
|
|
BOINC_DIAG_HEAPCHECKENABLED |
|
|
|
|
BOINC_DIAG_TRACETOSTDOUT;
|
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
if (gstate.redirect_io || gstate.executing_as_daemon || gstate.detach_console) {
|
2007-02-12 23:53:16 +00:00
|
|
|
flags |=
|
2005-12-01 08:07:07 +00:00
|
|
|
BOINC_DIAG_REDIRECTSTDERR |
|
2005-02-01 00:54:06 +00:00
|
|
|
BOINC_DIAG_REDIRECTSTDOUT;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:53:16 +00:00
|
|
|
diagnostics_init(flags, "stdoutdae", "stderrdae");
|
2004-09-06 20:30:22 +00:00
|
|
|
|
2010-05-11 20:54:55 +00:00
|
|
|
#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
|
|
|
|
|
2008-10-10 17:23:51 +00:00
|
|
|
read_config_file(true);
|
2008-05-09 05:57:11 +00:00
|
|
|
|
2013-03-05 23:52:04 +00:00
|
|
|
// Win32 - detach from console if requested
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (gstate.detach_console) {
|
|
|
|
FreeConsole();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-09 05:57:11 +00:00
|
|
|
// Unix: install signal handlers
|
2005-02-01 00:29:04 +00:00
|
|
|
#ifndef _WIN32
|
2003-04-08 18:36:03 +00:00
|
|
|
// Handle quit signals gracefully
|
2013-09-28 00:41:44 +00:00
|
|
|
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);
|
2003-06-05 19:00:05 +00:00
|
|
|
#ifdef SIGPWR
|
2013-09-28 00:41:44 +00:00
|
|
|
boinc_set_signal_handler(SIGPWR, (handler_t)signal_handler);
|
2003-06-05 19:00:05 +00:00
|
|
|
#endif
|
2004-01-15 01:42:49 +00:00
|
|
|
#endif
|
2005-02-01 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 12:55:53 +00:00
|
|
|
// 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);
|
2013-07-13 01:35:47 +00:00
|
|
|
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");
|
|
|
|
|
2013-07-09 14:37:01 +00:00
|
|
|
read_config_file(true);
|
2013-07-09 12:55:53 +00:00
|
|
|
|
|
|
|
coprocs.detect_gpus(warnings);
|
|
|
|
coprocs.write_coproc_info_file(warnings);
|
|
|
|
warnings.clear();
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:50:14 +00:00
|
|
|
static int initialize() {
|
2006-06-30 23:32:26 +00:00
|
|
|
int retval;
|
2006-03-31 00:43:50 +00:00
|
|
|
|
2014-05-08 07:51:18 +00:00
|
|
|
if (!cc_config.allow_multiple_clients) {
|
2008-05-12 21:27:14 +00:00
|
|
|
retval = wait_client_mutex(".", 10);
|
|
|
|
if (retval) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Another instance of BOINC is running.");
|
2008-05-12 21:27:14 +00:00
|
|
|
return ERR_EXEC;
|
|
|
|
}
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize WinSock
|
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2009-02-18 19:47:02 +00:00
|
|
|
if (WinsockInitialize() != 0) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Failed to initialize the Windows Sockets interface.");
|
2006-03-31 00:43:50 +00:00
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 23:09:13 +00:00
|
|
|
curl_init();
|
2006-03-31 00:43:50 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2009-11-13 21:23:15 +00:00
|
|
|
if(!startup_idle_monitor()) {
|
|
|
|
log_message_error(
|
2010-07-19 22:09:45 +00:00
|
|
|
"Failed to initialize the BOINC idle monitor interface."
|
2009-11-13 21:23:15 +00:00
|
|
|
"BOINC will not be able to determine if the user is idle or not...\n"
|
|
|
|
);
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-11-13 21:23:15 +00:00
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:50:14 +00:00
|
|
|
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()) {
|
2011-10-06 17:39:13 +00:00
|
|
|
log_message_error("WinSockCleanup() failed");
|
2010-05-11 19:50:14 +00:00
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cleanup_system_monitor();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 23:09:13 +00:00
|
|
|
curl_cleanup();
|
2010-05-11 19:50:14 +00:00
|
|
|
|
2011-10-24 23:39:21 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
gstate.free_mem();
|
|
|
|
#endif
|
2010-05-11 19:50:14 +00:00
|
|
|
|
2010-06-16 21:57:28 +00:00
|
|
|
diagnostics_finish();
|
2010-05-11 19:50:14 +00:00
|
|
|
gstate.cleanup_completed = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
int boinc_main_loop() {
|
|
|
|
int retval;
|
2011-01-06 23:09:13 +00:00
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
retval = initialize();
|
|
|
|
if (retval) return retval;
|
2005-02-01 00:29:04 +00:00
|
|
|
|
2015-08-09 03:15:59 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// If we run too soon during system boot we can cause a kernel panic
|
|
|
|
if (gstate.executing_as_daemon) {
|
2017-02-25 12:56:12 +00:00
|
|
|
if (get_system_uptime() < 120) { // If system has been up for less than 2 minutes
|
2015-08-09 03:15:59 +00:00
|
|
|
boinc_sleep(30.);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-01-15 01:42:49 +00:00
|
|
|
retval = gstate.init();
|
2004-04-15 20:42:15 +00:00
|
|
|
if (retval) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("gstate.init() failed", retval);
|
2006-03-31 00:43:50 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-12-06 04:32:57 +00:00
|
|
|
log_message_startup("Initialization completed");
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2005-08-16 20:48:21 +00:00
|
|
|
if (!gstate.poll_slow_events()) {
|
2006-05-21 22:03:36 +00:00
|
|
|
gstate.do_io_or_sleep(POLL_INTERVAL);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2009-08-03 20:55:00 +00:00
|
|
|
fflush(stderr);
|
2005-08-16 20:48:21 +00:00
|
|
|
fflush(stdout);
|
2002-08-12 23:18:47 +00:00
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
if (gstate.time_to_exit()) {
|
|
|
|
msg_printf(NULL, MSG_INFO, "Time to exit");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gstate.requested_exit) {
|
2014-05-08 07:51:18 +00:00
|
|
|
if (cc_config.abort_jobs_on_exit) {
|
2009-02-18 19:47:02 +00:00
|
|
|
if (!gstate.in_abort_sequence) {
|
|
|
|
msg_printf(NULL, MSG_INFO,
|
|
|
|
"Exit requested; starting abort sequence"
|
|
|
|
);
|
|
|
|
gstate.start_abort_sequence();
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-17 23:40:58 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Exiting");
|
2009-02-18 19:47:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gstate.in_abort_sequence) {
|
|
|
|
if (gstate.abort_sequence_done()) {
|
|
|
|
msg_printf(NULL, MSG_INFO, "Abort sequence done; exiting");
|
|
|
|
break;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-23 17:01:01 +00:00
|
|
|
|
2006-07-11 23:48:56 +00:00
|
|
|
return finalize();
|
2006-06-30 23:32:26 +00:00
|
|
|
}
|
2004-01-15 01:42:49 +00:00
|
|
|
|
2004-01-19 20:53:40 +00:00
|
|
|
int main(int argc, char** argv) {
|
2004-12-06 22:41:19 +00:00
|
|
|
int retval = 0;
|
2013-07-04 23:00:10 +00:00
|
|
|
|
2013-07-09 12:55:53 +00:00
|
|
|
coprocs.set_path_to_client(argv[0]); // Used to launch a child process for --detect_gpus
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
for (int index = 1; index < argc; index++) {
|
|
|
|
if (strcmp(argv[index], "-daemon") == 0 || strcmp(argv[index], "--daemon") == 0) {
|
2006-03-31 00:43:50 +00:00
|
|
|
gstate.executing_as_daemon = true;
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_startup("BOINC is initializing...");
|
|
|
|
#if !defined(_WIN32) && !defined(__EMX__) && !defined(__APPLE__)
|
|
|
|
// from <unistd.h>:
|
2013-07-04 23:00:10 +00:00
|
|
|
// Detach from the controlling terminal and run in the background
|
|
|
|
// as system daemon.
|
2009-08-03 20:55:00 +00:00
|
|
|
// Don't change working directory to root ("/"), but redirect
|
|
|
|
// standard input, standard output and standard error to /dev/null.
|
2013-07-04 23:00:10 +00:00
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
retval = daemon(1, 0);
|
|
|
|
break;
|
|
|
|
#endif
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
2013-03-05 23:52:04 +00:00
|
|
|
|
2013-07-04 23:00:10 +00:00
|
|
|
if (!strcmp(argv[index], "--detect_gpus")) {
|
2013-07-09 12:55:53 +00:00
|
|
|
do_gpu_detection(argc, argv);
|
2013-06-25 11:31:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-04 23:00:10 +00:00
|
|
|
|
|
|
|
if (!strcmp(argv[index], "--run_test_app")) {
|
2013-07-09 23:21:33 +00:00
|
|
|
read_config_file(true);
|
2013-07-04 23:00:10 +00:00
|
|
|
run_test_app();
|
|
|
|
}
|
|
|
|
|
2013-03-05 23:52:04 +00:00
|
|
|
#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.
|
2013-04-09 17:12:34 +00:00
|
|
|
if (strcmp(argv[index], "-detach") == 0 || strcmp(argv[index], "--detach") == 0 ||
|
|
|
|
strcmp(argv[index], "-detach_console") == 0 || strcmp(argv[index], "--detach_console") == 0
|
|
|
|
) {
|
2013-03-05 23:52:04 +00:00
|
|
|
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
|
2016-02-16 02:53:07 +00:00
|
|
|
strlcpy(&commandLine[1], argv[0], len);
|
|
|
|
strlcat(commandLine, "\"", len);
|
2013-03-05 23:52:04 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
2016-02-16 02:53:07 +00:00
|
|
|
strlcat(commandLine, " ", len);
|
|
|
|
strlcat(commandLine, argv[i], len);
|
2013-03-05 23:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
}
|
2005-12-01 08:07:07 +00:00
|
|
|
|
2005-03-07 21:19:09 +00:00
|
|
|
init_core_client(argc, argv);
|
2005-02-01 00:29:04 +00:00
|
|
|
|
2006-03-24 23:04:05 +00:00
|
|
|
#ifdef _WIN32
|
2008-05-09 05:57:11 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
retval = initialize_system_monitor(argc, argv);
|
|
|
|
if (retval) return retval;
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-04 04:46:49 +00:00
|
|
|
if ( (argc > 1) && (strcmp(argv[1], "-daemon") == 0 || strcmp(argv[1], "--daemon") == 0) ) {
|
2009-08-03 20:55:00 +00:00
|
|
|
retval = initialize_service_dispatcher(argc, argv);
|
2004-03-19 00:13:22 +00:00
|
|
|
} else {
|
2011-01-06 23:09:13 +00:00
|
|
|
retval = boinc_main_loop();
|
2004-12-06 22:41:19 +00:00
|
|
|
}
|
2009-08-03 20:55:00 +00:00
|
|
|
|
2005-02-01 00:29:04 +00:00
|
|
|
#else
|
2005-03-11 13:08:52 +00:00
|
|
|
|
2006-06-15 11:11:41 +00:00
|
|
|
#ifdef SANDBOX
|
2006-09-25 17:40:54 +00:00
|
|
|
// Make sure owners, groups and permissions are correct
|
|
|
|
// for the current setting of g_use_sandbox
|
|
|
|
//
|
2017-01-06 09:53:47 +00:00
|
|
|
// NOTE: GDB and LLDB can't attach to applications which are running as
|
|
|
|
// a different user or group.
|
|
|
|
// Normally, the Mac Development (Debug) builds do not define SANDBOX, so
|
|
|
|
// check_security() is never called. However, it is possible to use GDB
|
|
|
|
// or LLDB on sandbox-specific code, as long as the code is run as the
|
|
|
|
// current user (i.e., not as boinc_master or boinc_project), and the
|
|
|
|
// current user is a member of both groups boinc_master and boinc_project.
|
|
|
|
// However, this has not been thoroughly tested. Please see the comments
|
|
|
|
// in SetupSecurity.cpp and check_security.cpp for more details.
|
2013-06-04 03:24:48 +00:00
|
|
|
int securityErr = check_security(g_use_sandbox, false, NULL, 0);
|
2008-11-26 13:43:10 +00:00
|
|
|
if (securityErr) {
|
2017-01-06 09:53:47 +00:00
|
|
|
#if (defined(__APPLE__) && defined (_DEBUG))
|
|
|
|
printf(
|
|
|
|
"To debug with sandbox security enabled, the current user\n"
|
|
|
|
"must be a member of both groups boinc_master and boinc_project."
|
|
|
|
);
|
|
|
|
#else // ! (defined(__APPLE__) && defined (_DEBUG))
|
2006-09-25 17:40:54 +00:00
|
|
|
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"
|
2006-09-26 00:11:44 +00:00
|
|
|
"To change ownership/permission, reinstall BOINC"
|
|
|
|
#ifdef __APPLE__
|
|
|
|
" or run\n the shell script Mac_SA_Secure.sh"
|
2007-12-19 19:29:54 +00:00
|
|
|
#else
|
2006-09-26 00:11:44 +00:00
|
|
|
" or run\n the shell script secure.sh"
|
|
|
|
#endif
|
2008-11-26 13:43:10 +00:00
|
|
|
". (Error code %d)\n", securityErr
|
2006-09-25 17:40:54 +00:00
|
|
|
);
|
2017-01-06 09:53:47 +00:00
|
|
|
#endif // ! (defined(__APPLE__) && defined (_DEBUG))
|
2006-06-15 11:11:41 +00:00
|
|
|
return ERR_USER_PERMISSION;
|
|
|
|
}
|
|
|
|
#endif // SANDBOX
|
|
|
|
|
2011-01-06 23:09:13 +00:00
|
|
|
retval = boinc_main_loop();
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
#endif
|
2004-03-19 00:13:22 +00:00
|
|
|
return retval;
|
2004-01-19 20:53:40 +00:00
|
|
|
}
|
|
|
|
|