2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-11-12 18:11:31 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software 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.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
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
|
2005-12-16 03:45:01 +00:00
|
|
|
#define _CONSOLE 1
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-01-19 20:53:40 +00:00
|
|
|
#include "win_service.h"
|
2005-01-07 07:32:23 +00:00
|
|
|
|
2006-01-23 08:47:05 +00:00
|
|
|
extern HINSTANCE g_hClientLibraryDll;
|
2005-10-24 13:10:18 +00:00
|
|
|
static HANDLE g_hWin9xMonitorSystemThread = NULL;
|
|
|
|
static DWORD g_Win9xMonitorSystemThreadID = NULL;
|
|
|
|
static BOOL g_bIsWin9x = FALSE;
|
2006-07-11 23:48:56 +00:00
|
|
|
static bool requested_suspend = false;
|
|
|
|
static bool requested_resume = false;
|
2005-10-24 13:10:18 +00:00
|
|
|
|
2006-01-23 08:47:05 +00:00
|
|
|
typedef BOOL (CALLBACK* ClientLibraryStartup)();
|
|
|
|
typedef void (CALLBACK* ClientLibraryShutdown)();
|
2005-12-16 03:45:01 +00:00
|
|
|
#ifndef _T
|
|
|
|
#define _T(X) X
|
|
|
|
#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"
|
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>
|
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
|
|
|
|
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "diagnostics.h"
|
2002-06-21 06:52:47 +00:00
|
|
|
#include "error_numbers.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"
|
|
|
|
|
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"
|
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
|
|
|
|
2006-06-30 23:32:26 +00:00
|
|
|
int finalize();
|
|
|
|
|
2005-10-24 13:10:18 +00:00
|
|
|
static bool boinc_cleanup_completed = false;
|
|
|
|
// Used on Windows 95/98/ME to determine when it is safe to leave
|
|
|
|
// the WM_ENDSESSION message handler and allow Windows to finish
|
|
|
|
// cleaning up.
|
|
|
|
|
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);
|
2004-01-22 06:37:16 +00:00
|
|
|
#if defined(WIN32) && defined(_CONSOLE)
|
2004-01-19 20:53:40 +00:00
|
|
|
char event_message[2048];
|
2004-01-22 06:37:16 +00:00
|
|
|
#endif
|
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();
|
|
|
|
|
2006-03-30 22:21:18 +00:00
|
|
|
strlcpy(message, msg, sizeof(message));
|
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
|
|
|
|
2003-03-06 17:42:49 +00:00
|
|
|
switch (priority) {
|
2006-06-22 19:40:30 +00:00
|
|
|
case MSG_INFO:
|
2003-03-06 17:42:49 +00:00
|
|
|
case MSG_ERROR:
|
2006-06-22 19:40:30 +00:00
|
|
|
default:
|
|
|
|
// all messages handled the same now
|
|
|
|
//
|
2004-04-01 23:19:13 +00:00
|
|
|
printf("%s [%s] %s\n", time_string, x, message);
|
2005-01-07 20:47:40 +00:00
|
|
|
if (gstate.executing_as_daemon) {
|
2004-01-22 06:37:16 +00:00
|
|
|
#if defined(WIN32) && defined(_CONSOLE)
|
2005-12-16 03:45:01 +00:00
|
|
|
stprintf(event_message, TEXT("%s [%s] %s\n"), time_string, x, message);
|
2005-11-22 02:11:35 +00:00
|
|
|
::OutputDebugString(event_message);
|
2004-01-19 20:53:40 +00:00
|
|
|
#endif
|
2004-12-06 22:41:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-05-17 22:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-22 06:37:16 +00:00
|
|
|
#ifdef WIN32
|
2006-07-11 23:48:56 +00:00
|
|
|
// The following 3 functions are called in a separate thread,
|
|
|
|
// so we can't do anything directly.
|
|
|
|
// Set flags telling the main thread what to do.
|
|
|
|
//
|
2004-05-05 18:43:41 +00:00
|
|
|
void quit_client() {
|
|
|
|
gstate.requested_exit = true;
|
2006-07-11 23:23:27 +00:00
|
|
|
while (1) {
|
|
|
|
boinc_sleep(1.0);
|
2006-07-11 23:48:56 +00:00
|
|
|
if (boinc_cleanup_completed) break;
|
2006-07-11 23:23:27 +00:00
|
|
|
}
|
2004-05-05 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void suspend_client() {
|
2006-07-11 23:48:56 +00:00
|
|
|
requested_suspend = true;
|
2004-05-05 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resume_client() {
|
2006-07-11 23:48:56 +00:00
|
|
|
requested_resume = true;
|
2004-05-05 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2005-10-24 13:10:18 +00:00
|
|
|
// Trap logoff and shutdown events on Win9x so we can clean ourselves up.
|
|
|
|
LRESULT CALLBACK Win9xMonitorSystemWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2006-07-12 08:00:26 +00:00
|
|
|
if (uMsg == WM_QUERYENDSESSION) {
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("***** Win9x Monitor System Shutdown/Logoff Event Detected *****\n");
|
2006-07-12 08:00:26 +00:00
|
|
|
// Win95 is stupid, we really only need to wait until we have
|
|
|
|
// successfully shutdown the active tasks and cleaned everything
|
|
|
|
// up. Luckly WM_QUERYENDSESSION is sent before Win9x checks for any
|
|
|
|
// existing console and that gives us a chance to clean-up and
|
|
|
|
// then clear the console window. Win9x will not close down
|
|
|
|
// a console window if anything is displayed on it.
|
2005-10-24 13:10:18 +00:00
|
|
|
quit_client();
|
|
|
|
system("cls");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
|
|
}
|
|
|
|
|
2006-11-10 18:21:05 +00:00
|
|
|
DWORD WINAPI Win9xMonitorSystemThread( LPVOID ) {
|
2005-10-24 13:10:18 +00:00
|
|
|
HWND hwndMain;
|
|
|
|
WNDCLASS wc;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
wc.style = CS_GLOBALCLASS;
|
2005-12-01 08:07:07 +00:00
|
|
|
wc.lpfnWndProc = (WNDPROC)Win9xMonitorSystemWndProc;
|
2005-10-24 13:10:18 +00:00
|
|
|
wc.cbClsExtra = 0;
|
2005-12-01 08:07:07 +00:00
|
|
|
wc.cbWndExtra = 0;
|
2005-10-24 13:10:18 +00:00
|
|
|
wc.hInstance = NULL;
|
|
|
|
wc.hIcon = NULL;
|
|
|
|
wc.hCursor = NULL;
|
|
|
|
wc.hbrBackground = NULL;
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = "BOINCWin9xMonitorSystem";
|
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
if (!RegisterClass(&wc))
|
2005-10-24 13:10:18 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to register the Win9xMonitorSystem window class.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an invisible window */
|
|
|
|
hwndMain = CreateWindow(
|
2005-12-01 08:07:07 +00:00
|
|
|
wc.lpszClassName,
|
2005-10-24 13:10:18 +00:00
|
|
|
"BOINC Monitor System",
|
|
|
|
WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!hwndMain)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create the Win9xMonitorSystem window.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-01 08:07:07 +00:00
|
|
|
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0))
|
2005-10-24 13:10:18 +00:00
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-15 01:42:49 +00:00
|
|
|
BOOL WINAPI ConsoleControlHandler ( DWORD dwCtrlType ){
|
2004-12-06 22:41:19 +00:00
|
|
|
BOOL bReturnStatus = FALSE;
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("***** Console Event Detected *****\n");
|
2004-12-06 22:41:19 +00:00
|
|
|
switch( dwCtrlType ){
|
2004-04-03 00:43:55 +00:00
|
|
|
case CTRL_C_EVENT:
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("Event: CTRL-C Event\n");
|
2005-12-24 07:12:39 +00:00
|
|
|
if(gstate.tasks_suspended) {
|
2004-05-05 18:43:41 +00:00
|
|
|
resume_client();
|
2004-04-03 00:43:55 +00:00
|
|
|
} else {
|
2004-05-05 18:43:41 +00:00
|
|
|
suspend_client();
|
2004-04-03 00:43:55 +00:00
|
|
|
}
|
|
|
|
bReturnStatus = TRUE;
|
|
|
|
break;
|
|
|
|
case CTRL_BREAK_EVENT:
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("Event: CTRL-BREAK Event\n");
|
|
|
|
quit_client();
|
|
|
|
bReturnStatus = TRUE;
|
|
|
|
break;
|
2004-04-03 00:43:55 +00:00
|
|
|
case CTRL_CLOSE_EVENT:
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("Event: CTRL-CLOSE Event\n");
|
|
|
|
quit_client();
|
|
|
|
break;
|
2004-04-03 00:43:55 +00:00
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("Event: CTRL-SHUTDOWN Event\n");
|
2004-05-05 18:43:41 +00:00
|
|
|
quit_client();
|
2004-04-03 00:43:55 +00:00
|
|
|
break;
|
2004-07-07 18:35:25 +00:00
|
|
|
case CTRL_LOGOFF_EVENT:
|
2005-10-24 13:10:18 +00:00
|
|
|
BOINCTRACE("Event: CTRL-LOGOFF Event\n");
|
2005-01-07 20:47:40 +00:00
|
|
|
if (!gstate.executing_as_daemon) {
|
2004-07-07 18:35:25 +00:00
|
|
|
quit_client();
|
|
|
|
}
|
|
|
|
bReturnStatus = TRUE;
|
|
|
|
break;
|
2004-12-06 22:41:19 +00:00
|
|
|
}
|
|
|
|
return bReturnStatus;
|
2004-01-15 01:42:49 +00:00
|
|
|
}
|
2004-05-05 18:33:42 +00:00
|
|
|
#else
|
|
|
|
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:
|
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:59:37 +00:00
|
|
|
case SIGTSTP:
|
2006-11-10 17:55:22 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_NEVER, 0);
|
2004-05-05 18:33:42 +00:00
|
|
|
break;
|
|
|
|
case SIGCONT:
|
2006-11-10 17:55:22 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_AUTO, 0);
|
2004-05-05 18:33:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-06-21 17:21:16 +00:00
|
|
|
msg_printf(NULL, MSG_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
|
|
|
|
2005-03-07 21:19:09 +00:00
|
|
|
static void init_core_client(int argc, char** argv) {
|
2002-08-12 23:18:47 +00:00
|
|
|
setbuf(stdout, 0);
|
2005-02-01 06:16:13 +00:00
|
|
|
setbuf(stderr, 0);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
TCHAR szPath[MAX_PATH-1];
|
|
|
|
|
|
|
|
// change the current directory to the boinc install directory
|
|
|
|
GetModuleFileName(NULL, szPath, (sizeof(szPath)/sizeof(TCHAR)));
|
2005-03-10 00:43:22 +00:00
|
|
|
|
2005-02-01 06:16:13 +00:00
|
|
|
TCHAR *pszProg = strrchr(szPath, '\\');
|
|
|
|
if (pszProg) {
|
|
|
|
szPath[pszProg - szPath + 1] = 0;
|
|
|
|
SetCurrentDirectory(szPath);
|
|
|
|
}
|
|
|
|
|
2006-06-15 11:11:41 +00:00
|
|
|
#else
|
2005-03-25 12:23:30 +00:00
|
|
|
#endif
|
|
|
|
|
2006-05-21 21:11:28 +00:00
|
|
|
read_config_file();
|
2005-01-13 05:18:15 +00:00
|
|
|
gstate.parse_cmdline(argc, argv);
|
|
|
|
|
2006-08-01 12:36:19 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (g_use_sandbox)
|
|
|
|
umask (2); // Set file creation mask to be writable by both user and group
|
|
|
|
// Our umask will be inherited by all our child processes
|
|
|
|
#endif
|
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// Initialize the BOINC Diagnostics Framework
|
|
|
|
int dwDiagnosticsFlags =
|
2005-12-01 08:07:07 +00:00
|
|
|
BOINC_DIAG_DUMPCALLSTACKENABLED |
|
2005-02-01 00:54:06 +00:00
|
|
|
BOINC_DIAG_HEAPCHECKENABLED |
|
2005-02-27 08:28:52 +00:00
|
|
|
BOINC_DIAG_HEAPCHECKEVERYALLOC |
|
2005-02-01 00:54:06 +00:00
|
|
|
BOINC_DIAG_TRACETOSTDOUT;
|
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
if (gstate.redirect_io || gstate.executing_as_daemon || gstate.detach_console) {
|
|
|
|
dwDiagnosticsFlags |=
|
|
|
|
BOINC_DIAG_REDIRECTSTDERR |
|
2005-02-01 00:54:06 +00:00
|
|
|
BOINC_DIAG_REDIRECTSTDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostics_init(
|
|
|
|
dwDiagnosticsFlags,
|
|
|
|
"stdoutdae",
|
|
|
|
"stderrdae"
|
2004-03-25 22:24:33 +00:00
|
|
|
);
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2004-09-06 20:30:22 +00:00
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
// Win32 - detach from console if requested
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (gstate.detach_console) {
|
|
|
|
FreeConsole();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-28 19:25:52 +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
|
2003-04-29 19:53:18 +00:00
|
|
|
#endif
|
2003-04-08 18:36:03 +00:00
|
|
|
|
2005-06-28 19:25:52 +00:00
|
|
|
// Windows: install console controls
|
2005-02-01 00:29:04 +00:00
|
|
|
#ifdef _WIN32
|
2005-10-24 13:10:18 +00:00
|
|
|
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleControlHandler, TRUE)){
|
2006-03-31 00:43:50 +00:00
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, "Failed to register the console control handler\n");
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(TEXT("Failed to register the console control handler\n"));
|
|
|
|
}
|
2005-10-24 13:10:18 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf(
|
|
|
|
"\nTo pause/resume tasks hit CTRL-C, to exit hit CTRL-BREAK\n"
|
|
|
|
);
|
2004-12-06 22:41:19 +00:00
|
|
|
}
|
2004-01-15 01:42:49 +00:00
|
|
|
#endif
|
2005-02-01 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-31 00:43:50 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
char event_message[2048];
|
|
|
|
#endif
|
|
|
|
|
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) {
|
|
|
|
stprintf(event_message,
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to initialize the BOINC Client Library.\n"
|
|
|
|
"Load failed: %s\n"), windows_error_string(event_message, sizeof(event_message))
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, event_message);
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(event_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
retval = wait_client_mutex(".", 10);
|
2006-03-31 00:43:50 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Another instance of BOINC is running\n"
|
|
|
|
);
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
LogEventErrorMessage(
|
|
|
|
TEXT("Another instance of BOINC is running")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ERR_EXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize WinSock
|
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
|
|
|
if ( WinsockInitialize() != 0 ) {
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr,
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to initialize the Windows Sockets interface\n"
|
|
|
|
"Terminating Application...\n")
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to initialize the Windows Sockets interface\n"
|
|
|
|
"Terminating Application...\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
curl_init();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(g_hClientLibraryDll) {
|
|
|
|
ClientLibraryStartup fnClientLibraryStartup;
|
|
|
|
fnClientLibraryStartup = (ClientLibraryStartup)GetProcAddress(g_hClientLibraryDll, _T("ClientLibraryStartup"));
|
|
|
|
if(fnClientLibraryStartup) {
|
|
|
|
if(!fnClientLibraryStartup()) {
|
|
|
|
stprintf(event_message,
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to initialize the BOINC Idle Detection Interface.\n"
|
|
|
|
"BOINC will not be able to determine if the user is idle or not...\n"
|
|
|
|
"Load failed: %s\n"), windows_error_string(event_message, sizeof(event_message))
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, event_message);
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(event_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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) {
|
|
|
|
fprintf(stderr, "gstate.init() failed: %d\n", retval);
|
2006-03-31 00:43:50 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (gstate.executing_as_daemon) {
|
|
|
|
stprintf(event_message, TEXT("gstate.init() failed: %d\n"), retval);
|
|
|
|
LogEventErrorMessage(event_message);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (gstate.executing_as_daemon) {
|
|
|
|
LogEventInfoMessage(
|
|
|
|
TEXT("BOINC service is initialization completed, now begining process execution...\n")
|
|
|
|
);
|
2004-04-15 20:42:15 +00:00
|
|
|
}
|
2006-03-31 00:43:50 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
}
|
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) {
|
2004-05-05 17:48:39 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Exit requested by user");
|
2002-07-01 18:16:31 +00:00
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2006-07-11 23:48:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (requested_suspend) {
|
2006-11-10 18:21:05 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_NEVER, 0);
|
|
|
|
gstate.network_mode.set(RUN_MODE_NEVER, 0);
|
2006-07-11 23:48:56 +00:00
|
|
|
requested_suspend = false;
|
|
|
|
}
|
|
|
|
if (requested_resume) {
|
2006-11-10 18:21:05 +00:00
|
|
|
gstate.run_mode.set(RUN_MODE_AUTO, 0);
|
|
|
|
gstate.network_mode.set(RUN_MODE_AUTO, 0);
|
2006-07-11 23:48:56 +00:00
|
|
|
requested_resume = false;
|
|
|
|
}
|
|
|
|
#endif
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
// give timeslice also to other processes,
|
|
|
|
// otherwise we will get 100% cpu
|
|
|
|
DosSleep(0);
|
|
|
|
#endif
|
2002-04-30 22:22:54 +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) {
|
|
|
|
ClientLibraryShutdown fnClientLibraryShutdown;
|
|
|
|
fnClientLibraryShutdown = (ClientLibraryShutdown)GetProcAddress(g_hClientLibraryDll, _T("ClientLibraryShutdown"));
|
|
|
|
if(fnClientLibraryShutdown) {
|
|
|
|
fnClientLibraryShutdown();
|
|
|
|
}
|
|
|
|
if(!FreeLibrary(g_hClientLibraryDll)) {
|
|
|
|
stprintf(event_message,
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to cleanup the BOINC Idle Detection Interface\n"
|
|
|
|
"Unload failed: %s\n"), windows_error_string(event_message, sizeof(event_message))
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, event_message);
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(event_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_hClientLibraryDll = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
if ( WinsockCleanup() != 0 ) {
|
|
|
|
stprintf(event_message,
|
|
|
|
TEXT("BOINC Core Client Error Message\n"
|
|
|
|
"Failed to cleanup the Windows Sockets interface\n"
|
|
|
|
"Unload failed: %s\n"), windows_error_string(event_message, sizeof(event_message))
|
|
|
|
);
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
fprintf(stderr, event_message);
|
|
|
|
} else {
|
|
|
|
LogEventErrorMessage(event_message);
|
|
|
|
}
|
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_bIsWin9x && g_Win9xMonitorSystemThreadID)
|
|
|
|
PostThreadMessage(g_Win9xMonitorSystemThreadID, WM_QUIT, 0, 0);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
curl_cleanup();
|
|
|
|
boinc_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
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
#ifdef _WIN32
|
2006-03-31 02:30:06 +00:00
|
|
|
int i;
|
2006-03-31 00:43:50 +00:00
|
|
|
int len;
|
|
|
|
char *commandLine;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
// Allow the system to know it is running as a Windows service
|
|
|
|
// and adjust it's diagnostics schemes accordingly.
|
|
|
|
if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) {
|
|
|
|
if ( stricmp( "daemon", argv[1]+1 ) == 0 ) {
|
|
|
|
gstate.executing_as_daemon = true;
|
|
|
|
LogEventInfoMessage(
|
|
|
|
TEXT("BOINC service is initializing...\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-01 08:07:07 +00:00
|
|
|
// 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.
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
// 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[i], "-detach") == 0 || strcmp(argv[i], "--detach") == 0) {
|
|
|
|
argv[i] = "-detach_phase_two";
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2006-01-16 04:09:03 +00:00
|
|
|
#elif defined linux
|
2006-03-31 02:30:06 +00:00
|
|
|
int i;
|
|
|
|
|
2006-01-16 04:09:03 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-daemon") == 0 || strcmp(argv[i], "--daemon") == 0) {
|
2006-09-08 22:35:19 +00:00
|
|
|
syslog(LOG_DAEMON|LOG_INFO, "Starting Boinc-Daemon, listening on port %d.", GUI_RPC_PORT);
|
2006-01-16 04:09:03 +00:00
|
|
|
// 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.
|
|
|
|
daemon(1, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-18 04:40:23 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
if (getuid() == (uid_t)0) // If the real user ID is root, we are executing as a daemon
|
|
|
|
gstate.executing_as_daemon = true;
|
2005-12-01 08:07:07 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
2005-10-24 13:10:18 +00:00
|
|
|
// Figure out if we're on Win9x
|
2005-12-01 08:07:07 +00:00
|
|
|
OSVERSIONINFO osvi;
|
2005-10-24 13:10:18 +00:00
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
g_bIsWin9x = osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS;
|
|
|
|
if (g_bIsWin9x) {
|
|
|
|
// Win9x doesn't send us the shutdown or close console
|
|
|
|
// event, so we are going to create a hidden window
|
|
|
|
// to trap a WM_QUERYENDSESSION event.
|
|
|
|
g_hWin9xMonitorSystemThread = CreateThread(
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
Win9xMonitorSystemThread,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&g_Win9xMonitorSystemThreadID);
|
|
|
|
|
|
|
|
if (g_hWin9xMonitorSystemThread) {
|
|
|
|
CloseHandle(g_hWin9xMonitorSystemThread);
|
|
|
|
} else {
|
|
|
|
g_hWin9xMonitorSystemThread = NULL;
|
|
|
|
g_Win9xMonitorSystemThreadID = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-18 20:52:14 +00:00
|
|
|
|
2004-06-09 18:17:25 +00:00
|
|
|
SERVICE_TABLE_ENTRY dispatchTable[] = {
|
2004-12-06 22:41:19 +00:00
|
|
|
{ TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2004-03-19 00:13:22 +00:00
|
|
|
if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) {
|
2005-12-16 03:45:01 +00:00
|
|
|
if ( stricmp( "daemon", argv[1]+1 ) == 0 ) {
|
2004-04-29 00:57:26 +00:00
|
|
|
|
2004-12-06 22:41:19 +00:00
|
|
|
printf( "\nStartServiceCtrlDispatcher being called.\n" );
|
|
|
|
printf( "This may take several seconds. Please wait.\n" );
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2004-12-06 22:41:19 +00:00
|
|
|
if (!StartServiceCtrlDispatcher(dispatchTable)) {
|
|
|
|
LogEventErrorMessage(TEXT("StartServiceCtrlDispatcher failed."));
|
2004-03-19 00:13:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-02-01 00:29:04 +00:00
|
|
|
retval = boinc_main_loop();
|
2004-12-06 22:41:19 +00:00
|
|
|
}
|
2004-03-19 00:13:22 +00:00
|
|
|
} else {
|
2005-02-01 00:29:04 +00:00
|
|
|
retval = boinc_main_loop();
|
2004-12-06 22:41:19 +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__)
|
2006-09-25 17:40:54 +00:00
|
|
|
// GDB can't attach to applications which are running as a diferent user
|
|
|
|
// or group, so fix up data with current user and group during debugging
|
|
|
|
//
|
2006-08-01 12:36:19 +00:00
|
|
|
if (check_security(g_use_sandbox, false))
|
2006-06-15 11:11:41 +00:00
|
|
|
SetBOINCDataOwnersGroupsAndPermissions();
|
2006-07-28 09:05:54 +00:00
|
|
|
#endif // _DEBUG && __APPLE__
|
2006-08-01 12:36:19 +00:00
|
|
|
int i = check_security(g_use_sandbox, false);
|
2006-06-21 12:41:06 +00:00
|
|
|
if (i) {
|
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"
|
|
|
|
#elif defined linux
|
|
|
|
" or run\n the shell script secure.sh"
|
|
|
|
#endif
|
2006-09-26 00:12:09 +00:00
|
|
|
". (Error code %d)\n", i
|
2006-09-25 17:40:54 +00:00
|
|
|
);
|
2006-06-15 11:11:41 +00:00
|
|
|
return ERR_USER_PERMISSION;
|
|
|
|
}
|
|
|
|
#endif // SANDBOX
|
|
|
|
|
2006-07-28 09:05:54 +00:00
|
|
|
#ifdef __APPLE__
|
2005-03-11 13:08:52 +00:00
|
|
|
// Initialize Mac OS X idle time measurement / idle detection
|
|
|
|
gEventHandle = NXOpenEventStatus();
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
retval = boinc_main_loop();
|
2005-02-01 00:29:04 +00:00
|
|
|
#endif
|
2004-01-19 20:53:40 +00:00
|
|
|
|
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$";
|