VirtualBox

Changeset 10635 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jul 15, 2008 9:36:16 AM (16 years ago)
Author:
vboxsync
Message:

Additions/common: support in the R3 guest library creating a pidfile when daemonising

File:
1 edited

Legend:

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

    r10316 r10635  
    5050
    5151#include <iprt/string.h>
     52#include <iprt/file.h>
    5253#include <VBox/VBoxGuest.h>
    5354
     
    6263 * @param   fNoChDir    Pass false to change working directory to root.
    6364 * @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.
    6468 */
    65 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
     69VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose,
     70                                char const *pszPidfile)
    6671{
    6772#if defined(RT_OS_DARWIN)
     
    6974
    7075#elif defined(RT_OS_OS2)
     76    /** @todo create a pidfile if this is (/was :) ) usual on OS/2 */
     77    noref(pszPidfile);
    7178    PPIB pPib;
    7279    PTIB pTib;
     
    142149     *   fork() once more on Linux to get rid of the session leadership role.
    143150     */
     151
     152    /* We start off by opening the pidfile, so that we can fail straight away
     153     * if it already exists. */
     154    RTFILE hPidfile = NULL;
     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
    144166    struct sigaction OldSigAct;
    145167    struct sigaction SigAct;
     
    152174        return RTErrConvertFromErrno(errno);
    153175    if (pid != 0)
     176    {
     177# ifndef RT_OS_LINUX /* On Linux we do another fork later */
     178        if (hPidfile != NULL)
     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
    154186        exit(0);
     187    }
    155188
    156189    /*
     
    204237        return RTErrConvertFromErrno(errno);
    205238    if (pid != 0)
     239    {
     240        if (hPidfile != NULL)
     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        }
    206247        exit(0);
     248    }
    207249# endif /* RT_OS_LINUX */
    208250
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