process-posix.cpp revision 4c005be884f2ccbb47546d41f2a563f55e79db10
/* $Id$ */
/** @file
* IPRT - Process, POSIX.
*/
/*
* Copyright (C) 2006-2007 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)
{
/*
* Validate input.
*/
/* later: path searching. */
/*
* Check for execute access to the file.
*/
{
return rc;
}
#if 0
/*
* Squeeze gdb --args in front of what's being spawned.
*/
unsigned cArgs = 0;
cArgs++;
cArgs += 3;
papszArgsTmp[0] = "/usr/bin/gdb";
for (unsigned i = 1; papszArgs[i]; i++)
pszExec = papszArgsTmp[0];
#endif
/*
* Spawn the child.
*/
#ifdef HAVE_POSIX_SPAWN
/** @todo check if it requires any of those two attributes, don't remember atm. */
(char * const *)papszEnv);
if (!rc)
{
if (pProcess)
return VINF_SUCCESS;
}
#else
if (!pid)
{
int rc;
exit(127);
}
if (pid > 0)
{
if (pProcess)
return VINF_SUCCESS;
}
#endif
/* failure, errno value in rc. */
return RTErrConvertFromErrno(rc);
}
{
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;
}
{
/*
* I don't think there is a posix API for this, but
* because I'm lazy I'm not creating OS specific code
* files and code for this.
*/
# ifdef RT_OS_LINUX
# elif defined(RT_OS_SOLARIS)
char szFileBuf[80];
# else
# endif
{
return pszExecName;
}
return pszExecName;
#elif defined(RT_OS_DARWIN)
const char *pszImageName = _dyld_get_image_name(0);
if (pszImageName)
{
if (cchImageName < cchExecName)
}
#else
# error "Port me!"
#endif
return NULL;
}
/**
* 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. Child creates 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)
chdir("/");
/* 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;
}