VirtualBox

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

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

VBoxService: Not needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1/* $Id: VBoxService-win.cpp 25796 2010-01-13 08:54:27Z 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 {
152 VBoxServiceError("Could not open SCM! Error: %ld\n", GetLastError());
153 return 1;
154 }
155
156 hService = ::CreateService (hSCManager,
157 VBOXSERVICE_NAME, VBOXSERVICE_FRIENDLY_NAME,
158 SERVICE_ALL_ACCESS,
159 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
160 SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
161 imagePath, NULL, NULL, NULL, NULL, NULL);
162 int rc = VINF_SUCCESS;
163 if (NULL == hService)
164 {
165 DWORD dwErr = GetLastError();
166 switch (dwErr)
167 {
168
169 case ERROR_SERVICE_EXISTS:
170
171 VBoxServiceVerbose(1, "Service already exists, just updating the service config.\n");
172 hService = OpenService (hSCManager,
173 VBOXSERVICE_NAME,
174 SERVICE_ALL_ACCESS);
175 if (NULL == hService)
176 {
177 VBoxServiceError("Could not open service! Error: %ld\n", GetLastError());
178 rc = 1;
179 }
180 else
181 {
182 if (ChangeServiceConfig (hService,
183 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
184 SERVICE_DEMAND_START,
185 SERVICE_ERROR_NORMAL,
186 imagePath,
187 NULL,
188 NULL,
189 NULL,
190 NULL,
191 NULL,
192 VBOXSERVICE_FRIENDLY_NAME))
193 {
194 /* On W2K+ there's ChangeServiceConfig2() which lets us set some fields
195 like a longer service description. */
196 #ifndef TARGET_NT4
197 SERVICE_DESCRIPTION desc;
198 /** @todo On Vista+ SERVICE_DESCRIPTION also supports localized strings! */
199 desc. lpDescription = VBOXSERVICE_DESCRIPTION;
200 if (FALSE == ChangeServiceConfig2(hService,
201 SERVICE_CONFIG_DESCRIPTION, /* Service info level */
202 &desc))
203 {
204 VBoxServiceError("Cannot set the service description! Error: %ld\n", GetLastError());
205 }
206 #endif
207
208 VBoxServiceVerbose(1, "The service config has been successfully updated.\n");
209 }
210 else
211 {
212 VBoxServiceError("Could not change service config! Error: %ld\n", GetLastError());
213 rc = 1;
214 }
215 }
216 break;
217
218 default:
219
220 VBoxServiceError("Could not create service! Error: %ld\n", dwErr);
221 rc = 1;
222 break;
223 }
224 }
225 else
226 {
227 VBoxServiceVerbose(0, "Service successfully installed!\n");
228 }
229
230 CloseServiceHandle(hService);
231 CloseServiceHandle(hSCManager);
232 return rc;
233}
234
235int VBoxServiceWinUninstall(void)
236{
237 SC_HANDLE hService, hSCManager;
238 hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
239
240 VBoxServiceVerbose(1, "Uninstalling service ...\n");
241
242 if (NULL == hSCManager) {
243 VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
244 return 1;
245 }
246
247 hService = OpenService (hSCManager, VBOXSERVICE_NAME, SERVICE_ALL_ACCESS );
248 if (NULL == hService) {
249 VBoxServiceError("Could not open service! Error: %d\n", GetLastError());
250 CloseServiceHandle (hSCManager);
251 return 1;
252 }
253
254 if (FALSE == DeleteService (hService))
255 {
256 VBoxServiceError("Could not remove service! Error: %d\n", GetLastError());
257 CloseServiceHandle (hService);
258 CloseServiceHandle (hSCManager);
259 return 1;
260 }
261 else
262 {
263 HKEY hKey = NULL;
264 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
265 RegDeleteKey(hKey, VBOXSERVICE_NAME);
266 RegCloseKey(hKey);
267 }
268
269 VBoxServiceVerbose(0, "Service successfully uninstalled!\n");
270 }
271
272 CloseServiceHandle(hService);
273 CloseServiceHandle(hSCManager);
274
275 return 0;
276}
277
278
279int VBoxServiceWinStart(void)
280{
281 int rc = VINF_SUCCESS;
282
283#ifndef TARGET_NT4
284 /* Create a well-known SID for the "Builtin Users" group. */
285 PSID pBuiltinUsersSID = NULL;
286 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_LOCAL_SID_AUTHORITY;
287
288 if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
289 SECURITY_LOCAL_RID,
290 0, 0, 0, 0, 0, 0, 0,
291 &pBuiltinUsersSID))
292 {
293 VBoxServiceError("AllocateAndInitializeSid: Error %u\n", GetLastError());
294 }
295 else
296 {
297 DWORD dwRes = VBoxServiceWinAddAceToObjectsSecurityDescriptor (TEXT("\\\\.\\VBoxMiniRdrDN"),
298 SE_FILE_OBJECT,
299 (LPTSTR)pBuiltinUsersSID,
300 TRUSTEE_IS_SID,
301 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
302 SET_ACCESS,
303 NO_INHERITANCE);
304 if (dwRes != ERROR_SUCCESS)
305 {
306 if (dwRes == ERROR_FILE_NOT_FOUND)
307 {
308 /* If we don't find our "VBoxMiniRdrDN" (for Shared Folders) object above,
309 don't report an error; it just might be not installed. Otherwise this
310 would cause the SCM to hang on starting up the service. */
311 rc = VINF_SUCCESS;
312 }
313 else rc = RTErrConvertFromWin32(dwRes);
314 }
315 }
316#endif
317
318 if (RT_SUCCESS(rc))
319 {
320 /* Notify SCM *before* we're starting the services, because the last services
321 always starts in main thread (which causes the SCM to wait because of the non-responding
322 service). */
323 VBoxServiceWinSetStatus (SERVICE_RUNNING, 0);
324
325 /*
326 * Check that at least one service is enabled.
327 */
328 unsigned iMain = VBoxServiceGetStartedServices();
329 rc = VBoxServiceStartServices(iMain); /* Start all the services. */
330
331 if (RT_FAILURE(rc))
332 VBoxServiceWinSetStatus (SERVICE_STOPPED, 0);
333 }
334
335 if (RT_FAILURE(rc))
336 VBoxServiceError("Service failed to start with rc=%Rrc!\n", rc);
337
338 return rc;
339}
340
341
342#ifdef TARGET_NT4
343VOID WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl)
344#else
345DWORD WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl,
346 DWORD dwEventType,
347 LPVOID lpEventData,
348 LPVOID lpContext)
349#endif
350{
351 DWORD rc = NO_ERROR;
352
353 VBoxServiceVerbose(2, "Control handler: Control=%ld\n", dwControl);
354#ifndef TARGET_NT4
355 VBoxServiceVerbose(2, "Control handler: EventType=%ld\n", dwEventType);
356#endif
357
358 switch (dwControl)
359 {
360
361 case SERVICE_CONTROL_INTERROGATE:
362 VBoxServiceWinSetStatus(g_rcWinService, 0);
363 break;
364
365 case SERVICE_CONTROL_STOP:
366 case SERVICE_CONTROL_SHUTDOWN:
367 {
368 VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, 0);
369
370 rc = VBoxServiceStopServices();
371
372 VBoxServiceWinSetStatus(SERVICE_STOPPED, 0);
373 }
374 break;
375
376 case SERVICE_CONTROL_SESSIONCHANGE: /* Only Win XP and up. */
377
378#ifndef TARGET_NT4
379 switch (dwEventType)
380 {
381 /*case WTS_SESSION_LOGON:
382 VBoxServiceVerbose(2, "A user has logged on to the session.\n");
383 break;
384
385 case WTS_SESSION_LOGOFF:
386 VBoxServiceVerbose(2, "A user has logged off from the session.\n");
387 break;*/
388 default:
389 break;
390 }
391#endif /* TARGET_NT4 */
392 break;
393
394 default:
395
396 VBoxServiceVerbose(1, "Service control function not implemented: %ld\n", dwControl);
397 rc = ERROR_CALL_NOT_IMPLEMENTED;
398 break;
399 }
400
401#ifndef TARGET_NT4
402 return rc;
403#endif
404}
405
406
407void WINAPI VBoxServiceWinMain(DWORD argc, LPTSTR *argv)
408{
409 int rc = VINF_SUCCESS;
410
411 VBoxServiceVerbose(2, "Registering service control handler ...\n");
412#ifdef TARGET_NT4
413 g_hWinServiceStatus = RegisterServiceCtrlHandler (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler);
414#else
415 g_hWinServiceStatus = RegisterServiceCtrlHandlerEx (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler, NULL);
416#endif
417
418 if (NULL == g_hWinServiceStatus)
419 {
420 DWORD dwErr = GetLastError();
421
422 switch (dwErr)
423 {
424 case ERROR_INVALID_NAME:
425 VBoxServiceError("Invalid service name!\n");
426 break;
427 case ERROR_SERVICE_DOES_NOT_EXIST:
428 VBoxServiceError("Service does not exist!\n");
429 break;
430 default:
431 VBoxServiceError("Could not register service control handle! Error: %ld\n", dwErr);
432 break;
433 }
434 }
435 else
436 {
437 VBoxServiceVerbose(2, "Service control handler registered.\n");
438
439 rc = VBoxServiceWinStart();
440 }
441}
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