process-posix.cpp revision c98fb3e16fcd571a790eab772c0c66173d225205
137N/A/* $Id$ */
137N/A/** @file
137N/A * innotek Portable Runtime - Process, POSIX.
137N/A */
943N/A
137N/A/*
137N/A * Copyright (C) 2006-2007 innotek GmbH
919N/A *
919N/A * This file is part of VirtualBox Open Source Edition (OSE), as
919N/A * available from http://www.virtualbox.org. This file is free software;
919N/A * you can redistribute it and/or modify it under the terms of the GNU
919N/A * General Public License as published by the Free Software Foundation,
919N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
919N/A * distribution. VirtualBox OSE is distributed in the hope that it will
919N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
919N/A */
919N/A
919N/A
919N/A
919N/A/*******************************************************************************
919N/A* Header Files *
919N/A*******************************************************************************/
919N/A#define LOG_GROUP RTLOGGROUP_PROCESS
919N/A#include <unistd.h>
137N/A#include <stdlib.h>
137N/A#include <errno.h>
137N/A#include <sys/stat.h>
137N/A#include <sys/wait.h>
493N/A#include <signal.h>
137N/A#if defined(RT_OS_LINUX) || defined(RT_OS_OS2)
137N/A# define HAVE_POSIX_SPAWN 1
851N/A#endif
137N/A#ifdef HAVE_POSIX_SPAWN
911N/A# include <spawn.h>
911N/A#endif
911N/A#ifdef RT_OS_DARWIN
911N/A# include <mach-o/dyld.h>
137N/A#endif
137N/A
137N/A#include <iprt/process.h>
137N/A#include <iprt/string.h>
137N/A#include <iprt/assert.h>
137N/A#include <iprt/err.h>
137N/A#include "internal/process.h"
137N/A
137N/A
493N/A
137N/ARTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, const char * const *papszEnv, unsigned fFlags, PRTPROCESS pProcess)
137N/A{
137N/A /*
493N/A * Validate input.
137N/A */
137N/A if (!pszExec || !*pszExec)
493N/A {
137N/A AssertMsgFailed(("no exec\n"));
493N/A return VERR_INVALID_PARAMETER;
137N/A }
137N/A if (fFlags)
137N/A {
137N/A AssertMsgFailed(("invalid flags!\n"));
137N/A return VERR_INVALID_PARAMETER;
}
/* later: path searching. */
/*
* Check for execute access to the file.
*/
if (access(pszExec, X_OK))
{
int rc = RTErrConvertFromErrno(errno);
AssertMsgFailed(("'%s' %Vrc!\n", pszExec, rc));
return rc;
}
#if 0
/*
* Squeeze gdb --args in front of what's being spawned.
*/
unsigned cArgs = 0;
while (papszArgs[cArgs])
cArgs++;
cArgs += 3;
const char **papszArgsTmp = (const char **)alloca(cArgs * sizeof(char *));
papszArgsTmp[0] = "/usr/bin/gdb";
papszArgsTmp[1] = "--args";
papszArgsTmp[2] = pszExec;
for (unsigned i = 1; papszArgs[i]; i++)
papszArgsTmp[i + 2] = papszArgs[i];
papszArgsTmp[cArgs - 1] = NULL;
pszExec = papszArgsTmp[0];
papszArgs = papszArgsTmp;
#endif
/*
* Spawn the child.
*/
pid_t pid;
#ifdef HAVE_POSIX_SPAWN
/** @todo check if it requires any of those two attributes, don't remember atm. */
int rc = posix_spawn(&pid, pszExec, NULL, NULL, (char * const *)papszArgs,
papszEnv ? (char * const *)papszEnv : environ);
if (!rc)
{
if (pProcess)
*pProcess = pid;
return VINF_SUCCESS;
}
#else
pid = fork();
if (!pid)
{
int rc;
if (papszEnv)
rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
else
rc = execv(pszExec, (char * const *)papszArgs);
AssertReleaseMsgFailed(("execve returns %d errno=%d\n", rc, errno));
exit(127);
}
if (pid > 0)
{
if (pProcess)
*pProcess = pid;
return VINF_SUCCESS;
}
int rc = errno;
#endif
/* failure, errno value in rc. */
AssertMsgFailed(("spawn/exec failed rc=%d\n", rc)); /* this migth be annoying... */
return RTErrConvertFromErrno(rc);
}
RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
{
int rc;
do rc = RTProcWaitNoResume(Process, fFlags, pProcStatus);
while (rc == VERR_INTERRUPTED);
return rc;
}
RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
{
/*
* Validate input.
*/
if (Process <= 0)
{
AssertMsgFailed(("Invalid Process=%d\n", Process));
return VERR_INVALID_PARAMETER;
}
if (fFlags & ~(RTPROCWAIT_FLAGS_NOBLOCK | RTPROCWAIT_FLAGS_BLOCK))
{
AssertMsgFailed(("Invalid flags %#x\n", fFlags));
return VERR_INVALID_PARAMETER;
}
/*
* Performe the wait.
*/
int iStatus = 0;
int rc = waitpid(Process, &iStatus, fFlags & RTPROCWAIT_FLAGS_NOBLOCK ? WNOHANG : 0);
if (rc > 0)
{
/*
* Fill in the status structure.
*/
if (pProcStatus)
{
if (WIFEXITED(iStatus))
{
pProcStatus->enmReason = RTPROCEXITREASON_NORMAL;
pProcStatus->iStatus = WEXITSTATUS(iStatus);
}
else if (WIFSIGNALED(iStatus))
{
pProcStatus->enmReason = RTPROCEXITREASON_SIGNAL;
pProcStatus->iStatus = WTERMSIG(iStatus);
}
else
{
Assert(!WIFSTOPPED(iStatus));
pProcStatus->enmReason = RTPROCEXITREASON_ABEND;
pProcStatus->iStatus = iStatus;
}
}
return VINF_SUCCESS;
}
/*
* Child running?
*/
if (!rc)
{
Assert(fFlags & RTPROCWAIT_FLAGS_NOBLOCK);
return VERR_PROCESS_RUNNING;
}
/*
* Figure out which error to return.
*/
int iErr = errno;
if (iErr == ECHILD)
return VERR_PROCESS_NOT_FOUND;
return RTErrConvertFromErrno(iErr);
}
RTR3DECL(int) RTProcTerminate(RTPROCESS Process)
{
if (!kill(Process, SIGKILL))
return VINF_SUCCESS;
return RTErrConvertFromErrno(errno);
}
RTR3DECL(uint64_t) RTProcGetAffinityMask()
{
// @todo
return 1;
}
RTR3DECL(char *) RTProcGetExecutableName(char *pszExecName, size_t cchExecName)
{
/*
* 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.
*/
#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
# ifdef RT_OS_LINUX
int cchLink = readlink("/proc/self/exe", pszExecName, cchExecName - 1);
# elif defined(RT_OS_SOLARIS)
char szFileBuf[80];
RTStrPrintf(szFileBuf, sizeof(szFileBuf), "/proc/%ld/path/a.out", (long)getpid());
int cchLink = readlink(szFileBuf, pszExecName, cchExecName - 1);
# else
int cchLink = readlink("/proc/curproc/file", pszExecName, cchExecName - 1);
# endif
if (cchLink > 0 && (size_t)cchLink <= cchExecName - 1)
{
pszExecName[cchLink] = '\0';
return pszExecName;
}
#elif defined(RT_OS_OS2) || defined(RT_OS_L4)
if (!_execname(pszExecName, cchExecName))
return pszExecName;
#elif defined(RT_OS_DARWIN)
const char *pszImageName = _dyld_get_image_name(0);
if (pszImageName)
{
size_t cchImageName = strlen(pszImageName);
if (cchImageName < cchExecName)
return (char *)memcpy(pszExecName, pszImageName, cchImageName + 1);
}
#else
# error "Port me!"
#endif
return NULL;
}