VirtualBox

Changeset 68538 in vbox for trunk


Ignore:
Timestamp:
Aug 29, 2017 2:07:06 PM (7 years ago)
Author:
vboxsync
Message:

Main/glue: Split out non-COM bits from com.cpp so they can be used without getting any COM deps. Eventually we should probably move these to VBoxRT or VBoxDDU or somewhere like that.

Location:
trunk/src/VBox/Main/glue
Files:
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/GetVBoxUserHomeDirectory.cpp

    r68520 r68538  
    11/* $Id$ */
    22/** @file
    3  * MS COM / XPCOM Abstraction Layer
     3 * MS COM / XPCOM Abstraction Layer - GetVBoxUserHomeDirectory.
    44 */
    55
     
    1616 */
    1717
    18 #if !defined (VBOX_WITH_XPCOM)
     18/*********************************************************************************************************************************
     19*   Header Files                                                                                                                 *
     20*********************************************************************************************************************************/
     21#define LOG_GROUP LOG_GROUP_MAIN
     22#include <VBox/com/com.h>
    1923
    20 # include <iprt/win/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>
     24#include <iprt/env.h>
     25#include <iprt/dir.h>
    4626#include <iprt/param.h>
    4727#include <iprt/path.h>
    48 #include <iprt/dir.h>
    49 #include <iprt/env.h>
    5028#include <iprt/string.h>
    51 #include <iprt/system.h>
    52 #include <iprt/process.h>
    5329
    5430#include <VBox/err.h>
    55 #include <VBox/version.h>
     31#include <VBox/log.h>
    5632
     33
     34
     35/*********************************************************************************************************************************
     36*   Global Variables                                                                                                             *
     37*********************************************************************************************************************************/
    5738#if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS)
    5839char g_szXdgConfigHome[RTPATH_MAX] = "";
     
    6849 */
    6950const char * const g_apcszUserHome[] =
     51{
    7052#ifdef RT_OS_DARWIN
    71 { "Library/VirtualBox" };
     53    "Library/VirtualBox",
    7254#elif defined RT_OS_WINDOWS
    73 { ".VirtualBox" };
     55    ".VirtualBox",
    7456#else
    75 { ".VirtualBox", g_szXdgConfigHome };
     57    ".VirtualBox", g_szXdgConfigHome,
    7658#endif
     59};
    7760
    78 #include "Logging.h"
    7961
    8062namespace com
    8163{
    8264
    83 void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
    84 {
    85     AssertPtrReturnVoid(aName);
    86     *aName = NULL;
    87 
    88 #if !defined(VBOX_WITH_XPCOM)
    89 
    90     LONG rc;
    91     LPOLESTR iidStr = NULL;
    92     if (StringFromIID(aIID, &iidStr) == S_OK)
    93     {
    94         HKEY ifaceKey;
    95         rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
    96                            0, KEY_QUERY_VALUE, &ifaceKey);
    97         if (rc == ERROR_SUCCESS)
    98         {
    99             HKEY iidKey;
    100             rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
    101             if (rc == ERROR_SUCCESS)
    102             {
    103                 /* determine the size and type */
    104                 DWORD sz, type;
    105                 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
    106                 if (rc == ERROR_SUCCESS && type == REG_SZ)
    107                 {
    108                     /* query the value to BSTR */
    109                     *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
    110                     rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
    111                     if (rc != ERROR_SUCCESS)
    112                     {
    113                         SysFreeString(*aName);
    114                         *aName = NULL;
    115                     }
    116                 }
    117                 RegCloseKey(iidKey);
    118             }
    119             RegCloseKey(ifaceKey);
    120         }
    121         CoTaskMemFree(iidStr);
    122     }
    123 
    124 #else /* !defined (VBOX_WITH_XPCOM) */
    125 
    126     nsresult rv;
    127     nsCOMPtr<nsIInterfaceInfoManager> iim =
    128         do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
    129     if (NS_SUCCEEDED(rv))
    130     {
    131         nsCOMPtr<nsIInterfaceInfo> iinfo;
    132         rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
    133         if (NS_SUCCEEDED(rv))
    134         {
    135             const char *iname = NULL;
    136             iinfo->GetNameShared(&iname);
    137             char *utf8IName = NULL;
    138             if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
    139             {
    140                 PRTUTF16 utf16IName = NULL;
    141                 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
    142                 {
    143                     *aName = SysAllocString((OLECHAR *) utf16IName);
    144                     RTUtf16Free(utf16IName);
    145                 }
    146                 RTStrFree(utf8IName);
    147             }
    148         }
    149     }
    150 
    151 #endif /* !defined (VBOX_WITH_XPCOM) */
    152 }
    153 
    154 #ifdef VBOX_WITH_XPCOM
    155 
    156 HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
    157                                  const char *serverName,
    158                                  const nsIID &id,
    159                                  void** ppobj)
    160 {
    161     HRESULT rc;
    162     nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
    163     if (SUCCEEDED(rc))
    164     {
    165         PRUint32 serverID = 0;
    166         rc = ipcServ->ResolveClientName(serverName, &serverID);
    167         if (SUCCEEDED (rc))
    168         {
    169             nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
    170             if (SUCCEEDED(rc))
    171                 rc = dconServ->CreateInstance(serverID,
    172                                               clsid,
    173                                               id,
    174                                               ppobj);
    175         }
    176     }
    177     return rc;
    178 }
    179 
    180 HRESULT GlueCreateInstance(const CLSID &clsid,
    181                            const nsIID &id,
    182                            void** ppobj)
    183 {
    184     nsCOMPtr<nsIComponentManager> manager;
    185     HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
    186     if (SUCCEEDED(rc))
    187         rc = manager->CreateInstance(clsid,
    188                                      nsnull,
    189                                      id,
    190                                      ppobj);
    191     return rc;
    192 }
    193 
    194 #endif // VBOX_WITH_XPCOM
    195 
    196 static int composeHomePath(char *aDir, size_t aDirLen,
    197                            const char *pcszBase)
     65static int composeHomePath(char *aDir, size_t aDirLen, const char *pcszBase)
    19866{
    19967    int vrc;
     
    261129}
    262130
    263 static const char *g_pszLogEntity = NULL;
    264 
    265 static DECLCALLBACK(void) vboxHeaderFooter(PRTLOGGER pReleaseLogger, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
    266 {
    267     /* some introductory information */
    268     static RTTIMESPEC s_TimeSpec;
    269     char szTmp[256];
    270     if (enmPhase == RTLOGPHASE_BEGIN)
    271         RTTimeNow(&s_TimeSpec);
    272     RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
    273 
    274     switch (enmPhase)
    275     {
    276         case RTLOGPHASE_BEGIN:
    277         {
    278             bool fOldBuffered = RTLogSetBuffering(pReleaseLogger, true /*fBuffered*/);
    279             pfnLog(pReleaseLogger,
    280                    "VirtualBox %s %s r%u %s (%s %s) release log\n"
    281 #ifdef VBOX_BLEEDING_EDGE
    282                    "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
    283 #endif
    284                    "Log opened %s\n",
    285                    g_pszLogEntity, VBOX_VERSION_STRING, RTBldCfgRevision(),
    286                    RTBldCfgTargetDotArch(), __DATE__, __TIME__, szTmp);
    287 
    288             pfnLog(pReleaseLogger, "Build Type: %s\n", KBUILD_TYPE);
    289             int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
    290             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    291                 pfnLog(pReleaseLogger, "OS Product: %s\n", szTmp);
    292             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
    293             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    294                 pfnLog(pReleaseLogger, "OS Release: %s\n", szTmp);
    295             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
    296             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    297                 pfnLog(pReleaseLogger, "OS Version: %s\n", szTmp);
    298             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
    299             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    300                 pfnLog(pReleaseLogger, "OS Service Pack: %s\n", szTmp);
    301 
    302             vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szTmp, sizeof(szTmp));
    303             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    304                 pfnLog(pReleaseLogger, "DMI Product Name: %s\n", szTmp);
    305             vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_VERSION, szTmp, sizeof(szTmp));
    306             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    307                 pfnLog(pReleaseLogger, "DMI Product Version: %s\n", szTmp);
    308 
    309             uint64_t cbHostRam = 0, cbHostRamAvail = 0;
    310             vrc = RTSystemQueryTotalRam(&cbHostRam);
    311             if (RT_SUCCESS(vrc))
    312                 vrc = RTSystemQueryAvailableRam(&cbHostRamAvail);
    313             if (RT_SUCCESS(vrc))
    314             {
    315                 pfnLog(pReleaseLogger, "Host RAM: %lluMB", cbHostRam / _1M);
    316                 if (cbHostRam > _2G)
    317                     pfnLog(pReleaseLogger, " (%lld.%lldGB)",
    318                            cbHostRam / _1G, (cbHostRam % _1G) / (_1G / 10));
    319                 pfnLog(pReleaseLogger, " total, %lluMB", cbHostRamAvail / _1M);
    320                 if (cbHostRamAvail > _2G)
    321                     pfnLog(pReleaseLogger, " (%lld.%lldGB)",
    322                            cbHostRamAvail / _1G, (cbHostRamAvail % _1G) / (_1G / 10));
    323                 pfnLog(pReleaseLogger, " available\n");
    324             }
    325 
    326             /* the package type is interesting for Linux distributions */
    327             char szExecName[RTPATH_MAX];
    328             char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
    329             pfnLog(pReleaseLogger,
    330                    "Executable: %s\n"
    331                    "Process ID: %u\n"
    332                    "Package type: %s"
    333 #ifdef VBOX_OSE
    334                    " (OSE)"
    335 #endif
    336                    "\n",
    337                    pszExecName ? pszExecName : "unknown",
    338                    RTProcSelf(),
    339                    VBOX_PACKAGE_STRING);
    340             RTLogSetBuffering(pReleaseLogger, fOldBuffered);
    341             break;
    342         }
    343         case RTLOGPHASE_PREROTATE:
    344             pfnLog(pReleaseLogger, "Log rotated - Log started %s\n", szTmp);
    345             break;
    346 
    347         case RTLOGPHASE_POSTROTATE:
    348             pfnLog(pReleaseLogger, "Log continuation - Log started %s\n", szTmp);
    349             break;
    350 
    351         case RTLOGPHASE_END:
    352             pfnLog(pReleaseLogger, "End of log file - Log started %s\n", szTmp);
    353             break;
    354 
    355         default:
    356             /* nothing */;
    357     }
    358 }
    359 
    360 int VBoxLogRelCreate(const char *pcszEntity, const char *pcszLogFile,
    361                      uint32_t fFlags, const char *pcszGroupSettings,
    362                      const char *pcszEnvVarBase, uint32_t fDestFlags,
    363                      uint32_t cMaxEntriesPerGroup, uint32_t cHistory,
    364                      uint32_t uHistoryFileTime, uint64_t uHistoryFileSize,
    365                      char *pszError, size_t cbError)
    366 {
    367     Assert(cbError >= RTPATH_MAX + 128);
    368 
    369     /* create release logger */
    370     PRTLOGGER pReleaseLogger;
    371     static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
    372 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
    373     fFlags |= RTLOGFLAGS_USECRLF;
    374 #endif
    375     g_pszLogEntity = pcszEntity;
    376     int vrc = RTLogCreateEx(&pReleaseLogger, fFlags, pcszGroupSettings,
    377                             pcszEnvVarBase, RT_ELEMENTS(s_apszGroups), s_apszGroups, fDestFlags,
    378                             vboxHeaderFooter, cHistory, uHistoryFileSize, uHistoryFileTime,
    379                             pszError, cbError,
    380                             pcszLogFile ? "%s" : NULL, pcszLogFile);
    381     if (RT_SUCCESS(vrc))
    382     {
    383         /* make sure that we don't flood logfiles */
    384         RTLogSetGroupLimit(pReleaseLogger, cMaxEntriesPerGroup);
    385 
    386         /* explicitly flush the log, to have some info when buffering */
    387         RTLogFlush(pReleaseLogger);
    388 
    389         /* register this logger as the release logger */
    390         RTLogRelSetDefaultInstance(pReleaseLogger);
    391     }
    392     return vrc;
    393 }
    394 
    395 
    396 /* static */
    397 const Guid Guid::Empty; /* default ctor is OK */
    398 
    399 #if defined (VBOX_WITH_XPCOM)
    400 
    401 /* static */
    402 const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
    403 
    404 #endif /* (VBOX_WITH_XPCOM) */
    405 
    406131} /* namespace com */
  • trunk/src/VBox/Main/glue/VBoxLogRelCreate.cpp

    r68520 r68538  
    11/* $Id$ */
    22/** @file
    3  * MS COM / XPCOM Abstraction Layer
     3 * MS COM / XPCOM Abstraction Layer - VBoxLogRelCreate.
    44 */
    55
     
    1616 */
    1717
    18 #if !defined (VBOX_WITH_XPCOM)
    1918
    20 # include <iprt/win/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>
     19/*********************************************************************************************************************************
     20*   Header Files                                                                                                                 *
     21*********************************************************************************************************************************/
     22#include <VBox/com/com.h>
    4423
    4524#include <iprt/buildconfig.h>
    4625#include <iprt/param.h>
    47 #include <iprt/path.h>
    48 #include <iprt/dir.h>
    49 #include <iprt/env.h>
    5026#include <iprt/string.h>
    5127#include <iprt/system.h>
    5228#include <iprt/process.h>
     29#include <iprt/time.h>
    5330
    5431#include <VBox/err.h>
     32#include <VBox/log.h>
    5533#include <VBox/version.h>
     34#include "package-generated.h"
    5635
    57 #if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS)
    58 char g_szXdgConfigHome[RTPATH_MAX] = "";
    59 #endif
    6036
    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  */
    69 const char * const g_apcszUserHome[] =
    70 #ifdef RT_OS_DARWIN
    71 { "Library/VirtualBox" };
    72 #elif defined RT_OS_WINDOWS
    73 { ".VirtualBox" };
    74 #else
    75 { ".VirtualBox", g_szXdgConfigHome };
    76 #endif
    77 
    78 #include "Logging.h"
    7937
    8038namespace com
    8139{
    82 
    83 void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
    84 {
    85     AssertPtrReturnVoid(aName);
    86     *aName = NULL;
    87 
    88 #if !defined(VBOX_WITH_XPCOM)
    89 
    90     LONG rc;
    91     LPOLESTR iidStr = NULL;
    92     if (StringFromIID(aIID, &iidStr) == S_OK)
    93     {
    94         HKEY ifaceKey;
    95         rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
    96                            0, KEY_QUERY_VALUE, &ifaceKey);
    97         if (rc == ERROR_SUCCESS)
    98         {
    99             HKEY iidKey;
    100             rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
    101             if (rc == ERROR_SUCCESS)
    102             {
    103                 /* determine the size and type */
    104                 DWORD sz, type;
    105                 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
    106                 if (rc == ERROR_SUCCESS && type == REG_SZ)
    107                 {
    108                     /* query the value to BSTR */
    109                     *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
    110                     rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
    111                     if (rc != ERROR_SUCCESS)
    112                     {
    113                         SysFreeString(*aName);
    114                         *aName = NULL;
    115                     }
    116                 }
    117                 RegCloseKey(iidKey);
    118             }
    119             RegCloseKey(ifaceKey);
    120         }
    121         CoTaskMemFree(iidStr);
    122     }
    123 
    124 #else /* !defined (VBOX_WITH_XPCOM) */
    125 
    126     nsresult rv;
    127     nsCOMPtr<nsIInterfaceInfoManager> iim =
    128         do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
    129     if (NS_SUCCEEDED(rv))
    130     {
    131         nsCOMPtr<nsIInterfaceInfo> iinfo;
    132         rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
    133         if (NS_SUCCEEDED(rv))
    134         {
    135             const char *iname = NULL;
    136             iinfo->GetNameShared(&iname);
    137             char *utf8IName = NULL;
    138             if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
    139             {
    140                 PRTUTF16 utf16IName = NULL;
    141                 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
    142                 {
    143                     *aName = SysAllocString((OLECHAR *) utf16IName);
    144                     RTUtf16Free(utf16IName);
    145                 }
    146                 RTStrFree(utf8IName);
    147             }
    148         }
    149     }
    150 
    151 #endif /* !defined (VBOX_WITH_XPCOM) */
    152 }
    153 
    154 #ifdef VBOX_WITH_XPCOM
    155 
    156 HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
    157                                  const char *serverName,
    158                                  const nsIID &id,
    159                                  void** ppobj)
    160 {
    161     HRESULT rc;
    162     nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
    163     if (SUCCEEDED(rc))
    164     {
    165         PRUint32 serverID = 0;
    166         rc = ipcServ->ResolveClientName(serverName, &serverID);
    167         if (SUCCEEDED (rc))
    168         {
    169             nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
    170             if (SUCCEEDED(rc))
    171                 rc = dconServ->CreateInstance(serverID,
    172                                               clsid,
    173                                               id,
    174                                               ppobj);
    175         }
    176     }
    177     return rc;
    178 }
    179 
    180 HRESULT GlueCreateInstance(const CLSID &clsid,
    181                            const nsIID &id,
    182                            void** ppobj)
    183 {
    184     nsCOMPtr<nsIComponentManager> manager;
    185     HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
    186     if (SUCCEEDED(rc))
    187         rc = manager->CreateInstance(clsid,
    188                                      nsnull,
    189                                      id,
    190                                      ppobj);
    191     return rc;
    192 }
    193 
    194 #endif // VBOX_WITH_XPCOM
    195 
    196 static int composeHomePath(char *aDir, size_t aDirLen,
    197                            const char *pcszBase)
    198 {
    199     int vrc;
    200     if (RTPathStartsWithRoot(pcszBase))
    201         vrc = RTStrCopy(aDir, aDirLen, pcszBase);
    202     else
    203     {
    204         /* compose the config directory (full path) */
    205         /** @todo r=bird: RTPathUserHome doesn't necessarily return a
    206          * full (abs) path like the comment above seems to indicate. */
    207         vrc = RTPathUserHome(aDir, aDirLen);
    208         if (RT_SUCCESS(vrc))
    209             vrc = RTPathAppend(aDir, aDirLen, pcszBase);
    210     }
    211     return vrc;
    212 }
    213 
    214 int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen, bool fCreateDir)
    215 {
    216     AssertReturn(aDir, VERR_INVALID_POINTER);
    217     AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
    218 
    219     /* start with null */
    220     *aDir = 0;
    221 
    222     char szTmp[RTPATH_MAX];
    223     int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
    224     if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
    225     {
    226         bool fFound = false;
    227         if (RT_SUCCESS(vrc))
    228         {
    229             /* get the full path name */
    230             vrc = RTPathAbs(szTmp, aDir, aDirLen);
    231         }
    232         else
    233         {
    234 #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
    235             vrc = RTEnvGetEx(RTENV_DEFAULT, "XDG_CONFIG_HOME", g_szXdgConfigHome, sizeof(g_szXdgConfigHome), NULL);
    236             if (RT_SUCCESS(vrc))
    237                 vrc = RTPathAppend(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), "VirtualBox");
    238             AssertMsg(vrc == VINF_SUCCESS || vrc == VERR_ENV_VAR_NOT_FOUND, ("%Rrc\n", vrc));
    239             if (RT_FAILURE_NP(vrc))
    240                 vrc = RTStrCopy(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), ".config/VirtualBox");
    241 #endif
    242             for (unsigned i = 0; i < RT_ELEMENTS(g_apcszUserHome); ++i)
    243             {
    244                 vrc = composeHomePath(aDir, aDirLen, g_apcszUserHome[i]);
    245                 if (   RT_SUCCESS(vrc)
    246                     && RTDirExists(aDir))
    247                 {
    248                     fFound = true;
    249                     break;
    250                 }
    251             }
    252         }
    253 
    254         /* ensure the home directory exists */
    255         if (RT_SUCCESS(vrc))
    256             if (!fFound && fCreateDir)
    257                 vrc = RTDirCreateFullPath(aDir, 0700);
    258     }
    259 
    260     return vrc;
    261 }
    26240
    26341static const char *g_pszLogEntity = NULL;
     
    393171}
    394172
    395 
    396 /* static */
    397 const Guid Guid::Empty; /* default ctor is OK */
    398 
    399 #if defined (VBOX_WITH_XPCOM)
    400 
    401 /* static */
    402 const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
    403 
    404 #endif /* (VBOX_WITH_XPCOM) */
    405 
    406173} /* namespace com */
  • trunk/src/VBox/Main/glue/com.cpp

    r68096 r68538  
    1616 */
    1717
    18 #if !defined (VBOX_WITH_XPCOM)
     18
     19/*********************************************************************************************************************************
     20*   Header Files                                                                                                                 *
     21*********************************************************************************************************************************/
     22#define LOG_GROUP LOG_GROUP_MAIN
     23#if !defined(VBOX_WITH_XPCOM)
    1924
    2025# include <iprt/win/objbase.h>
     
    3035# include <nsIInterfaceInfo.h>
    3136# include <nsIInterfaceInfoManager.h>
    32 // official XPCOM headers don't define it yet
    33 #define IPC_DCONNECTSERVICE_CONTRACTID \
    34     "@mozilla.org/ipc/dconnect-service;1"
     37# define IPC_DCONNECTSERVICE_CONTRACTID "@mozilla.org/ipc/dconnect-service;1" // official XPCOM headers don't define it yet
    3538#endif /* !defined (VBOX_WITH_XPCOM) */
    3639
     
    4144#include "VBox/com/array.h"
    4245
    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>
    5046#include <iprt/string.h>
    51 #include <iprt/system.h>
    52 #include <iprt/process.h>
    5347
    5448#include <VBox/err.h>
    55 #include <VBox/version.h>
     49#include <VBox/log.h>
    5650
    57 #if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS)
    58 char g_szXdgConfigHome[RTPATH_MAX] = "";
    59 #endif
    6051
    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  */
    69 const char * const g_apcszUserHome[] =
    70 #ifdef RT_OS_DARWIN
    71 { "Library/VirtualBox" };
    72 #elif defined RT_OS_WINDOWS
    73 { ".VirtualBox" };
    74 #else
    75 { ".VirtualBox", g_szXdgConfigHome };
    76 #endif
    77 
    78 #include "Logging.h"
    79 
     52/*********************************************************************************************************************************
     53*   Global Variables                                                                                                             *
     54*********************************************************************************************************************************/
    8055namespace com
    8156{
     57/* static */
     58const Guid Guid::Empty; /* default ctor is OK */
     59
     60#if defined (VBOX_WITH_XPCOM)
     61
     62/* static */
     63const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
     64
     65#endif /* (VBOX_WITH_XPCOM) */
     66
     67
    8268
    8369void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
     
    194180#endif // VBOX_WITH_XPCOM
    195181
    196 static int composeHomePath(char *aDir, size_t aDirLen,
    197                            const char *pcszBase)
    198 {
    199     int vrc;
    200     if (RTPathStartsWithRoot(pcszBase))
    201         vrc = RTStrCopy(aDir, aDirLen, pcszBase);
    202     else
    203     {
    204         /* compose the config directory (full path) */
    205         /** @todo r=bird: RTPathUserHome doesn't necessarily return a
    206          * full (abs) path like the comment above seems to indicate. */
    207         vrc = RTPathUserHome(aDir, aDirLen);
    208         if (RT_SUCCESS(vrc))
    209             vrc = RTPathAppend(aDir, aDirLen, pcszBase);
    210     }
    211     return vrc;
    212 }
     182} /* namespace com */
    213183
    214 int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen, bool fCreateDir)
    215 {
    216     AssertReturn(aDir, VERR_INVALID_POINTER);
    217     AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
    218 
    219     /* start with null */
    220     *aDir = 0;
    221 
    222     char szTmp[RTPATH_MAX];
    223     int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
    224     if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
    225     {
    226         bool fFound = false;
    227         if (RT_SUCCESS(vrc))
    228         {
    229             /* get the full path name */
    230             vrc = RTPathAbs(szTmp, aDir, aDirLen);
    231         }
    232         else
    233         {
    234 #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
    235             vrc = RTEnvGetEx(RTENV_DEFAULT, "XDG_CONFIG_HOME", g_szXdgConfigHome, sizeof(g_szXdgConfigHome), NULL);
    236             if (RT_SUCCESS(vrc))
    237                 vrc = RTPathAppend(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), "VirtualBox");
    238             AssertMsg(vrc == VINF_SUCCESS || vrc == VERR_ENV_VAR_NOT_FOUND, ("%Rrc\n", vrc));
    239             if (RT_FAILURE_NP(vrc))
    240                 vrc = RTStrCopy(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), ".config/VirtualBox");
    241 #endif
    242             for (unsigned i = 0; i < RT_ELEMENTS(g_apcszUserHome); ++i)
    243             {
    244                 vrc = composeHomePath(aDir, aDirLen, g_apcszUserHome[i]);
    245                 if (   RT_SUCCESS(vrc)
    246                     && RTDirExists(aDir))
    247                 {
    248                     fFound = true;
    249                     break;
    250                 }
    251             }
    252         }
    253 
    254         /* ensure the home directory exists */
    255         if (RT_SUCCESS(vrc))
    256             if (!fFound && fCreateDir)
    257                 vrc = RTDirCreateFullPath(aDir, 0700);
    258     }
    259 
    260     return vrc;
    261 }
    262 
    263 static const char *g_pszLogEntity = NULL;
    264 
    265 static DECLCALLBACK(void) vboxHeaderFooter(PRTLOGGER pReleaseLogger, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
    266 {
    267     /* some introductory information */
    268     static RTTIMESPEC s_TimeSpec;
    269     char szTmp[256];
    270     if (enmPhase == RTLOGPHASE_BEGIN)
    271         RTTimeNow(&s_TimeSpec);
    272     RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
    273 
    274     switch (enmPhase)
    275     {
    276         case RTLOGPHASE_BEGIN:
    277         {
    278             bool fOldBuffered = RTLogSetBuffering(pReleaseLogger, true /*fBuffered*/);
    279             pfnLog(pReleaseLogger,
    280                    "VirtualBox %s %s r%u %s (%s %s) release log\n"
    281 #ifdef VBOX_BLEEDING_EDGE
    282                    "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
    283 #endif
    284                    "Log opened %s\n",
    285                    g_pszLogEntity, VBOX_VERSION_STRING, RTBldCfgRevision(),
    286                    RTBldCfgTargetDotArch(), __DATE__, __TIME__, szTmp);
    287 
    288             pfnLog(pReleaseLogger, "Build Type: %s\n", KBUILD_TYPE);
    289             int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
    290             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    291                 pfnLog(pReleaseLogger, "OS Product: %s\n", szTmp);
    292             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
    293             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    294                 pfnLog(pReleaseLogger, "OS Release: %s\n", szTmp);
    295             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
    296             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    297                 pfnLog(pReleaseLogger, "OS Version: %s\n", szTmp);
    298             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
    299             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    300                 pfnLog(pReleaseLogger, "OS Service Pack: %s\n", szTmp);
    301 
    302             vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szTmp, sizeof(szTmp));
    303             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    304                 pfnLog(pReleaseLogger, "DMI Product Name: %s\n", szTmp);
    305             vrc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_VERSION, szTmp, sizeof(szTmp));
    306             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    307                 pfnLog(pReleaseLogger, "DMI Product Version: %s\n", szTmp);
    308 
    309             uint64_t cbHostRam = 0, cbHostRamAvail = 0;
    310             vrc = RTSystemQueryTotalRam(&cbHostRam);
    311             if (RT_SUCCESS(vrc))
    312                 vrc = RTSystemQueryAvailableRam(&cbHostRamAvail);
    313             if (RT_SUCCESS(vrc))
    314             {
    315                 pfnLog(pReleaseLogger, "Host RAM: %lluMB", cbHostRam / _1M);
    316                 if (cbHostRam > _2G)
    317                     pfnLog(pReleaseLogger, " (%lld.%lldGB)",
    318                            cbHostRam / _1G, (cbHostRam % _1G) / (_1G / 10));
    319                 pfnLog(pReleaseLogger, " total, %lluMB", cbHostRamAvail / _1M);
    320                 if (cbHostRamAvail > _2G)
    321                     pfnLog(pReleaseLogger, " (%lld.%lldGB)",
    322                            cbHostRamAvail / _1G, (cbHostRamAvail % _1G) / (_1G / 10));
    323                 pfnLog(pReleaseLogger, " available\n");
    324             }
    325 
    326             /* the package type is interesting for Linux distributions */
    327             char szExecName[RTPATH_MAX];
    328             char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
    329             pfnLog(pReleaseLogger,
    330                    "Executable: %s\n"
    331                    "Process ID: %u\n"
    332                    "Package type: %s"
    333 #ifdef VBOX_OSE
    334                    " (OSE)"
    335 #endif
    336                    "\n",
    337                    pszExecName ? pszExecName : "unknown",
    338                    RTProcSelf(),
    339                    VBOX_PACKAGE_STRING);
    340             RTLogSetBuffering(pReleaseLogger, fOldBuffered);
    341             break;
    342         }
    343         case RTLOGPHASE_PREROTATE:
    344             pfnLog(pReleaseLogger, "Log rotated - Log started %s\n", szTmp);
    345             break;
    346 
    347         case RTLOGPHASE_POSTROTATE:
    348             pfnLog(pReleaseLogger, "Log continuation - Log started %s\n", szTmp);
    349             break;
    350 
    351         case RTLOGPHASE_END:
    352             pfnLog(pReleaseLogger, "End of log file - Log started %s\n", szTmp);
    353             break;
    354 
    355         default:
    356             /* nothing */;
    357     }
    358 }
    359 
    360 int VBoxLogRelCreate(const char *pcszEntity, const char *pcszLogFile,
    361                      uint32_t fFlags, const char *pcszGroupSettings,
    362                      const char *pcszEnvVarBase, uint32_t fDestFlags,
    363                      uint32_t cMaxEntriesPerGroup, uint32_t cHistory,
    364                      uint32_t uHistoryFileTime, uint64_t uHistoryFileSize,
    365                      char *pszError, size_t cbError)
    366 {
    367     Assert(cbError >= RTPATH_MAX + 128);
    368 
    369     /* create release logger */
    370     PRTLOGGER pReleaseLogger;
    371     static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
    372 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
    373     fFlags |= RTLOGFLAGS_USECRLF;
    374 #endif
    375     g_pszLogEntity = pcszEntity;
    376     int vrc = RTLogCreateEx(&pReleaseLogger, fFlags, pcszGroupSettings,
    377                             pcszEnvVarBase, RT_ELEMENTS(s_apszGroups), s_apszGroups, fDestFlags,
    378                             vboxHeaderFooter, cHistory, uHistoryFileSize, uHistoryFileTime,
    379                             pszError, cbError,
    380                             pcszLogFile ? "%s" : NULL, pcszLogFile);
    381     if (RT_SUCCESS(vrc))
    382     {
    383         /* make sure that we don't flood logfiles */
    384         RTLogSetGroupLimit(pReleaseLogger, cMaxEntriesPerGroup);
    385 
    386         /* explicitly flush the log, to have some info when buffering */
    387         RTLogFlush(pReleaseLogger);
    388 
    389         /* register this logger as the release logger */
    390         RTLogRelSetDefaultInstance(pReleaseLogger);
    391     }
    392     return vrc;
    393 }
    394 
    395 
    396 /* static */
    397 const Guid Guid::Empty; /* default ctor is OK */
    398 
    399 #if defined (VBOX_WITH_XPCOM)
    400 
    401 /* static */
    402 const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
    403 
    404 #endif /* (VBOX_WITH_XPCOM) */
    405 
    406 } /* namespace com */
Note: See TracChangeset for help on using the changeset viewer.

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