VirtualBox

Changeset 18320 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Mar 26, 2009 2:53:12 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
45092
Message:

Additions/common/VbglR3Lib: separate pidfile and daemonising code. The pidfile code has not yet been tested

Location:
trunk/src/VBox/Additions
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibDaemonize.cpp

    r11179 r18320  
    5151#include <iprt/string.h>
    5252#include <iprt/file.h>
     53#include <iprt/process.h>
    5354#include <VBox/VBoxGuest.h>
    5455
     
    6364 * @param   fNoChDir    Pass false to change working directory to root.
    6465 * @param   fNoClose    Pass false to redirect standard file streams to /dev/null.
    65  * @param   pszPidfile  Path to a file to write the pid of the daemon process
    66  *                      to.  Daemonising will fail if this file already exists
    67  *                      or cannot be written.  Optional.
    68  */
    69 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose,
    70                                 char const *pszPidfile)
     66 */
     67VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
    7168{
    7269#if defined(RT_OS_DARWIN)
     
    7471
    7572#elif defined(RT_OS_OS2)
    76     /** @todo create a pidfile if this is (/was :) ) usual on OS/2 */
    77     NOREF(pszPidfile);
    7873    PPIB pPib;
    7974    PTIB pTib;
     
    150145     */
    151146
    152     /* We start off by opening the pidfile, so that we can fail straight away
    153      * if it already exists. */
    154     RTFILE hPidfile = NIL_RTFILE;
    155     if (pszPidfile != NULL)
    156     {
    157         /* @note the exclusive create is not guaranteed on all file
    158          * systems (e.g. NFSv2) */
    159         int rc = RTFileOpen(&hPidfile, pszPidfile,
    160                               RTFILE_O_READWRITE | RTFILE_O_CREATE
    161                             | (0644 << RTFILE_O_CREATE_MODE_SHIFT));
    162         if (!RT_SUCCESS(rc))
    163             return rc;
    164     }
    165 
    166147    struct sigaction OldSigAct;
    167148    struct sigaction SigAct;
     
    174155        return RTErrConvertFromErrno(errno);
    175156    if (pid != 0)
    176     {
    177 # ifndef RT_OS_LINUX /* On Linux we do another fork later */
    178         if (hPidfile != NIL_RTFILE)
    179         {
    180             char szBuf[256];
    181             size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid);
    182             RTFileWrite(hPidfile, szBuf, cbPid, NULL);
    183             RTFileClose(hPidfile);
    184         }
    185 # endif
    186157        exit(0);
    187     }
    188158
    189159    /*
     
    237207        return RTErrConvertFromErrno(errno);
    238208    if (pid != 0)
    239     {
    240         if (hPidfile != NIL_RTFILE)
    241         {
    242             char szBuf[256];
    243             size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid);
    244             RTFileWrite(hPidfile, szBuf, cbPid, NULL);
    245             RTFileClose(hPidfile);
    246         }
    247209        exit(0);
    248     }
    249210# endif /* RT_OS_LINUX */
    250211
     
    253214}
    254215
     216/**
     217 * Creates a pidfile and returns the open file descriptor.
     218 * On Unix-y systems, this call also places an advisory lock on the open
     219 * file.  It will overwrite any existing pidfiles without a lock on them,
     220 * on the assumption that they are stale files which an old process did not
     221 * properly clean up.
     222 *
     223 * @returns iprt status code
     224 * @param   pszPath  the path and filename to create the pidfile under
     225 * @param   pFile    where to store the file descriptor of the open
     226 *                   (and locked on Unix-y systems) pidfile.
     227 *                   On failure, or if another process owns the pidfile,
     228 *                   this will be set to NIL_RTFILE.
     229 */
     230VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE pFile)
     231{
     232    AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER);
     233    AssertPtrReturn(pFile, VERR_INVALID_PARAMETER);
     234#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
     235    RTFILE hPidFile = NIL_RTFILE;
     236    int rc = RTFileOpen(&hPidFile, pszPath,
     237                          RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE
     238                        | (0644 << RTFILE_O_CREATE_MODE_SHIFT));
     239    if (RT_SUCCESS(rc))
     240        rc = RTFileLock(hPidFile, RTFILE_LOCK_WRITE, 0, ~0);
     241    if (RT_SUCCESS(rc))
     242    {
     243        char szBuf[256];
     244        size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n",
     245                                   RTProcSelf());
     246        RTFileWrite(hPidFile, szBuf, cbPid, NULL);
     247    }
     248    *pFile = hPidFile;
     249    return rc;
     250#else  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
     251# error portme
     252#endif  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
     253}
     254
     255/**
     256 * Close and remove an open pidfile.
     257 * @param  pszPath  the path to the pidfile
     258 * @param  File     the open file descriptor
     259 */
     260VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE File)
     261{
     262    AssertPtrReturnVoid(pszPath);
     263    AssertReturnVoid(File != NIL_RTFILE);
     264#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
     265    RTFileDelete(pszPath);
     266    RTFileClose(File);
     267#else  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
     268# error portme
     269#endif  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
     270}
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r11822 r18320  
    374374    {
    375375        VBoxServiceVerbose(1, "Daemonizing...\n");
    376         rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */, NULL);
     376        rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
    377377        if (RT_FAILURE(rc))
    378378            return VBoxServiceError("daemon failed: %Rrc\n", rc);
  • trunk/src/VBox/Additions/x11/VBoxClient/main.cpp

    r11822 r18320  
    218218    if (fDaemonise)
    219219    {
    220         rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */,
    221                              NULL);
     220        rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
    222221        if (RT_FAILURE(rc))
    223222        {
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