bool RTCALL VBoxOglIs3DAccelerationSupported(void) { if (RTEnvExist("VBOX_CROGL_FORCE_SUPPORTED")) { LogRel(("VBOX_CROGL_FORCE_SUPPORTED is specified, skipping 3D test, and treating as supported\n")); return true; } static char pszVBoxPath[RTPATH_MAX]; const char *papszArgs[4] = { NULL, "-test", "3D", NULL}; int rc; RTPROCESS Process; RTPROCSTATUS ProcStatus; uint64_t StartTS; rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX); AssertRCReturn(rc, false); #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL.exe"); #else rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL"); #endif papszArgs[0] = pszVBoxPath; /* argv[0] */ AssertRCReturn(rc, false); rc = RTProcCreate(pszVBoxPath, papszArgs, RTENV_DEFAULT, 0, &Process); if (RT_FAILURE(rc)) return false; StartTS = RTTimeMilliTS(); while (1) { rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); if (rc != VERR_PROCESS_RUNNING) break; #ifndef DEBUG_misha if (RTTimeMilliTS() - StartTS > 30*1000 /* 30 sec */) { RTProcTerminate(Process); RTThreadSleep(100); RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); return false; } #endif RTThreadSleep(100); } if (RT_SUCCESS(rc)) { if ((ProcStatus.enmReason==RTPROCEXITREASON_NORMAL) && (ProcStatus.iStatus==0)) { return true; } } return false; }
bool is3DAccelerationSupported() { static char pszVBoxPath[RTPATH_MAX]; const char *papszArgs[4] = { NULL, "-test", "3D", NULL}; int rc; RTPROCESS Process; RTPROCSTATUS ProcStatus; uint64_t StartTS; rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX); AssertRCReturn(rc, false); #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL.exe"); #else rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL"); #endif papszArgs[0] = pszVBoxPath; /* argv[0] */ AssertRCReturn(rc, false); rc = RTProcCreate(pszVBoxPath, papszArgs, RTENV_DEFAULT, 0, &Process); if (RT_FAILURE(rc)) return false; StartTS = RTTimeMilliTS(); while (1) { rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); if (rc != VERR_PROCESS_RUNNING) break; if (RTTimeMilliTS() - StartTS > 30*1000 /* 30 sec */) { RTProcTerminate(Process); RTThreadSleep(100); RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); return false; } RTThreadSleep(100); } if (RT_SUCCESS(rc)) { if ((ProcStatus.enmReason==RTPROCEXITREASON_NORMAL) && (ProcStatus.iStatus==0)) { return true; } } return false; }
static void tstRTPipe5(void) { RTTestISub("Inherit non-standard pipe handle, read end"); char szPathSelf[RTPATH_MAX]; RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szPathSelf, sizeof(szPathSelf)) == szPathSelf); RTPIPE hPipeR; RTPIPE hPipeW; RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ), VINF_SUCCESS); RTHCINTPTR hNative = RTPipeToNative(hPipeR); RTTESTI_CHECK_RETV(hNative != -1); char szNative[64]; RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative); const char *papszArgs[4] = { szPathSelf, "--child-5", szNative, NULL }; RTPROCESS hChild; RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS); RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPipeWriteBlocking(hPipeW, g_szTest5Message, sizeof(g_szTest5Message) - 1, NULL), VINF_SUCCESS); int rc; RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeW), VINF_SUCCESS); if (RT_FAILURE(rc)) RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS); RTPROCSTATUS ProcStatus; RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (RT_FAILURE(rc)) return; RTTESTI_CHECK( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL && ProcStatus.iStatus == 0); }
static void tstRTCreateProcEx5(const char *pszUser, const char *pszPassword) { RTTestISubF("As user \"%s\" with password \"%s\"", pszUser, pszPassword); const char * apszArgs[3] = { "test", /* user name */ "--testcase-child-5", NULL }; RTPROCESS hProc; /* Test for invalid logons. */ RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL, "non-existing-user", "wrong-password", &hProc), VERR_AUTHENTICATION_FAILURE); /* Test for invalid application. */ RTTESTI_CHECK_RC_RETV(RTProcCreateEx("non-existing-app", apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL, NULL, NULL, &hProc), VERR_FILE_NOT_FOUND); /* Test a (hopefully) valid user/password logon (given by parameters of this function). */ RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL, pszUser, pszPassword, &hProc), VINF_SUCCESS); RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND }; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); else RTTestIPassed(NULL); }
static int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask) { const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL }; char szAdpCtl[RTPATH_MAX]; int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME)); if (RT_FAILURE(rc)) { LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\n", rc)); return rc; } strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME); args[0] = szAdpCtl; if (!RTPathExists(szAdpCtl)) { LogRel(("NetIfAdpCtl: path %s does not exist. Failed to run " VBOXNETADPCTL_NAME " helper.\n", szAdpCtl)); return VERR_FILE_NOT_FOUND; } RTPROCESS pid; rc = RTProcCreate(szAdpCtl, args, RTENV_DEFAULT, 0, &pid); if (RT_SUCCESS(rc)) { RTPROCSTATUS Status; rc = RTProcWait(pid, 0, &Status); if ( RT_SUCCESS(rc) && Status.iStatus == 0 && Status.enmReason == RTPROCEXITREASON_NORMAL) return VINF_SUCCESS; } else LogRel(("NetIfAdpCtl: failed to create process for %s: %Rrc\n", szAdpCtl, rc)); return rc; }
RTDECL(int) RTSystemShutdown(RTMSINTERVAL cMsDelay, uint32_t fFlags, const char *pszLogMsg) { AssertPtrReturn(pszLogMsg, VERR_INVALID_POINTER); AssertReturn(!(fFlags & ~RTSYSTEM_SHUTDOWN_VALID_MASK), VERR_INVALID_PARAMETER); /* * Assemble the argument vector. */ int iArg = 0; const char *apszArgs[6]; RT_BZERO(apszArgs, sizeof(apszArgs)); apszArgs[iArg++] = "/sbin/shutdown"; switch (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) { case RTSYSTEM_SHUTDOWN_HALT: apszArgs[iArg++] = "-h"; apszArgs[iArg++] = "-H"; break; case RTSYSTEM_SHUTDOWN_REBOOT: apszArgs[iArg++] = "-r"; break; case RTSYSTEM_SHUTDOWN_POWER_OFF: case RTSYSTEM_SHUTDOWN_POWER_OFF_HALT: apszArgs[iArg++] = "-h"; apszArgs[iArg++] = "-P"; break; } char szWhen[80]; if (cMsDelay < 500) strcpy(szWhen, "now"); else RTStrPrintf(szWhen, sizeof(szWhen), "%u", (unsigned)((cMsDelay + 499) / 1000)); apszArgs[iArg++] = szWhen; apszArgs[iArg++] = pszLogMsg; /* * Start the shutdown process and wait for it to complete. */ RTPROCESS hProc; int rc = RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc); if (RT_FAILURE(rc)) return rc; RTPROCSTATUS ProcStatus; rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus); if (RT_SUCCESS(rc)) { if ( ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) rc = VERR_SYS_SHUTDOWN_FAILED; } return rc; }
int NetworkServiceRunner::stop() { if (!isRunning()) return VINF_OBJECT_DESTROYED; int rc = RTProcTerminate(m->mProcess); RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL); m->mProcess = NIL_RTPROCESS; return rc; }
/** * The parent main routine. * @param argv0 The executable name (or whatever). */ static int mainParent(const char *argv0) { /* * Init. */ RTTEST hTest; int rc = RTTestInitAndCreate("tstSupSem-Zombie", &hTest); if (rc) return rc; RTTestBanner(hTest); /* * Spin of the child process which may or may not turn into a zombie */ for (uint32_t iPass = 0; iPass < 32; iPass++) { RTTestSubF(hTest, "Pass %u", iPass); RTPROCESS hProcess; const char *apszArgs[3] = { argv0, "--child", NULL }; RTTESTI_CHECK_RC_OK(rc = RTProcCreate(argv0, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProcess)); if (RT_SUCCESS(rc)) { /* * Wait for 60 seconds then give up. */ RTPROCSTATUS Status; uint64_t StartTS = RTTimeMilliTS(); for (;;) { rc = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &Status); if (RT_SUCCESS(rc)) break; uint64_t cElapsed = RTTimeMilliTS() - StartTS; if (cElapsed > 60*1000) break; RTThreadSleep(cElapsed < 60 ? 30 : cElapsed < 200 ? 10 : 100); } RTTESTI_CHECK_RC_OK(rc); if ( RT_SUCCESS(rc) && ( Status.enmReason != RTPROCEXITREASON_NORMAL || Status.iStatus != 0)) { RTTestIFailed("child %d (%#x) reason %d\n", Status.iStatus, Status.iStatus, Status.enmReason); rc = VERR_PERMISSION_DENIED; } } /* one zombie process is enough. */ if (RT_FAILURE(rc)) break; } return RTTestSummaryAndDestroy(hTest); }
static void tstRTCreateProcEx2(const char *pszAsUser, const char *pszPassword) { RTTestISub("Standard Err"); RTPIPE hPipeR, hPipeW; RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS); const char * apszArgs[3] = { "non-existing-non-executable-file", "--testcase-child-2", NULL }; RTHANDLE Handle; Handle.enmType = RTHANDLETYPE_PIPE; Handle.u.hPipe = hPipeW; RTPROCESS hProc; RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, &Handle, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS); char szOutput[_4K]; size_t offOutput = 0; for (;;) { size_t cbLeft = sizeof(szOutput) - 1 - offOutput; RTTESTI_CHECK(cbLeft > 0); if (cbLeft == 0) break; size_t cbRead; int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead); if (RT_FAILURE(rc)) { RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE); break; } offOutput += cbRead; } szOutput[offOutput] = '\0'; RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS); RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND }; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); RTThreadSleep(10); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); else if ( offOutput != sizeof("howdy") - 1 || strcmp(szOutput, "howdy")) RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput); else RTTestIPassed(NULL); }
int NetworkServiceRunner::stop() { /* * If the process already terminated, this function will also grab the exit * status and transition the process out of zombie status. */ if (!isRunning()) return VINF_OBJECT_DESTROYED; bool fDoKillProc = true; if (!m->mKillProcOnStop) { /* * This is a VBoxSVC Main client. Do NOT kill it but assume it was shut * down politely. Wait up to 1 second until the process is killed before * doing the final hard kill. */ int rc = VINF_SUCCESS; for (unsigned int i = 0; i < 100; i++) { rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL); if (RT_SUCCESS(rc)) break; RTThreadSleep(10); } if (rc != VERR_PROCESS_RUNNING) fDoKillProc = false; } if (fDoKillProc) { RTProcTerminate(m->mProcess); int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL); NOREF(rc); } m->mProcess = NIL_RTPROCESS; return VINF_SUCCESS; }
static void tstRTCreateProcEx4(const char *pszAsUser, const char *pszPassword) { RTTestISub("Argument with spaces and stuff"); RTPROCESS hProc; RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, g_apszArgs4, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND }; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); }
static int testSessionWait(RTTEST hTest, const char *pszExecPath) { RTTestSub(hTest, "testSessionWait"); RTLOCALIPCSERVER ipcServer; int rc = RTLocalIpcServerCreate(&ipcServer, "tstRTLocalIpcSessionWait", RTLOCALIPC_FLAGS_MULTI_SESSION); if (RT_SUCCESS(rc)) { LOCALIPCTHREADCTX threadCtx = { ipcServer, hTest }; /* Spawn a simple worker thread and let it listen for incoming connections. * In the meanwhile we try to cancel the server and see what happens. */ RTTHREAD hThread; rc = RTThreadCreate(&hThread, testSessionWaitThread, &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc3"); if (RT_SUCCESS(rc)) { do { RTPROCESS hProc; const char *apszArgs[4] = { pszExecPath, "child", "tstRTLocalIpcSessionWaitFork", NULL }; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(pszExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); RTThreadSleep(5000); /* Let the server run for some time ... */ RTTestPrintf(hTest, RTTESTLVL_INFO, "Cancelling server listening\n"); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcServerCancel(ipcServer), VINF_SUCCESS); /* Wait for the server thread to terminate. */ int threadRc; RTTEST_CHECK_RC(hTest, RTThreadWait(hThread, 30 * 1000 /* 30s timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VERR_CANCELLED); RTTEST_CHECK_RC(hTest, RTLocalIpcServerDestroy(ipcServer), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Server thread terminated successfully\n"); /* Check if the child ran successfully. */ RTPROCSTATUS stsChild; RTTEST_CHECK_RC_BREAK(hTest, RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &stsChild), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Child terminated\n"); RTTEST_CHECK_BREAK(hTest, stsChild.enmReason == RTPROCEXITREASON_NORMAL); RTTEST_CHECK_BREAK(hTest, stsChild.iStatus == 0); } while (0); } else RTTestFailed(hTest, "Unable to create thread for cancelling server, rc=%Rrc\n", rc); } else RTTestFailed(hTest, "Unable to create IPC server, rc=%Rrc\n", rc); return VINF_SUCCESS; }
bool NetworkServiceRunner::isRunning() { if (m->mProcess == NIL_RTPROCESS) return false; RTPROCSTATUS status; int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status); if (rc == VERR_PROCESS_RUNNING) return true; m->mProcess = NIL_RTPROCESS; return false; }
int main(int argc, char **argv) { RTR3InitExe(argc, &argv, 0); if (argc == 2 && !strcmp(argv[1], "child")) return 42; RTPrintf("tstRTWait: spawning a child in a separate thread and waits for it in the main thread...\n"); RTTHREAD Thread; SPAWNERARGS Args = { NIL_RTPROCESS, argv[0] }; int rc = RTThreadCreate(&Thread, SpawnerThread, &Args, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "SPAWNER"); if (RT_SUCCESS(rc)) { /* Wait for it to complete. */ int rc2; rc = RTThreadWait(Thread, RT_INDEFINITE_WAIT, &rc2); if (RT_SUCCESS(rc)) rc = rc2; if (RT_SUCCESS(rc)) { /* wait for the process to complete */ RTPROCSTATUS Status; rc = RTProcWait(Args.Process, 0, &Status); if (RT_SUCCESS(rc)) { if ( Status.enmReason == RTPROCEXITREASON_NORMAL && Status.iStatus == 42) RTPrintf("tstRTWait: Success!\n"); else { rc = VERR_GENERAL_FAILURE; if (Status.enmReason != RTPROCEXITREASON_NORMAL) RTPrintf("tstRTWait: Expected exit reason RTPROCEXITREASON_NORMAL, got %d.\n", Status.enmReason); else RTPrintf("tstRTWait: Expected exit status 42, got %d.\n", Status.iStatus); } } else RTPrintf("tstRTWait: RTProcWait failed with rc=%Rrc!\n", rc); } else RTPrintf("tstRTWait: RTThreadWait or SpawnerThread failed with rc=%Rrc!\n", rc); } else RTPrintf("tstRTWait: RTThreadCreate failed with rc=%Rrc!\n", rc); return RT_SUCCESS(rc) ? 0 : 1; }
static void tstRTPipe4(void) { RTTestISub("Inherit non-standard pipe handle, write end"); char szPathSelf[RTPATH_MAX]; RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szPathSelf, sizeof(szPathSelf)) == szPathSelf); RTPIPE hPipeR; RTPIPE hPipeW; RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS); RTHCINTPTR hNative = RTPipeToNative(hPipeW); RTTESTI_CHECK_RETV(hNative != -1); char szNative[64]; RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative); const char *papszArgs[4] = { szPathSelf, "--child-4", szNative, NULL }; RTPROCESS hChild; RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS); RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS); char szTmp[1024]; size_t cbRead = 0; int rc; RTTESTI_CHECK_RC(rc = RTPipeReadBlocking(hPipeR, szTmp, sizeof(szTmp) - 1, &cbRead), VINF_SUCCESS); if (RT_FAILURE(rc)) cbRead = 0; RTTESTI_CHECK_RETV(cbRead < sizeof(szTmp)); szTmp[cbRead] = '\0'; size_t cbRead2; char szTmp2[4]; RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeR, szTmp2, sizeof(szTmp2), &cbRead2), VERR_BROKEN_PIPE); RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeR), VINF_SUCCESS); if (RT_FAILURE(rc)) RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS); RTPROCSTATUS ProcStatus; RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (RT_FAILURE(rc)) return; RTTESTI_CHECK( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL && ProcStatus.iStatus == 0); if (memcmp(szTmp, g_szTest4Message, sizeof(g_szTest4Message))) RTTestIFailed("Message mismatch.\n:Expected '%s'\nGot '%s'\n", g_szTest4Message, szTmp); }
int main(int argc, char **argv) { /* the child response. */ if (argc != 1) return 0; RTTEST hTest; RTEXITCODE rcExit = RTTestInitAndCreate("tstRTProcCreatePrf", &hTest); if (rcExit) return rcExit; RTTestBanner(hTest); char szExecPath[RTPATH_MAX]; if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath))) RTStrCopy(szExecPath, sizeof(szExecPath), argv[0]); const char *apszArgs[4] = { szExecPath, "child", "process", NULL }; uint64_t NsStart = RTTimeNanoTS(); uint32_t i; #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) || defined(RT_OS_DARWIN) for (i = 0; i < 1000; i++) #else for (i = 0; i < 10000; i++) #endif { RTPROCESS hProc; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(szExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); RTPROCSTATUS ChildStatus; RTTEST_CHECK_RC_BREAK(hTest, RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ChildStatus), VINF_SUCCESS); RTTEST_CHECK_BREAK(hTest, ChildStatus.enmReason == RTPROCEXITREASON_NORMAL); RTTEST_CHECK_BREAK(hTest, ChildStatus.iStatus == 0); } uint64_t cNsElapsed = RTTimeNanoTS() - NsStart; if (i) { RTTestValue(hTest, "Time per process", cNsElapsed / i, RTTESTUNIT_NS); } /* * Summary. */ return RTTestSummaryAndDestroy(hTest); }
/** * Runs a process, collecting the standard output and/or standard error. * * * @returns IPRT status code * @retval VERR_NO_TRANSLATION if the output of the program isn't valid UTF-8 * or contains a nul character. * @retval VERR_TOO_MUCH_DATA if the process produced too much data. * * @param pszExec Executable image to use to create the child process. * @param papszArgs Pointer to an array of arguments to the child. The * array terminated by an entry containing NULL. * @param hEnv Handle to the environment block for the child. * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX. The @a * ppszStdOut and @a ppszStdErr parameters takes precedence * over redirection flags. * @param pStatus Where to return the status on success. * @param ppszStdOut Where to return the text written to standard output. If * NULL then standard output will not be collected and go * to the standard output handle of the process. * Free with RTStrFree, regardless of return status. * @param ppszStdErr Where to return the text written to standard error. If * NULL then standard output will not be collected and go * to the standard error handle of the process. * Free with RTStrFree, regardless of return status. */ int RTProcExecToString(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags, PRTPROCSTATUS pStatus, char **ppszStdOut, char **ppszStdErr) { int rc2; /* * Clear output arguments (no returning failure here, simply crash!). */ AssertPtr(pStatus); pStatus->enmReason = RTPROCEXITREASON_ABEND; pStatus->iStatus = RTEXITCODE_FAILURE; AssertPtrNull(ppszStdOut); if (ppszStdOut) *ppszStdOut = NULL; AssertPtrNull(ppszStdOut); if (ppszStdErr) *ppszStdErr = NULL; /* * Check input arguments. */ AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER); /* * Do we need a standard input bitbucket? */ int rc = VINF_SUCCESS; PRTHANDLE phChildStdIn = NULL; RTHANDLE hChildStdIn; hChildStdIn.enmType = RTHANDLETYPE_FILE; hChildStdIn.u.hFile = NIL_RTFILE; if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc)) { phChildStdIn = &hChildStdIn; rc = RTFileOpenBitBucket(&hChildStdIn.u.hFile, RTFILE_O_READ); } /* * Create the output pipes / bitbuckets. */ RTPIPE hPipeStdOutR = NIL_RTPIPE; PRTHANDLE phChildStdOut = NULL; RTHANDLE hChildStdOut; hChildStdOut.enmType = RTHANDLETYPE_PIPE; hChildStdOut.u.hPipe = NIL_RTPIPE; if (ppszStdOut && RT_SUCCESS(rc)) { phChildStdOut = &hChildStdOut; rc = RTPipeCreate(&hPipeStdOutR, &hChildStdOut.u.hPipe, 0 /*fFlags*/); } else if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc)) { phChildStdOut = &hChildStdOut; hChildStdOut.enmType = RTHANDLETYPE_FILE; hChildStdOut.u.hFile = NIL_RTFILE; rc = RTFileOpenBitBucket(&hChildStdOut.u.hFile, RTFILE_O_WRITE); } RTPIPE hPipeStdErrR = NIL_RTPIPE; PRTHANDLE phChildStdErr = NULL; RTHANDLE hChildStdErr; hChildStdErr.enmType = RTHANDLETYPE_PIPE; hChildStdErr.u.hPipe = NIL_RTPIPE; if (ppszStdErr && RT_SUCCESS(rc)) { phChildStdErr = &hChildStdErr; rc = RTPipeCreate(&hPipeStdErrR, &hChildStdErr.u.hPipe, 0 /*fFlags*/); } else if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc)) { phChildStdErr = &hChildStdErr; hChildStdErr.enmType = RTHANDLETYPE_FILE; hChildStdErr.u.hFile = NIL_RTFILE; rc = RTFileOpenBitBucket(&hChildStdErr.u.hFile, RTFILE_O_WRITE); } if (RT_SUCCESS(rc)) { RTPOLLSET hPollSet; rc = RTPollSetCreate(&hPollSet); if (RT_SUCCESS(rc)) { if (hPipeStdOutR != NIL_RTPIPE && RT_SUCCESS(rc)) rc = RTPollSetAddPipe(hPollSet, hPipeStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 1); if (hPipeStdErrR != NIL_RTPIPE) rc = RTPollSetAddPipe(hPollSet, hPipeStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 2); } if (RT_SUCCESS(rc)) { /* * Create the process. */ RTPROCESS hProc; rc = RTProcCreateEx(g_szSvnPath, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL /*phStdIn*/, phChildStdOut, phChildStdErr, NULL /*pszAsUser*/, NULL /*pszPassword*/, &hProc); rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2); rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2); if (RT_SUCCESS(rc)) { /* * Process output and wait for the process to finish. */ size_t cbStdOut = 0; size_t offStdOut = 0; size_t cbStdErr = 0; size_t offStdErr = 0; for (;;) { if (hPipeStdOutR != NIL_RTPIPE) rc = rtProcProcessOutput(rc, &hPipeStdOutR, &cbStdOut, &offStdOut, ppszStdOut, hPollSet, 1); if (hPipeStdErrR != NIL_RTPIPE) rc = rtProcProcessOutput(rc, &hPipeStdErrR, &cbStdErr, &offStdErr, ppszStdErr, hPollSet, 2); if (hPipeStdOutR == NIL_RTPIPE && hPipeStdErrR == NIL_RTPIPE) break; if (hProc != NIL_RTPROCESS) { rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_NOBLOCK, pStatus); if (rc2 != VERR_PROCESS_RUNNING) { if (RT_FAILURE(rc2)) rc = rc2; hProc = NIL_RTPROCESS; } } rc2 = RTPoll(hPollSet, 10000, NULL, NULL); Assert(RT_SUCCESS(rc2) || rc2 == VERR_TIMEOUT); } if (RT_SUCCESS(rc)) { if ( (ppszStdOut && *ppszStdOut && !RTStrIsValidEncoding(*ppszStdOut)) || (ppszStdErr && *ppszStdErr && !RTStrIsValidEncoding(*ppszStdErr)) ) rc = VERR_NO_TRANSLATION; } /* * No more output, just wait for it to finish. */ if (hProc != NIL_RTPROCESS) { rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus); if (RT_FAILURE(rc2)) rc = rc2; } } RTPollSetDestroy(hPollSet); } } rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2); rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2); rc2 = RTHandleClose(&hChildStdIn); AssertRC(rc2); rc2 = RTPipeClose(hPipeStdErrR); AssertRC(rc2); rc2 = RTPipeClose(hPipeStdOutR); AssertRC(rc2); return rc; }
static int testSessionData(RTTEST hTest, const char *pszExecPath) { RTTestSub(hTest, "testSessionData"); RTLOCALIPCSERVER ipcServer; int rc = RTLocalIpcServerCreate(&ipcServer, "tstRTLocalIpcSessionData", RTLOCALIPC_FLAGS_MULTI_SESSION); if (RT_SUCCESS(rc)) { LOCALIPCTHREADCTX threadCtx = { ipcServer, hTest }; #if 0 /* Run server + client in threads instead of fork'ed processes (useful for debugging). */ RTTHREAD hThreadServer, hThreadClient; rc = RTThreadCreate(&hThreadServer, testSessionDataThread, &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc4"); if (RT_SUCCESS(rc)) rc = RTThreadCreate(&hThreadClient, testSessionDataChildAsThread, &hTest, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc5"); if (RT_SUCCESS(rc)) { do { int threadRc; RTTEST_CHECK_RC(hTest, RTThreadWait(hThreadServer, 5 * 60 * 1000 /* 5 minutes timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VINF_SUCCESS); RTTEST_CHECK_RC(hTest, RTThreadWait(hThreadClient, 5 * 60 * 1000 /* 5 minutes timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VINF_SUCCESS); } while (0); } #else /* Spawn a simple worker thread and let it listen for incoming connections. * In the meanwhile we try to cancel the server and see what happens. */ RTTHREAD hThread; rc = RTThreadCreate(&hThread, testSessionDataThread, &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc4"); if (RT_SUCCESS(rc)) { do { RTPROCESS hProc; const char *apszArgs[4] = { pszExecPath, "child", "tstRTLocalIpcSessionDataFork", NULL }; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(pszExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); /* Wait for the server thread to terminate. */ int threadRc; RTTEST_CHECK_RC(hTest, RTThreadWait(hThread, 5 * 60 * 1000 /* 5 minutes timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VINF_SUCCESS); RTTEST_CHECK_RC(hTest, RTLocalIpcServerDestroy(ipcServer), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Server thread terminated successfully\n"); /* Check if the child ran successfully. */ RTPROCSTATUS stsChild; RTTEST_CHECK_RC_BREAK(hTest, RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &stsChild), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Child terminated\n"); RTTEST_CHECK_BREAK(hTest, stsChild.enmReason == RTPROCEXITREASON_NORMAL); RTTEST_CHECK_BREAK(hTest, stsChild.iStatus == 0); } while (0); } else RTTestFailed(hTest, "Unable to create thread for cancelling server, rc=%Rrc\n", rc); #endif } else RTTestFailed(hTest, "Unable to create IPC server, rc=%Rrc\n", rc); return !RTTestErrorCount(hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */; }
static int testSessionConnection(RTTEST hTest, const char *pszExecPath) { RTTestSub(hTest, "testSessionConnection"); RTLOCALIPCSERVER ipcServer; int rc = RTLocalIpcServerCreate(&ipcServer, "tstRTLocalIpcSessionConnection", RTLOCALIPC_FLAGS_MULTI_SESSION); if (RT_SUCCESS(rc)) { #ifndef VBOX_TESTCASES_WITH_NO_THREADING LOCALIPCTHREADCTX threadCtx = { ipcServer, hTest }; /* Spawn a simple worker thread and let it listen for incoming connections. * In the meanwhile we try to cancel the server and see what happens. */ RTTHREAD hThread; rc = RTThreadCreate(&hThread, testSessionConnectionThread, &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc2"); if (RT_SUCCESS(rc)) { do { RTPROCESS hProc; const char *apszArgs[4] = { pszExecPath, "child", "tstRTLocalIpcSessionConnectionFork", NULL }; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(pszExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); RTPROCSTATUS stsChild; RTTEST_CHECK_RC_BREAK(hTest, RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &stsChild), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Child terminated, waiting for server thread ...\n"); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcServerCancel(ipcServer), VINF_SUCCESS); int threadRc; RTTEST_CHECK_RC(hTest, RTThreadWait(hThread, 30 * 1000 /* 30s timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VERR_CANCELLED); RTTestPrintf(hTest, RTTESTLVL_INFO, "Server thread terminated successfully\n"); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcServerDestroy(ipcServer), VINF_SUCCESS); RTTEST_CHECK_BREAK(hTest, stsChild.enmReason == RTPROCEXITREASON_NORMAL); RTTEST_CHECK_BREAK(hTest, stsChild.iStatus == 0); } while (0); } else RTTestFailed(hTest, "Unable to create thread for cancelling server, rc=%Rrc\n", rc); #else do { RTPROCESS hProc; const char *apszArgs[4] = { pszExecPath, "child", "tstRTLocalIpcSessionConnectionFork", NULL }; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(pszExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); RTLOCALIPCSESSION ipcSession; rc = RTLocalIpcServerListen(ipcServer, &ipcSession); if (RT_SUCCESS(rc)) { RTTestPrintf(hTest, RTTESTLVL_INFO, "testSessionConnectionThread: Got new client connection\n"); } else RTTestFailed(hTest, "Error while listening, rc=%Rrc\n", rc); } while (0); #endif } else RTTestFailed(hTest, "Unable to create IPC server, rc=%Rrc\n", rc); return VINF_SUCCESS; }
static void tstRTCreateProcEx6(const char *pszAsUser, const char *pszPassword) { RTTestISub("Profile environment"); const char *apszArgs[5] = { g_szExecName, "--testcase-child-6", "inherit", pszAsUser, NULL }; RTTESTI_CHECK_RC_RETV(RTEnvSetEx(RTENV_DEFAULT, "testcase-child-6", "true"), VINF_SUCCESS); /* Use the process environment first. */ RTPROCESS hProc; RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND }; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); /* Use the process environment first with a little change. */ apszArgs[2] = "change-record"; RTENV hEnvChange; RTTESTI_CHECK_RC_RETV(RTEnvCreateChangeRecord(&hEnvChange), VINF_SUCCESS); RTTESTI_CHECK_RC_RETV(RTEnvSetEx(hEnvChange, "testcase-child-6", "changed"), VINF_SUCCESS); int rc; RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange, RTPROC_FLAGS_ENV_CHANGE_RECORD, NULL, NULL, NULL, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); if (RT_SUCCESS(rc)) { ProcStatus.enmReason = RTPROCEXITREASON_ABEND; ProcStatus.iStatus = -1; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); } /* Use profile environment this time. */ apszArgs[2] = "noinherit"; RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, RTPROC_FLAGS_PROFILE, NULL, NULL, NULL, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); if (RT_SUCCESS(rc)) { ProcStatus.enmReason = RTPROCEXITREASON_ABEND; ProcStatus.iStatus = -1; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); } /* Use profile environment this time. */ apszArgs[2] = "noinherit-change-record"; RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange, RTPROC_FLAGS_PROFILE | RTPROC_FLAGS_ENV_CHANGE_RECORD, NULL, NULL, NULL, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); if (RT_SUCCESS(rc)) { ProcStatus.enmReason = RTPROCEXITREASON_ABEND; ProcStatus.iStatus = -1; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); } RTTESTI_CHECK_RC(RTEnvDestroy(hEnvChange), VINF_SUCCESS); /* * Restore the environment and check that the PROFILE flag didn't mess with * the process environment. (Note! The bug may be elsewhere as well.) */ RTTESTI_CHECK_RC(RTEnvUnsetEx(RTENV_DEFAULT, "testcase-child-6"), VINF_SUCCESS); RTENV hEnvCur; RTTESTI_CHECK_RC_RETV(RTEnvClone(&hEnvCur, RTENV_DEFAULT), VINF_SUCCESS); uint32_t cCurrent = RTEnvCountEx(hEnvCur); uint32_t cInitial = RTEnvCountEx(g_hEnvInitial); RTTESTI_CHECK_MSG(cCurrent == cInitial, ("cCurrent=%u cInitial=%u\n", cCurrent, cInitial)); uint32_t cVars1; RTENV hEnv1, hEnv2; const char *pszEnv1, *pszEnv2; if (cCurrent >= cInitial) { hEnv1 = hEnvCur; pszEnv1 = "current"; cVars1 = cCurrent; hEnv2 = g_hEnvInitial; pszEnv2 = "initial"; } else { hEnv2 = hEnvCur; pszEnv2 = "current"; hEnv1 = g_hEnvInitial; pszEnv1 = "initial"; cVars1 = cInitial; } for (uint32_t i = 0; i < cVars1; i++) { char szValue1[_16K]; char szVarNm[_1K]; rc = RTEnvGetByIndexEx(hEnv1, i, szVarNm, sizeof(szVarNm), szValue1, sizeof(szValue1)); if (RT_SUCCESS(rc)) { char szValue2[_16K]; rc = RTEnvGetEx(hEnv2, szVarNm, szValue2, sizeof(szValue2), NULL); if (RT_SUCCESS(rc)) { if (strcmp(szValue1, szValue2) != 0) { RTTestIFailed("Variable '%s' differs", szVarNm); RTTestIFailureDetails("%s: '%s'\n" "%s: '%s'\n", pszEnv1, szValue1, pszEnv2, szValue2); } } else RTTestIFailed("RTEnvGetEx(%s,%s,,) failed: %Rrc", pszEnv2, szVarNm, rc); } else RTTestIFailed("RTEnvGetByIndexEx(%s,%u,,,,) failed: %Rrc", pszEnv1, i, rc); } }
/** * Process the testcases found in the filter. * * @param pszFilter The filter (winnt) to pass to RTDirOpenFiltered for * selecting the testcases. * @param pszDir The directory we're processing. */ static void Process(const char *pszFilter, const char *pszDir) { /* * Open and enumerate the directory. */ PRTDIR pDir; int rc = RTDirOpenFiltered(&pDir, pszFilter, RTDIRFILTER_WINNT, 0); if (RT_SUCCESS(rc)) { for (;;) { RTDIRENTRY DirEntry; rc = RTDirRead(pDir, &DirEntry, NULL); if (RT_FAILURE(rc)) { if (rc == VERR_NO_MORE_FILES) rc = VINF_SUCCESS; else RTPrintf("tstRunTestcases: reading '%s' -> %Rrc\n", pszFilter, rc); break; } /* * Construct the testcase name. */ char *pszTestcase; RTStrAPrintf(&pszTestcase, "%s/%s", pszDir, DirEntry.szName); if (!pszTestcase) { RTPrintf("tstRunTestcases: out of memory!\n"); rc = VERR_NO_MEMORY; break; } if (IsTestcaseIncluded(pszTestcase)) { /* * Execute the testcase. */ RTPrintf("*** %s: Executing...\n", pszTestcase); RTStrmFlush(g_pStdOut); const char *papszArgs[2]; papszArgs[0] = pszTestcase; papszArgs[1] = NULL; RTPROCESS Process; rc = RTProcCreate(pszTestcase, papszArgs, RTENV_DEFAULT, 0, &Process); if (RT_SUCCESS(rc)) { /* * Wait for the process and collect it's return code. * If it takes too long, we'll terminate it and continue. */ RTTIMESPEC Start; RTTimeNow(&Start); RTPROCSTATUS ProcStatus; for (;;) { rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); if (rc != VERR_PROCESS_RUNNING) break; RTTIMESPEC Now; if (RTTimeSpecGetMilli(RTTimeSpecSub(RTTimeNow(&Now), &Start)) > 120*1000 /* 1 min */) { RTPrintf("*** %s: FAILED - timed out. killing it.\n", pszTestcase); RTProcTerminate(Process); RTThreadSleep(100); RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); g_cFailures++; break; } RTThreadSleep(100); } /* * Examin the exit status. */ if (RT_SUCCESS(rc)) { if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL && ProcStatus.iStatus == 0) { RTPrintf("*** %s: PASSED\n", pszTestcase); g_cPasses++; } else { RTPrintf("*** %s: FAILED\n", pszTestcase); g_cFailures++; } } else if (rc != VERR_PROCESS_RUNNING) { RTPrintf("tstRunTestcases: %s: RTProcWait failed -> %Rrc\n", pszTestcase, rc); g_cFailures++; } } else { RTPrintf("tstRunTestcases: %s: failed to start -> %Rrc\n", pszTestcase, rc); g_cFailures++; } } else { RTPrintf("tstRunTestcases: %s: SKIPPED\n", pszTestcase); g_cSkipped++; } RTStrFree(pszTestcase); } /* enumeration loop */ RTDirClose(pDir); } else RTPrintf("tstRunTestcases: opening '%s' -> %Rrc\n", pszDir, rc); }
int main(int argc, char **argv) { /* * Init IPRT. */ int rc = RTR3InitExe(argc, &argv, 0); if (RT_FAILURE(rc)) return RTMsgInitFailure(rc); /* * Locate a native DTrace command binary. */ bool fIsNativeDTrace = false; char szDTraceCmd[RTPATH_MAX]; szDTraceCmd[0] = '\0'; #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) /* * 1. Try native first on platforms where it's applicable. */ static const char * const s_apszNativeDTrace[] = { "/usr/sbin/dtrace", "/sbin/dtrace", "/usr/bin/dtrace", "/bin/dtrace", "/usr/local/sbin/dtrace", "/usr/local/bin/dtrace" }; if (!RTEnvExist("VBOX_DTRACE_NO_NATIVE")) for (uint32_t i = 0; i < RT_ELEMENTS(s_apszNativeDTrace); i++) if (RTFileExists(s_apszNativeDTrace[i])) { fIsNativeDTrace = true; strcpy(szDTraceCmd, s_apszNativeDTrace[i]); # ifdef RT_OS_LINUX /** @todo Warn if the dtrace modules haven't been loaded or vboxdrv isn't * compiled against them. */ # endif break; } if (szDTraceCmd[0] == '\0') #endif { /* * 2. VBoxDTrace extension pack installed? * * Note! We cannot use the COM API here because this program is usually * run thru sudo or directly as root, even if the target * VirtualBox process is running as regular user. This is due to * the privileges required to run dtrace scripts on a host. */ rc = RTPathAppPrivateArch(szDTraceCmd, sizeof(szDTraceCmd)); if (RT_SUCCESS(rc)) rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd), VBOX_EXTPACK_INSTALL_DIR RTPATH_SLASH_STR VBOX_EXTPACK_VBOXDTRACE_MANGLED_NAME); if (RT_SUCCESS(rc)) rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd), RTBldCfgTargetDotArch()); if (RT_SUCCESS(rc)) rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd), "VBoxDTraceCmd"); if (RT_SUCCESS(rc)) rc = RTStrCat(szDTraceCmd, sizeof(szDTraceCmd), RTLdrGetSuff()); if (RT_FAILURE(rc)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing extension pack path: %Rrc", rc); if (!RTFileExists(szDTraceCmd)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to find a DTrace implementation. VBoxDTrace Extension Pack installed?"); fIsNativeDTrace = false; } /* * Construct a new command line that includes our libary. */ char szDTraceLibDir[RTPATH_MAX]; rc = RTPathAppPrivateNoArch(szDTraceLibDir, sizeof(szDTraceLibDir)); if (RT_SUCCESS(rc)) rc = RTPathAppend(szDTraceLibDir, sizeof(szDTraceLibDir), "dtrace" RTPATH_SLASH_STR "lib"); if (RT_SUCCESS(rc)) rc = RTPathAppend(szDTraceLibDir, sizeof(szDTraceLibDir), RTBldCfgTargetArch()); if (RT_FAILURE(rc)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing dtrace library path for VBox: %Rrc", rc); char **papszArgs = (char **)RTMemAlloc((argc + 3) * sizeof(char *)); if (!papszArgs) return RTMsgErrorExit(RTEXITCODE_FAILURE, "No memory for argument list."); int cArgs = 1; papszArgs[0] = fIsNativeDTrace ? szDTraceCmd : argv[0]; if (argc > 1) { papszArgs[cArgs++] = (char *)"-L"; papszArgs[cArgs++] = szDTraceLibDir; } for (int i = 1; i < argc; i++) papszArgs[cArgs++] = argv[i]; papszArgs[cArgs] = NULL; Assert(cArgs <= argc + 3); /* * The native DTrace we execute as a sub-process and wait for. */ RTEXITCODE rcExit; if (fIsNativeDTrace) { RTPROCESS hProc; rc = RTProcCreate(szDTraceCmd, papszArgs, RTENV_DEFAULT, 0, &hProc); if (RT_SUCCESS(rc)) { RTPROCSTATUS Status; rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &Status); if (RT_SUCCESS(rc)) { if (Status.enmReason == RTPROCEXITREASON_NORMAL) rcExit = (RTEXITCODE)Status.iStatus; else rcExit = RTEXITCODE_FAILURE; } else rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error waiting for child process: %Rrc", rc); } else rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error executing '%s': %Rrc", szDTraceCmd, rc); } /* * While the VBoxDTrace we load and call the main function of. */ else { RTERRINFOSTATIC ErrInfo; RTLDRMOD hMod; rc = SUPR3HardenedLdrLoadPlugIn(szDTraceCmd, &hMod, RTErrInfoInitStatic(&ErrInfo)); if (RT_SUCCESS(rc)) { PFNVBOXDTRACEMAIN pfnVBoxDTraceMain; rc = RTLdrGetSymbol(hMod, "VBoxDTraceMain", (void **)&pfnVBoxDTraceMain); if (RT_SUCCESS(rc)) rcExit = (RTEXITCODE)pfnVBoxDTraceMain(cArgs, papszArgs); else rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error locating 'VBoxDTraceMain' in '%s': %Rrc", szDTraceCmd, rc); } else rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error loading '%s': %Rrc (%s)", szDTraceCmd, rc, ErrInfo.szMsg); } return rcExit; }
/** * Runs a process, waiting for it to complete. * * @returns IPRT status code * * @param pszExec Executable image to use to create the child process. * @param papszArgs Pointer to an array of arguments to the child. The * array terminated by an entry containing NULL. * @param hEnv Handle to the environment block for the child. * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX. * @param pStatus Where to return the status on success. */ int RTProcExec(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags, PRTPROCSTATUS pStatus) { int rc; /* * Clear output argument (no returning failure here, simply crash!). */ AssertPtr(pStatus); pStatus->enmReason = RTPROCEXITREASON_ABEND; pStatus->iStatus = RTEXITCODE_FAILURE; /* * Check input arguments. */ AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER); /* * Set up /dev/null redirections. */ PRTHANDLE aph[3] = { NULL, NULL, NULL }; RTHANDLE ah[3]; for (uint32_t i = 0; i < 3; i++) { ah[i].enmType = RTHANDLETYPE_FILE; ah[i].u.hFile = NIL_RTFILE; } rc = VINF_SUCCESS; if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc)) { aph[0] = &ah[0]; rc = RTFileOpenBitBucket(&ah[0].u.hFile, RTFILE_O_READ); } if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc)) { aph[1] = &ah[1]; rc = RTFileOpenBitBucket(&ah[1].u.hFile, RTFILE_O_WRITE); } if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc)) { aph[2] = &ah[2]; rc = RTFileOpenBitBucket(&ah[2].u.hFile, RTFILE_O_WRITE); } /* * Create the process. */ RTPROCESS hProc; if (RT_SUCCESS(rc)) rc = RTProcCreateEx(g_szSvnPath, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, aph[0], aph[1], aph[2], NULL /*pszAsUser*/, NULL /*pszPassword*/, &hProc); for (uint32_t i = 0; i < 3; i++) RTFileClose(ah[i].u.hFile); if (RT_SUCCESS(rc)) rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus); return rc; }