VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxService-win.cpp@ 25390

Last change on this file since 25390 was 25166, checked in by vboxsync, 15 years ago

VBoxService: Removed default parameter, cosmetics.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1/* $Id: VBoxService-win.cpp 25166 2009-12-03 13:35:13Z vboxsync $ */
2/** @file
3 * VBoxService - Guest Additions Service Skeleton, Windows Specific Parts.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/assert.h>
27#include <iprt/err.h>
28#include <VBox/VBoxGuestLib.h>
29#include "VBoxServiceInternal.h"
30
31#include <Windows.h>
32#include <process.h>
33#include <aclapi.h>
34
35DWORD g_rcWinService = 0;
36SERVICE_STATUS_HANDLE g_hWinServiceStatus = NULL;
37
38void WINAPI VBoxServiceWinMain (DWORD argc, LPTSTR *argv);
39
40static SERVICE_TABLE_ENTRY const g_aServiceTable[]=
41{
42 {VBOXSERVICE_NAME, VBoxServiceWinMain},
43 {NULL,NULL}
44};
45
46
47/**
48 * @todo Format code style.
49 * @todo Add full unicode support.
50 * @todo Add event log capabilities / check return values.
51 */
52DWORD VBoxServiceWinAddAceToObjectsSecurityDescriptor(LPTSTR pszObjName,
53 SE_OBJECT_TYPE ObjectType,
54 LPTSTR pszTrustee,
55 TRUSTEE_FORM TrusteeForm,
56 DWORD dwAccessRights,
57 ACCESS_MODE AccessMode,
58 DWORD dwInheritance)
59{
60 DWORD dwRes = 0;
61 PACL pOldDACL = NULL, pNewDACL = NULL;
62 PSECURITY_DESCRIPTOR pSD = NULL;
63 EXPLICIT_ACCESS ea;
64
65 if (NULL == pszObjName)
66 return ERROR_INVALID_PARAMETER;
67
68 /* Get a pointer to the existing DACL. */
69 dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
70 DACL_SECURITY_INFORMATION,
71 NULL, NULL, &pOldDACL, NULL, &pSD);
72 if (ERROR_SUCCESS != dwRes)
73 {
74 if (dwRes == ERROR_FILE_NOT_FOUND)
75 VBoxServiceError("AddAceToObjectsSecurityDescriptor: Object not found/installed: %s\n", pszObjName);
76 else
77 VBoxServiceError("AddAceToObjectsSecurityDescriptor: GetNamedSecurityInfo: Error %u\n", dwRes);
78 goto Cleanup;
79 }
80
81 /* Initialize an EXPLICIT_ACCESS structure for the new ACE. */
82 ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
83 ea.grfAccessPermissions = dwAccessRights;
84 ea.grfAccessMode = AccessMode;
85 ea.grfInheritance= dwInheritance;
86 ea.Trustee.TrusteeForm = TrusteeForm;
87 ea.Trustee.ptstrName = pszTrustee;
88
89 /* Create a new ACL that merges the new ACE into the existing DACL. */
90 dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
91 if (ERROR_SUCCESS != dwRes)
92 {
93 VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetEntriesInAcl: Error %u\n", dwRes);
94 goto Cleanup;
95 }
96
97 /* Attach the new ACL as the object's DACL. */
98 dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
99 DACL_SECURITY_INFORMATION,
100 NULL, NULL, pNewDACL, NULL);
101 if (ERROR_SUCCESS != dwRes)
102 {
103 VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetNamedSecurityInfo: Error %u\n", dwRes);
104 goto Cleanup;
105 }
106
107 /** @todo get rid of that spaghetti jump ... */
108Cleanup:
109
110 if(pSD != NULL)
111 LocalFree((HLOCAL) pSD);
112 if(pNewDACL != NULL)
113 LocalFree((HLOCAL) pNewDACL);
114
115 return dwRes;
116}
117
118
119BOOL VBoxServiceWinSetStatus(DWORD dwStatus, DWORD dwCheckPoint)
120{
121 if (NULL == g_hWinServiceStatus) /* Program could be in testing mode, so no service environment available. */
122 return FALSE;
123
124 VBoxServiceVerbose(2, "Setting service status to: %ld\n", dwStatus);
125 g_rcWinService = dwStatus;
126
127 SERVICE_STATUS ss;
128 ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
129 ss.dwCurrentState = g_rcWinService;
130 ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
131 ss.dwWin32ExitCode = NO_ERROR;
132 ss.dwServiceSpecificExitCode = 0; /* Not used */
133 ss.dwCheckPoint = dwCheckPoint;
134 ss.dwWaitHint = 3000;
135
136 return SetServiceStatus(g_hWinServiceStatus, &ss);
137}
138
139
140int VBoxServiceWinInstall(void)
141{
142 SC_HANDLE hService, hSCManager;
143 TCHAR imagePath[MAX_PATH] = { 0 };
144
145 GetModuleFileName(NULL,imagePath,MAX_PATH);
146 VBoxServiceVerbose(1, "Installing service ...\n");
147
148 hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
149
150 if (NULL == hSCManager) {
151 VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
152 return 1;
153 }
154
155 hService = ::CreateService (hSCManager,
156 VBOXSERVICE_NAME, VBOXSERVICE_FRIENDLY_NAME,
157 SERVICE_ALL_ACCESS,
158 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
159 SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,
160 imagePath, NULL, NULL, NULL, NULL, NULL);
161
162 if (NULL == hService) {
163 VBoxServiceError("Could not create service! Error: %d\n", GetLastError());
164 CloseServiceHandle(hSCManager);
165 return 1;
166 }
167 else
168 {
169 VBoxServiceVerbose(0, "Service successfully installed!\n");
170 }
171
172 CloseServiceHandle(hService);
173 CloseServiceHandle(hSCManager);
174
175 return 0;
176}
177
178int VBoxServiceWinUninstall(void)
179{
180 SC_HANDLE hService, hSCManager;
181 hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
182
183 VBoxServiceVerbose(1, "Uninstalling service ...\n");
184
185 if (NULL == hSCManager) {
186 VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
187 return 1;
188 }
189
190 hService = OpenService (hSCManager, VBOXSERVICE_NAME, SERVICE_ALL_ACCESS );
191 if (NULL == hService) {
192 VBoxServiceError("Could not open service! Error: %d\n", GetLastError());
193 CloseServiceHandle (hSCManager);
194 return 1;
195 }
196
197 if (FALSE == DeleteService (hService))
198 {
199 VBoxServiceError("Could not remove service! Error: %d\n", GetLastError());
200 CloseServiceHandle (hService);
201 CloseServiceHandle (hSCManager);
202 return 1;
203 }
204 else
205 {
206 HKEY hKey = NULL;
207 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
208 RegDeleteKey(hKey, VBOXSERVICE_NAME);
209 RegCloseKey(hKey);
210 }
211
212 VBoxServiceVerbose(0, "Service successfully uninstalled!\n");
213 }
214
215 CloseServiceHandle(hService);
216 CloseServiceHandle(hSCManager);
217
218 return 0;
219}
220
221
222int VBoxServiceWinStart(void)
223{
224 int rc = VINF_SUCCESS;
225
226#ifndef TARGET_NT4
227 /* Create a well-known SID for the "Builtin Users" group. */
228 PSID pBuiltinUsersSID = NULL;
229 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_LOCAL_SID_AUTHORITY;
230
231 if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
232 SECURITY_LOCAL_RID,
233 0, 0, 0, 0, 0, 0, 0,
234 &pBuiltinUsersSID))
235 {
236 VBoxServiceError("AllocateAndInitializeSid: Error %u\n", GetLastError());
237 }
238 else
239 {
240 DWORD dwRes = VBoxServiceWinAddAceToObjectsSecurityDescriptor (TEXT("\\\\.\\VBoxMiniRdrDN"),
241 SE_FILE_OBJECT,
242 (LPTSTR)pBuiltinUsersSID,
243 TRUSTEE_IS_SID,
244 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
245 SET_ACCESS,
246 NO_INHERITANCE);
247 if (dwRes != ERROR_SUCCESS)
248 {
249 if (dwRes == ERROR_FILE_NOT_FOUND)
250 {
251 /* If we don't find our "VBoxMiniRdrDN" (for Shared Folders) object above,
252 don't report an error; it just might be not installed. Otherwise this
253 would cause the SCM to hang on starting up the service. */
254 rc = VINF_SUCCESS;
255 }
256 else rc = RTErrConvertFromWin32(dwRes);
257 }
258 }
259#endif
260
261 if (RT_SUCCESS(rc))
262 {
263 /* Notify SCM *before* we're starting the services, because the last services
264 always starts in main thread (which causes the SCM to wait because of the non-responding
265 service). */
266 VBoxServiceWinSetStatus (SERVICE_RUNNING, 0);
267
268 /*
269 * Check that at least one service is enabled.
270 */
271 unsigned iMain = VBoxServiceGetStartedServices();
272 rc = VBoxServiceStartServices(iMain); /* Start all the services. */
273
274 if (RT_FAILURE(rc))
275 VBoxServiceWinSetStatus (SERVICE_STOPPED, 0);
276 }
277
278 if (RT_FAILURE(rc))
279 VBoxServiceError("Service failed to start with rc=%Rrc!\n", rc);
280
281 return rc;
282}
283
284
285#ifdef TARGET_NT4
286VOID WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl)
287#else
288DWORD WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl,
289 DWORD dwEventType,
290 LPVOID lpEventData,
291 LPVOID lpContext)
292#endif
293{
294 DWORD rc = NO_ERROR;
295
296 VBoxServiceVerbose(2, "Control handler: Control=%ld\n", dwControl);
297#ifndef TARGET_NT4
298 VBoxServiceVerbose(2, "Control handler: EventType=%ld\n", dwEventType);
299#endif
300
301 switch (dwControl)
302 {
303
304 case SERVICE_CONTROL_INTERROGATE:
305 VBoxServiceWinSetStatus(g_rcWinService, 0);
306 break;
307
308 case SERVICE_CONTROL_STOP:
309 case SERVICE_CONTROL_SHUTDOWN:
310 {
311 VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, 0);
312
313 rc = VBoxServiceStopServices();
314
315 VBoxServiceWinSetStatus(SERVICE_STOPPED, 0);
316 }
317 break;
318
319 case SERVICE_CONTROL_SESSIONCHANGE: /* Only Win XP and up. */
320
321#ifndef TARGET_NT4
322 switch (dwEventType)
323 {
324 /*case WTS_SESSION_LOGON:
325 VBoxServiceVerbose(2, "A user has logged on to the session.\n");
326 break;
327
328 case WTS_SESSION_LOGOFF:
329 VBoxServiceVerbose(2, "A user has logged off from the session.\n");
330 break;*/
331 default:
332 break;
333 }
334#endif /* TARGET_NT4 */
335 break;
336
337 default:
338
339 VBoxServiceVerbose(1, "Service control function not implemented: %ld\n", dwControl);
340 rc = ERROR_CALL_NOT_IMPLEMENTED;
341 break;
342 }
343
344#ifndef TARGET_NT4
345 return rc;
346#endif
347}
348
349
350void WINAPI VBoxServiceWinMain(DWORD argc, LPTSTR *argv)
351{
352 int rc = VINF_SUCCESS;
353
354 VBoxServiceVerbose(2, "Registering service control handler ...\n");
355#ifdef TARGET_NT4
356 g_hWinServiceStatus = RegisterServiceCtrlHandler (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler);
357#else
358 g_hWinServiceStatus = RegisterServiceCtrlHandlerEx (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler, NULL);
359#endif
360
361 if (NULL == g_hWinServiceStatus)
362 {
363 DWORD dwErr = GetLastError();
364
365 switch (dwErr)
366 {
367 case ERROR_INVALID_NAME:
368 VBoxServiceError("Invalid service name!\n");
369 break;
370 case ERROR_SERVICE_DOES_NOT_EXIST:
371 VBoxServiceError("Service does not exist!\n");
372 break;
373 default:
374 VBoxServiceError("Could not register service control handle! Error: %ld\n", dwErr);
375 break;
376 }
377 }
378 else
379 {
380 VBoxServiceVerbose(2, "Service control handler registered.\n");
381
382 rc = VBoxServiceWinStart();
383 }
384}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette