VirtualBox

Changeset 14999 in vbox


Ignore:
Timestamp:
Dec 4, 2008 5:12:09 PM (16 years ago)
Author:
vboxsync
Message:

IPRT: Extended RTProcCreate with a daemonize flag.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/process.h

    r11337 r14999  
    131131 * @param   papszArgs   Pointer to an array of arguments to the child. The array terminated by an entry containing NULL.
    132132 * @param   Env         Handle to the environment block for the child.
    133  * @param   fFlags      Flags. This is currently reserved and must be 0.
     133 * @param   fFlags      Flags, one of the RTPROC_FLAGS_* defines.
    134134 * @param   pProcess    Where to store the process identifier on successful return.
    135135 *                      The content is not changed on failure. NULL is allowed.
    136136 */
    137137RTR3DECL(int)   RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess);
     138
     139/** @name RTProcCreate flags
     140 * @{ */
     141/** Daemonize the child process, without changing the directory.
     142 * @remarks Not implemented on all platforms yet... */
     143#define RTPROC_FLAGS_DAEMONIZE          RT_BIT(0)
     144/** @}  */
     145
    138146
    139147/**
  • trunk/src/VBox/Runtime/r3/posix/process-posix.cpp

    r13837 r14999  
    6464RTR3DECL(int)   RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess)
    6565{
     66    int rc;
     67
    6668    /*
    6769     * Validate input.
     
    6971    AssertPtrReturn(pszExec, VERR_INVALID_POINTER);
    7072    AssertReturn(*pszExec, VERR_INVALID_PARAMETER);
    71     AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
     73    AssertReturn(fFlags & ~RTPROC_FLAGS_DAEMONIZE, VERR_INVALID_PARAMETER);
    7274    AssertReturn(Env != NIL_RTENV, VERR_INVALID_PARAMETER);
    7375    const char * const *papszEnv = RTEnvGetExecEnvP(Env);
     
    110112    pid_t pid;
    111113#ifdef HAVE_POSIX_SPAWN
    112     /** @todo check if it requires any of those two attributes, don't remember atm. */
    113     int rc = posix_spawn(&pid, pszExec, NULL, NULL, (char * const *)papszArgs,
     114    if (!(fFlags & RTPROC_FLAGS_DAEMONIZE))
     115    {
     116        /** @todo check if it requires any of those two attributes, don't remember atm. */
     117        rc = posix_spawn(&pid, pszExec, NULL, NULL, (char * const *)papszArgs,
    114118                         (char * const *)papszEnv);
    115     if (!rc)
    116     {
    117         if (pProcess)
    118             *pProcess = pid;
    119         return VINF_SUCCESS;
    120     }
    121 
    122 #else
    123 
    124     pid = fork();
    125     if (!pid)
    126     {
    127         int rc;
    128         rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
    129         AssertReleaseMsgFailed(("execve returns %d errno=%d\n", rc, errno));
    130         exit(127);
    131     }
    132     if (pid > 0)
    133     {
    134         if (pProcess)
    135             *pProcess = pid;
    136         return VINF_SUCCESS;
    137     }
    138     int rc = errno;
    139 #endif
     119        if (!rc)
     120        {
     121            if (pProcess)
     122                *pProcess = pid;
     123            return VINF_SUCCESS;
     124        }
     125    }
     126    else
     127#endif
     128    {
     129        pid = fork();
     130        if (!pid)
     131        {
     132            if (fFlags & RTPROC_FLAGS_DAEMONIZE)
     133            {
     134                rc = RTProcDaemonize(true /* fNoChDir */, false /* fNoClose */, NULL /* pszPidFile */);
     135                AssertReleaseMsgFailed(("RTProcDaemonize returns %Rrc errno=%d\n", rc, errno));
     136                exit(127);
     137            }
     138            rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
     139            AssertReleaseMsgFailed(("execve returns %d errno=%d\n", rc, errno));
     140            exit(127);
     141        }
     142        if (pid > 0)
     143        {
     144            if (pProcess)
     145                *pProcess = pid;
     146            return VINF_SUCCESS;
     147        }
     148        int rc = errno;
     149    }
    140150
    141151    /* failure, errno value in rc. */
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