VirtualBox

Changeset 25558 in vbox


Ignore:
Timestamp:
Dec 22, 2009 12:45:27 PM (15 years ago)
Author:
vboxsync
Message:

FreeBSD/iprt,suplib: Review and cleanup of r56138. Hope it compiles...

Location:
trunk/src/VBox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp

    r25472 r25558  
    141141#endif
    142142
     143
    143144/*******************************************************************************
    144145*   Internal Functions                                                         *
     
    324325# ifdef RT_OS_LINUX
    325326    int cchLink = readlink("/proc/self/exe", &g_szSupLibHardenedExePath[0], sizeof(g_szSupLibHardenedExePath) - 1);
     327
    326328# elif defined(RT_OS_SOLARIS)
    327329    char szFileBuf[PATH_MAX + 1];
    328330    sprintf(szFileBuf, "/proc/%ld/path/a.out", (long)getpid());
    329331    int cchLink = readlink(szFileBuf, &g_szSupLibHardenedExePath[0], sizeof(g_szSupLibHardenedExePath) - 1);
    330 # else /* RT_OS_FREEBSD: */
     332
     333# else /* RT_OS_FREEBSD */
    331334    int aiName[4];
    332     size_t cbPath;
    333 
    334335    aiName[0] = CTL_KERN;
    335336    aiName[1] = KERN_PROC;
     
    337338    aiName[3] = getpid();
    338339
    339     cbPath = sizeof(g_szSupLibHardenedExePath) - 1;
    340     if(sysctl(aiName, RT_ELEMENTS(aiName), g_szSupLibHardenedExePath, &cbPath, NULL, 0) < 0)
    341        supR3HardenedFatal("supR3HardenedExecDir: sysctl failed\n");
    342 
    343     int cchLink = strlen(g_szSupLibHardenedExePath);
     340    size_t cbPath = sizeof(g_szSupLibHardenedExePath);
     341    if (sysctl(aiName, RT_ELEMENTS(aiName), g_szSupLibHardenedExePath, &cbPath, NULL, 0) < 0)
     342        supR3HardenedFatal("supR3HardenedExecDir: sysctl failed\n");
     343    g_szSupLibHardenedExePath[sizeof(g_szSupLibHardenedExePath) - 1] = '\0';
     344    int cchLink = strlen(g_szSupLibHardenedExePath); /* paranoid? can't we use cbPath? */
     345
    344346# endif
    345347    if (cchLink < 0 || cchLink == sizeof(g_szSupLibHardenedExePath) - 1)
  • trunk/src/VBox/Runtime/VBox/log-vbox.cpp

    r25474 r25558  
    374374            fclose(pFile);
    375375        }
     376
    376377#  elif defined(RT_OS_FREEBSD)
    377         char *pszArgFileBuf = NULL;
     378        /* Retrieve the required length first */
    378379        int aiName[4];
    379         size_t cchArgs;
    380 
    381380        aiName[0] = CTL_KERN;
    382381        aiName[1] = KERN_PROC;
    383         aiName[2] = KERN_PROC_ARGS;
    384         aiName[3] = -1;
    385 
    386         /* Retrieve the required length first */
    387         cchArgs = 0;
     382        aiName[2] = KERN_PROC_ARGS;     /* Introduced in FreeBSD 4.0 */
     383        aiName[3] = getpid();
     384        size_t cchArgs = 0;
    388385        int rcBSD = sysctl(aiName, RT_ELEMENTS(aiName), NULL, &cchArgs, NULL, 0);
    389 
    390386        if (cchArgs > 0)
    391387        {
    392             pszArgFileBuf = (char *)RTMemAllocZ(cchArgs + 1 /* Safety */);
     388            char *pszArgFileBuf = (char *)RTMemAllocZ(cchArgs + 1 /* Safety */);
    393389            if (pszArgFileBuf)
    394390            {
     
    397393                if (!rcBSD)
    398394                {
    399                     /*
    400                      * cmdline is a flattened argument list so we need
    401                      * to convert all \0 to blanks
    402                      */
    403                     for(size_t i = 0; i < cchArgs - 1; i++)
     395                    unsigned    iArg = 0;
     396                    size_t      off = 0;
     397                    while (off < cchArgs)
    404398                    {
    405                        if(pszArgFileBuf[i] == '\0')
    406                           pszArgFileBuf[i] = ' ';
     399                        size_t cchArg = strlen(&pszArgFileBuf[off]);
     400                        RTLogLoggerEx(pLogger, 0, ~0U, "Arg[%u]: %s\n", iArg, &pszArgFileBuf[off]);
     401
     402                        /* advance */
     403                        off += cchArg + 1;
     404                        iArg++;
    407405                    }
    408 
    409                     RTLogLoggerEx(pLogger, 0, ~0U, "Commandline: %s\n", pszArgFileBuf);
    410406                }
    411407                RTMemFree(pszArgFileBuf);
    412408            }
    413409        }
     410
    414411#  elif defined(RT_OS_L4) || defined(RT_OS_OS2) || defined(RT_OS_DARWIN)
    415412        /* commandline? */
  • trunk/src/VBox/Runtime/r3/freebsd/rtProcInitExePath-freebsd.cpp

    r25472 r25558  
    5050DECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
    5151{
     52#ifdef KERN_PROC_PATHNAME
    5253    int aiName[4];
    53     size_t cchExePath;
    54 
    5554    aiName[0] = CTL_KERN;
    5655    aiName[1] = KERN_PROC;
    57     aiName[2] = KERN_PROC_PATHNAME;
    58     aiName[3] = getpid();
     56    aiName[2] = KERN_PROC_PATHNAME;     /* This was introduced in FreeBSD 6.0, thus the #ifdef above. */
     57    aiName[3] = -1;                     /* Shorthand for the current process. */
    5958
    60     cchExePath = cchPath - 1;
    61     if(sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
     59    size_t cchExePath = cchPath;
     60    if (sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
    6261    {
     62
    6363        char *pszTmp = NULL;
    6464        int rc = rtPathFromNative(&pszTmp, pszPath);
    65         AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchExePath, pszPath), rc);
     65        AssertMsgRCReturn(rc, ("rc=%Rrc pszPath=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchExePath, pszPath), rc);
    6666
    6767        size_t cch = strlen(pszTmp);
     
    7575
    7676    int rc = RTErrConvertFromErrno(errno);
    77     AssertMsgFailed(("rc=%Rrc errno=%d cchLink=%d\n", rc, errno, cchExePath));
     77    AssertMsgFailed(("rc=%Rrc errno=%d cchExePath=%d\n", rc, errno, cchExePath));
    7878    return rc;
     79
     80#else
     81
     82    /*
     83     * Read the /proc/curproc/file link, convert to native and return it.
     84     */
     85    int cchLink = readlink("/proc/curproc/file", pszPath, cchPath - 1);
     86    if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
     87    {
     88        pszPath[cchLink] = '\0';
     89
     90        char *pszTmp = NULL;
     91        int rc = rtPathFromNative(&pszTmp, pszPath);
     92        AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchLink, pszPath), rc);
     93
     94        size_t cch = strlen(pszTmp);
     95        AssertReturn(cch <= cchPath, VERR_BUFFER_OVERFLOW);
     96
     97        memcpy(pszPath, pszTmp, cch + 1);
     98        RTStrFree(pszTmp);
     99
     100        return VINF_SUCCESS;
     101    }
     102
     103    int err = errno;
     104
     105    /*
     106     * Fall back on the dynamic linker since /proc is optional.
     107     */
     108    void *hExe = dlopen(NULL, 0);
     109    if (hExe)
     110    {
     111        struct link_map const *pLinkMap = 0;
     112        if (dlinfo(hExe, RTLD_DI_LINKMAP, &pLinkMap) == 0)
     113        {
     114            const char *pszImageName = pLinkMap->l_name;
     115            if (*pszImageName == '/') /* this may not always be absolute, despite the docs. :-( */
     116            {
     117                char *pszTmp = NULL;
     118                int rc = rtPathFromNative(&pszTmp, pszImageName);
     119                AssertMsgRCReturn(rc, ("rc=%Rrc pszImageName=\"%s\"\n", rc, pszImageName), rc);
     120
     121                size_t cch = strlen(pszTmp);
     122                AssertReturn(cch <= cchPath, VERR_BUFFER_OVERFLOW);
     123
     124                memcpy(pszPath, pszTmp, cch + 1);
     125                RTStrFree(pszTmp);
     126
     127                return VINF_SUCCESS;
     128            }
     129            /** @todo Try search the PATH for the file name or append the current
     130             *        directory, which ever makes sense... */
     131        }
     132    }
     133
     134    int rc = RTErrConvertFromErrno(err);
     135    AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d hExe=%p\n", rc, err, cchLink, hExe));
     136    return rc;
     137#endif
    79138}
    80139
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