ReactOS  r76032
service.c
Go to the documentation of this file.
1 /*
2  * ServiceMain function for qmgr running within svchost
3  *
4  * Copyright 2007 (C) Google (Roy Shea)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "qmgr.h"
22 
24 
27 
28 static VOID
29 UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
30 {
32  status.dwCurrentState = dwCurrentState;
33  if (dwCurrentState == SERVICE_START_PENDING)
34  status.dwControlsAccepted = 0;
35  else
36  status.dwControlsAccepted
39  status.dwWin32ExitCode = 0;
40  status.dwServiceSpecificExitCode = 0;
41  status.dwCheckPoint = 0;
42  status.dwWaitHint = dwWaitHint;
43 
44  if (!SetServiceStatus(status_handle, &status)) {
45  ERR("failed to set service status\n");
47  }
48 }
49 
50 /* Handle incoming ControlService signals */
51 static DWORD WINAPI
52 ServiceHandler(DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context)
53 {
54  switch (ctrl) {
57  TRACE("shutting down service\n");
60  break;
61  default:
62  FIXME("ignoring handle service ctrl %x\n", ctrl);
64  break;
65  }
66 
67  return NO_ERROR;
68 }
69 
70 /* Main thread of the service */
71 static BOOL
73 {
74  HRESULT hr;
75  DWORD dwReg;
76 
77  TRACE("\n");
78 
80  if (FAILED(hr))
81  return FALSE;
82 
85  NULL);
86  if (FAILED(hr))
87  return FALSE;
88 
89  hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
91  CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwReg);
92  if (FAILED(hr))
93  return FALSE;
94 
95  return TRUE;
96 }
97 
98 /* Service entry point */
100 ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
101 {
102  HANDLE fileTxThread;
103  static const WCHAR qmgr_nameW[] = {'B','I','T','S',0};
104  DWORD threadId;
105  TRACE("\n");
106 
108  if (!stop_event) {
109  ERR("failed to create stop_event\n");
110  return;
111  }
112 
114  if (!status_handle) {
115  ERR("failed to register handler: %u\n", GetLastError());
116  return;
117  }
118 
120  if (!StartCount()) {
121  ERR("failed starting service thread\n");
123  return;
124  }
125 
127  if (!globalMgr.jobEvent) {
128  ERR("Couldn't create event: error %d\n", GetLastError());
130  return;
131  }
132 
133  fileTxThread = CreateThread(NULL, 0, fileTransfer, NULL, 0, &threadId);
134  if (!fileTxThread)
135  {
136  ERR("Failed starting file transfer thread\n");
138  return;
139  }
140 
142 
143  WaitForSingleObject(fileTxThread, INFINITE);
146  TRACE("service stopped\n");
147 
148  CoUninitialize();
149 }
static SERVICE_STATUS_HANDLE status_handle
Definition: service.c:25
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
DWORD dwCurrentState
Definition: winsvc.h:100
Definition: http.c:6587
__wchar_t WCHAR
Definition: xmlstorage.h:180
BackgroundCopyManagerImpl globalMgr
Definition: qmgr.c:23
VOID WINAPI ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
Definition: service.c:100
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1056
#define SERVICE_START_PENDING
Definition: winsvc.h:22
ClassFactoryImpl BITS_ClassFactory
Definition: factory.c:85
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
#define RPC_C_IMP_LEVEL_IMPERSONATE
Definition: rpcdce.h:176
HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc, SOLE_AUTHENTICATION_SERVICE *asAuthSvc, void *pReserved1, DWORD dwAuthnLevel, DWORD dwImpLevel, void *pReserved2, DWORD dwCapabilities, void *pReserved3)
Definition: compobj.c:3980
#define NO_ERROR
Definition: dderror.h:5
DWORD DWORD
Definition: winlogon.h:75
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:597
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:885
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
DWORD dwCheckPoint
Definition: winsvc.h:104
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define FALSE
Definition: types.h:117
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define FIXME(fmt,...)
Definition: debug.h:110
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:135
smooth NULL
Definition: ftsmooth.c:557
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define ctrl
Definition: input.c:1595
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define TRACE(s)
Definition: solgame.cpp:4
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG HRESULT
Definition: typedefs.h:77
DWORD dwWaitHint
Definition: winsvc.h:105
HANDLE stop_event
Definition: service.c:23
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwServiceType
Definition: winsvc.h:99
static VOID UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
Definition: service.c:29
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:724
#define WINAPI
Definition: msvc.h:20
IClassFactory IClassFactory_iface
#define ERR(fmt,...)
Definition: debug.h:109
DWORD dwControlsAccepted
Definition: winsvc.h:101
HRESULT WINAPI CoRegisterClassObject(REFCLSID rclsid, LPUNKNOWN pUnk, DWORD dwClsContext, DWORD flags, LPDWORD lpdwRegister)
Definition: compobj.c:2760
DWORD *typedef HANDLE
Definition: winlogon.h:52
void WINAPI DECLSPEC_HOTPATCH CoUninitialize(void)
Definition: compobj.c:1944
DWORD WINAPI fileTransfer(void *param)
Definition: qmgr.c:145
HRESULT WINAPI DECLSPEC_HOTPATCH CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
Definition: compobj.c:1870
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
static DWORD WINAPI ServiceHandler(DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context)
Definition: service.c:52
#define INFINITE
Definition: serial.h:102
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
WCHAR * LPWSTR
Definition: xmlstorage.h:184
static SERVICE_STATUS status
Definition: service.c:26
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define RPC_C_AUTHN_LEVEL_NONE
Definition: rpcdce.h:146
static BOOL StartCount(void)
Definition: service.c:72
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40