2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-07-09 05:23:40 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-07-09 05:23:40 +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.
|
2005-07-09 05:23:40 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-07-09 05:23:40 +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.
|
|
|
|
//
|
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/>.
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "boinc_win.h"
|
2004-04-29 00:57:26 +00:00
|
|
|
#include "diagnostics.h"
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "error_numbers.h"
|
2009-11-05 18:11:20 +00:00
|
|
|
#include "url.h"
|
2004-02-04 19:14:39 +00:00
|
|
|
#include "util.h"
|
2009-08-07 18:16:21 +00:00
|
|
|
#include "win_util.h"
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "network.h"
|
2009-11-05 18:11:20 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "client_state.h"
|
|
|
|
#include "log_flags.h"
|
|
|
|
#include "client_msgs.h"
|
|
|
|
#include "http_curl.h"
|
|
|
|
#include "sandbox.h"
|
2004-05-05 18:43:41 +00:00
|
|
|
#include "main.h"
|
2009-12-05 01:02:46 +00:00
|
|
|
#include "cs_proxy.h"
|
2010-07-30 20:51:08 +00:00
|
|
|
#include "net_stats.h"
|
2009-12-05 01:02:46 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
#include "sysmon_win.h"
|
|
|
|
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
static HANDLE g_hWindowsMonitorSystemPowerThread = NULL;
|
|
|
|
static HWND g_hWndWindowsMonitorSystemPower = NULL;
|
|
|
|
static HANDLE g_hWindowsMonitorSystemProxyThread = NULL;
|
2009-08-03 20:55:00 +00:00
|
|
|
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-03 20:55:00 +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.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Quit operations
|
2009-08-03 21:46:16 +00:00
|
|
|
static void quit_client() {
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.requested_exit = true;
|
|
|
|
while (1) {
|
|
|
|
boinc_sleep(1.0);
|
|
|
|
if (gstate.cleanup_completed) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suspend client operations
|
2009-08-03 21:46:16 +00:00
|
|
|
static void suspend_client(bool wait) {
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.requested_suspend = true;
|
|
|
|
if (wait) {
|
|
|
|
while (1) {
|
|
|
|
boinc_sleep(1.0);
|
|
|
|
if (!gstate.active_tasks.is_task_executing()) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume client operations
|
2009-08-03 21:46:16 +00:00
|
|
|
static void resume_client() {
|
2009-08-03 20:55:00 +00:00
|
|
|
gstate.requested_resume = true;
|
|
|
|
}
|
2004-04-29 00:57:26 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
// Process console messages sent by the system
|
2009-08-07 18:16:21 +00:00
|
|
|
static BOOL WINAPI console_control_handler( DWORD dwCtrlType ){
|
2009-08-03 20:55:00 +00:00
|
|
|
BOOL bReturnStatus = FALSE;
|
|
|
|
BOINCTRACE("***** Console Event Detected *****\n");
|
|
|
|
switch( dwCtrlType ){
|
|
|
|
case CTRL_LOGOFF_EVENT:
|
|
|
|
BOINCTRACE("Event: CTRL-LOGOFF Event\n");
|
|
|
|
if (!gstate.executing_as_daemon) {
|
|
|
|
quit_client();
|
|
|
|
}
|
|
|
|
bReturnStatus = TRUE;
|
|
|
|
break;
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_BREAK_EVENT:
|
|
|
|
BOINCTRACE("Event: CTRL-C or CTRL-BREAK Event\n");
|
|
|
|
quit_client();
|
|
|
|
bReturnStatus = TRUE;
|
|
|
|
break;
|
|
|
|
case CTRL_CLOSE_EVENT:
|
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
|
|
|
BOINCTRACE("Event: CTRL-CLOSE or CTRL-SHUTDOWN Event\n");
|
|
|
|
quit_client();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return bReturnStatus;
|
|
|
|
}
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
// Trap events on Windows so we can clean ourselves up.
|
|
|
|
static LRESULT CALLBACK WindowsMonitorSystemPowerWndProc(
|
|
|
|
HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
|
|
|
|
) {
|
|
|
|
switch(uMsg) {
|
|
|
|
// On Windows power events are broadcast via the WM_POWERBROADCAST
|
|
|
|
// window message. It has the following parameters:
|
|
|
|
// PBT_APMQUERYSUSPEND
|
|
|
|
// PBT_APMQUERYSUSPENDFAILED
|
|
|
|
// PBT_APMSUSPEND
|
|
|
|
// PBT_APMRESUMECRITICAL
|
|
|
|
// PBT_APMRESUMESUSPEND
|
|
|
|
// PBT_APMBATTERYLOW
|
|
|
|
// PBT_APMPOWERSTATUSCHANGE
|
|
|
|
// PBT_APMOEMEVENT
|
|
|
|
// PBT_APMRESUMEAUTOMATIC
|
|
|
|
case WM_POWERBROADCAST:
|
|
|
|
switch(wParam) {
|
|
|
|
// System is preparing to suspend. This is valid on
|
|
|
|
// Windows versions older than Vista
|
|
|
|
case PBT_APMQUERYSUSPEND:
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// System is resuming from a failed request to suspend
|
|
|
|
// activity. This is only valid on Windows versions
|
|
|
|
// older than Vista
|
|
|
|
case PBT_APMQUERYSUSPENDFAILED:
|
|
|
|
resume_client();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// System is critically low on battery power. This is
|
|
|
|
// only valid on Windows versions older than Vista
|
|
|
|
case PBT_APMBATTERYLOW:
|
|
|
|
msg_printf(NULL, MSG_INFO, "Critical battery alarm, Windows is suspending operations");
|
|
|
|
suspend_client(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// System is suspending
|
|
|
|
case PBT_APMSUSPEND:
|
|
|
|
msg_printf(NULL, MSG_INFO, "Windows is suspending operations");
|
|
|
|
suspend_client(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// System is resuming from a normal power event
|
|
|
|
case PBT_APMRESUMESUSPEND:
|
|
|
|
msg_printf(NULL, MSG_INFO, "Windows is resuming operations");
|
|
|
|
|
|
|
|
// Check for a proxy
|
|
|
|
working_proxy_info.need_autodetect_proxy_settings = true;
|
|
|
|
|
|
|
|
resume_client();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a thread to monitor system events
|
|
|
|
static DWORD WINAPI WindowsMonitorSystemPowerThread( LPVOID ) {
|
|
|
|
WNDCLASS wc;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
wc.style = CS_GLOBALCLASS;
|
|
|
|
wc.lpfnWndProc = (WNDPROC)WindowsMonitorSystemPowerWndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = NULL;
|
|
|
|
wc.hIcon = NULL;
|
|
|
|
wc.hCursor = NULL;
|
|
|
|
wc.hbrBackground = NULL;
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = "BOINCWindowsMonitorSystemPower";
|
|
|
|
|
|
|
|
if (!RegisterClass(&wc)) {
|
|
|
|
log_message_error("Failed to register the WindowsMonitorSystem window class.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hWndWindowsMonitorSystemPower = CreateWindow(
|
|
|
|
wc.lpszClassName,
|
|
|
|
"BOINC Monitor System (Power)",
|
|
|
|
WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!g_hWndWindowsMonitorSystemPower) {
|
|
|
|
log_message_error("Failed to create the WindowsMonitorSystem window.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-07 18:16:21 +00:00
|
|
|
// Detect any proxy configuration settings automatically.
|
|
|
|
static void windows_detect_autoproxy_settings() {
|
2009-08-21 18:52:35 +00:00
|
|
|
if (log_flags.proxy_debug) {
|
2010-04-29 20:32:51 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[proxy] automatic proxy check in progress");
|
2009-08-21 18:52:35 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 18:16:21 +00:00
|
|
|
HMODULE hModWinHttp = LoadLibrary("winhttp.dll");
|
2009-08-28 18:01:51 +00:00
|
|
|
if (!hModWinHttp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pfnWinHttpOpen pWinHttpOpen =
|
|
|
|
(pfnWinHttpOpen)GetProcAddress(hModWinHttp, "WinHttpOpen");
|
|
|
|
if (!pWinHttpOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pfnWinHttpCloseHandle pWinHttpCloseHandle =
|
|
|
|
(pfnWinHttpCloseHandle)(GetProcAddress(hModWinHttp, "WinHttpCloseHandle"));
|
|
|
|
if (!pWinHttpCloseHandle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pfnWinHttpGetProxyForUrl pWinHttpGetProxyForUrl =
|
|
|
|
(pfnWinHttpGetProxyForUrl)(GetProcAddress(hModWinHttp, "WinHttpGetProxyForUrl"));
|
|
|
|
if (!pWinHttpGetProxyForUrl) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-07 18:16:21 +00:00
|
|
|
|
2009-08-28 18:01:51 +00:00
|
|
|
HINTERNET hWinHttp = NULL;
|
|
|
|
WINHTTP_AUTOPROXY_OPTIONS autoproxy_options;
|
|
|
|
WINHTTP_PROXY_INFO proxy_info;
|
2009-11-07 06:00:08 +00:00
|
|
|
PARSED_URL purl;
|
2009-08-28 18:01:51 +00:00
|
|
|
std::wstring network_test_url;
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
|
|
|
|
memset(&autoproxy_options, 0, sizeof(autoproxy_options));
|
|
|
|
memset(&proxy_info, 0, sizeof(proxy_info));
|
|
|
|
|
|
|
|
autoproxy_options.dwFlags =
|
|
|
|
WINHTTP_AUTOPROXY_AUTO_DETECT;
|
|
|
|
autoproxy_options.dwAutoDetectFlags =
|
|
|
|
WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
|
|
|
|
autoproxy_options.fAutoLogonIfChallenged = TRUE;
|
|
|
|
|
|
|
|
network_test_url = A2W(config.network_test_url).c_str();
|
|
|
|
|
|
|
|
hWinHttp = pWinHttpOpen(
|
2009-09-03 04:20:09 +00:00
|
|
|
L"BOINC client",
|
2009-10-23 16:52:50 +00:00
|
|
|
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
2009-08-28 18:01:51 +00:00
|
|
|
WINHTTP_NO_PROXY_NAME,
|
|
|
|
WINHTTP_NO_PROXY_BYPASS,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
if (pWinHttpGetProxyForUrl(hWinHttp, network_test_url.c_str(), &autoproxy_options, &proxy_info)) {
|
|
|
|
|
|
|
|
if (log_flags.proxy_debug) {
|
2010-04-29 20:32:51 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[proxy] successfully executed proxy check", hWinHttp);
|
2009-08-28 18:01:51 +00:00
|
|
|
}
|
2009-08-07 18:16:21 +00:00
|
|
|
|
2009-09-30 18:13:03 +00:00
|
|
|
// Apparently there are some conditions where WinHttpGetProxyForUrl can return
|
|
|
|
// success but where proxy_info.lpszProxy is null. Maybe related to UPNP?
|
|
|
|
//
|
|
|
|
// For the time being check to see if proxy_info.lpszProxy is non-null.
|
|
|
|
//
|
2009-09-30 18:18:42 +00:00
|
|
|
if (proxy_info.lpszProxy) {
|
2009-09-30 18:13:03 +00:00
|
|
|
std::string proxy(W2A(std::wstring(proxy_info.lpszProxy)));
|
|
|
|
std::string new_proxy;
|
2009-08-21 18:52:35 +00:00
|
|
|
|
2009-09-30 18:13:03 +00:00
|
|
|
if (log_flags.proxy_debug) {
|
2010-04-29 20:32:51 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[proxy] proxy list '%s'", proxy.c_str());
|
2009-08-28 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 18:13:03 +00:00
|
|
|
if (!proxy.empty()) {
|
|
|
|
// Trim string if more than one proxy is defined
|
|
|
|
// proxy list is defined as:
|
|
|
|
// ([<scheme>=][<scheme>"://"]<server>[":"<port>])
|
|
|
|
|
|
|
|
// Find and erase first delimeter type.
|
|
|
|
pos = proxy.find(';');
|
|
|
|
if (pos != -1 ) {
|
|
|
|
new_proxy = proxy.erase(pos);
|
|
|
|
proxy = new_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and erase second delimeter type.
|
|
|
|
pos = proxy.find(' ');
|
|
|
|
if (pos != -1 ) {
|
|
|
|
new_proxy = proxy.erase(pos);
|
|
|
|
proxy = new_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the remaining url
|
2009-11-07 06:00:08 +00:00
|
|
|
parse_url(proxy.c_str(), purl);
|
2009-08-28 18:01:51 +00:00
|
|
|
|
2009-09-30 18:13:03 +00:00
|
|
|
// Store the results for future use.
|
2010-07-30 20:51:08 +00:00
|
|
|
if (0 != strcmp(working_proxy_info.autodetect_server_name, purl.host)) {
|
|
|
|
// Reset clients connection error detection path
|
|
|
|
net_status.need_physical_connection = false;
|
|
|
|
|
|
|
|
working_proxy_info.autodetect_protocol = purl.protocol;
|
|
|
|
strcpy(working_proxy_info.autodetect_server_name, purl.host);
|
|
|
|
working_proxy_info.autodetect_port = purl.port;
|
|
|
|
}
|
2009-08-28 18:01:51 +00:00
|
|
|
|
2009-09-30 18:13:03 +00:00
|
|
|
if (log_flags.proxy_debug) {
|
|
|
|
msg_printf(NULL, MSG_INFO,
|
2010-04-29 20:32:51 +00:00
|
|
|
"[proxy] automatic proxy detected %s:%d",
|
2009-11-07 06:00:08 +00:00
|
|
|
purl.host, purl.port
|
2009-09-30 18:13:03 +00:00
|
|
|
);
|
|
|
|
}
|
2009-08-07 18:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-28 18:01:51 +00:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
if (proxy_info.lpszProxy) GlobalFree(proxy_info.lpszProxy);
|
|
|
|
if (proxy_info.lpszProxyBypass) GlobalFree(proxy_info.lpszProxyBypass);
|
|
|
|
} else {
|
|
|
|
// We can get here if the user is switching from a network that
|
|
|
|
// requires a proxy to one that does not require a proxy.
|
2009-12-05 01:02:46 +00:00
|
|
|
working_proxy_info.autodetect_protocol = 0;
|
|
|
|
strcpy(working_proxy_info.autodetect_server_name, "");
|
|
|
|
working_proxy_info.autodetect_port = 0;
|
2009-08-28 18:01:51 +00:00
|
|
|
if (log_flags.proxy_debug) {
|
2010-04-29 20:32:51 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[proxy] no automatic proxy detected");
|
2009-08-28 18:01:51 +00:00
|
|
|
}
|
2009-08-07 18:16:21 +00:00
|
|
|
}
|
2009-08-28 18:01:51 +00:00
|
|
|
if (hWinHttp) pWinHttpCloseHandle(hWinHttp);
|
|
|
|
FreeLibrary(hModWinHttp);
|
2009-08-21 18:52:35 +00:00
|
|
|
if (log_flags.proxy_debug) {
|
2010-04-29 20:32:51 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[proxy] automatic proxy check finished");
|
2009-08-21 18:52:35 +00:00
|
|
|
}
|
2009-08-07 18:16:21 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
static DWORD WINAPI WindowsMonitorSystemProxyThread( LPVOID ) {
|
|
|
|
|
|
|
|
while (1) {
|
2009-08-03 20:55:00 +00:00
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
if (working_proxy_info.need_autodetect_proxy_settings) {
|
|
|
|
working_proxy_info.have_autodetect_proxy_settings = false;
|
|
|
|
windows_detect_autoproxy_settings();
|
|
|
|
working_proxy_info.need_autodetect_proxy_settings = false;
|
|
|
|
working_proxy_info.have_autodetect_proxy_settings = true;
|
|
|
|
}
|
2009-08-03 20:55:00 +00:00
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
Sleep(1000);
|
2009-08-07 18:16:21 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the client software to monitor various system events
|
|
|
|
int initialize_system_monitor(int /*argc*/, char** /*argv*/) {
|
|
|
|
|
|
|
|
// Windows: install console controls
|
2009-08-07 18:16:21 +00:00
|
|
|
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)console_control_handler, TRUE)){
|
2009-08-03 20:55:00 +00:00
|
|
|
log_message_error("Failed to register the console control handler.");
|
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
// Create a window to receive system power events.
|
|
|
|
g_hWindowsMonitorSystemPowerThread = CreateThread(
|
2009-08-03 20:55:00 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
2010-07-19 22:09:45 +00:00
|
|
|
WindowsMonitorSystemPowerThread,
|
2009-08-03 20:55:00 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
2010-07-19 22:09:45 +00:00
|
|
|
NULL);
|
2009-08-03 20:55:00 +00:00
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
if (!g_hWindowsMonitorSystemPowerThread) {
|
|
|
|
g_hWindowsMonitorSystemPowerThread = NULL;
|
|
|
|
g_hWndWindowsMonitorSystemPower = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a thread to handle proxy auto-detection.
|
|
|
|
g_hWindowsMonitorSystemProxyThread = CreateThread(
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
WindowsMonitorSystemProxyThread,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!g_hWindowsMonitorSystemProxyThread) {
|
|
|
|
g_hWindowsMonitorSystemProxyThread = NULL;
|
2009-08-03 20:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup the system event monitor
|
|
|
|
int cleanup_system_monitor() {
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
if (g_hWindowsMonitorSystemPowerThread) {
|
|
|
|
TerminateThread(g_hWindowsMonitorSystemPowerThread, 0);
|
|
|
|
CloseHandle(g_hWindowsMonitorSystemPowerThread);
|
|
|
|
g_hWindowsMonitorSystemPowerThread = NULL;
|
|
|
|
g_hWndWindowsMonitorSystemPower = NULL;
|
2009-08-03 20:55:00 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 22:09:45 +00:00
|
|
|
if (g_hWindowsMonitorSystemProxyThread) {
|
|
|
|
TerminateThread(g_hWindowsMonitorSystemProxyThread, 0);
|
|
|
|
CloseHandle(g_hWindowsMonitorSystemProxyThread);
|
|
|
|
g_hWindowsMonitorSystemProxyThread = NULL;
|
2009-08-03 20:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal variables for managing the service
|
2004-01-19 20:53:40 +00:00
|
|
|
SERVICE_STATUS ssStatus; // current status of the service
|
|
|
|
SERVICE_STATUS_HANDLE sshStatusHandle;
|
|
|
|
DWORD dwErr = 0;
|
2004-02-04 19:14:39 +00:00
|
|
|
TCHAR szErr[1024];
|
2004-01-19 20:53:40 +00:00
|
|
|
|
2009-08-03 20:55:00 +00:00
|
|
|
SERVICE_TABLE_ENTRY service_dispatch_table[] = {
|
|
|
|
{ TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)BOINCServiceMain },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Inform the service control manager that the service is about to
|
|
|
|
// start.
|
|
|
|
int initialize_service_dispatcher(int /*argc*/, char** /*argv*/) {
|
|
|
|
fprintf(stdout, "\nStartServiceCtrlDispatcher being called.\n");
|
|
|
|
fprintf(stdout, "This may take several seconds. Please wait.\n");
|
|
|
|
|
|
|
|
if (!StartServiceCtrlDispatcher(service_dispatch_table)) {
|
|
|
|
log_message_error("StartServiceCtrlDispatcher failed.");
|
|
|
|
return ERR_IO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-29 00:57:26 +00:00
|
|
|
|
2004-01-19 20:53:40 +00:00
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
// FUNCTION: BOINCServiceMain
|
2004-01-19 20:53:40 +00:00
|
|
|
//
|
|
|
|
// PURPOSE: To perform actual initialization of the service
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// dwArgc - number of command line arguments
|
|
|
|
// lpszArgv - array of command line arguments
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// none
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
// This routine performs the service initialization and then calls
|
|
|
|
// the user defined main() routine to perform majority
|
|
|
|
// of the work.
|
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
void WINAPI BOINCServiceMain(DWORD /*dwArgc*/, LPTSTR * /*lpszArgv*/)
|
2004-01-19 20:53:40 +00:00
|
|
|
{
|
|
|
|
// SERVICE_STATUS members that don't change in example
|
|
|
|
//
|
|
|
|
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
2004-04-29 00:57:26 +00:00
|
|
|
ssStatus.dwControlsAccepted = SERVICE_ACCEPTED_ACTIONS;
|
2004-01-19 20:53:40 +00:00
|
|
|
ssStatus.dwServiceSpecificExitCode = 0;
|
|
|
|
|
2004-04-29 00:57:26 +00:00
|
|
|
|
2004-01-19 20:53:40 +00:00
|
|
|
// register our service control handler:
|
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), BOINCServiceCtrl);
|
2004-01-19 20:53:40 +00:00
|
|
|
if (!sshStatusHandle)
|
|
|
|
goto cleanup;
|
|
|
|
|
2004-04-29 00:57:26 +00:00
|
|
|
if (!ReportStatus(
|
2004-01-19 20:53:40 +00:00
|
|
|
SERVICE_RUNNING, // service state
|
|
|
|
ERROR_SUCCESS, // exit code
|
|
|
|
0)) // wait hint
|
|
|
|
goto cleanup;
|
|
|
|
|
2005-02-01 00:29:04 +00:00
|
|
|
dwErr = boinc_main_loop();
|
2004-01-19 20:53:40 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
// try to report the stopped status to the service control manager.
|
|
|
|
//
|
|
|
|
if (sshStatusHandle)
|
|
|
|
(VOID)ReportStatus(
|
2004-04-29 00:57:26 +00:00
|
|
|
SERVICE_STOPPED,
|
2004-01-19 20:53:40 +00:00
|
|
|
dwErr,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
// FUNCTION: BOINCServiceCtrl
|
2004-01-19 20:53:40 +00:00
|
|
|
//
|
|
|
|
// PURPOSE: This function is called by the SCM whenever
|
|
|
|
// ControlService() is called on this service.
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// dwCtrlCode - type of control requested
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// none
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
2009-08-03 20:55:00 +00:00
|
|
|
VOID WINAPI BOINCServiceCtrl(DWORD dwCtrlCode)
|
2004-01-19 20:53:40 +00:00
|
|
|
{
|
|
|
|
// Handle the requested control code.
|
|
|
|
//
|
|
|
|
switch(dwCtrlCode)
|
|
|
|
{
|
|
|
|
// Stop the service.
|
|
|
|
//
|
|
|
|
// SERVICE_STOP_PENDING should be reported before
|
|
|
|
// setting the Stop Event - hServerStopEvent - in
|
|
|
|
// ServiceStop(). This avoids a race condition
|
|
|
|
// which may result in a 1053 - The Service did not respond...
|
|
|
|
// error.
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
2006-05-04 17:39:00 +00:00
|
|
|
ReportStatus(SERVICE_STOP_PENDING, ERROR_SUCCESS, 30000);
|
2004-05-05 18:43:41 +00:00
|
|
|
quit_client();
|
2004-01-19 20:53:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Pause the service.
|
|
|
|
//
|
|
|
|
case SERVICE_CONTROL_PAUSE:
|
|
|
|
ReportStatus(SERVICE_PAUSE_PENDING, ERROR_SUCCESS, 10000);
|
2009-08-03 20:55:00 +00:00
|
|
|
suspend_client(true);
|
2004-01-19 20:53:40 +00:00
|
|
|
ReportStatus(SERVICE_PAUSED, ERROR_SUCCESS, 10000);
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Continue the service.
|
|
|
|
//
|
|
|
|
case SERVICE_CONTROL_CONTINUE:
|
|
|
|
ReportStatus(SERVICE_CONTINUE_PENDING, ERROR_SUCCESS, 10000);
|
2004-05-05 18:43:41 +00:00
|
|
|
resume_client();
|
2004-01-19 20:53:40 +00:00
|
|
|
ReportStatus(SERVICE_RUNNING, ERROR_SUCCESS, 10000);
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the service status.
|
|
|
|
//
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// invalid control code
|
|
|
|
//
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportStatus(ssStatus.dwCurrentState, ERROR_SUCCESS, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FUNCTION: ReportStatus()
|
|
|
|
//
|
|
|
|
// PURPOSE: Sets the current status of the service and
|
|
|
|
// reports it to the Service Control Manager
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// dwCurrentState - the state of the service
|
|
|
|
// dwWin32ExitCode - error code to report
|
|
|
|
// dwWaitHint - worst case estimate to next checkpoint
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// TRUE - success
|
|
|
|
// FALSE - failure
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
|
|
|
BOOL ReportStatus(DWORD dwCurrentState,
|
|
|
|
DWORD dwWin32ExitCode,
|
|
|
|
DWORD dwWaitHint)
|
|
|
|
{
|
|
|
|
static DWORD dwCheckPoint = 1;
|
|
|
|
BOOL fResult = TRUE;
|
|
|
|
|
|
|
|
if (dwCurrentState == SERVICE_START_PENDING)
|
|
|
|
ssStatus.dwControlsAccepted = 0;
|
|
|
|
else
|
2004-04-29 00:57:26 +00:00
|
|
|
ssStatus.dwControlsAccepted = SERVICE_ACCEPTED_ACTIONS;
|
2004-01-19 20:53:40 +00:00
|
|
|
|
|
|
|
ssStatus.dwCurrentState = dwCurrentState;
|
|
|
|
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
|
|
|
|
ssStatus.dwWaitHint = dwWaitHint;
|
|
|
|
|
|
|
|
if ( ( dwCurrentState == SERVICE_RUNNING ) ||
|
|
|
|
( dwCurrentState == SERVICE_STOPPED ) )
|
|
|
|
ssStatus.dwCheckPoint = 0;
|
|
|
|
else
|
|
|
|
ssStatus.dwCheckPoint = dwCheckPoint++;
|
|
|
|
|
|
|
|
|
|
|
|
// Report the status of the service to the service control manager.
|
|
|
|
//
|
2006-11-20 17:12:55 +00:00
|
|
|
fResult = SetServiceStatus( sshStatusHandle, &ssStatus);
|
|
|
|
if (!fResult) {
|
2004-01-19 20:53:40 +00:00
|
|
|
LogEventErrorMessage(TEXT("SetServiceStatus"));
|
|
|
|
}
|
|
|
|
return fResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FUNCTION: LogEventErrorMessage(LPTSTR lpszMsg)
|
|
|
|
//
|
|
|
|
// PURPOSE: Allows any thread to log an error message
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// lpszMsg - text for message
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// none
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
|
|
|
VOID LogEventErrorMessage(LPTSTR lpszMsg)
|
|
|
|
{
|
|
|
|
TCHAR szMsg[1024];
|
|
|
|
HANDLE hEventSource;
|
|
|
|
LPTSTR lpszStrings[2];
|
|
|
|
|
|
|
|
dwErr = GetLastError();
|
|
|
|
|
|
|
|
// Use event logging to log the error.
|
|
|
|
//
|
|
|
|
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
|
|
|
|
|
|
|
|
_stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
|
|
|
|
lpszStrings[0] = szMsg;
|
|
|
|
lpszStrings[1] = lpszMsg;
|
|
|
|
|
|
|
|
if (hEventSource != NULL) {
|
|
|
|
ReportEvent(hEventSource, // handle of event source
|
|
|
|
EVENTLOG_ERROR_TYPE, // event type
|
|
|
|
0, // event category
|
2004-04-29 00:57:26 +00:00
|
|
|
1, // event ID
|
2004-01-19 20:53:40 +00:00
|
|
|
NULL, // current user's SID
|
|
|
|
2, // strings in lpszStrings
|
|
|
|
0, // no bytes of raw data
|
|
|
|
(LPCSTR*)lpszStrings, // array of error strings
|
|
|
|
NULL); // no raw data
|
|
|
|
|
|
|
|
(VOID) DeregisterEventSource(hEventSource);
|
|
|
|
}
|
2004-04-29 00:57:26 +00:00
|
|
|
}
|
2004-01-19 20:53:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FUNCTION: LogEventWarningMessage(LPTSTR lpszMsg)
|
|
|
|
//
|
|
|
|
// PURPOSE: Allows any thread to log an warning message
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// lpszMsg - text for message
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// none
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
|
|
|
VOID LogEventWarningMessage(LPTSTR lpszMsg)
|
|
|
|
{
|
|
|
|
HANDLE hEventSource;
|
|
|
|
LPTSTR lpszStrings[2];
|
|
|
|
|
|
|
|
// Use event logging to log the error.
|
|
|
|
//
|
|
|
|
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
|
|
|
|
|
2004-05-05 20:23:52 +00:00
|
|
|
lpszStrings[0] = lpszMsg;
|
|
|
|
lpszStrings[1] = '\0';
|
2004-01-19 20:53:40 +00:00
|
|
|
|
|
|
|
if (hEventSource != NULL) {
|
|
|
|
ReportEvent(hEventSource, // handle of event source
|
|
|
|
EVENTLOG_WARNING_TYPE,// event type
|
|
|
|
0, // event category
|
2004-04-29 00:57:26 +00:00
|
|
|
1, // event ID
|
2004-01-19 20:53:40 +00:00
|
|
|
NULL, // current user's SID
|
|
|
|
2, // strings in lpszStrings
|
|
|
|
0, // no bytes of raw data
|
|
|
|
(LPCSTR*)lpszStrings, // array of error strings
|
|
|
|
NULL); // no raw data
|
|
|
|
|
|
|
|
(VOID) DeregisterEventSource(hEventSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FUNCTION: LogEventInfoMessage(LPTSTR lpszMsg)
|
|
|
|
//
|
|
|
|
// PURPOSE: Allows any thread to log an info message
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// lpszMsg - text for message
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// none
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
|
|
|
VOID LogEventInfoMessage(LPTSTR lpszMsg)
|
|
|
|
{
|
|
|
|
HANDLE hEventSource;
|
|
|
|
LPTSTR lpszStrings[2];
|
|
|
|
|
|
|
|
// Use event logging to log the error.
|
|
|
|
//
|
|
|
|
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
|
|
|
|
|
2004-05-05 20:23:52 +00:00
|
|
|
lpszStrings[0] = lpszMsg;
|
|
|
|
lpszStrings[1] = '\0';
|
2004-01-19 20:53:40 +00:00
|
|
|
|
|
|
|
if (hEventSource != NULL) {
|
|
|
|
ReportEvent(hEventSource, // handle of event source
|
|
|
|
EVENTLOG_INFORMATION_TYPE,// event type
|
|
|
|
0, // event category
|
2004-04-29 00:57:26 +00:00
|
|
|
1, // event ID
|
2004-01-19 20:53:40 +00:00
|
|
|
NULL, // current user's SID
|
|
|
|
2, // strings in lpszStrings
|
|
|
|
0, // no bytes of raw data
|
|
|
|
(LPCSTR*)lpszStrings, // array of error strings
|
|
|
|
NULL); // no raw data
|
|
|
|
|
|
|
|
(VOID) DeregisterEventSource(hEventSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|