VirtualBox

source: vbox/trunk/src/VBox/Main/glue/initterm.cpp@ 47777

Last change on this file since 47777 was 47777, checked in by vboxsync, 11 years ago

Main/glue: for XPCOM, look for the components subdirectory one level up if we're running testcases in a non-hardened build

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
Line 
1/* $Id: initterm.cpp 47777 2013-08-15 17:42:39Z vboxsync $ */
2
3/** @file
4 * MS COM / XPCOM Abstraction Layer - Initialization and Termination.
5 */
6
7/*
8 * Copyright (C) 2006-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#if !defined(VBOX_WITH_XPCOM)
20
21# include <objbase.h>
22
23#else /* !defined (VBOX_WITH_XPCOM) */
24
25# include <stdlib.h>
26
27 /* XPCOM_GLUE is defined when the client uses the standalone glue
28 * (i.e. dynamically picks up the existing XPCOM shared library installation).
29 * This is not the case for VirtualBox XPCOM clients (they are always
30 * distributed with the self-built XPCOM library, and therefore have a binary
31 * dependency on it) but left here for clarity.
32 */
33# if defined(XPCOM_GLUE)
34# include <nsXPCOMGlue.h>
35# endif
36
37# include <nsIComponentRegistrar.h>
38# include <nsIServiceManager.h>
39# include <nsCOMPtr.h>
40# include <nsEventQueueUtils.h>
41# include <nsEmbedString.h>
42
43# include <nsILocalFile.h>
44# include <nsIDirectoryService.h>
45# include <nsDirectoryServiceDefs.h>
46
47#endif /* !defined(VBOX_WITH_XPCOM) */
48
49#include "VBox/com/com.h"
50#include "VBox/com/assert.h"
51#include "VBox/com/NativeEventQueue.h"
52#include "VBox/com/AutoLock.h"
53
54#include "../include/Logging.h"
55
56#include <iprt/asm.h>
57#include <iprt/env.h>
58#include <iprt/param.h>
59#include <iprt/path.h>
60#include <iprt/string.h>
61#include <iprt/thread.h>
62
63#include <VBox/err.h>
64
65namespace com
66{
67
68#if defined(VBOX_WITH_XPCOM)
69
70class DirectoryServiceProvider : public nsIDirectoryServiceProvider
71{
72public:
73
74 NS_DECL_ISUPPORTS
75
76 DirectoryServiceProvider()
77 : mCompRegLocation(NULL), mXPTIDatLocation(NULL)
78 , mComponentDirLocation(NULL), mCurrProcDirLocation(NULL)
79 {}
80
81 virtual ~DirectoryServiceProvider();
82
83 HRESULT init(const char *aCompRegLocation,
84 const char *aXPTIDatLocation,
85 const char *aComponentDirLocation,
86 const char *aCurrProcDirLocation);
87
88 NS_DECL_NSIDIRECTORYSERVICEPROVIDER
89
90private:
91 /** @remarks This is not a UTF-8 string. */
92 char *mCompRegLocation;
93 /** @remarks This is not a UTF-8 string. */
94 char *mXPTIDatLocation;
95 /** @remarks This is not a UTF-8 string. */
96 char *mComponentDirLocation;
97 /** @remarks This is not a UTF-8 string. */
98 char *mCurrProcDirLocation;
99};
100
101NS_IMPL_ISUPPORTS1(DirectoryServiceProvider, nsIDirectoryServiceProvider)
102
103DirectoryServiceProvider::~DirectoryServiceProvider()
104{
105 if (mCompRegLocation)
106 {
107 RTStrFree(mCompRegLocation);
108 mCompRegLocation = NULL;
109 }
110 if (mXPTIDatLocation)
111 {
112 RTStrFree(mXPTIDatLocation);
113 mXPTIDatLocation = NULL;
114 }
115 if (mComponentDirLocation)
116 {
117 RTStrFree(mComponentDirLocation);
118 mComponentDirLocation = NULL;
119 }
120 if (mCurrProcDirLocation)
121 {
122 RTStrFree(mCurrProcDirLocation);
123 mCurrProcDirLocation = NULL;
124 }
125}
126
127/**
128 * @param aCompRegLocation Path to compreg.dat, in Utf8.
129 * @param aXPTIDatLocation Path to xpti.data, in Utf8.
130 */
131HRESULT
132DirectoryServiceProvider::init(const char *aCompRegLocation,
133 const char *aXPTIDatLocation,
134 const char *aComponentDirLocation,
135 const char *aCurrProcDirLocation)
136{
137 AssertReturn(aCompRegLocation, NS_ERROR_INVALID_ARG);
138 AssertReturn(aXPTIDatLocation, NS_ERROR_INVALID_ARG);
139
140/** @todo r=bird: Gotta check how this encoding stuff plays out on darwin!
141 * We get down to [VBoxNsxp]NS_NewNativeLocalFile and that file isn't
142 * nsLocalFileUnix.cpp on 32-bit darwin. On 64-bit darwin it's a question
143 * of what we're doing in IPRT and such... We should probably add a
144 * RTPathConvertToNative for use here. */
145 int vrc = RTStrUtf8ToCurrentCP(&mCompRegLocation, aCompRegLocation);
146 if (RT_SUCCESS(vrc))
147 vrc = RTStrUtf8ToCurrentCP(&mXPTIDatLocation, aXPTIDatLocation);
148 if (RT_SUCCESS(vrc) && aComponentDirLocation)
149 vrc = RTStrUtf8ToCurrentCP(&mComponentDirLocation, aComponentDirLocation);
150 if (RT_SUCCESS(vrc) && aCurrProcDirLocation)
151 vrc = RTStrUtf8ToCurrentCP(&mCurrProcDirLocation, aCurrProcDirLocation);
152
153 return RT_SUCCESS(vrc) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
154}
155
156NS_IMETHODIMP
157DirectoryServiceProvider::GetFile(const char *aProp,
158 PRBool *aPersistent,
159 nsIFile **aRetval)
160{
161 nsCOMPtr <nsILocalFile> localFile;
162 nsresult rv = NS_ERROR_FAILURE;
163
164 *aRetval = nsnull;
165 *aPersistent = PR_TRUE;
166
167 const char *fileLocation = NULL;
168
169 if (strcmp(aProp, NS_XPCOM_COMPONENT_REGISTRY_FILE) == 0)
170 fileLocation = mCompRegLocation;
171 else if (strcmp(aProp, NS_XPCOM_XPTI_REGISTRY_FILE) == 0)
172 fileLocation = mXPTIDatLocation;
173 else if (mComponentDirLocation && strcmp(aProp, NS_XPCOM_COMPONENT_DIR) == 0)
174 fileLocation = mComponentDirLocation;
175 else if (mCurrProcDirLocation && strcmp(aProp, NS_XPCOM_CURRENT_PROCESS_DIR) == 0)
176 fileLocation = mCurrProcDirLocation;
177 else
178 return NS_ERROR_FAILURE;
179
180 rv = NS_NewNativeLocalFile(nsEmbedCString(fileLocation),
181 PR_TRUE, getter_AddRefs(localFile));
182 if (NS_FAILED(rv))
183 return rv;
184
185 return localFile->QueryInterface(NS_GET_IID (nsIFile), (void **)aRetval);
186}
187
188/**
189 * Global XPCOM initialization flag (we maintain it ourselves since XPCOM
190 * doesn't provide such functionality)
191 */
192static bool volatile gIsXPCOMInitialized = false;
193
194/**
195 * Number of Initialize() calls on the main thread.
196 */
197static unsigned int gXPCOMInitCount = 0;
198
199#else /* !defined (VBOX_WITH_XPCOM) */
200
201/**
202 * The COM main thread handle. (The first caller of com::Initialize().)
203 */
204static RTTHREAD volatile gCOMMainThread = NIL_RTTHREAD;
205
206/**
207 * Number of Initialize() calls on the main thread.
208 */
209static uint32_t gCOMMainInitCount = 0;
210
211#endif /* !defined (VBOX_WITH_XPCOM) */
212
213
214/**
215 * Initializes the COM runtime.
216 *
217 * This method must be called on each thread of the client application that
218 * wants to access COM facilities. The initialization must be performed before
219 * calling any other COM method or attempting to instantiate COM objects.
220 *
221 * On platforms using XPCOM, this method uses the following scheme to search for
222 * XPCOM runtime:
223 *
224 * 1. If the VBOX_APP_HOME environment variable is set, the path it specifies
225 * is used to search XPCOM libraries and components. If this method fails to
226 * initialize XPCOM runtime using this path, it will immediately return a
227 * failure and will NOT check for other paths as described below.
228 *
229 * 2. If VBOX_APP_HOME is not set, this methods tries the following paths in the
230 * given order:
231 *
232 * a) Compiled-in application data directory (as returned by
233 * RTPathAppPrivateArch())
234 * b) "/usr/lib/virtualbox" (Linux only)
235 * c) "/opt/VirtualBox" (Linux only)
236 *
237 * The first path for which the initialization succeeds will be used.
238 *
239 * On MS COM platforms, the COM runtime is provided by the system and does not
240 * need to be searched for.
241 *
242 * Once the COM subsystem is no longer necessary on a given thread, Shutdown()
243 * must be called to free resources allocated for it. Note that a thread may
244 * call Initialize() several times but for each of tese calls there must be a
245 * corresponding Shutdown() call.
246 *
247 * @return S_OK on success and a COM result code in case of failure.
248 */
249HRESULT Initialize(bool fGui)
250{
251 HRESULT rc = E_FAIL;
252
253#if !defined(VBOX_WITH_XPCOM)
254
255 /*
256 * We initialize COM in GUI thread in STA, to be compliant with QT and
257 * OLE requirments (for example to allow D&D), while other threads
258 * initialized in regular MTA. To allow fast proxyless access from
259 * GUI thread to COM objects, we explicitly provide our COM objects
260 * with free threaded marshaller.
261 * !!!!! Please think twice before touching this code !!!!!
262 */
263 DWORD flags = fGui ?
264 COINIT_APARTMENTTHREADED
265 | COINIT_SPEED_OVER_MEMORY
266 :
267 COINIT_MULTITHREADED
268 | COINIT_DISABLE_OLE1DDE
269 | COINIT_SPEED_OVER_MEMORY;
270
271 rc = CoInitializeEx(NULL, flags);
272
273 /* the overall result must be either S_OK or S_FALSE (S_FALSE means
274 * "already initialized using the same apartment model") */
275 AssertMsg(rc == S_OK || rc == S_FALSE, ("rc=%08X\n", rc));
276
277 /* To be flow compatible with the XPCOM case, we return here if this isn't
278 * the main thread or if it isn't its first initialization call.
279 * Note! CoInitializeEx and CoUninitialize does it's own reference
280 * counting, so this exercise is entirely for the EventQueue init. */
281 bool fRc;
282 RTTHREAD hSelf = RTThreadSelf();
283 if (hSelf != NIL_RTTHREAD)
284 ASMAtomicCmpXchgHandle(&gCOMMainThread, hSelf, NIL_RTTHREAD, fRc);
285 else
286 fRc = false;
287
288 if (fGui)
289 Assert(RTThreadIsMain(hSelf));
290
291 if (!fRc)
292 {
293 if ( gCOMMainThread == hSelf
294 && SUCCEEDED(rc))
295 gCOMMainInitCount++;
296
297 AssertComRC(rc);
298 return rc;
299 }
300 Assert(RTThreadIsMain(hSelf));
301
302 /* this is the first main thread initialization */
303 Assert(gCOMMainInitCount == 0);
304 if (SUCCEEDED(rc))
305 gCOMMainInitCount = 1;
306
307#else /* !defined (VBOX_WITH_XPCOM) */
308
309 /* Unused here */
310 NOREF(fGui);
311
312 if (ASMAtomicXchgBool(&gIsXPCOMInitialized, true) == true)
313 {
314 /* XPCOM is already initialized on the main thread, no special
315 * initialization is necessary on additional threads. Just increase
316 * the init counter if it's a main thread again (to correctly support
317 * nested calls to Initialize()/Shutdown() for compatibility with
318 * Win32). */
319
320 nsCOMPtr<nsIEventQueue> eventQ;
321 rc = NS_GetMainEventQ(getter_AddRefs(eventQ));
322
323 if (NS_SUCCEEDED(rc))
324 {
325 PRBool isOnMainThread = PR_FALSE;
326 rc = eventQ->IsOnCurrentThread(&isOnMainThread);
327 if (NS_SUCCEEDED(rc) && isOnMainThread)
328 ++gXPCOMInitCount;
329 }
330
331 AssertComRC(rc);
332 return rc;
333 }
334 Assert(RTThreadIsMain(RTThreadSelf()));
335
336 /* this is the first initialization */
337 gXPCOMInitCount = 1;
338 bool const fInitEventQueues = true;
339
340 /* prepare paths for registry files */
341 char szCompReg[RTPATH_MAX];
342 char szXptiDat[RTPATH_MAX];
343
344 int vrc = GetVBoxUserHomeDirectory(szCompReg, sizeof(szCompReg));
345 if (vrc == VERR_ACCESS_DENIED)
346 return NS_ERROR_FILE_ACCESS_DENIED;
347 AssertRCReturn(vrc, NS_ERROR_FAILURE);
348 vrc = RTStrCopy(szXptiDat, sizeof(szXptiDat), szCompReg);
349 AssertRCReturn(vrc, NS_ERROR_FAILURE);
350 vrc = RTPathAppend(szCompReg, sizeof(szCompReg), "compreg.dat");
351 AssertRCReturn(vrc, NS_ERROR_FAILURE);
352 vrc = RTPathAppend(szXptiDat, sizeof(szXptiDat), "xpti.dat");
353 AssertRCReturn(vrc, NS_ERROR_FAILURE);
354
355 LogFlowFunc(("component registry : \"%s\"\n", szCompReg));
356 LogFlowFunc(("XPTI data file : \"%s\"\n", szXptiDat));
357
358#if defined (XPCOM_GLUE)
359 XPCOMGlueStartup(nsnull);
360#endif
361
362 static const char *kAppPathsToProbe[] =
363 {
364 NULL, /* 0: will use VBOX_APP_HOME */
365 NULL, /* 1: will try RTPathAppPrivateArch(), correctly installed release builds will never go further */
366 NULL, /* 2: will try parent directory of RTPathAppPrivateArch(), only for testcases in non-hardened builds */
367 /* There used to be hard coded paths, but they only caused trouble
368 * because they often led to mixing of builds or even versions.
369 * If you feel tempted to add anything here, think again. They would
370 * only be used if option 1 would not work, which is a sign of a big
371 * problem, as it returns a fixed location defined at compile time.
372 * It is better to fail than blindly trying to cover the problem. */
373 };
374
375 /* Find out the directory where VirtualBox binaries are located */
376 for (size_t i = 0; i < RT_ELEMENTS(kAppPathsToProbe); ++ i)
377 {
378 char szAppHomeDir[RTPATH_MAX];
379
380 if (i == 0)
381 {
382 /* Use VBOX_APP_HOME if present */
383 vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_APP_HOME", szAppHomeDir, sizeof(szAppHomeDir), NULL);
384 if (vrc == VERR_ENV_VAR_NOT_FOUND)
385 continue;
386 AssertRC(vrc);
387 }
388 else if (i == 1)
389 {
390 /* Use RTPathAppPrivateArch() first */
391 vrc = RTPathAppPrivateArch(szAppHomeDir, sizeof(szAppHomeDir));
392 AssertRC(vrc);
393 }
394 else if (i == 2)
395 {
396#ifdef VBOX_WITH_HARDENING
397 continue;
398#else /* !VBOX_WITH_HARDENING */
399 /* Use parent of RTPathAppPrivateArch() if ends with "testcase" */
400 vrc = RTPathAppPrivateArch(szAppHomeDir, sizeof(szAppHomeDir));
401 AssertRC(vrc);
402 vrc = RTPathStripTrailingSlash(szAppHomeDir);
403 AssertRC(vrc);
404 char *filename = RTPathFilename(szAppHomeDir);
405 if (!filename || strcmp(filename, "testcase"))
406 continue;
407 RTPathStripFilename(szAppHomeDir);
408#endif /* !VBOX_WITH_HARDENING */
409 }
410 else
411 {
412 /* Iterate over all other paths */
413 RTStrCopy(szAppHomeDir, sizeof(szAppHomeDir), kAppPathsToProbe[i]);
414 vrc = VINF_SUCCESS;
415 }
416 if (RT_FAILURE(vrc))
417 {
418 rc = NS_ERROR_FAILURE;
419 continue;
420 }
421 char szCompDir[RTPATH_MAX];
422 vrc = RTStrCopy(szCompDir, sizeof(szCompDir), szAppHomeDir);
423 if (RT_FAILURE(vrc))
424 {
425 rc = NS_ERROR_FAILURE;
426 continue;
427 }
428 vrc = RTPathAppend(szCompDir, sizeof(szCompDir), "components");
429 if (RT_FAILURE(vrc))
430 {
431 rc = NS_ERROR_FAILURE;
432 continue;
433 }
434 LogFlowFunc(("component directory : \"%s\"\n", szCompDir));
435
436 nsCOMPtr<DirectoryServiceProvider> dsProv;
437 dsProv = new DirectoryServiceProvider();
438 if (dsProv)
439 rc = dsProv->init(szCompReg, szXptiDat, szCompDir, szAppHomeDir);
440 else
441 rc = NS_ERROR_OUT_OF_MEMORY;
442 if (NS_FAILED(rc))
443 break;
444
445 /* Setup the application path for NS_InitXPCOM2. Note that we properly
446 * answer the NS_XPCOM_CURRENT_PROCESS_DIR query in our directory
447 * service provider but it seems to be activated after the directory
448 * service is used for the first time (see the source NS_InitXPCOM2). So
449 * use the same value here to be on the safe side. */
450 nsCOMPtr <nsIFile> appDir;
451 {
452 char *appDirCP = NULL;
453 vrc = RTStrUtf8ToCurrentCP(&appDirCP, szAppHomeDir);
454 if (RT_SUCCESS(vrc))
455 {
456 nsCOMPtr<nsILocalFile> file;
457 rc = NS_NewNativeLocalFile(nsEmbedCString(appDirCP),
458 PR_FALSE, getter_AddRefs(file));
459 if (NS_SUCCEEDED(rc))
460 appDir = do_QueryInterface(file, &rc);
461
462 RTStrFree(appDirCP);
463 }
464 else
465 rc = NS_ERROR_FAILURE;
466 }
467 if (NS_FAILED(rc))
468 break;
469
470 /* Set VBOX_XPCOM_HOME to the same app path to make XPCOM sources that
471 * still use it instead of the directory service happy */
472 vrc = RTEnvSetEx(RTENV_DEFAULT, "VBOX_XPCOM_HOME", szAppHomeDir);
473 AssertRC(vrc);
474
475 /* Finally, initialize XPCOM */
476 {
477 nsCOMPtr<nsIServiceManager> serviceManager;
478 rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), appDir, dsProv);
479 if (NS_SUCCEEDED(rc))
480 {
481 nsCOMPtr<nsIComponentRegistrar> registrar =
482 do_QueryInterface(serviceManager, &rc);
483 if (NS_SUCCEEDED(rc))
484 {
485 rc = registrar->AutoRegister(nsnull);
486 if (NS_SUCCEEDED(rc))
487 {
488 /* We succeeded, stop probing paths */
489 LogFlowFunc(("Succeeded.\n"));
490 break;
491 }
492 }
493 }
494 }
495
496 /* clean up before the new try */
497 rc = NS_ShutdownXPCOM(nsnull);
498
499 if (i == 0)
500 {
501 /* We failed with VBOX_APP_HOME, don't probe other paths */
502 break;
503 }
504 }
505
506#endif /* !defined (VBOX_WITH_XPCOM) */
507
508 // for both COM and XPCOM, we only get here if this is the main thread;
509 // only then initialize the autolock system (AutoLock.cpp)
510 Assert(RTThreadIsMain(RTThreadSelf()));
511 util::InitAutoLockSystem();
512
513 AssertComRC(rc);
514
515 /*
516 * Init the main event queue (ASSUMES it cannot fail).
517 */
518 if (SUCCEEDED(rc))
519 NativeEventQueue::init();
520
521 return rc;
522}
523
524HRESULT Shutdown()
525{
526 HRESULT rc = S_OK;
527
528#if !defined(VBOX_WITH_XPCOM)
529
530 /* EventQueue::uninit reference counting fun. */
531 RTTHREAD hSelf = RTThreadSelf();
532 if ( hSelf == gCOMMainThread
533 && hSelf != NIL_RTTHREAD)
534 {
535 if (-- gCOMMainInitCount == 0)
536 {
537 NativeEventQueue::uninit();
538 ASMAtomicWriteHandle(&gCOMMainThread, NIL_RTTHREAD);
539 }
540 }
541
542 CoUninitialize();
543
544#else /* !defined (VBOX_WITH_XPCOM) */
545
546 nsCOMPtr<nsIEventQueue> eventQ;
547 rc = NS_GetMainEventQ(getter_AddRefs(eventQ));
548
549 if (NS_SUCCEEDED(rc) || rc == NS_ERROR_NOT_AVAILABLE)
550 {
551 /* NS_ERROR_NOT_AVAILABLE seems to mean that
552 * nsIEventQueue::StopAcceptingEvents() has been called (see
553 * nsEventQueueService.cpp). We hope that this error code always means
554 * just that in this case and assume that we're on the main thread
555 * (it's a kind of unexpected behavior if a non-main thread ever calls
556 * StopAcceptingEvents() on the main event queue). */
557
558 PRBool isOnMainThread = PR_FALSE;
559 if (NS_SUCCEEDED(rc))
560 {
561 rc = eventQ->IsOnCurrentThread(&isOnMainThread);
562 eventQ = nsnull; /* early release before shutdown */
563 }
564 else
565 {
566 isOnMainThread = PR_TRUE;
567 rc = NS_OK;
568 }
569
570 if (NS_SUCCEEDED(rc) && isOnMainThread)
571 {
572 /* only the main thread needs to uninitialize XPCOM and only if
573 * init counter drops to zero */
574 if (--gXPCOMInitCount == 0)
575 {
576 NativeEventQueue::uninit();
577 rc = NS_ShutdownXPCOM(nsnull);
578
579 /* This is a thread initialized XPCOM and set gIsXPCOMInitialized to
580 * true. Reset it back to false. */
581 bool wasInited = ASMAtomicXchgBool(&gIsXPCOMInitialized, false);
582 Assert(wasInited == true);
583 NOREF(wasInited);
584
585# if defined (XPCOM_GLUE)
586 XPCOMGlueShutdown();
587# endif
588 }
589 }
590 }
591
592#endif /* !defined(VBOX_WITH_XPCOM) */
593
594 AssertComRC(rc);
595
596 return rc;
597}
598
599} /* namespace com */
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