[SHELL32] CDrivesFolder: Implement the eject and disconnect menu items. CORE-13841
[reactos.git] / dll / win32 / qmgr / service.c
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
23 HANDLE stop_event = NULL;
24
25 static SERVICE_STATUS_HANDLE status_handle;
26 static SERVICE_STATUS status;
27
28 static VOID
29 UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
30 {
31 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
32 status.dwCurrentState = dwCurrentState;
33 if (dwCurrentState == SERVICE_START_PENDING)
34 status.dwControlsAccepted = 0;
35 else
36 status.dwControlsAccepted
37 = (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE
38 | SERVICE_ACCEPT_SHUTDOWN);
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");
46 SetEvent(stop_event);
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) {
55 case SERVICE_CONTROL_STOP:
56 case SERVICE_CONTROL_SHUTDOWN:
57 TRACE("shutting down service\n");
58 UpdateStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
59 SetEvent(stop_event);
60 break;
61 default:
62 FIXME("ignoring handle service ctrl %x\n", ctrl);
63 UpdateStatus(status.dwCurrentState, NO_ERROR, 0);
64 break;
65 }
66
67 return NO_ERROR;
68 }
69
70 /* Main thread of the service */
71 static BOOL
72 StartCount(void)
73 {
74 HRESULT hr;
75 DWORD dwReg;
76
77 TRACE("\n");
78
79 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
80 if (FAILED(hr))
81 return FALSE;
82
83 hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
84 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE,
85 NULL);
86 if (FAILED(hr))
87 return FALSE;
88
89 hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
90 (IUnknown *) &BITS_ClassFactory.IClassFactory_iface,
91 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwReg);
92 if (FAILED(hr))
93 return FALSE;
94
95 return TRUE;
96 }
97
98 /* Service entry point */
99 VOID WINAPI
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
107 stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
108 if (!stop_event) {
109 ERR("failed to create stop_event\n");
110 return;
111 }
112
113 status_handle = RegisterServiceCtrlHandlerExW(qmgr_nameW, ServiceHandler, NULL);
114 if (!status_handle) {
115 ERR("failed to register handler: %u\n", GetLastError());
116 return;
117 }
118
119 UpdateStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
120 if (!StartCount()) {
121 ERR("failed starting service thread\n");
122 UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
123 return;
124 }
125
126 globalMgr.jobEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
127 if (!globalMgr.jobEvent) {
128 ERR("Couldn't create event: error %d\n", GetLastError());
129 UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
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");
137 UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
138 return;
139 }
140
141 UpdateStatus(SERVICE_RUNNING, NO_ERROR, 0);
142
143 WaitForSingleObject(fileTxThread, INFINITE);
144 UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
145 CloseHandle(stop_event);
146 TRACE("service stopped\n");
147
148 CoUninitialize();
149 }