VirtualBox

Changeset 25472 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Dec 17, 2009 9:48:33 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
56138
Message:

FreeBSD: Use sysctl instead of procfs to retrieve executable path and arguments. Contributed by Bernhard Froehlich and Baptiste Daroussin

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/VBox/log-vbox.cpp

    r22479 r25472  
    135135#  include <Windows.h>
    136136# elif defined(RT_OS_LINUX)
     137#  include <unistd.h>
     138# elif defined(RT_OS_FREEBSD)
     139#  include <sys/param.h>
     140#  include <sys/sysctl.h>
     141#  include <sys/user.h>
     142#  include <stdlib.h>
    137143#  include <unistd.h>
    138144# elif defined(RT_OS_SOLARIS)
     
    158164# include <iprt/process.h>
    159165# include <iprt/string.h>
     166# include <iprt/mem.h>
    160167# include <stdio.h>
    161168#endif
     
    340347        }
    341348
    342 #  elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
    343 #   ifdef RT_OS_LINUX
     349#  elif defined(RT_OS_LINUX)
    344350        FILE *pFile = fopen("/proc/self/cmdline", "r");
    345 #   else /* RT_OS_FREEBSD: */
    346         FILE *pFile = fopen("/proc/curproc/cmdline", "r");
    347 #   endif
    348351        if (pFile)
    349352        {
     
    371374            fclose(pFile);
    372375        }
    373 
     376#  elif defined(RT_OS_FREEBSD)
     377        char *pszArgFileBuf = NULL;
     378        int aiName[4];
     379        size_t cchArgs;
     380
     381        aiName[0] = CTL_KERN;
     382        aiName[1] = KERN_PROC;
     383        aiName[2] = KERN_PROC_ARGS;
     384        aiName[3] = -1;
     385
     386        /* Retrieve the required length first */
     387        cchArgs = 0;
     388        int rcBSD = sysctl(aiName, RT_ELEMENTS(aiName), NULL, &cchArgs, NULL, 0);
     389
     390        if (cchArgs > 0)
     391        {
     392            pszArgFileBuf = (char *)RTMemAllocZ(cchArgs + 1 /* Safety */);
     393            if (pszArgFileBuf)
     394            {
     395                /* Retrieve the argument list */
     396                rcBSD = sysctl(aiName, RT_ELEMENTS(aiName), NULL, &cchArgs, NULL, 0);
     397                if (!rcBSD)
     398                {
     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++)
     404                    {
     405                       if(pszArgFileBuf[i] == '\0')
     406                          pszArgFileBuf[i] = ' ';
     407                    }
     408
     409                    RTLogLoggerEx(pLogger, 0, ~0U, "Commandline: %s\n", pszArgFileBuf);
     410                }
     411                RTMemFree(pszArgFileBuf);
     412            }
     413        }
    374414#  elif defined(RT_OS_L4) || defined(RT_OS_OS2) || defined(RT_OS_DARWIN)
    375415        /* commandline? */
  • trunk/src/VBox/Runtime/r3/freebsd/rtProcInitExePath-freebsd.cpp

    r16024 r25472  
    3333*******************************************************************************/
    3434#define LOG_GROUP RTLOGGROUP_PROCESS
     35#include <sys/param.h>
     36#include <sys/sysctl.h>
    3537#include <unistd.h>
    3638#include <errno.h>
     
    4850DECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
    4951{
    50     /*
    51      * Read the /proc/curproc/file link, convert to native and return it.
    52      */
    53     int cchLink = readlink("/proc/curproc/file", pszPath, cchPath - 1);
    54     if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
     52    int aiName[4];
     53    size_t cchExePath;
     54
     55    aiName[0] = CTL_KERN;
     56    aiName[1] = KERN_PROC;
     57    aiName[2] = KERN_PROC_PATHNAME;
     58    aiName[3] = getpid();
     59
     60    cchExePath = cchPath - 1;
     61    if(sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
    5562    {
    56         pszPath[cchLink] = '\0';
    57 
    5863        char *pszTmp = NULL;
    5964        int rc = rtPathFromNative(&pszTmp, pszPath);
    60         AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchLink, pszPath), rc);
     65        AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchExePath, pszPath), rc);
    6166
    6267        size_t cch = strlen(pszTmp);
     
    6974    }
    7075
    71     int err = errno;
    72 
    73     /*
    74      * Fall back on the dynamic linker since /proc is optional.
    75      */
    76     void *hExe = dlopen(NULL, 0);
    77     if (hExe)
    78     {
    79         struct link_map const *pLinkMap = 0;
    80         if (dlinfo(hExe, RTLD_DI_LINKMAP, &pLinkMap) == 0)
    81         {
    82             const char *pszImageName = pLinkMap->l_name;
    83             if (*pszImageName == '/') /* this may not always be absolute, despite the docs. :-( */
    84             {
    85                 char *pszTmp = NULL;
    86                 int rc = rtPathFromNative(&pszTmp, pszImageName);
    87                 AssertMsgRCReturn(rc, ("rc=%Rrc pszImageName=\"%s\"\n", rc, pszImageName), rc);
    88 
    89                 size_t cch = strlen(pszTmp);
    90                 AssertReturn(cch <= cchPath, VERR_BUFFER_OVERFLOW);
    91 
    92                 memcpy(pszPath, pszTmp, cch + 1);
    93                 RTStrFree(pszTmp);
    94 
    95                 return VINF_SUCCESS;
    96             }
    97             /** @todo Try search the PATH for the file name or append the current
    98              *        directory, which ever makes sense... */
    99         }
    100     }
    101 
    102     int rc = RTErrConvertFromErrno(err);
    103     AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d hExe=%p\n", rc, err, cchLink, hExe));
     76    int rc = RTErrConvertFromErrno(errno);
     77    AssertMsgFailed(("rc=%Rrc errno=%d cchLink=%d\n", rc, errno, cchExePath));
    10478    return rc;
    10579}
Note: See TracChangeset for help on using the changeset viewer.

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