VirtualBox

Ignore:
Timestamp:
Dec 1, 2014 4:00:30 PM (10 years ago)
Author:
vboxsync
Message:

VBoxGuestLib: add re-spawn option to VbglR3Daemonize().

File:
1 edited

Legend:

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

    r48938 r53421  
    4343# include <sys/types.h>
    4444# include <sys/stat.h>
     45# include <sys/wait.h>
    4546# include <stdio.h>
    4647# include <fcntl.h>
     
    6667 * @param   fNoChDir    Pass false to change working directory to root.
    6768 * @param   fNoClose    Pass false to redirect standard file streams to /dev/null.
     69 * @param   fRespawn    Restart the daemonised process after five seconds if it
     70 *                      terminates abnormally.
    6871 *
    6972 * @todo    Use RTProcDaemonize instead of this.
    70  */
    71 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
     73 * @todo    Implement fRespawn on OS/2.
     74 * @todo    Make the respawn interval configurable.  But not until someone
     75 *          actually needs that.
     76 */
     77VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose, bool fRespawn)
    7278{
    7379#if defined(RT_OS_OS2)
     
    7682    DosGetInfoBlocks(&pTib, &pPib);
    7783
     84    AssertRelease(!fRespawn);
    7885    /* Get the full path to the executable. */
    7986    char szExe[CCHMAXPATH];
     
    211218# endif /* RT_OS_LINUX */
    212219
     220    if (fRespawn)
     221        /* We implement re-spawning as a third fork(), with the parent process
     222         * monitoring the child and re-starting it after a delay if it exits
     223         * abnormally. */
     224        for (;;)
     225        {
     226            int iStatus, rcWait;
     227
     228            pid = fork();
     229            if (pid == -1)
     230                return RTErrConvertFromErrno(errno);
     231            if (pid == 0)
     232                return VINF_SUCCESS;
     233            do
     234                rcWait = waitpid(pid, &iStatus, 0);
     235            while (rcWait == -1 && errno == EINTR);
     236            if (rcWait == -1)
     237                exit(1);
     238            if (WIFEXITED(iStatus) && WEXITSTATUS(iStatus) == 0)
     239                exit(0);
     240            sleep(5);
     241        }
    213242    return VINF_SUCCESS;
    214243#endif
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