2008-02-11 20:09:34 +01:00
|
|
|
/*
|
|
|
|
* ServiceMain function for qmgr running within svchost
|
|
|
|
*
|
|
|
|
* Copyright 2007 (C) Google (Roy Shea)
|
|
|
|
*
|
|
|
|
* This library 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.
|
|
|
|
*
|
|
|
|
* This library 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winsvc.h"
|
|
|
|
#include "qmgr.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
|
|
|
|
|
2008-03-13 23:54:01 +01:00
|
|
|
HANDLE stop_event = NULL;
|
|
|
|
|
2008-02-11 20:09:34 +01:00
|
|
|
static SERVICE_STATUS_HANDLE status_handle;
|
|
|
|
static SERVICE_STATUS status;
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
|
|
|
|
{
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
status.dwCurrentState = dwCurrentState;
|
|
|
|
if (dwCurrentState == SERVICE_START_PENDING)
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
else
|
|
|
|
status.dwControlsAccepted
|
|
|
|
= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE
|
|
|
|
| SERVICE_ACCEPT_SHUTDOWN);
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = dwWaitHint;
|
|
|
|
|
|
|
|
if (!SetServiceStatus(status_handle, &status)) {
|
|
|
|
ERR("failed to set service status\n");
|
|
|
|
SetEvent(stop_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle incoming ControlService signals */
|
|
|
|
static DWORD WINAPI
|
|
|
|
ServiceHandler(DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context)
|
|
|
|
{
|
|
|
|
switch (ctrl) {
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
TRACE("shutting down service\n");
|
|
|
|
UpdateStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
|
|
|
SetEvent(stop_event);
|
|
|
|
break;
|
|
|
|
default:
|
2022-02-17 07:11:19 +01:00
|
|
|
FIXME("ignoring handle service ctrl %lx\n", ctrl);
|
2008-02-11 20:09:34 +01:00
|
|
|
UpdateStatus(status.dwCurrentState, NO_ERROR, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main thread of the service */
|
|
|
|
static BOOL
|
|
|
|
StartCount(void)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
DWORD dwReg;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
2008-10-08 01:33:25 +02:00
|
|
|
if (FAILED(hr))
|
2008-02-11 20:09:34 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
|
|
|
|
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE,
|
|
|
|
NULL);
|
2008-10-08 01:33:25 +02:00
|
|
|
if (FAILED(hr))
|
2008-02-11 20:09:34 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
|
2011-07-20 15:43:49 +02:00
|
|
|
(IUnknown *) &BITS_ClassFactory.IClassFactory_iface,
|
|
|
|
CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwReg);
|
2008-10-08 01:33:25 +02:00
|
|
|
if (FAILED(hr))
|
2008-02-11 20:09:34 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Service entry point */
|
|
|
|
VOID WINAPI
|
|
|
|
ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
|
|
|
|
{
|
2008-03-13 23:54:01 +01:00
|
|
|
HANDLE fileTxThread;
|
|
|
|
DWORD threadId;
|
2008-02-11 20:09:34 +01:00
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!stop_event) {
|
|
|
|
ERR("failed to create stop_event\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:05:23 +02:00
|
|
|
status_handle = RegisterServiceCtrlHandlerExW(L"BITS", ServiceHandler, NULL);
|
2008-02-11 20:09:34 +01:00
|
|
|
if (!status_handle) {
|
2022-02-17 07:11:19 +01:00
|
|
|
ERR("failed to register handler: %lu\n", GetLastError());
|
2008-02-11 20:09:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
|
|
|
|
if (!StartCount()) {
|
|
|
|
ERR("failed starting service thread\n");
|
|
|
|
UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-13 23:54:01 +01:00
|
|
|
globalMgr.jobEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!globalMgr.jobEvent) {
|
2022-02-17 07:11:19 +01:00
|
|
|
ERR("Couldn't create event: error %ld\n", GetLastError());
|
2008-03-13 23:54:01 +01:00
|
|
|
UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileTxThread = CreateThread(NULL, 0, fileTransfer, NULL, 0, &threadId);
|
|
|
|
if (!fileTxThread)
|
|
|
|
{
|
|
|
|
ERR("Failed starting file transfer thread\n");
|
|
|
|
UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-11 20:09:34 +01:00
|
|
|
UpdateStatus(SERVICE_RUNNING, NO_ERROR, 0);
|
|
|
|
|
2008-03-13 23:54:01 +01:00
|
|
|
WaitForSingleObject(fileTxThread, INFINITE);
|
2008-02-11 20:09:34 +01:00
|
|
|
UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
|
|
|
CloseHandle(stop_event);
|
2016-05-15 15:32:42 +02:00
|
|
|
TRACE("service stopped\n");
|
2009-03-07 22:56:27 +01:00
|
|
|
|
|
|
|
CoUninitialize();
|
2008-02-11 20:09:34 +01:00
|
|
|
}
|