VirtualBox

Changeset 11836 in vbox for trunk/src/VBox/Runtime/r3/posix


Ignore:
Timestamp:
Aug 29, 2008 4:52:20 PM (16 years ago)
Author:
vboxsync
Message:

IPRT: Implemented RTR3Init*WithProgramPath. Added RTPathParse. Cleaned up the RTPathProgram and RTProcGetExecutableName implementations.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/path-posix.cpp

    r11468 r11836  
    4343#include <sys/types.h>
    4444#include <pwd.h>
    45 #ifdef RT_OS_DARWIN
    46 # include <mach-o/dyld.h>
    47 #endif
    4845
    4946#include <iprt/path.h>
     
    5350#include <iprt/log.h>
    5451#include "internal/path.h"
     52#include "internal/process.h"
    5553#include "internal/fs.h"
    5654
     
    402400    return rc;
    403401}
    404 
    405 
    406 #ifndef RT_MINI
    407 RTDECL(int) RTPathProgram(char *pszPath, unsigned cchPath)
    408 {
    409     /*
    410      * First time only.
    411      */
    412     if (!g_szrtProgramPath[0])
    413     {     
    414         char* envPath = getenv("VBOX_PROGRAM_PATH");
    415         if (envPath)
    416         {
    417           strncpy(g_szrtProgramPath, envPath, sizeof(g_szrtProgramPath));
    418         } else {
    419         /*
    420          * Linux have no API for obtaining the executable path, but provides a symbolic link
    421          * in the proc file system. Note that readlink is one of the weirdest Unix apis around.
    422          *
    423          * OS/2 have an api for getting the program file name.
    424          */
    425 /** @todo use RTProcGetExecutableName() */
    426 #if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
    427 # ifdef RT_OS_LINUX
    428         int cchLink = readlink("/proc/self/exe", &g_szrtProgramPath[0], sizeof(g_szrtProgramPath) - 1);
    429 # elif defined(RT_OS_SOLARIS)
    430         char szFileBuf[PATH_MAX + 1];
    431         sprintf(szFileBuf, "/proc/%ld/path/a.out", (long)getpid());
    432         int cchLink = readlink(szFileBuf, &g_szrtProgramPath[0], sizeof(g_szrtProgramPath) - 1);
    433 # else /* RT_OS_FREEBSD: */
    434         int cchLink = readlink("/proc/curproc/file", &g_szrtProgramPath[0], sizeof(g_szrtProgramPath) - 1);
    435 # endif
    436         if (cchLink < 0 || cchLink == sizeof(g_szrtProgramPath) - 1)
    437         {
    438             int rc = RTErrConvertFromErrno(errno);
    439             AssertMsgFailed(("couldn't read /proc/self/exe. errno=%d cchLink=%d\n", errno, cchLink));
    440             LogFlow(("RTPathProgram(%p, %u): returns %Rrc\n", pszPath, cchPath, rc));
    441             return rc;
    442         }
    443         g_szrtProgramPath[cchLink] = '\0';
    444 
    445 #elif defined(RT_OS_OS2) || defined(RT_OS_L4)
    446         _execname(g_szrtProgramPath, sizeof(g_szrtProgramPath));
    447 
    448 #elif defined(RT_OS_DARWIN)
    449         const char *pszImageName = _dyld_get_image_name(0);
    450         AssertReturn(pszImageName, VERR_INTERNAL_ERROR);
    451         size_t cchImageName = strlen(pszImageName);
    452         if (cchImageName >= sizeof(g_szrtProgramPath))
    453             AssertReturn(pszImageName, VERR_INTERNAL_ERROR);
    454         memcpy(g_szrtProgramPath, pszImageName, cchImageName + 1);
    455 
    456 #else
    457 # error needs porting.
    458 #endif
    459         }
    460         /*
    461          * Convert to UTF-8 and strip of the filename.
    462          */
    463         char *pszTmp = NULL;
    464         int rc = rtPathFromNative(&pszTmp, &g_szrtProgramPath[0]);
    465         if (RT_FAILURE(rc))
    466         {
    467             LogFlow(("RTPathProgram(%p, %u): returns %Rrc\n", pszPath, cchPath, rc));
    468             return rc;
    469         }
    470         size_t cch = strlen(pszTmp);
    471         if (cch >= sizeof(g_szrtProgramPath))
    472         {
    473             RTStrFree(pszTmp);
    474             LogFlow(("RTPathProgram(%p, %u): returns %Rrc\n", pszPath, cchPath, VERR_BUFFER_OVERFLOW));
    475             return VERR_BUFFER_OVERFLOW;
    476         }
    477         memcpy(g_szrtProgramPath, pszTmp, cch + 1);
    478         RTPathStripFilename(g_szrtProgramPath);
    479         RTStrFree(pszTmp);
    480     }
    481 
    482     /*
    483      * Calc the length and check if there is space before copying.
    484      */
    485     unsigned cch = strlen(g_szrtProgramPath) + 1;
    486     if (cch <= cchPath)
    487     {
    488         memcpy(pszPath, g_szrtProgramPath, cch + 1);
    489         LogFlow(("RTPathProgram(%p:{%s}, %u): returns %Rrc\n", pszPath, pszPath, cchPath, VINF_SUCCESS));
    490         return VINF_SUCCESS;
    491     }
    492 
    493     AssertMsgFailed(("Buffer too small (%d < %d)\n", cchPath, cch));
    494     LogFlow(("RTPathProgram(%p, %u): returns %Rrc\n", pszPath, cchPath, VERR_BUFFER_OVERFLOW));
    495     return VERR_BUFFER_OVERFLOW;
    496 }
    497 #endif /* !RT_MINI */
    498402
    499403
  • trunk/src/VBox/Runtime/r3/posix/process-posix.cpp

    r11337 r11836  
    235235
    236236
    237 RTR3DECL(char *) RTProcGetExecutableName(char *pszExecName, size_t cchExecName)
    238 {
    239     /*
    240      * I don't think there is a posix API for this, but
    241      * because I'm lazy I'm not creating OS specific code
    242      * files and code for this.
    243      */
    244 #if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
    245 # ifdef RT_OS_LINUX
    246     int cchLink = readlink("/proc/self/exe", pszExecName, cchExecName - 1);
    247 # elif defined(RT_OS_SOLARIS)
    248     char szFileBuf[80];
    249     RTStrPrintf(szFileBuf, sizeof(szFileBuf), "/proc/%ld/path/a.out", (long)getpid());
    250     int cchLink = readlink(szFileBuf, pszExecName, cchExecName - 1);
    251 # else
    252     int cchLink = readlink("/proc/curproc/file", pszExecName, cchExecName - 1);
    253 # endif
    254     if (cchLink > 0 && (size_t)cchLink <= cchExecName - 1)
    255     {
    256         pszExecName[cchLink] = '\0';
    257         return pszExecName;
    258     }
    259 
    260 #elif defined(RT_OS_OS2) || defined(RT_OS_L4)
    261     if (!_execname(pszExecName, cchExecName))
    262         return pszExecName;
    263 
    264 #elif defined(RT_OS_DARWIN)
    265     const char *pszImageName = _dyld_get_image_name(0);
    266     if (pszImageName)
    267     {
    268         size_t cchImageName = strlen(pszImageName);
    269         if (cchImageName < cchExecName)
    270             return (char *)memcpy(pszExecName, pszImageName, cchImageName + 1);
    271     }
    272 
    273 #else
    274 #   error "Port me!"
    275 #endif
    276     return NULL;
    277 }
    278 
    279237/**
    280238 * Daemonize the current process, making it a background process. The current
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