process-posix.cpp revision 26ce84897df60fed28f68a98b91ce82aed9f0ee7
199767f8919635c4928607450d9e0abb932109ceToomas Soome * innotek Portable Runtime - Process, POSIX.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 2006-2007 innotek GmbH
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This file is part of VirtualBox Open Source Edition (OSE), as
199767f8919635c4928607450d9e0abb932109ceToomas Soome * available from http://www.virtualbox.org. This file is free software;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * you can redistribute it and/or modify it under the terms of the GNU
199767f8919635c4928607450d9e0abb932109ceToomas Soome * General Public License as published by the Free Software Foundation,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * distribution. VirtualBox OSE is distributed in the hope that it will
199767f8919635c4928607450d9e0abb932109ceToomas Soome * be useful, but WITHOUT ANY WARRANTY of any kind.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If you received this file as part of a commercial VirtualBox
199767f8919635c4928607450d9e0abb932109ceToomas Soome * distribution, then only the terms of your commercial VirtualBox
199767f8919635c4928607450d9e0abb932109ceToomas Soome * license agreement apply instead of the previous paragraph.
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*******************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome* Header Files *
199767f8919635c4928607450d9e0abb932109ceToomas Soome*******************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas SoomeRTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, const char * const *papszEnv, unsigned fFlags, PRTPROCESS pProcess)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Validate input.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* later: path searching. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check for execute access to the file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome AssertMsgFailed(("'%s' %Vrc!\n", pszExec, rc));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Squeeze gdb --args in front of what's being spawned.
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned cArgs = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char **papszArgsTmp = (const char **)alloca(cArgs * sizeof(char *));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Spawn the child.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /** @todo check if it requires any of those two attributes, don't remember atm. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc = posix_spawn(&pid, pszExec, NULL, NULL, (char * const *)papszArgs,
199767f8919635c4928607450d9e0abb932109ceToomas Soome papszEnv ? (char * const *)papszEnv : environ);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = execv(pszExec, (char * const *)papszArgs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome AssertReleaseMsgFailed(("execve returns %d errno=%d\n", rc, errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* failure, errno value in rc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome AssertMsgFailed(("spawn/exec failed rc=%d\n", rc)); /* this migth be annoying... */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeRTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
199767f8919635c4928607450d9e0abb932109ceToomas Soome do rc = RTProcWaitNoResume(Process, fFlags, pProcStatus);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeRTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Validate input.
199767f8919635c4928607450d9e0abb932109ceToomas Soome AssertMsgFailed(("Invalid Process=%d\n", Process));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fFlags & ~(RTPROCWAIT_FLAGS_NOBLOCK | RTPROCWAIT_FLAGS_BLOCK))
199767f8919635c4928607450d9e0abb932109ceToomas Soome AssertMsgFailed(("Invalid flags %#x\n", fFlags));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Performe the wait.
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc = waitpid(Process, &iStatus, fFlags & RTPROCWAIT_FLAGS_NOBLOCK ? WNOHANG : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Fill in the status structure.
199767f8919635c4928607450d9e0abb932109ceToomas Soome pProcStatus->enmReason = RTPROCEXITREASON_NORMAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pProcStatus->enmReason = RTPROCEXITREASON_SIGNAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pProcStatus->enmReason = RTPROCEXITREASON_ABEND;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Child running?
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Figure out which error to return.
199767f8919635c4928607450d9e0abb932109ceToomas SoomeRTR3DECL(int) RTProcTerminate(RTPROCESS Process)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeRTR3DECL(char *) RTProcGetExecutableName(char *pszExecName, size_t cchExecName)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * I don't think there is a posix API for this, but
199767f8919635c4928607450d9e0abb932109ceToomas Soome * because I'm lazy I'm not creating OS specific code
199767f8919635c4928607450d9e0abb932109ceToomas Soome * files and code for this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cchLink = readlink("/proc/self/exe", pszExecName, cchExecName - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome RTStrPrintf(szFileBuf, sizeof(szFileBuf), "/proc/%ld/path/a.out", (long)getpid());
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cchLink = readlink(szFileBuf, pszExecName, cchExecName - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cchLink = readlink("/proc/curproc/file", pszExecName, cchExecName - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cchLink > 0 && (size_t)cchLink <= cchExecName - 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *pszImageName = _dyld_get_image_name(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (char *)memcpy(pszExecName, pszImageName, cchImageName + 1);