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
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 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
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
// command-line version of the BOINC core client
|
|
|
|
|
2006-06-28 17:46:37 +00:00
|
|
|
// This file contains no GUI-related code.
|
2002-08-09 21:43:19 +00:00
|
|
|
|
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"
|
2005-01-07 07:32:23 +00:00
|
|
|
|
2006-01-23 08:47:05 +00:00
|
|
|
extern HINSTANCE g_hClientLibraryDll;
|
|
|
|
typedef BOOL (CALLBACK* ClientLibraryStartup)();
|
2008-08-07 16:59:52 +00:00
|
|
|
typedef BOOL (CALLBACK* IdleTrackerStartup)();
|
|
|
|
typedef void (CALLBACK* IdleTrackerShutdown)();
|
2006-01-23 08:47:05 +00:00
|
|
|
typedef void (CALLBACK* ClientLibraryShutdown)();
|
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"
|
2005-05-10 18:17:28 +00:00
|
|
|
#ifdef 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>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2003-04-08 18:36:03 +00:00
|
|
|
|
2006-06-15 11:11:41 +00:00
|
|
|
#if (defined (__APPLE__) && defined(SANDBOX) && defined(_DEBUG))
|
|
|
|
#include "SetupSecurity.h"
|
2005-03-25 12:23:30 +00:00
|
|
|
#endif
|
|
|
|
|
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"
|
2008-04-01 21:44:12 +00:00
|
|
|
#include "unix_util.h"
|
2005-03-07 21:19:09 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "filesys.h"
|
2005-03-30 22:01:23 +00:00
|
|
|
#include "network.h"
|
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-07-23 17:01:01 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
int initialize();
|
|
|
|
int finalize();
|
2009-07-23 17:01:01 +00:00
|
|
|
|
2005-10-24 13:10:18 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
// Display a message to the user.
|
|
|
|
// Depending on the priority, the message may be more or less obtrusive
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-05-21 17:59:10 +00:00
|
|
|
void show_message(PROJECT *p, char* msg, int priority) {
|
2005-02-16 23:17:43 +00:00
|
|
|
const char* x;
|
2003-05-21 17:59:10 +00:00
|
|
|
char message[1024];
|
2004-04-01 23:19:13 +00:00
|
|
|
time_t now = time(0);
|
2005-07-06 09:14:43 +00:00
|
|
|
char* time_string = time_to_string((double)now);
|
2003-05-21 17:59:10 +00:00
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// Cycle the log files if we need to
|
|
|
|
diagnostics_cycle_logs();
|
|
|
|
|
2007-01-25 23:39:06 +00:00
|
|
|
if (priority == MSG_INTERNAL_ERROR) {
|
|
|
|
strcpy(message, "[error] ");
|
|
|
|
strlcpy(message+8, msg, sizeof(message)-8);
|
|
|
|
} else {
|
|
|
|
strlcpy(message, msg, sizeof(message));
|
|
|
|
}
|
2008-08-12 19:06:35 +00:00
|
|
|
|
|
|
|
// trim trailing \n's
|
|
|
|
//
|
2004-04-20 05:10:40 +00:00
|
|
|
while (strlen(message)&&message[strlen(message)-1] == '\n') {
|
2003-05-21 17:59:10 +00:00
|
|
|
message[strlen(message)-1] = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-13 18:55:07 +00:00
|
|
|
if (p) {
|
|
|
|
x = p->get_project_name();
|
|
|
|
} else {
|
2004-03-23 01:44:47 +00:00
|
|
|
x = "---";
|
2003-05-13 18:55:07 +00:00
|
|
|
}
|
2004-04-01 23:19:13 +00:00
|
|
|
|
2005-07-06 09:14:43 +00:00
|
|
|
record_message(p, priority, (int)now, message);
|
2004-04-01 23:19:13 +00:00
|
|
|
|
2007-01-25 23:39:06 +00:00
|
|
|
printf("%s [%s] %s\n", time_string, x, message);
|
|
|
|
if (gstate.executing_as_daemon) {
|
2009-08-03 20:55:00 +00:00
|
|
|
#ifdef _WIN32
|
2009-07-23 17:01:01 +00:00
|
|
|
char event_message[2048];
|
2009-08-03 20:55:00 +00:00
|
|
|
sprintf(event_message, "%s [%s] %s\n", time_string, x, message);
|
2007-01-25 23:39:06 +00:00
|
|
|
::OutputDebugString(event_message);
|
2004-01-19 20:53:40 +00:00
|
|
|
#endif
|
2002-05-17 22:33:57 +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
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
void log_message_startup(char* msg) {
|
|
|
|
char evt_msg[2048];
|
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
|
|
|
"%s\n",
|
|
|
|
msg
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stdout, evt_msg);
|
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventInfoMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
|
|
|
#else
|
|
|
|
syslog(LOG_DAEMON|LOG_INFO, evt_msg);
|
|
|
|
#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
|
|
|
|
//
|
|
|
|
void log_message_error(char* msg) {
|
|
|
|
char evt_msg[2048];
|
|
|
|
#ifdef _WIN32
|
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
|
|
|
"%s\n"
|
|
|
|
"GLE: %s\n",
|
|
|
|
msg, windows_error_string(evt_msg, (sizeof(evt_msg)-((int)strlen(msg)+7)))
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
2009-08-04 05:52:04 +00:00
|
|
|
"%s\n",
|
2009-08-03 20:55:00 +00:00
|
|
|
msg
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, evt_msg);
|
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventErrorMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
|
|
|
#else
|
|
|
|
syslog(LOG_DAEMON|LOG_ERR, evt_msg);
|
|
|
|
#endif
|
2005-10-24 13:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
void log_message_error(char* msg, int error_code) {
|
|
|
|
char evt_msg[2048];
|
|
|
|
snprintf(evt_msg, sizeof(evt_msg),
|
|
|
|
"%s\n"
|
|
|
|
"Error Code: %d\n",
|
|
|
|
msg, error_code
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, evt_msg);
|
|
|
|
} else {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LogEventErrorMessage(evt_msg);
|
|
|
|
#elif defined(__EMX__)
|
|
|
|
#elif defined (__APPLE__)
|
|
|
|
#else
|
|
|
|
syslog(LOG_DAEMON|LOG_ERR, evt_msg);
|
|
|
|
#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);
|
|
|
|
|
2008-05-12 22:46:07 +00:00
|
|
|
gstate.parse_cmdline(argc, argv);
|
|
|
|
|
2008-05-12 21:27:14 +00:00
|
|
|
#ifdef _WIN32
|
2008-05-12 22:46:07 +00:00
|
|
|
if (!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 =
|
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
|
|
|
|
2008-10-10 17:23:51 +00:00
|
|
|
read_config_file(true);
|
2008-05-09 05:57:11 +00:00
|
|
|
|
2008-07-10 21:49:24 +00:00
|
|
|
// Set the max file sizes of the logs based on user preferences.
|
|
|
|
//
|
|
|
|
diagnostics_set_max_file_sizes(
|
|
|
|
config.max_stdout_file_size, config.max_stderr_file_size
|
|
|
|
);
|
|
|
|
|
|
|
|
// Win32 - detach from console if requested
|
2005-12-01 08:07:07 +00:00
|
|
|
#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
|
2004-05-05 18:33:42 +00:00
|
|
|
boinc_set_signal_handler(SIGHUP, signal_handler);
|
|
|
|
boinc_set_signal_handler(SIGINT, signal_handler);
|
|
|
|
boinc_set_signal_handler(SIGQUIT, signal_handler);
|
|
|
|
boinc_set_signal_handler(SIGTERM, signal_handler);
|
2003-06-05 19:00:05 +00:00
|
|
|
#ifdef SIGPWR
|
2004-05-05 18:33:42 +00:00
|
|
|
boinc_set_signal_handler(SIGPWR, 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
|
|
|
}
|
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
int initialize() {
|
|
|
|
int retval;
|
2006-03-31 00:43:50 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
g_hClientLibraryDll = LoadLibrary("boinc.dll");
|
|
|
|
if(!g_hClientLibraryDll) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Failed to initialize the BOINC Client Library.");
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-12 21:27:14 +00:00
|
|
|
if (!config.allow_multiple_clients) {
|
|
|
|
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
|
|
|
|
|
|
|
|
curl_init();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(g_hClientLibraryDll) {
|
|
|
|
ClientLibraryStartup fnClientLibraryStartup;
|
2008-08-07 16:59:52 +00:00
|
|
|
IdleTrackerStartup fnIdleTrackerStartup;
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
fnClientLibraryStartup = (ClientLibraryStartup)GetProcAddress(g_hClientLibraryDll, "ClientLibraryStartup");
|
2006-03-31 00:43:50 +00:00
|
|
|
if(fnClientLibraryStartup) {
|
|
|
|
if(!fnClientLibraryStartup()) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error(
|
|
|
|
"Failed to initialize the BOINC Client Library Interface."
|
|
|
|
"BOINC will not be able to determine if the user is idle or not...\n"
|
2008-08-07 16:59:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
fnIdleTrackerStartup = (IdleTrackerStartup)GetProcAddress(g_hClientLibraryDll, "IdleTrackerStartup");
|
2008-08-07 16:59:52 +00:00
|
|
|
if(fnIdleTrackerStartup) {
|
|
|
|
if(!fnIdleTrackerStartup()) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error(
|
|
|
|
"Failed to initialize the BOINC Client Library Interface."
|
|
|
|
"BOINC will not be able to determine if the user is idle or not...\n"
|
2006-03-31 00:43:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2006-03-31 00:43:50 +00:00
|
|
|
#endif
|
2006-06-30 23:32:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_main_loop() {
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = initialize();
|
|
|
|
if (retval) return retval;
|
2005-02-01 00:29:04 +00:00
|
|
|
|
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-08-03 20:55:00 +00:00
|
|
|
log_message_startup("BOINC initialization completed, beginning process execution...");
|
2006-03-31 00:43:50 +00:00
|
|
|
|
2005-06-28 19:25:52 +00:00
|
|
|
// must parse env vars after gstate.init();
|
|
|
|
// otherwise items will get overwritten with state file info
|
2005-04-01 23:52:19 +00:00
|
|
|
//
|
|
|
|
gstate.parse_env_vars();
|
|
|
|
|
2005-06-28 19:25:52 +00:00
|
|
|
if (gstate.projects.size() == 0) {
|
2006-01-17 22:48:09 +00:00
|
|
|
msg_printf(NULL, MSG_INFO,
|
|
|
|
"This computer is not attached to any projects"
|
|
|
|
);
|
|
|
|
msg_printf(NULL, MSG_INFO,
|
|
|
|
"Visit http://boinc.berkeley.edu for instructions"
|
|
|
|
);
|
2005-06-28 19:25:52 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2009-02-18 19:47:02 +00:00
|
|
|
if (gstate.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, "Exit requested by user");
|
|
|
|
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-08-03 20:55:00 +00:00
|
|
|
if (gstate.requested_suspend) {
|
2009-07-23 19:56:17 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_NEVER, 3600);
|
|
|
|
gstate.network_mode.set(RUN_MODE_NEVER, 3600);
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.requested_suspend = false;
|
2006-07-11 23:48:56 +00:00
|
|
|
}
|
2009-08-03 20:55:00 +00:00
|
|
|
if (gstate.requested_resume) {
|
2008-06-20 16:21:55 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_RESTORE, 0);
|
|
|
|
gstate.network_mode.set(RUN_MODE_RESTORE, 0);
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.requested_resume = false;
|
2006-07-11 23:48:56 +00:00
|
|
|
}
|
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
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
int finalize() {
|
|
|
|
static bool finalized = false;
|
|
|
|
if (finalized) return 0;
|
|
|
|
finalized = true;
|
|
|
|
gstate.quit_activities();
|
2006-03-31 00:43:50 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(g_hClientLibraryDll) {
|
2008-08-07 16:59:52 +00:00
|
|
|
IdleTrackerShutdown fnIdleTrackerShutdown;
|
2006-03-31 00:43:50 +00:00
|
|
|
ClientLibraryShutdown fnClientLibraryShutdown;
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
fnIdleTrackerShutdown = (IdleTrackerShutdown)GetProcAddress(g_hClientLibraryDll, "IdleTrackerShutdown");
|
2008-08-07 16:59:52 +00:00
|
|
|
if(fnIdleTrackerShutdown) {
|
|
|
|
fnIdleTrackerShutdown();
|
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
fnClientLibraryShutdown = (ClientLibraryShutdown)GetProcAddress(g_hClientLibraryDll, "ClientLibraryShutdown");
|
2006-03-31 00:43:50 +00:00
|
|
|
if(fnClientLibraryShutdown) {
|
|
|
|
fnClientLibraryShutdown();
|
|
|
|
}
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2006-03-31 00:43:50 +00:00
|
|
|
if(!FreeLibrary(g_hClientLibraryDll)) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Failed to cleanup the BOINC Idle Detection Interface");
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2006-03-31 00:43:50 +00:00
|
|
|
g_hClientLibraryDll = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WINSOCK
|
2007-02-12 23:53:16 +00:00
|
|
|
if (WinsockCleanup()) {
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Failed to cleanup the Windows Sockets interface");
|
2006-03-31 00:43:50 +00:00
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
cleanup_system_monitor();
|
2006-03-31 00:43:50 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
curl_cleanup();
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.cleanup_completed = true;
|
2004-12-06 22:41:19 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +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;
|
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>:
|
|
|
|
// 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
|
2006-03-31 00:43:50 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +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.
|
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
// 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.
|
2009-08-03 20:55:00 +00:00
|
|
|
if (strcmp(argv[index], "-detach") == 0 || strcmp(argv[index], "--detach") == 0) {
|
|
|
|
int i, len;
|
|
|
|
char *commandLine;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
argv[index] = "-detach_phase_two";
|
2005-12-01 08:07:07 +00:00
|
|
|
|
|
|
|
// start with space for two '"'s
|
|
|
|
len = 2;
|
|
|
|
for (i = 0; i < argc; i++) {
|
2006-09-07 18:19:25 +00:00
|
|
|
len += (int)strlen(argv[i]) + 1;
|
2005-12-01 08:07:07 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
strcpy(&commandLine[1], argv[0]);
|
|
|
|
strcat(commandLine, "\"");
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
strcat(commandLine, " ");
|
|
|
|
strcat(commandLine, argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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, 0, 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 {
|
2009-08-03 20:55:00 +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
|
|
|
|
//
|
2006-07-28 09:05:54 +00:00
|
|
|
#if defined(_DEBUG) && defined(__APPLE__)
|
2007-10-05 16:47:07 +00:00
|
|
|
// GDB can't attach to applications which are running as a different user
|
2006-09-25 17:40:54 +00:00
|
|
|
// or group, so fix up data with current user and group during debugging
|
|
|
|
//
|
2007-02-12 23:53:16 +00:00
|
|
|
if (check_security(g_use_sandbox, false)) {
|
2006-06-15 11:11:41 +00:00
|
|
|
SetBOINCDataOwnersGroupsAndPermissions();
|
2007-02-12 23:53:16 +00:00
|
|
|
}
|
2006-07-28 09:05:54 +00:00
|
|
|
#endif // _DEBUG && __APPLE__
|
2008-11-26 13:43:10 +00:00
|
|
|
int securityErr = check_security(g_use_sandbox, false);
|
|
|
|
if (securityErr) {
|
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
|
|
|
);
|
2006-06-15 11:11:41 +00:00
|
|
|
return ERR_USER_PERMISSION;
|
|
|
|
}
|
|
|
|
#endif // SANDBOX
|
|
|
|
|
2009-08-03 20:55:00 +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
|
|
|
}
|
|
|
|
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_f02264aefe = "$Id$";
|