ReactOS  0.4.11-dev-433-g473ca91
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 "windef.h"
22 #include "winsvc.h"
23 #include "qmgr.h"
24 #include "wine/debug.h"
25 
27 
29 
32 
33 static VOID
34 UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
35 {
37  status.dwCurrentState = dwCurrentState;
38  if (dwCurrentState == SERVICE_START_PENDING)
39  status.dwControlsAccepted = 0;
40  else
41  status.dwControlsAccepted
44  status.dwWin32ExitCode = 0;
45  status.dwServiceSpecificExitCode = 0;
46  status.dwCheckPoint = 0;
47  status.dwWaitHint = dwWaitHint;
48 
49  if (!SetServiceStatus(status_handle, &status)) {
50  ERR("failed to set service status\n");
52  }
53 }
54 
55 /* Handle incoming ControlService signals */
56 static DWORD WINAPI
57 ServiceHandler(DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context)
58 {
59  switch (ctrl) {
62  TRACE("shutting down service\n");
65  break;
66  default:
67  FIXME("ignoring handle service ctrl %x\n", ctrl);
69  break;
70  }
71 
72  return NO_ERROR;
73 }
74 
75 /* Main thread of the service */
76 static BOOL
78 {
79  HRESULT hr;
80  DWORD dwReg;
81 
82  TRACE("\n");
83 
85  if (FAILED(hr))
86  return FALSE;
87 
90  NULL);
91  if (FAILED(hr))
92  return FALSE;
93 
94  hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
96  CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwReg);
97  if (FAILED(hr))
98  return FALSE;
99 
100  return TRUE;
101 }
102 
103 /* Service entry point */
104 VOID WINAPI
105 ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
106 {
107  HANDLE fileTxThread;
108  static const WCHAR qmgr_nameW[] = {'B','I','T','S',0};
109  DWORD threadId;
110  TRACE("\n");
111 
113  if (!stop_event) {
114  ERR("failed to create stop_event\n");
115  return;
116  }
117 
119  if (!status_handle) {
120  ERR("failed to register handler: %u\n", GetLastError());
121  return;
122  }
123 
125  if (!StartCount()) {
126  ERR("failed starting service thread\n");
128  return;
129  }
130 
132  if (!globalMgr.jobEvent) {
133  ERR("Couldn't create event: error %d\n", GetLastError());
135  return;
136  }
137 
138  fileTxThread = CreateThread(NULL, 0, fileTransfer, NULL, 0, &threadId);
139  if (!fileTxThread)
140  {
141  ERR("Failed starting file transfer thread\n");
143  return;
144  }
145 
147 
148  WaitForSingleObject(fileTxThread, INFINITE);
151  TRACE("service stopped\n");
152 
153  CoUninitialize();
154 }
static SERVICE_STATUS_HANDLE status_handle
Definition: service.c:30
#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:26
VOID WINAPI ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
Definition: service.c:105
#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:1059
#define SERVICE_START_PENDING
Definition: winsvc.h:22
ClassFactoryImpl BITS_ClassFactory
Definition: factory.c:88
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
WINE_DEFAULT_DEBUG_CHANNEL(msiexec)
#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:4034
#define NO_ERROR
Definition: dderror.h:5
DWORD DWORD
Definition: winlogon.h:84
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:957
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:416
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define ctrl
Definition: input.c:1669
#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:28
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwServiceType
Definition: winsvc.h:99
static VOID UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
Definition: service.c:34
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:773
#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:2809
DWORD *typedef HANDLE
Definition: winlogon.h:61
void WINAPI DECLSPEC_HOTPATCH CoUninitialize(void)
Definition: compobj.c:1994
DWORD WINAPI fileTransfer(void *param)
Definition: qmgr.c:148
HRESULT WINAPI DECLSPEC_HOTPATCH CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
Definition: compobj.c:1938
#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:57
#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:31
#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:77
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40