VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxService/VBoxService.cpp@ 8078

Last change on this file since 8078 was 8078, checked in by vboxsync, 17 years ago

Rebranding: Guest Additions VBoxService.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/** @file
2 * VBoxService - Guest Additions Service
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "VBoxService.h"
18#include "VBoxSeamless.h"
19#include "VBoxClipboard.h"
20#include "VBoxDisplay.h"
21#include "VBoxRestore.h"
22#include "VBoxVRDP.h"
23#include "VBoxStatistics.h"
24#include "VBoxMemBalloon.h"
25#include <VBoxHook.h>
26#include "resource.h"
27#include <malloc.h>
28#include <VBoxGuestInternal.h>
29#include <iprt/string.h>
30
31#include "helpers.h"
32#include <sddl.h>
33
34/* global variables */
35HANDLE gVBoxDriver;
36HANDLE gStopSem;
37HANDLE ghSeamlessNotifyEvent = 0;
38SERVICE_STATUS gVBoxServiceStatus;
39SERVICE_STATUS_HANDLE gVBoxServiceStatusHandle;
40HINSTANCE gInstance;
41HWND gToolWindow;
42
43
44/* prototypes */
45VOID DisplayChangeThread(void *dummy);
46LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
47
48
49#ifdef DEBUG
50/**
51 * Helper function to send a message to WinDbg
52 *
53 * @param String message string
54 */
55void WriteLog(char *String, ...)
56{
57 DWORD cbReturned;
58 CHAR Buffer[1024];
59 VMMDevReqLogString *pReq = (VMMDevReqLogString *)Buffer;
60
61 va_list va;
62
63 va_start(va, String);
64
65 vmmdevInitRequest(&pReq->header, VMMDevReq_LogString);
66 RTStrPrintfV(pReq->szString, sizeof(Buffer)-sizeof(*pReq), String, va);
67 OutputDebugStringA(pReq->szString);
68 pReq->header.size += strlen(pReq->szString);
69
70 printf("%s\n", pReq->szString);
71
72 FILE* pFh = fopen("c:\\VBoxServiceDebug.txt", "at");
73
74 /* Does maybe not work on Vista (write protection when starting without admin rights),
75 so do this check! */
76 if (NULL != pFh)
77 {
78 fprintf(pFh, "%s", pReq->szString);
79 fclose(pFh);
80 }
81
82 DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_VMMREQUEST, pReq, pReq->header.size,
83 pReq, pReq->header.size, &cbReturned, NULL);
84
85 va_end (va);
86 return;
87}
88#endif
89
90
91
92/* The service table. */
93static VBOXSERVICEINFO vboxServiceTable[] =
94{
95 {
96 "Display",
97 VBoxDisplayInit,
98 VBoxDisplayThread,
99 VBoxDisplayDestroy,
100 },
101 {
102 "Shared Clipboard",
103 VBoxClipboardInit,
104 VBoxClipboardThread,
105 VBoxClipboardDestroy
106 },
107 {
108 "Seamless Windows",
109 VBoxSeamlessInit,
110 VBoxSeamlessThread,
111 VBoxSeamlessDestroy
112 },
113#ifdef VBOX_WITH_MANAGEMENT
114 {
115 "Memory Balloon",
116 VBoxMemBalloonInit,
117 VBoxMemBalloonThread,
118 VBoxMemBalloonDestroy,
119 },
120 {
121 "Guest Statistics",
122 VBoxStatsInit,
123 VBoxStatsThread,
124 VBoxStatsDestroy,
125 },
126#endif
127#ifdef VBOX_WITH_VRDP_SESSION_HANDLING
128 {
129 "Restore",
130 VBoxRestoreInit,
131 VBoxRestoreThread,
132 VBoxRestoreDestroy,
133 },
134#endif
135 {
136 "VRDP",
137 VBoxVRDPInit,
138 VBoxVRDPThread,
139 VBoxVRDPDestroy,
140 },
141 {
142 NULL
143 }
144};
145
146static int vboxStartServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
147{
148 dprintf(("VBoxService: Starting services...\n"));
149
150 pEnv->hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
151
152 if (!pEnv->hStopEvent)
153 {
154 /* Could not create event. */
155 return VERR_NOT_SUPPORTED;
156 }
157
158 while (pTable->pszName)
159 {
160 dprintf(("Starting %s...\n", pTable->pszName));
161
162 int rc = VINF_SUCCESS;
163
164 bool fStartThread = false;
165
166 pTable->hThread = (HANDLE)0;
167 pTable->pInstance = NULL;
168 pTable->fStarted = false;
169
170 if (pTable->pfnInit)
171 {
172 rc = pTable->pfnInit (pEnv, &pTable->pInstance, &fStartThread);
173 }
174
175 if (VBOX_FAILURE (rc))
176 {
177 dprintf(("Failed to initialize rc = %Vrc.\n", rc));
178 }
179 else
180 {
181 if (pTable->pfnThread && fStartThread)
182 {
183 unsigned threadid;
184
185 pTable->hThread = (HANDLE)_beginthreadex (NULL, /* security */
186 0, /* stacksize */
187 pTable->pfnThread,
188 pTable->pInstance,
189 0, /* initflag */
190 &threadid);
191
192 if (pTable->hThread == (HANDLE)(0))
193 {
194 rc = VERR_NOT_SUPPORTED;
195 }
196 }
197
198 if (VBOX_FAILURE (rc))
199 {
200 dprintf(("Failed to start the thread.\n"));
201
202 if (pTable->pfnDestroy)
203 {
204 pTable->pfnDestroy (pEnv, pTable->pInstance);
205 }
206 }
207 else
208 {
209 pTable->fStarted = true;
210 }
211 }
212
213 /* Advance to next table element. */
214 pTable++;
215 }
216
217 return VINF_SUCCESS;
218}
219
220static void vboxStopServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
221{
222 if (!pEnv->hStopEvent)
223 {
224 return;
225 }
226
227 /* Signal to all threads. */
228 SetEvent(pEnv->hStopEvent);
229
230 while (pTable->pszName)
231 {
232 if (pTable->fStarted)
233 {
234 if (pTable->pfnThread)
235 {
236 /* There is a thread, wait for termination. */
237 WaitForSingleObject(pTable->hThread, INFINITE);
238
239 CloseHandle (pTable->hThread);
240 pTable->hThread = 0;
241 }
242
243 if (pTable->pfnDestroy)
244 {
245 pTable->pfnDestroy (pEnv, pTable->pInstance);
246 }
247
248 pTable->fStarted = false;
249 }
250
251 /* Advance to next table element. */
252 pTable++;
253 }
254
255 CloseHandle (pEnv->hStopEvent);
256}
257
258
259void WINAPI VBoxServiceStart(void)
260{
261 dprintf(("VBoxService: Start\n"));
262
263 VBOXSERVICEENV svcEnv;
264
265 DWORD status = NO_ERROR;
266
267 /* open VBox guest driver */
268 gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
269 GENERIC_READ | GENERIC_WRITE,
270 FILE_SHARE_READ | FILE_SHARE_WRITE,
271 NULL,
272 OPEN_EXISTING,
273 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
274 NULL);
275 if (gVBoxDriver == INVALID_HANDLE_VALUE)
276 {
277 dprintf(("VBoxService: could not open VBox Guest Additions driver! Please install / start it first! rc = %d\n", GetLastError()));
278 status = ERROR_GEN_FAILURE;
279 }
280
281 dprintf(("VBoxService: Driver h %p, st %p\n", gVBoxDriver, status));
282
283 if (status == NO_ERROR)
284 {
285 /* create a custom window class */
286 WNDCLASS windowClass = {0};
287 windowClass.style = CS_NOCLOSE;
288 windowClass.lpfnWndProc = (WNDPROC)VBoxToolWndProc;
289 windowClass.hInstance = gInstance;
290 windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
291 windowClass.lpszClassName = "VirtualBoxTool";
292 if (!RegisterClass(&windowClass))
293 status = GetLastError();
294 }
295
296 dprintf(("VBoxService: Class st %p\n", status));
297
298 if (status == NO_ERROR)
299 {
300 /* create our window */
301 gToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
302 "VirtualBoxTool", "VirtualBoxTool",
303 WS_POPUPWINDOW,
304 -200, -200, 100, 100, NULL, NULL, gInstance, NULL);
305 if (!gToolWindow)
306 status = GetLastError();
307 else
308 {
309 /* move the window beneach the mouse pointer so that we get access to it */
310 POINT mousePos;
311 GetCursorPos(&mousePos);
312 SetWindowPos(gToolWindow, HWND_TOPMOST, mousePos.x - 10, mousePos.y - 10, 0, 0,
313 SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
314 /* change the mouse pointer so that we can go for a hardware shape */
315 SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
316 SetCursor(LoadCursor(NULL, IDC_ARROW));
317 /* move back our tool window */
318 SetWindowPos(gToolWindow, HWND_TOPMOST, -200, -200, 0, 0,
319 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
320 }
321 }
322
323 dprintf(("VBoxService: Window h %p, st %p\n", gToolWindow, status));
324
325 if (status == NO_ERROR)
326 {
327 gStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
328 if (gStopSem == NULL)
329 {
330 dprintf(("VBoxService: CreateEvent for Stopping failed: rc = %d\n", GetLastError()));
331 return;
332 }
333
334 /* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore */
335 SECURITY_ATTRIBUTES SecAttr;
336 OSVERSIONINFO info;
337 char secDesc[SECURITY_DESCRIPTOR_MIN_LENGTH];
338 DWORD dwMajorVersion = 5; /* default XP */
339 BOOL ret;
340
341 SecAttr.nLength = sizeof(SecAttr);
342 SecAttr.bInheritHandle = FALSE;
343 SecAttr.lpSecurityDescriptor = &secDesc;
344 InitializeSecurityDescriptor(SecAttr.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
345 ret = SetSecurityDescriptorDacl(SecAttr.lpSecurityDescriptor, TRUE, 0, FALSE);
346 if (!ret)
347 dprintf(("SetSecurityDescriptorDacl failed with %d\n", GetLastError()));
348
349 info.dwOSVersionInfoSize = sizeof(info);
350 if (GetVersionEx(&info))
351 {
352 dprintf(("VBoxService: Windows version major %d minor %d\n", info.dwMajorVersion, info.dwMinorVersion));
353 dwMajorVersion = info.dwMajorVersion;
354 }
355
356 /* For Vista and up we need to change the integrity of the security descriptor too */
357 if (dwMajorVersion >= 6)
358 {
359 HMODULE hModule;
360
361 BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
362
363 hModule = LoadLibrary("ADVAPI32.DLL");
364 if (hModule)
365 {
366 PSECURITY_DESCRIPTOR pSD;
367 PACL pSacl = NULL;
368 BOOL fSaclPresent = FALSE;
369 BOOL fSaclDefaulted = FALSE;
370
371 *(uintptr_t *)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA = (uintptr_t)GetProcAddress(hModule, "ConvertStringSecurityDescriptorToSecurityDescriptorA");
372
373 dprintf(("pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
374 if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
375 {
376 ret = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
377 SDDL_REVISION_1, &pSD, NULL);
378 if (!ret)
379 dprintf(("ConvertStringSecurityDescriptorToSecurityDescriptorA failed with %d\n", GetLastError()));
380
381 ret = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
382 if (!ret)
383 dprintf(("GetSecurityDescriptorSacl failed with %d\n", GetLastError()));
384
385 ret = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
386 if (!ret)
387 dprintf(("SetSecurityDescriptorSacl failed with %d\n", GetLastError()));
388 }
389 }
390 }
391
392 if (dwMajorVersion >= 5) /* Only for W2K and up ... */
393 {
394 ghSeamlessNotifyEvent = CreateEvent(&SecAttr, FALSE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
395 if (ghSeamlessNotifyEvent == NULL)
396 {
397 dprintf(("VBoxService: CreateEvent for Seamless failed: rc = %d\n", GetLastError()));
398 return;
399 }
400 }
401 }
402
403 /*
404 * Start services listed in the vboxServiceTable.
405 */
406 svcEnv.hInstance = gInstance;
407 svcEnv.hDriver = gVBoxDriver;
408
409 if (status == NO_ERROR)
410 {
411 int rc = vboxStartServices (&svcEnv, vboxServiceTable);
412
413 if (VBOX_FAILURE (rc))
414 {
415 status = ERROR_GEN_FAILURE;
416 }
417 }
418
419 /* terminate service if something went wrong */
420 if (status != NO_ERROR)
421 {
422 vboxStopServices (&svcEnv, vboxServiceTable);
423 return;
424 }
425
426 BOOL fTrayIconCreated = false;
427
428 /* prepare the system tray icon */
429 NOTIFYICONDATA ndata;
430 memset (&ndata, 0, sizeof (ndata));
431 ndata.cbSize = NOTIFYICONDATA_V1_SIZE; // sizeof(NOTIFYICONDATA);
432 ndata.hWnd = gToolWindow;
433 ndata.uID = 2000;
434 ndata.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
435 ndata.uCallbackMessage = WM_USER;
436 ndata.hIcon = LoadIcon(gInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
437 sprintf(ndata.szTip, "Sun xVM VirtualBox Guest Additions %d.%d.%dr%d", VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
438
439 dprintf(("VBoxService: ndata.hWnd %08X, ndata.hIcon = %p\n", ndata.hWnd, ndata.hIcon));
440
441 /* Boost thread priority to make sure we wake up early for seamless window notifications (not sure if it actually makes any difference though) */
442 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
443
444 /*
445 * Main execution loop
446 * Wait for the stop semaphore to be posted or a window event to arrive
447 */
448
449 DWORD dwEventCount = 2;
450 HANDLE hWaitEvent[2] = {gStopSem, ghSeamlessNotifyEvent};
451
452 if (0 == ghSeamlessNotifyEvent) /* If seamless mode is not active / supported, reduce event array count */
453 dwEventCount = 1;
454
455 dprintf(("VBoxService: Number of events to wait in main loop: %ld\n", dwEventCount));
456
457 while(true)
458 {
459 DWORD waitResult = MsgWaitForMultipleObjectsEx(dwEventCount, hWaitEvent, 500, QS_ALLINPUT, 0);
460 waitResult = waitResult - WAIT_OBJECT_0;
461
462 dprintf(("VBoxService: Wait result = %ld.\n", waitResult));
463
464 if (waitResult == 0)
465 {
466 dprintf(("VBoxService: Event 'Exit' triggered.\n"));
467 /* exit */
468 break;
469 }
470 else if ((waitResult == 1) && (ghSeamlessNotifyEvent!=0)) /* Only jump in, if seamless is active! */
471 {
472 dprintf(("VBoxService: Event 'Seamless' triggered.\n"));
473
474 /* seamless window notification */
475 VBoxSeamlessCheckWindows();
476 }
477 else
478 {
479 /* timeout or a window message, handle it */
480 MSG msg;
481 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
482 {
483 dprintf(("VBoxService: msg %p\n", msg.message));
484 if (msg.message == WM_QUIT)
485 {
486 dprintf(("VBoxService: WM_QUIT!\n"));
487 SetEvent(gStopSem);
488 continue;
489 }
490 TranslateMessage(&msg);
491 DispatchMessage(&msg);
492 }
493 /* we might have to repeat this operation because the shell might not be loaded yet */
494 if (!fTrayIconCreated)
495 {
496 fTrayIconCreated = Shell_NotifyIcon(NIM_ADD, &ndata);
497 dprintf(("VBoxService: fTrayIconCreated = %d, err %08X\n", fTrayIconCreated, GetLastError ()));
498 }
499 }
500 }
501
502 dprintf(("VBoxService: returned from main loop, exiting...\n"));
503
504 /* remove the system tray icon */
505 Shell_NotifyIcon(NIM_DELETE, &ndata);
506
507 dprintf(("VBoxService: waiting for display change thread...\n"));
508
509 vboxStopServices (&svcEnv, vboxServiceTable);
510
511 dprintf(("VBoxService: destroying tool window...\n"));
512
513 /* destroy the tool window */
514 DestroyWindow(gToolWindow);
515
516 UnregisterClass("VirtualBoxTool", gInstance);
517
518 CloseHandle(gVBoxDriver);
519 CloseHandle(gStopSem);
520 CloseHandle(ghSeamlessNotifyEvent);
521
522 dprintf(("VBoxService: leaving service main function\n"));
523
524 return;
525}
526
527
528/**
529 * Main function
530 */
531int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
532{
533 dprintf(("VBoxService: WinMain\n"));
534 gInstance = hInstance;
535 VBoxServiceStart();
536
537 return 0;
538}
539
540/**
541 * Window procedure for our tool window
542 */
543LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
544{
545 switch (msg)
546 {
547 case WM_CLOSE:
548 break;
549
550 case WM_DESTROY:
551 break;
552
553 case WM_VBOX_INSTALL_SEAMLESS_HOOK:
554 VBoxSeamlessInstallHook();
555 break;
556
557 case WM_VBOX_REMOVE_SEAMLESS_HOOK:
558 VBoxSeamlessRemoveHook();
559 break;
560
561 case WM_VBOX_SEAMLESS_UPDATE:
562 VBoxSeamlessCheckWindows();
563 break;
564
565 case WM_VBOX_RESTORED:
566 VBoxRestoreSession();
567 break;
568
569 case WM_VBOX_CHECK_VRDP:
570 VBoxRestoreCheckVRDP();
571 break;
572
573 default:
574 return DefWindowProc(hwnd, msg, wParam, lParam);
575 }
576 return 0;
577}
Note: See TracBrowser for help on using the repository browser.

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