2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-03-02 19:30:39 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-03-02 19:30:39 +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.
|
2006-03-02 19:30:39 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-03-02 19:30:39 +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/>.
|
2006-03-02 19:30:39 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2009-11-13 21:23:15 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2009-11-13 21:23:15 +00:00
|
|
|
#endif
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if BOINC has been installed as a service.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool is_daemon_installed()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if BOINC has been told to start.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool is_daemon_starting()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
SERVICE_STATUS ssStatus;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (QueryServiceStatus(schService, &ssStatus))
|
|
|
|
{
|
|
|
|
if (ssStatus.dwCurrentState == SERVICE_START_PENDING)
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if BOINC is executing as a service.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool is_daemon_running()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
SERVICE_STATUS ssStatus;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (QueryServiceStatus(schService, &ssStatus))
|
|
|
|
{
|
|
|
|
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if BOINC has been told to stop.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool is_daemon_stopping()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
SERVICE_STATUS ssStatus;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (QueryServiceStatus(schService, &ssStatus))
|
|
|
|
{
|
|
|
|
if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if BOINC has stopped executing as a service.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool is_daemon_stopped()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
SERVICE_STATUS ssStatus;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (QueryServiceStatus(schService, &ssStatus))
|
|
|
|
{
|
|
|
|
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-07-25 05:19:29 +00:00
|
|
|
* Start the BOINC Service via the BOINC Service Control utility.
|
2006-03-02 19:30:39 +00:00
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool start_daemon_via_daemonctrl()
|
2009-07-25 05:19:29 +00:00
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2009-07-25 05:19:29 +00:00
|
|
|
BOOL bProcessStarted;
|
2009-07-25 16:47:04 +00:00
|
|
|
SHELLEXECUTEINFO shex;
|
2009-07-25 05:19:29 +00:00
|
|
|
TCHAR szPath[MAX_PATH+1];
|
2009-07-25 16:47:04 +00:00
|
|
|
TCHAR szExe[MAX_PATH+1];
|
2009-07-25 05:19:29 +00:00
|
|
|
unsigned long ulExitCode;
|
|
|
|
|
|
|
|
// Determine the path to the BOINC Service Control utility
|
|
|
|
// by finding out the path to the executable that requested
|
|
|
|
// that we start the service.
|
|
|
|
GetModuleFileName(NULL, szPath, (sizeof(szPath)/sizeof(TCHAR)));
|
|
|
|
|
|
|
|
TCHAR *pszProg = _tcsrchr(szPath, '\\');
|
|
|
|
if (pszProg) {
|
2009-07-25 16:47:04 +00:00
|
|
|
szPath[pszProg - szPath] = 0;
|
2009-07-25 05:19:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 16:47:04 +00:00
|
|
|
// The executable needs to contain the path.
|
|
|
|
_sntprintf(
|
|
|
|
szExe, (sizeof(szPath)/sizeof(TCHAR)),
|
|
|
|
_T("\"%s\\boincsvcctrl.exe\""),
|
|
|
|
szPath
|
2009-07-25 05:19:29 +00:00
|
|
|
);
|
|
|
|
|
2009-07-25 16:47:04 +00:00
|
|
|
memset( &shex, 0, sizeof( shex) );
|
|
|
|
|
|
|
|
shex.cbSize = sizeof( SHELLEXECUTEINFO );
|
|
|
|
shex.fMask = SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
shex.hwnd = NULL;
|
2009-07-26 01:33:33 +00:00
|
|
|
if ((LOBYTE(LOWORD(GetVersion()))) >= 6) {
|
|
|
|
shex.lpVerb = _T("runas");
|
|
|
|
}
|
2009-07-25 16:47:04 +00:00
|
|
|
shex.lpFile = (LPCTSTR)&szExe;
|
|
|
|
shex.lpParameters = _T("--start");
|
|
|
|
shex.lpDirectory = (LPCTSTR)&szPath;
|
|
|
|
shex.nShow = SW_HIDE;
|
|
|
|
|
|
|
|
bProcessStarted = ShellExecuteEx( &shex );
|
|
|
|
|
2009-07-25 05:19:29 +00:00
|
|
|
if (bProcessStarted) {
|
2009-07-25 16:47:04 +00:00
|
|
|
WaitForSingleObject(shex.hProcess, INFINITE);
|
|
|
|
if (GetExitCodeProcess(shex.hProcess, &ulExitCode)) {
|
2009-07-25 05:19:29 +00:00
|
|
|
if (ulExitCode == 0) {
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2009-07-25 05:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the BOINC Service.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool start_daemon()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ | GENERIC_EXECUTE);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (StartService(schService, 0, NULL))
|
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-07-25 05:19:29 +00:00
|
|
|
* Stop the BOINC Service via the BOINC Service Control utility.
|
2006-03-02 19:30:39 +00:00
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool stop_daemon_via_daemonctrl()
|
2009-07-25 05:19:29 +00:00
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2009-07-25 05:19:29 +00:00
|
|
|
BOOL bProcessStarted;
|
2009-07-25 16:47:04 +00:00
|
|
|
SHELLEXECUTEINFO shex;
|
2009-07-25 05:19:29 +00:00
|
|
|
TCHAR szPath[MAX_PATH+1];
|
2009-07-25 16:47:04 +00:00
|
|
|
TCHAR szExe[MAX_PATH+1];
|
2009-07-25 05:19:29 +00:00
|
|
|
unsigned long ulExitCode;
|
|
|
|
|
|
|
|
// Determine the path to the BOINC Service Control utility
|
|
|
|
// by finding out the path to the executable that requested
|
|
|
|
// that we start the service.
|
|
|
|
GetModuleFileName(NULL, szPath, (sizeof(szPath)/sizeof(TCHAR)));
|
|
|
|
|
|
|
|
TCHAR *pszProg = _tcsrchr(szPath, '\\');
|
|
|
|
if (pszProg) {
|
2009-07-25 16:47:04 +00:00
|
|
|
szPath[pszProg - szPath] = 0;
|
2009-07-25 05:19:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 16:47:04 +00:00
|
|
|
// The executable needs to contain the path.
|
|
|
|
_sntprintf(
|
|
|
|
szExe, (sizeof(szPath)/sizeof(TCHAR)),
|
|
|
|
_T("\"%s\\boincsvcctrl.exe\""),
|
|
|
|
szPath
|
2009-07-25 05:19:29 +00:00
|
|
|
);
|
|
|
|
|
2009-07-25 16:47:04 +00:00
|
|
|
memset( &shex, 0, sizeof( shex) );
|
|
|
|
|
|
|
|
shex.cbSize = sizeof( SHELLEXECUTEINFO );
|
|
|
|
shex.fMask = SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
shex.hwnd = NULL;
|
2009-07-26 01:33:33 +00:00
|
|
|
if ((LOBYTE(LOWORD(GetVersion()))) >= 6) {
|
|
|
|
shex.lpVerb = _T("runas");
|
|
|
|
}
|
2009-07-25 16:47:04 +00:00
|
|
|
shex.lpFile = (LPCTSTR)&szExe;
|
|
|
|
shex.lpParameters = _T("--stop");
|
|
|
|
shex.lpDirectory = (LPCTSTR)&szPath;
|
|
|
|
shex.nShow = SW_HIDE;
|
|
|
|
|
|
|
|
bProcessStarted = ShellExecuteEx( &shex );
|
|
|
|
|
2009-07-25 05:19:29 +00:00
|
|
|
if (bProcessStarted) {
|
2009-07-25 16:47:04 +00:00
|
|
|
WaitForSingleObject(shex.hProcess, INFINITE);
|
|
|
|
if (GetExitCodeProcess(shex.hProcess, &ulExitCode)) {
|
2009-07-25 05:19:29 +00:00
|
|
|
if (ulExitCode == 0) {
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2009-07-25 05:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the BOINC Service.
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
bool stop_daemon()
|
2006-03-02 19:30:39 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
SERVICE_STATUS ssStatus;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bRetVal = false;
|
2006-03-02 19:30:39 +00:00
|
|
|
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
GENERIC_READ); // full access rights
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
{
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
|
|
|
_T("BOINC"), // service name
|
|
|
|
GENERIC_READ | GENERIC_EXECUTE);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
{
|
|
|
|
if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus))
|
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
bRetVal = true;
|
2006-03-02 19:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
2009-07-25 05:19:29 +00:00
|
|
|
|