VirtualBox

source: vbox/trunk/src/VBox/Main/glue/com.cpp@ 55436

Last change on this file since 55436 was 53834, checked in by vboxsync, 10 years ago

missing error handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 KB
Line 
1/* $Id: com.cpp 53834 2015-01-15 20:02:36Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer
4 */
5
6/*
7 * Copyright (C) 2005-2013 Oracle Corporation
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
18#if !defined (VBOX_WITH_XPCOM)
19
20# include <objbase.h>
21
22#else /* !defined (VBOX_WITH_XPCOM) */
23# include <stdlib.h>
24# include <nsCOMPtr.h>
25# include <nsIServiceManagerUtils.h>
26# include <nsIComponentManager.h>
27# include <ipcIService.h>
28# include <ipcCID.h>
29# include <ipcIDConnectService.h>
30# include <nsIInterfaceInfo.h>
31# include <nsIInterfaceInfoManager.h>
32// official XPCOM headers don't define it yet
33#define IPC_DCONNECTSERVICE_CONTRACTID \
34 "@mozilla.org/ipc/dconnect-service;1"
35#endif /* !defined (VBOX_WITH_XPCOM) */
36
37#include "VBox/com/com.h"
38#include "VBox/com/assert.h"
39
40#include "VBox/com/Guid.h"
41#include "VBox/com/array.h"
42
43#include <package-generated.h>
44
45#include <iprt/buildconfig.h>
46#include <iprt/param.h>
47#include <iprt/path.h>
48#include <iprt/dir.h>
49#include <iprt/env.h>
50#include <iprt/string.h>
51#include <iprt/system.h>
52#include <iprt/process.h>
53
54#include <VBox/err.h>
55#include <VBox/version.h>
56
57#if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS)
58char szXdgConfigHome[RTPATH_MAX] = "";
59#endif
60
61/**
62 * Possible locations for the VirtualBox user configuration folder,
63 * listed from oldest (as in legacy) to newest. These can be either
64 * absolute or relative to the home directory. We use the first entry
65 * of the list which corresponds to a real folder on storage, or
66 * create a folder corresponding to the last in the list (the least
67 * legacy) if none do.
68 */
69const char *const apcszUserHome[] =
70#ifdef RT_OS_DARWIN
71{ "Library/VirtualBox" };
72#elif defined RT_OS_WINDOWS
73{ ".VirtualBox" };
74#else
75{ ".VirtualBox", szXdgConfigHome };
76#endif
77
78#include "Logging.h"
79
80namespace com
81{
82
83void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
84{
85 Assert(aName);
86 if (!aName)
87 return;
88
89 *aName = NULL;
90
91#if !defined(VBOX_WITH_XPCOM)
92
93 LONG rc;
94 LPOLESTR iidStr = NULL;
95 if (StringFromIID(aIID, &iidStr) == S_OK)
96 {
97 HKEY ifaceKey;
98 rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
99 0, KEY_QUERY_VALUE, &ifaceKey);
100 if (rc == ERROR_SUCCESS)
101 {
102 HKEY iidKey;
103 rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
104 if (rc == ERROR_SUCCESS)
105 {
106 /* determine the size and type */
107 DWORD sz, type;
108 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
109 if (rc == ERROR_SUCCESS && type == REG_SZ)
110 {
111 /* query the value to BSTR */
112 *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
113 rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
114 if (rc != ERROR_SUCCESS)
115 {
116 SysFreeString(*aName);
117 aName = NULL;
118 }
119 }
120 RegCloseKey(iidKey);
121 }
122 RegCloseKey(ifaceKey);
123 }
124 CoTaskMemFree(iidStr);
125 }
126
127#else /* !defined (VBOX_WITH_XPCOM) */
128
129 nsresult rv;
130 nsCOMPtr<nsIInterfaceInfoManager> iim =
131 do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
132 if (NS_SUCCEEDED(rv))
133 {
134 nsCOMPtr<nsIInterfaceInfo> iinfo;
135 rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
136 if (NS_SUCCEEDED(rv))
137 {
138 const char *iname = NULL;
139 iinfo->GetNameShared(&iname);
140 char *utf8IName = NULL;
141 if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
142 {
143 PRTUTF16 utf16IName = NULL;
144 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
145 {
146 *aName = SysAllocString((OLECHAR *) utf16IName);
147 RTUtf16Free(utf16IName);
148 }
149 RTStrFree(utf8IName);
150 }
151 }
152 }
153
154#endif /* !defined (VBOX_WITH_XPCOM) */
155}
156
157#ifdef VBOX_WITH_XPCOM
158
159HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
160 const char *serverName,
161 const nsIID &id,
162 void** ppobj)
163{
164 HRESULT rc;
165 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
166 if (SUCCEEDED(rc))
167 {
168 PRUint32 serverID = 0;
169 rc = ipcServ->ResolveClientName(serverName, &serverID);
170 if (SUCCEEDED (rc))
171 {
172 nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
173 if (SUCCEEDED(rc))
174 rc = dconServ->CreateInstance(serverID,
175 clsid,
176 id,
177 ppobj);
178 }
179 }
180 return rc;
181}
182
183HRESULT GlueCreateInstance(const CLSID &clsid,
184 const nsIID &id,
185 void** ppobj)
186{
187 nsCOMPtr<nsIComponentManager> manager;
188 HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
189 if (SUCCEEDED(rc))
190 rc = manager->CreateInstance(clsid,
191 nsnull,
192 id,
193 ppobj);
194 return rc;
195}
196
197#endif // VBOX_WITH_XPCOM
198
199static int composeHomePath(char *aDir, size_t aDirLen,
200 const char *pcszBase)
201{
202 int vrc;
203 if (RTPathStartsWithRoot(pcszBase))
204 vrc = RTStrCopy(aDir, aDirLen, pcszBase);
205 else
206 {
207 /* compose the config directory (full path) */
208 /** @todo r=bird: RTPathUserHome doesn't necessarily return a
209 * full (abs) path like the comment above seems to indicate. */
210 vrc = RTPathUserHome(aDir, aDirLen);
211 if (RT_SUCCESS(vrc))
212 vrc = RTPathAppend(aDir, aDirLen, pcszBase);
213 }
214 return vrc;
215}
216
217int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen, bool fCreateDir)
218{
219 AssertReturn(aDir, VERR_INVALID_POINTER);
220 AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
221
222 /* start with null */
223 *aDir = 0;
224
225 char szTmp[RTPATH_MAX];
226 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
227 if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
228 {
229 bool fFound = false;
230 if (RT_SUCCESS(vrc))
231 {
232 /* get the full path name */
233 vrc = RTPathAbs(szTmp, aDir, aDirLen);
234 }
235 else
236 {
237#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
238 const char *pcszConfigHome = RTEnvGet("XDG_CONFIG_HOME");
239 if (pcszConfigHome && pcszConfigHome[0])
240 {
241 vrc = RTStrCopy(szXdgConfigHome,
242 sizeof(szXdgConfigHome),
243 pcszConfigHome);
244 if (RT_SUCCESS(vrc))
245 vrc = RTPathAppend(szXdgConfigHome,
246 sizeof(szXdgConfigHome),
247 "VirtualBox");
248 }
249 else
250 vrc = RTStrCopy(szXdgConfigHome,
251 sizeof(szXdgConfigHome),
252 ".config/VirtualBox");
253#endif
254 for (unsigned i = 0; i < RT_ELEMENTS(apcszUserHome); ++i)
255 {
256 vrc = composeHomePath(aDir, aDirLen, apcszUserHome[i]);
257 if ( RT_SUCCESS(vrc)
258 && RTDirExists(aDir))
259 {
260 fFound = true;
261 break;
262 }
263 }
264 }
265
266 /* ensure the home directory exists */
267 if (RT_SUCCESS(vrc))
268 if (!fFound && fCreateDir)
269 vrc = RTDirCreateFullPath(aDir, 0700);
270 }
271
272 return vrc;
273}
274
275static const char *g_pszLogEntity = NULL;
276
277static void vboxHeaderFooter(PRTLOGGER pReleaseLogger, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
278{
279 /* some introductory information */
280 static RTTIMESPEC s_TimeSpec;
281 char szTmp[256];
282 if (enmPhase == RTLOGPHASE_BEGIN)
283 RTTimeNow(&s_TimeSpec);
284 RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
285
286 switch (enmPhase)
287 {
288 case RTLOGPHASE_BEGIN:
289 {
290 bool fOldBuffered = RTLogSetBuffering(pReleaseLogger, true /*fBuffered*/);
291 pfnLog(pReleaseLogger,
292 "VirtualBox %s %s r%u %s (%s %s) release log\n"
293#ifdef VBOX_BLEEDING_EDGE
294 "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
295#endif
296 "Log opened %s\n",
297 g_pszLogEntity, VBOX_VERSION_STRING, RTBldCfgRevision(),
298 RTBldCfgTargetDotArch(), __DATE__, __TIME__, szTmp);
299
300 pfnLog(pReleaseLogger, "Build Type: %s\n", KBUILD_TYPE);
301 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
302 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
303 pfnLog(pReleaseLogger, "OS Product: %s\n", szTmp);
304 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
305 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
306 pfnLog(pReleaseLogger, "OS Release: %s\n", szTmp);
307 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
308 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
309 pfnLog(pReleaseLogger, "OS Version: %s\n", szTmp);
310 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
311 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
312 pfnLog(pReleaseLogger, "OS Service Pack: %s\n", szTmp);
313
314 vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szTmp, sizeof(szTmp));
315 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
316 pfnLog(pReleaseLogger, "DMI Product Name: %s\n", szTmp);
317 vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_VERSION, szTmp, sizeof(szTmp));
318 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
319 pfnLog(pReleaseLogger, "DMI Product Version: %s\n", szTmp);
320
321 uint64_t cbHostRam = 0, cbHostRamAvail = 0;
322 vrc = RTSystemQueryTotalRam(&cbHostRam);
323 if (RT_SUCCESS(vrc))
324 vrc = RTSystemQueryAvailableRam(&cbHostRamAvail);
325 if (RT_SUCCESS(vrc))
326 pfnLog(pReleaseLogger, "Host RAM: %lluMB total, %lluMB available\n",
327 cbHostRam / _1M, cbHostRamAvail / _1M);
328
329 /* the package type is interesting for Linux distributions */
330 char szExecName[RTPATH_MAX];
331 char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
332 pfnLog(pReleaseLogger,
333 "Executable: %s\n"
334 "Process ID: %u\n"
335 "Package type: %s"
336#ifdef VBOX_OSE
337 " (OSE)"
338#endif
339 "\n",
340 pszExecName ? pszExecName : "unknown",
341 RTProcSelf(),
342 VBOX_PACKAGE_STRING);
343 RTLogSetBuffering(pReleaseLogger, fOldBuffered);
344 break;
345 }
346 case RTLOGPHASE_PREROTATE:
347 pfnLog(pReleaseLogger, "Log rotated - Log started %s\n", szTmp);
348 break;
349
350 case RTLOGPHASE_POSTROTATE:
351 pfnLog(pReleaseLogger, "Log continuation - Log started %s\n", szTmp);
352 break;
353
354 case RTLOGPHASE_END:
355 pfnLog(pReleaseLogger, "End of log file - Log started %s\n", szTmp);
356 break;
357
358 default:
359 /* nothing */;
360 }
361}
362
363int VBoxLogRelCreate(const char *pcszEntity, const char *pcszLogFile,
364 uint32_t fFlags, const char *pcszGroupSettings,
365 const char *pcszEnvVarBase, uint32_t fDestFlags,
366 uint32_t cMaxEntriesPerGroup, uint32_t cHistory,
367 uint32_t uHistoryFileTime, uint64_t uHistoryFileSize,
368 char *pszError, size_t cbError)
369{
370 Assert(cbError >= RTPATH_MAX + 128);
371
372 /* create release logger */
373 PRTLOGGER pReleaseLogger;
374 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
375#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
376 fFlags |= RTLOGFLAGS_USECRLF;
377#endif
378 g_pszLogEntity = pcszEntity;
379 int vrc = RTLogCreateEx(&pReleaseLogger, fFlags, pcszGroupSettings,
380 pcszEnvVarBase, RT_ELEMENTS(s_apszGroups), s_apszGroups, fDestFlags,
381 vboxHeaderFooter, cHistory, uHistoryFileSize, uHistoryFileTime,
382 pszError, cbError,
383 pcszLogFile ? "%s" : NULL, pcszLogFile);
384 if (RT_SUCCESS(vrc))
385 {
386 /* make sure that we don't flood logfiles */
387 RTLogSetGroupLimit(pReleaseLogger, cMaxEntriesPerGroup);
388
389 /* explicitly flush the log, to have some info when buffering */
390 RTLogFlush(pReleaseLogger);
391
392 /* register this logger as the release logger */
393 RTLogRelSetDefaultInstance(pReleaseLogger);
394 }
395 return vrc;
396}
397
398
399/* static */
400const Guid Guid::Empty; /* default ctor is OK */
401
402#if defined (VBOX_WITH_XPCOM)
403
404/* static */
405const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
406
407#endif /* (VBOX_WITH_XPCOM) */
408
409/**
410 * Used by ComPtr and friends to log details about reference counting.
411 * @param pcszFormat
412 */
413void LogRef(const char *pcszFormat, ...)
414{
415 char *pszNewMsg;
416 va_list args;
417 va_start(args, pcszFormat);
418 RTStrAPrintfV(&pszNewMsg, pcszFormat, args);
419 LogDJ((pszNewMsg));
420 RTStrFree(pszNewMsg);
421 va_end(args);
422}
423
424} /* 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