2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package 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 General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArchDaemonWindows.h"
|
|
|
|
#include "CArch.h"
|
|
|
|
#include "CArchMiscWindows.h"
|
|
|
|
#include "XArchWindows.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
#include "stdvector.h"
|
2002-06-04 12:26:23 +00:00
|
|
|
|
|
|
|
//
|
2003-01-04 22:01:32 +00:00
|
|
|
// CArchDaemonWindows
|
2002-06-04 12:26:23 +00:00
|
|
|
//
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows* CArchDaemonWindows::s_daemon = NULL;
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-01-22 08:36:43 +00:00
|
|
|
CArchDaemonWindows::CArchDaemonWindows() :
|
|
|
|
m_daemonThread(NULL)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::~CArchDaemonWindows()
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
int
|
2003-01-22 08:36:43 +00:00
|
|
|
CArchDaemonWindows::runDaemon(RunFunc runFunc)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(s_daemon != NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-01-22 08:36:43 +00:00
|
|
|
return s_daemon->doRunDaemon(runFunc);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::daemonRunning(bool running)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
// if s_daemon is NULL we assume we're running on the windows
|
|
|
|
// 95 family and we just ignore this call so the caller doesn't
|
|
|
|
// have to go through the trouble of not calling it on the
|
|
|
|
// windows 95 family.
|
|
|
|
if (s_daemon != NULL) {
|
|
|
|
s_daemon->doDaemonRunning(running);
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::daemonFailed(int result)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
// if s_daemon is NULL we assume we're running on the windows
|
|
|
|
// 95 family and we just ignore this call so the caller doesn't
|
|
|
|
// have to go through the trouble of not calling it on the
|
|
|
|
// windows 95 family.
|
|
|
|
if (s_daemon != NULL) {
|
|
|
|
throw XArchDaemonRunFailed(result);
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
void
|
|
|
|
CArchDaemonWindows::installDaemon(const char* name,
|
|
|
|
const char* description,
|
|
|
|
const char* pathname,
|
|
|
|
const char* commandLine,
|
|
|
|
bool allUsers)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-09-02 17:30:04 +00:00
|
|
|
// if not for all users then use the user's autostart registry.
|
|
|
|
// key. if windows 95 family then use windows 95 services key.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!allUsers || CArchMiscWindows::isWindows95Family()) {
|
2002-06-08 21:48:00 +00:00
|
|
|
// open registry
|
2003-01-04 22:01:32 +00:00
|
|
|
HKEY key = CArchMiscWindows::isWindows95Family() ?
|
|
|
|
open95ServicesKey() : openUserStartupKey();
|
2002-06-08 21:48:00 +00:00
|
|
|
if (key == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// can't open key
|
|
|
|
throw XArchDaemonInstallFailed(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// construct entry
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string value;
|
2002-06-08 21:48:00 +00:00
|
|
|
value += "\"";
|
|
|
|
value += pathname;
|
|
|
|
value += "\" ";
|
|
|
|
value += commandLine;
|
|
|
|
|
|
|
|
// install entry
|
|
|
|
setValue(key, name, value);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
closeKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows NT family services
|
|
|
|
else {
|
|
|
|
// open service manager
|
|
|
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
|
|
|
if (mgr == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// can't open service manager
|
|
|
|
throw XArchDaemonInstallFailed(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// create the service
|
2002-06-08 21:48:00 +00:00
|
|
|
SC_HANDLE service = CreateService(mgr,
|
|
|
|
name,
|
|
|
|
name,
|
|
|
|
0,
|
|
|
|
SERVICE_WIN32_OWN_PROCESS |
|
|
|
|
SERVICE_INTERACTIVE_PROCESS,
|
|
|
|
SERVICE_AUTO_START,
|
|
|
|
SERVICE_ERROR_NORMAL,
|
|
|
|
pathname,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2003-01-04 22:01:32 +00:00
|
|
|
if (service == NULL) {
|
|
|
|
// can't create service
|
|
|
|
// FIXME -- handle ERROR_SERVICE_EXISTS
|
|
|
|
DWORD err = GetLastError();
|
2002-06-08 21:48:00 +00:00
|
|
|
CloseServiceHandle(mgr);
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XArchDaemonInstallFailed(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-09-02 17:30:04 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// done with service and manager
|
|
|
|
CloseServiceHandle(service);
|
|
|
|
CloseServiceHandle(mgr);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// open the registry key for this service
|
|
|
|
HKEY key = openNTServicesKey();
|
|
|
|
key = openKey(key, name);
|
|
|
|
if (key == NULL) {
|
|
|
|
// can't open key
|
2003-01-04 22:01:32 +00:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
try {
|
|
|
|
uninstallDaemon(name, allUsers);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw XArchDaemonInstallFailed(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the description
|
2003-01-04 22:01:32 +00:00
|
|
|
setValue(key, _T("Description"), description);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// set command line
|
2003-01-04 22:01:32 +00:00
|
|
|
key = openKey(key, _T("Parameters"));
|
2002-06-08 21:48:00 +00:00
|
|
|
if (key == NULL) {
|
|
|
|
// can't open key
|
2003-01-04 22:01:32 +00:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
closeKey(key);
|
|
|
|
try {
|
|
|
|
uninstallDaemon(name, allUsers);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw XArchDaemonInstallFailed(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
setValue(key, _T("CommandLine"), commandLine);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// done with registry
|
|
|
|
closeKey(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
void
|
|
|
|
CArchDaemonWindows::uninstallDaemon(const char* name, bool allUsers)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
2002-09-02 17:30:04 +00:00
|
|
|
// if not for all users then use the user's autostart registry.
|
|
|
|
// key. if windows 95 family then use windows 95 services key.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!allUsers || CArchMiscWindows::isWindows95Family()) {
|
2002-06-08 21:48:00 +00:00
|
|
|
// open registry
|
2003-01-04 22:01:32 +00:00
|
|
|
HKEY key = CArchMiscWindows::isWindows95Family() ?
|
|
|
|
open95ServicesKey() : openUserStartupKey();
|
2002-06-08 21:48:00 +00:00
|
|
|
if (key == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// can't open key. daemon is probably not installed.
|
|
|
|
throw XArchDaemonUninstallNotInstalled(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove entry
|
|
|
|
deleteValue(key, name);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
closeKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows NT family services
|
|
|
|
else {
|
2003-01-04 22:01:32 +00:00
|
|
|
// remove parameters for this service. ignore failures.
|
2002-06-08 21:48:00 +00:00
|
|
|
HKEY key = openNTServicesKey();
|
|
|
|
key = openKey(key, name);
|
|
|
|
if (key != NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
deleteKey(key, _T("Parameters"));
|
2002-06-08 21:48:00 +00:00
|
|
|
closeKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// open service manager
|
|
|
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
|
|
|
if (mgr == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// can't open service manager
|
|
|
|
throw XArchDaemonUninstallFailed(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open the service. oddly, you must open a service to delete it.
|
|
|
|
SC_HANDLE service = OpenService(mgr, name, DELETE);
|
|
|
|
if (service == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
if (err != ERROR_SERVICE_DOES_NOT_EXIST) {
|
|
|
|
throw XArchDaemonUninstallFailed(new XArchEvalWindows(err));
|
2002-06-14 18:08:20 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XArchDaemonUninstallNotInstalled(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// delete the service
|
|
|
|
const bool okay = (DeleteService(service) == 0);
|
|
|
|
const DWORD err = GetLastError();
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
CloseServiceHandle(service);
|
2002-06-08 21:48:00 +00:00
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// handle failure. ignore error if service isn't installed anymore.
|
|
|
|
if (!okay && isDaemonInstalled(name, allUsers)) {
|
|
|
|
if (err != ERROR_SERVICE_MARKED_FOR_DELETE) {
|
|
|
|
throw XArchDaemonUninstallFailed(new XArchEvalWindows(err));
|
|
|
|
}
|
|
|
|
throw XArchDaemonUninstallNotInstalled(new XArchEvalWindows(err));
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::daemonize(const char* name, DaemonFunc func)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
2002-06-08 21:48:00 +00:00
|
|
|
assert(name != NULL);
|
|
|
|
assert(func != NULL);
|
|
|
|
|
|
|
|
// windows 95 family services
|
2003-01-04 22:01:32 +00:00
|
|
|
if (CArchMiscWindows::isWindows95Family()) {
|
2002-06-08 21:48:00 +00:00
|
|
|
typedef DWORD (WINAPI *RegisterServiceProcessT)(DWORD, DWORD);
|
|
|
|
|
|
|
|
// mark this process as a service so it's not killed when the
|
|
|
|
// user logs off.
|
|
|
|
HINSTANCE kernel = LoadLibrary("kernel32.dll");
|
|
|
|
if (kernel == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XArchDaemonFailed(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
RegisterServiceProcessT RegisterServiceProcess =
|
|
|
|
reinterpret_cast<RegisterServiceProcessT>(
|
|
|
|
GetProcAddress(kernel,
|
2003-01-04 22:01:32 +00:00
|
|
|
"RegisterServiceProcess"));
|
2002-06-08 21:48:00 +00:00
|
|
|
if (RegisterServiceProcess == NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// missing RegisterServiceProcess function
|
|
|
|
DWORD err = GetLastError();
|
2002-06-08 21:48:00 +00:00
|
|
|
FreeLibrary(kernel);
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XArchDaemonFailed(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
if (RegisterServiceProcess(NULL, 1) == 0) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// RegisterServiceProcess failed
|
|
|
|
DWORD err = GetLastError();
|
2002-06-08 21:48:00 +00:00
|
|
|
FreeLibrary(kernel);
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XArchDaemonFailed(new XArchEvalWindows(err));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
FreeLibrary(kernel);
|
|
|
|
|
|
|
|
// now simply call the daemon function
|
2003-01-04 22:01:32 +00:00
|
|
|
return func(1, &name);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// windows NT family services
|
|
|
|
else {
|
|
|
|
// save daemon function
|
|
|
|
m_daemonFunc = func;
|
|
|
|
|
|
|
|
// construct the service entry
|
|
|
|
SERVICE_TABLE_ENTRY entry[2];
|
|
|
|
entry[0].lpServiceName = const_cast<char*>(name);
|
2003-01-04 22:01:32 +00:00
|
|
|
entry[0].lpServiceProc = &CArchDaemonWindows::serviceMainEntry;
|
2002-06-08 21:48:00 +00:00
|
|
|
entry[1].lpServiceName = NULL;
|
|
|
|
entry[1].lpServiceProc = NULL;
|
|
|
|
|
|
|
|
// hook us up to the service control manager. this won't return
|
|
|
|
// (if successful) until the processes have terminated.
|
2003-01-04 22:01:32 +00:00
|
|
|
s_daemon = this;
|
2003-01-22 08:36:43 +00:00
|
|
|
if (StartServiceCtrlDispatcher(entry) == 0) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// StartServiceCtrlDispatcher failed
|
|
|
|
s_daemon = NULL;
|
|
|
|
throw XArchDaemonFailed(new XArchEvalWindows);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-04 12:26:23 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
s_daemon = NULL;
|
|
|
|
return m_daemonResult;
|
2002-06-14 18:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
bool
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::canInstallDaemon(const char* name, bool allUsers)
|
2002-09-02 17:30:04 +00:00
|
|
|
{
|
|
|
|
// if not for all users then use the user's autostart registry.
|
|
|
|
// key. if windows 95 family then use windows 95 services key.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!allUsers || CArchMiscWindows::isWindows95Family()) {
|
2002-09-02 17:30:04 +00:00
|
|
|
// check if we can open the registry key
|
2003-01-04 22:01:32 +00:00
|
|
|
HKEY key = CArchMiscWindows::isWindows95Family() ?
|
|
|
|
open95ServicesKey() : openUserStartupKey();
|
2002-09-02 17:30:04 +00:00
|
|
|
closeKey(key);
|
|
|
|
return (key != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows NT family services
|
|
|
|
else {
|
|
|
|
// check if we can open service manager for write
|
|
|
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
|
|
|
if (mgr == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
|
|
|
|
// check if we can open the registry key for this service
|
|
|
|
HKEY key = openNTServicesKey();
|
|
|
|
key = openKey(key, name);
|
2003-01-04 22:01:32 +00:00
|
|
|
key = openKey(key, _T("Parameters"));
|
2002-09-02 17:30:04 +00:00
|
|
|
closeKey(key);
|
|
|
|
|
|
|
|
return (key != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::isDaemonInstalled(const char* name, bool allUsers)
|
2002-09-02 17:30:04 +00:00
|
|
|
{
|
|
|
|
// if not for all users then use the user's autostart registry.
|
|
|
|
// key. if windows 95 family then use windows 95 services key.
|
2003-01-04 22:01:32 +00:00
|
|
|
if (!allUsers || CArchMiscWindows::isWindows95Family()) {
|
2002-09-02 17:30:04 +00:00
|
|
|
// check if we can open the registry key
|
2003-01-04 22:01:32 +00:00
|
|
|
HKEY key = CArchMiscWindows::isWindows95Family() ?
|
|
|
|
open95ServicesKey() : openUserStartupKey();
|
2002-09-02 17:30:04 +00:00
|
|
|
if (key == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for entry
|
|
|
|
const bool installed = !readValueString(key, name).empty();
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
closeKey(key);
|
|
|
|
|
|
|
|
return installed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows NT family services
|
|
|
|
else {
|
|
|
|
// check parameters for this service
|
|
|
|
HKEY key = openNTServicesKey();
|
|
|
|
key = openKey(key, name);
|
2003-01-04 22:01:32 +00:00
|
|
|
key = openKey(key, _T("Parameters"));
|
2002-09-02 17:30:04 +00:00
|
|
|
if (key != NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
const bool installed = !readValueString(key,
|
|
|
|
_T("CommandLine")).empty();
|
2002-09-02 17:30:04 +00:00
|
|
|
closeKey(key);
|
|
|
|
if (!installed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open service manager
|
|
|
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_READ);
|
|
|
|
if (mgr == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the service
|
|
|
|
SC_HANDLE service = OpenService(mgr, name, GENERIC_READ);
|
|
|
|
|
|
|
|
// clean up
|
2003-01-04 22:01:32 +00:00
|
|
|
if (service != NULL) {
|
|
|
|
CloseServiceHandle(service);
|
|
|
|
}
|
2002-09-02 17:30:04 +00:00
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
|
|
|
|
return (service != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
HKEY
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::openKey(HKEY key, const TCHAR* keyName)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-09-02 17:30:04 +00:00
|
|
|
// ignore if parent is NULL
|
|
|
|
if (key == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// open next key
|
|
|
|
HKEY newKey;
|
|
|
|
LONG result = RegOpenKeyEx(key, keyName, 0,
|
|
|
|
KEY_WRITE | KEY_QUERY_VALUE, &newKey);
|
|
|
|
if (result != ERROR_SUCCESS) {
|
|
|
|
DWORD disp;
|
|
|
|
result = RegCreateKeyEx(key, keyName, 0, _T(""),
|
|
|
|
0, KEY_WRITE | KEY_QUERY_VALUE,
|
|
|
|
NULL, &newKey, &disp);
|
|
|
|
}
|
|
|
|
if (result != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch to new key
|
|
|
|
RegCloseKey(key);
|
|
|
|
return newKey;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
HKEY
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::openKey(HKEY key, const TCHAR** keyNames)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
for (size_t i = 0; key != NULL && keyNames[i] != NULL; ++i) {
|
2002-06-08 21:48:00 +00:00
|
|
|
// open next key
|
|
|
|
key = openKey(key, keyNames[i]);
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::closeKey(HKEY key)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
assert(key != NULL);
|
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::deleteKey(HKEY key, const TCHAR* name)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
assert(key != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
RegDeleteKey(key, name);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::deleteValue(HKEY key, const TCHAR* name)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
assert(key != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
RegDeleteValue(key, name);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::setValue(HKEY key,
|
|
|
|
const TCHAR* name, const std::string& value)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
assert(key != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
RegSetValueEx(key, name, 0, REG_SZ,
|
|
|
|
reinterpret_cast<const BYTE*>(value.c_str()),
|
|
|
|
value.size() + 1);
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string
|
|
|
|
CArchDaemonWindows::readValueString(HKEY key, const TCHAR* name)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
// get the size of the string
|
|
|
|
DWORD type;
|
|
|
|
DWORD size = 0;
|
|
|
|
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, &size);
|
|
|
|
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
2003-01-04 22:01:32 +00:00
|
|
|
return std::string();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate space
|
|
|
|
char* buffer = new char[size];
|
|
|
|
|
|
|
|
// read it
|
|
|
|
result = RegQueryValueEx(key, name, 0, &type,
|
|
|
|
reinterpret_cast<BYTE*>(buffer), &size);
|
|
|
|
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
|
|
|
delete[] buffer;
|
2003-01-04 22:01:32 +00:00
|
|
|
return std::string();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean up and return value
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string value(buffer);
|
2002-06-08 21:48:00 +00:00
|
|
|
delete[] buffer;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
HKEY
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::openNTServicesKey()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
static const char* s_keyNames[] = {
|
|
|
|
_T("SYSTEM"),
|
|
|
|
_T("CurrentControlSet"),
|
|
|
|
_T("Services"),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
HKEY
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::open95ServicesKey()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
static const char* s_keyNames[] = {
|
|
|
|
_T("Software"),
|
|
|
|
_T("Microsoft"),
|
|
|
|
_T("Windows"),
|
|
|
|
_T("CurrentVersion"),
|
|
|
|
_T("RunServices"),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
HKEY
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::openUserStartupKey()
|
2002-09-02 17:30:04 +00:00
|
|
|
{
|
|
|
|
static const char* s_keyNames[] = {
|
|
|
|
_T("Software"),
|
|
|
|
_T("Microsoft"),
|
|
|
|
_T("Windows"),
|
|
|
|
_T("CurrentVersion"),
|
|
|
|
_T("Run"),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
return openKey(HKEY_CURRENT_USER, s_keyNames);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2003-01-22 08:36:43 +00:00
|
|
|
CArchDaemonWindows::doRunDaemon(RunFunc run)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
// should only be called from DaemonFunc
|
|
|
|
assert(m_serviceMutex != NULL);
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(run != NULL);
|
|
|
|
assert(stop != NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->lockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
try {
|
|
|
|
int result;
|
|
|
|
m_serviceHandlerWaiting = false;
|
|
|
|
m_serviceRunning = false;
|
|
|
|
for (;;) {
|
|
|
|
// mark server as running
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatus(SERVICE_RUNNING);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-07-24 19:26:18 +00:00
|
|
|
// run callback in another thread
|
2002-06-08 21:48:00 +00:00
|
|
|
m_serviceRunning = true;
|
2003-01-22 08:36:43 +00:00
|
|
|
m_daemonThread = ARCH->newThread(
|
2003-01-04 22:01:32 +00:00
|
|
|
&CArchDaemonWindows::runDaemonThreadEntry, run);
|
2003-01-22 08:36:43 +00:00
|
|
|
ARCH->wait(m_daemonThread, -1.0);
|
|
|
|
result = reinterpret_cast<int>(
|
|
|
|
ARCH->getResultOfThread(m_daemonThread));
|
2002-06-08 21:48:00 +00:00
|
|
|
m_serviceRunning = false;
|
|
|
|
|
|
|
|
// notify handler that the server stopped. if handler
|
|
|
|
// isn't waiting then we stopped unexpectedly and we
|
|
|
|
// quit.
|
|
|
|
if (m_serviceHandlerWaiting) {
|
|
|
|
m_serviceHandlerWaiting = false;
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->broadcastCondVar(m_serviceCondVar);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait until we're told what to do next
|
2003-01-04 22:01:32 +00:00
|
|
|
while (m_serviceState != SERVICE_RUNNING &&
|
|
|
|
m_serviceState != SERVICE_STOPPED) {
|
|
|
|
ARCH->waitCondVar(m_serviceCondVar, m_serviceMutex, -1.0);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit loop if we've been told to stop
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_serviceState == SERVICE_STOPPED) {
|
2002-06-08 21:48:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-01-22 08:36:43 +00:00
|
|
|
|
|
|
|
// done with callback thread
|
|
|
|
ARCH->closeThread(m_daemonThread);
|
|
|
|
m_daemonThread = NULL;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prevent daemonHandler from changing state
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_STOPPED;
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// tell service control that the service is stopped.
|
|
|
|
// FIXME -- hopefully this will ensure that our handler won't
|
|
|
|
// be called again but i can't find documentation that
|
|
|
|
// verifies that. if it does it'll crash on the mutex that
|
|
|
|
// we're about to destroy.
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatus(m_serviceState);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// clean up
|
2003-01-22 08:36:43 +00:00
|
|
|
if (m_daemonThread != NULL) {
|
|
|
|
ARCH->closeThread(m_daemonThread);
|
|
|
|
m_daemonThread = NULL;
|
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// FIXME -- report error
|
|
|
|
|
|
|
|
// prevent serviceHandler from changing state
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_STOPPED;
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// set status
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatusError(0);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// wake up serviceHandler if it's waiting then wait for it
|
|
|
|
if (m_serviceHandlerWaiting) {
|
|
|
|
m_serviceHandlerWaiting = false;
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->broadcastCondVar(m_serviceCondVar);
|
|
|
|
ARCH->waitCondVar(m_serviceCondVar, m_serviceMutex, -1.0);
|
2002-06-08 21:48:00 +00:00
|
|
|
// serviceHandler has exited by now
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-24 19:26:18 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::doDaemonRunning(bool running)
|
|
|
|
{
|
|
|
|
if (running) {
|
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ARCH->lockMutex(m_serviceMutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
CArchDaemonWindows::runDaemonThread(RunFunc run)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<void*>(run());
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
CArchDaemonWindows::runDaemonThreadEntry(void* vrun)
|
|
|
|
{
|
|
|
|
assert(s_daemon != NULL);
|
|
|
|
|
|
|
|
return s_daemon->runDaemonThread(reinterpret_cast<RunFunc>(vrun));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CArchDaemonWindows::setStatus(DWORD state)
|
|
|
|
{
|
|
|
|
setStatus(state, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CArchDaemonWindows::setStatus(DWORD state, DWORD step, DWORD waitHint)
|
|
|
|
{
|
|
|
|
assert(s_daemon != NULL);
|
|
|
|
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS |
|
|
|
|
SERVICE_INTERACTIVE_PROCESS;
|
|
|
|
status.dwCurrentState = state;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
|
|
|
SERVICE_ACCEPT_PAUSE_CONTINUE |
|
|
|
|
SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = step;
|
|
|
|
status.dwWaitHint = waitHint;
|
|
|
|
SetServiceStatus(s_daemon->m_statusHandle, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CArchDaemonWindows::setStatusError(DWORD error)
|
2002-07-24 19:26:18 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
SERVICE_STATUS status;
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS |
|
|
|
|
SERVICE_INTERACTIVE_PROCESS;
|
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
|
|
|
SERVICE_ACCEPT_PAUSE_CONTINUE |
|
|
|
|
SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
|
|
|
status.dwServiceSpecificExitCode = error;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
SetServiceStatus(s_daemon->m_statusHandle, &status);
|
2002-07-24 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::serviceMain(DWORD argc, LPTSTR* argvIn)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
typedef std::vector<LPCTSTR> ArgList;
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef std::vector<std::string> Arguments;
|
2002-06-08 21:48:00 +00:00
|
|
|
const char** argv = const_cast<const char**>(argvIn);
|
|
|
|
|
|
|
|
// create synchronization objects
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceMutex = ARCH->newMutex();
|
|
|
|
m_serviceCondVar = ARCH->newCondVar();
|
|
|
|
m_serviceState = SERVICE_RUNNING;
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// register our service handler functiom
|
|
|
|
m_statusHandle = RegisterServiceCtrlHandler(argv[0],
|
2003-01-04 22:01:32 +00:00
|
|
|
&CArchDaemonWindows::serviceHandlerEntry);
|
2002-06-08 21:48:00 +00:00
|
|
|
if (m_statusHandle == NULL) {
|
|
|
|
// cannot start as service
|
|
|
|
m_daemonResult = -1;
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->closeCondVar(m_serviceCondVar);
|
|
|
|
ARCH->closeMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell service control manager that we're starting
|
2003-01-22 08:36:43 +00:00
|
|
|
setStatus(SERVICE_START_PENDING, 0, 10000);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// if no arguments supplied then try getting them from the registry.
|
|
|
|
// the first argument doesn't count because it's the service name.
|
|
|
|
Arguments args;
|
|
|
|
ArgList myArgv;
|
|
|
|
if (argc <= 1) {
|
|
|
|
// read command line
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string commandLine;
|
2002-06-08 21:48:00 +00:00
|
|
|
HKEY key = openNTServicesKey();
|
2003-01-04 22:01:32 +00:00
|
|
|
key = openKey(key, argvIn[0]);
|
|
|
|
key = openKey(key, _T("Parameters"));
|
2002-06-08 21:48:00 +00:00
|
|
|
if (key != NULL) {
|
2003-01-04 22:01:32 +00:00
|
|
|
commandLine = readValueString(key, _T("CommandLine"));
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the command line isn't empty then parse and use it
|
|
|
|
if (!commandLine.empty()) {
|
|
|
|
// parse, honoring double quoted substrings
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string::size_type i = commandLine.find_first_not_of(" \t");
|
|
|
|
while (i != std::string::npos && i != commandLine.size()) {
|
2002-06-08 21:48:00 +00:00
|
|
|
// find end of string
|
2003-01-04 22:01:32 +00:00
|
|
|
std::string::size_type e;
|
2002-06-08 21:48:00 +00:00
|
|
|
if (commandLine[i] == '\"') {
|
|
|
|
// quoted. find closing quote.
|
|
|
|
++i;
|
|
|
|
e = commandLine.find("\"", i);
|
|
|
|
|
|
|
|
// whitespace must follow closing quote
|
2003-01-04 22:01:32 +00:00
|
|
|
if (e == std::string::npos ||
|
2002-06-08 21:48:00 +00:00
|
|
|
(e + 1 != commandLine.size() &&
|
|
|
|
commandLine[e + 1] != ' ' &&
|
|
|
|
commandLine[e + 1] != '\t')) {
|
|
|
|
args.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract
|
|
|
|
args.push_back(commandLine.substr(i, e - i));
|
|
|
|
i = e + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// unquoted. find next whitespace.
|
|
|
|
e = commandLine.find_first_of(" \t", i);
|
2003-01-04 22:01:32 +00:00
|
|
|
if (e == std::string::npos) {
|
2002-06-08 21:48:00 +00:00
|
|
|
e = commandLine.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract
|
|
|
|
args.push_back(commandLine.substr(i, e - i));
|
|
|
|
i = e + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// next argument
|
|
|
|
i = commandLine.find_first_not_of(" \t", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// service name goes first
|
|
|
|
myArgv.push_back(argv[0]);
|
|
|
|
|
|
|
|
// get pointers
|
2003-01-04 22:01:32 +00:00
|
|
|
for (size_t i = 0; i < args.size(); ++i) {
|
2002-06-08 21:48:00 +00:00
|
|
|
myArgv.push_back(args[i].c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust argc/argv
|
|
|
|
argc = myArgv.size();
|
|
|
|
argv = &myArgv[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// invoke daemon function
|
2003-01-04 22:01:32 +00:00
|
|
|
m_daemonResult = m_daemonFunc(static_cast<int>(argc), argv);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XArchDaemonRunFailed& e) {
|
|
|
|
setStatusError(e.m_result);
|
2002-06-08 21:48:00 +00:00
|
|
|
m_daemonResult = -1;
|
|
|
|
}
|
|
|
|
catch (...) {
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatusError(1);
|
2002-06-08 21:48:00 +00:00
|
|
|
m_daemonResult = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->closeCondVar(m_serviceCondVar);
|
|
|
|
ARCH->closeMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void WINAPI
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::serviceMainEntry(DWORD argc, LPTSTR* argv)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
s_daemon->serviceMain(argc, argv);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchDaemonWindows::serviceHandler(DWORD ctrl)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(m_serviceMutex != NULL);
|
|
|
|
assert(m_serviceCondVar != NULL);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->lockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// ignore request if service is already stopped
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_serviceState == SERVICE_STOPPED) {
|
|
|
|
setStatus(m_serviceState);
|
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ctrl) {
|
|
|
|
case SERVICE_CONTROL_PAUSE:
|
|
|
|
// update state
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_PAUSE_PENDING;
|
2003-01-22 08:36:43 +00:00
|
|
|
setStatus(m_serviceState, 0, 5000);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// stop run callback if running and wait for it to finish
|
|
|
|
if (m_serviceRunning) {
|
|
|
|
m_serviceHandlerWaiting = true;
|
2003-01-22 08:36:43 +00:00
|
|
|
ARCH->cancelThread(m_daemonThread);
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->waitCondVar(m_serviceCondVar, m_serviceMutex, -1.0);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update state if service hasn't stopped while we were waiting
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_serviceState != SERVICE_STOPPED) {
|
|
|
|
m_serviceState = SERVICE_PAUSED;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->broadcastCondVar(m_serviceCondVar);
|
2002-06-08 21:48:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_CONTINUE:
|
|
|
|
// required status update
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatus(m_serviceState);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// update state but let main loop send RUNNING notification
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_RUNNING;
|
|
|
|
ARCH->broadcastCondVar(m_serviceCondVar);
|
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
// update state
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_STOP_PENDING;
|
2003-01-22 08:36:43 +00:00
|
|
|
setStatus(m_serviceState, 0, 5000);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// stop run callback if running and wait for it to finish
|
|
|
|
if (m_serviceRunning) {
|
|
|
|
m_serviceHandlerWaiting = true;
|
2003-01-22 08:36:43 +00:00
|
|
|
ARCH->cancelThread(m_daemonThread);
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->waitCondVar(m_serviceCondVar, m_serviceMutex, -1.0);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update state
|
2003-01-04 22:01:32 +00:00
|
|
|
m_serviceState = SERVICE_STOPPED;
|
|
|
|
ARCH->broadcastCondVar(m_serviceCondVar);
|
2002-06-08 21:48:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-01-04 22:01:32 +00:00
|
|
|
// unknown service command
|
2002-06-08 21:48:00 +00:00
|
|
|
// fall through
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send update
|
2003-01-04 22:01:32 +00:00
|
|
|
setStatus(m_serviceState);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->unlockMutex(m_serviceMutex);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
void WINAPI
|
|
|
|
CArchDaemonWindows::serviceHandlerEntry(DWORD ctrl)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
s_daemon->serviceHandler(ctrl);
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|