process-posix.cpp revision 5383c976362d5b18656cf7b9248b2063a764a203
/* $Id$ */
/** @file
* IPRT - Process, POSIX.
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* 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_OS2)
# define HAVE_POSIX_SPAWN 1
#endif
#ifdef HAVE_POSIX_SPAWN
# include <spawn.h>
#endif
#ifdef RT_OS_DARWIN
#endif
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
*/
/** @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)
{
AssertMsgFailed(("Implement get uid by name lookup\n"));
return VERR_NOT_IMPLEMENTED;
}
/*
* Check for execute access to the file.
*/
{
return rc;
}
/*
* Spawn the child.
*
* 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 waipit call doesn't race anyone (read XPCOM) doing group wide
* waits.
*/
#ifdef HAVE_POSIX_SPAWN
if ( !(fFlags & RTPROC_FLAGS_DAEMONIZE)
)
{
/* 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)
{
if (phProcess)
return VINF_SUCCESS;
}
}
}
else
#endif
{
if (!pid)
{
setpgid(0, 0); /* see comment above */
/*
* Change group and user if requested.
*/
{
exit(126);
}
{
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)
exit(125);
for (int j = i + 1; j < 3; j++)
{
fd = -1;
break;
}
if (fd >= 0)
}
}
/*
* Daemonize the process if requested.
*/
if (fFlags & RTPROC_FLAGS_DAEMONIZE)
{
if (RT_FAILURE(rc))
{
exit(127);
}
}
/*
* Finally, execute the requested program.
*/
exit(127);
}
if (pid > 0)
{
if (phProcess)
return VINF_SUCCESS;
}
}
return VERR_NOT_IMPLEMENTED;
}
{
int rc;
while (rc == VERR_INTERRUPTED);
return rc;
}
{
/*
* Validate input.
*/
if (Process <= 0)
{
return VERR_INVALID_PARAMETER;
}
{
return VERR_INVALID_PARAMETER;
}
/*
* Performe the wait.
*/
int iStatus = 0;
if (rc > 0)
{
/*
* Fill in the status structure.
*/
if (pProcStatus)
{
{
}
else if (WIFSIGNALED(iStatus))
{
}
else
{
}
}
return VINF_SUCCESS;
}
/*
* Child running?
*/
if (!rc)
{
return VERR_PROCESS_RUNNING;
}
/*
* Figure out which error to return.
*/
return VERR_PROCESS_NOT_FOUND;
return RTErrConvertFromErrno(iErr);
}
{
return VINF_SUCCESS;
return RTErrConvertFromErrno(errno);
}
{
// @todo
return 1;
}
/**
* Daemonize the current process, making it a background process. The current
* process will exit if daemonizing is successful.
*
* @returns iprt status code.
* @param fNoChDir Pass false to change working directory to "/".
* @param fNoClose Pass false to redirect standard file streams to the null device.
* @param pszPidfile Path to a file to write the process id of the daemon
* process to. Daemonizing will fail if this file already
* exists or cannot be written. May be NULL.
*/
{
/*
* 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. */
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;
}