process-creation-posix.cpp revision 9c0e21f5aec1d320af6cb8baa5f17a528cb7ee62
/* $Id$ */
/** @file
* IPRT - Process Creation, POSIX.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_PROCESS
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
# include <crypt.h>
# include <pwd.h>
# include <shadow.h>
#endif
#if defined(RT_OS_LINUX) || defined(RT_OS_OS2)
/* While Solaris has posix_spawn() of course we don't want to use it as
* we need to have the child in a different process contract, no matter
* whether it is started detached or not. */
# define HAVE_POSIX_SPAWN 1
#endif
#ifdef HAVE_POSIX_SPAWN
# include <spawn.h>
#endif
#ifdef RT_OS_DARWIN
#endif
#ifdef RT_OS_SOLARIS
# include <limits.h>
# include <libcontract.h>
#endif
/**
*
* @param pszUser username
* @param pszPasswd password
* @param gid where to store the GID of the user
* @param uid where to store the UID of the user
* @returns IPRT status code
*/
{
#if defined(RT_OS_LINUX)
if (!pw)
return VERR_PERMISSION_DENIED;
if (!pszPasswd)
pszPasswd = "";
if (spwd)
/* be reentrant */
return VERR_PERMISSION_DENIED;
return VINF_SUCCESS;
#elif defined(RT_OS_SOLARIS)
char szBuf[1024];
return VERR_PERMISSION_DENIED;
if (!pszPasswd)
pszPasswd = "";
char szPwdBuf[1024];
return VERR_PERMISSION_DENIED;
return VINF_SUCCESS;
#else
return VERR_PERMISSION_DENIED;
#endif
}
#ifdef RT_OS_SOLARIS
/** @todo the error reporting of the Solaris process contract code could be
* a lot better, but essentially it is not meant to run into errors after
* the debugging phase. */
static int rtSolarisContractPreFork(void)
{
if (templateFd < 0)
return -1;
/* Set template parameters and event sets. */
{
return -1;
}
{
return -1;
}
if (ct_tmpl_set_critical(templateFd, 0))
{
return -1;
}
{
return -1;
}
/* Make this the active template for the process. */
if (ct_tmpl_activate(templateFd))
{
return -1;
}
return templateFd;
}
static void rtSolarisContractPostForkChild(int templateFd)
{
if (templateFd == -1)
return;
/* Clear the active template. */
}
{
if (templateFd == -1)
return;
/* Clear the active template. */
/* If the clearing failed or the fork failed there's nothing more to do. */
return;
/* Look up the contract which was created by this thread. */
if (statFd == -1)
return;
{
return;
}
if (ctId < 0)
return;
/* Abandon this contract we just created. */
return;
if (statFd == -1)
return;
if (ct_ctl_abandon(ctlFd) < 0)
{
return;
}
}
#endif /* RT_OS_SOLARIS */
RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess)
{
pProcess);
}
RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
{
int rc;
/*
* Input validation
*/
AssertReturn(!(fFlags & ~(RTPROC_FLAGS_DETACHED | RTPROC_FLAGS_HIDDEN | RTPROC_FLAGS_SERVICE | RTPROC_FLAGS_SAME_CONTRACT | RTPROC_FLAGS_NO_PROFILE)), VERR_INVALID_PARAMETER);
/** @todo search the PATH (add flag for this). */
/*
* Get the file descriptors for the handles we've been passed.
*/
for (int i = 0; i < 3; i++)
{
if (paHandles[i])
{
{
case RTHANDLETYPE_FILE:
: -2 /* close it */;
break;
case RTHANDLETYPE_PIPE:
: -2 /* close it */;
break;
case RTHANDLETYPE_SOCKET:
: -2 /* close it */;
break;
default:
}
/** @todo check the close-on-execness of these handles? */
}
}
for (int i = 0; i < 3; i++)
if (aStdFds[i] == i)
aStdFds[i] = -1;
for (int i = 0; i < 3; i++)
("%i := %i not possible because we're lazy\n", i, aStdFds[i]),
/*
* Resolve the user id if specified.
*/
if (pszAsUser)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Check for execute access to the file.
*/
return RTErrConvertFromErrno(errno);
/*
* Take care of detaching the process.
*
* HACK ALERT! Put the process into a new process group with pgid = pid
* to make sure it differs from that of the parent process to ensure that
* the IPRT waitpid call doesn't race anyone (read XPCOM) doing group wide
* waits. setsid() includes the setpgid() functionality.
* 2010-10-11 XPCOM no longer waits for anything, but it cannot hurt.
*/
#ifndef RT_OS_OS2
if (fFlags & RTPROC_FLAGS_DETACHED)
{
# ifdef RT_OS_SOLARIS
int templateFd = -1;
if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
{
if (templateFd == -1)
return VERR_OPEN_FAILED;
}
# endif /* RT_OS_SOLARIS */
if (!pid)
{
# ifdef RT_OS_SOLARIS
if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
# endif /* RT_OS_SOLARIS */
setsid(); /* see comment above */
pid = -1;
/* Child falls through to the actual spawn code below. */
}
else
{
#ifdef RT_OS_SOLARIS
if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
#endif /* RT_OS_SOLARIS */
if (pid > 0)
{
/* Must wait for the temporary process to avoid a zombie. */
int status = 0;
/* Restart if we get interrupted. */
do
{
} while ( pidChild == -1
/* Assume that something wasn't found. No detailed info. */
if (status)
return VERR_PROCESS_NOT_FOUND;
if (phProcess)
*phProcess = 0;
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
}
#endif
/*
* Spawn the child.
*
* Any spawn code MUST not execute any atexit functions if it is for a
* detached process. It would lead to running the atexit functions which
* make only sense for the parent. libORBit e.g. gets confused by multiple
* execution. Remember, there was only a fork() so far, and until exec()
* is successfully run there is nothing which would prevent doing anything
* silly with the (duplicated) file descriptors.
*/
#ifdef HAVE_POSIX_SPAWN
/** @todo OS/2: implement DETACHED (BACKGROUND stuff), see VbglR3Daemonize. */
{
/* Spawn attributes. */
if (!rc)
{
# ifndef RT_OS_OS2 /* We don't need this on OS/2 and I don't recall if it's actually implemented. */
if (!rc)
{
}
# endif
/* File changes. */
{
if (!rc)
{
for (int i = 0; i < 3; i++)
{
if (fd == -2)
{
if (!rc)
{
for (int j = i + 1; j < 3; j++)
{
fd = -1;
break;
}
if (fd >= 0)
}
}
if (rc)
break;
}
}
}
if (!rc)
(char * const *)papszEnv);
/* cleanup */
if (pFileActions)
{
}
/* return on success.*/
if (!rc)
{
/* For a detached process this happens in the temp process, so
* it's not worth doing anything as this process must exit. */
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(0);
if (phProcess)
return VINF_SUCCESS;
}
}
/* For a detached process this happens in the temp process, so
* it's not worth doing anything as this process must exit. */
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(124);
}
else
#endif
{
#ifdef RT_OS_SOLARIS
int templateFd = rtSolarisContractPreFork();
if (templateFd == -1)
return VERR_OPEN_FAILED;
#endif /* RT_OS_SOLARIS */
if (!pid)
{
#ifdef RT_OS_SOLARIS
#endif /* RT_OS_SOLARIS */
if (!(fFlags & RTPROC_FLAGS_DETACHED))
setpgid(0, 0); /* see comment above */
/*
* Change group and user if requested.
*/
{
{
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(126);
else
exit(126);
}
}
{
{
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(126);
else
exit(126);
}
}
#endif
/*
* Apply changes to the standard file descriptor and stuff.
*/
for (int i = 0; i < 3; i++)
{
if (fd == -2)
close(i);
else if (fd >= 0)
{
if (rc2 != i)
{
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(125);
else
exit(125);
}
for (int j = i + 1; j < 3; j++)
{
fd = -1;
break;
}
if (fd >= 0)
}
}
/*
* Finally, execute the requested program.
*/
{
RTAssertMsg2Weak("Cannot execute this binary format!\n");
}
else
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(127);
else
exit(127);
}
#ifdef RT_OS_SOLARIS
#endif /* RT_OS_SOLARIS */
if (pid > 0)
{
/* For a detached process this happens in the temp process, so
* it's not worth doing anything as this process must exit. */
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(0);
if (phProcess)
return VINF_SUCCESS;
}
/* For a detached process this happens in the temp process, so
* it's not worth doing anything as this process must exit. */
if (fFlags & RTPROC_FLAGS_DETACHED)
_Exit(124);
return RTErrConvertFromErrno(errno);
}
return VERR_NOT_IMPLEMENTED;
}
{
/*
* Fork the child process in a new session and quit the parent.
*
* - fork once and create a new session (setsid). This will detach us
* from the controlling tty meaning that we won't receive the SIGHUP
* (or any other signal) sent to that session.
* us one before we get to the setsid.
* - When the parent exit(0) we will become an orphan and re-parented to
* the init process.
* - Because of the sometimes unexpected semantics of assigning the
* controlling tty automagically when a session leader first opens a tty,
* we will fork() once more to get rid of the session leadership role.
*/
/* We start off by opening the pidfile, so that we can fail straight away
* if it already exists. */
int fdPidfile = -1;
if (pszPidfile != NULL)
{
/* @note the exclusive create is not guaranteed on all file
* systems (e.g. NFSv2) */
return RTErrConvertFromErrno(errno);
}
/* Ignore SIGHUP straight away. */
/* First fork, to become independent process. */
if (pid == -1)
return RTErrConvertFromErrno(errno);
if (pid != 0)
{
/* Parent exits, no longer necessary. The child gets reparented
* to the init process. */
exit(0);
}
/* Create new session, fix up the standard file descriptors and the
* current working directory. */
/** @todo r=klaus the webservice uses this function and assumes that the
* contract id of the daemon is the same as that of the original process.
* Whenever this code is changed this must still remain possible. */
int SavedErrno = errno;
if (rcSigAct != -1)
if (newpgid == -1)
return RTErrConvertFromErrno(SavedErrno);
if (!fNoClose)
{
{
}
if (fd != -1)
{
if (fd > 2)
}
}
if (!fNoChDir)
{
}
/* Second fork to lose session leader status. */
if (pid == -1)
return RTErrConvertFromErrno(errno);
if (pid != 0)
{
/* Write the pid file, this is done in the parent, before exiting. */
if (fdPidfile != -1)
{
char szBuf[256];
}
exit(0);
}
return VINF_SUCCESS;
}