[llvm] [llvm][Support] Add support for executing a detached process (PR #81708)
Alexandre Ganea via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 26 06:49:23 PST 2024
================
@@ -260,6 +260,79 @@ TEST_F(ProgramEnvTest, TestExecuteNoWait) {
ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
}
+TEST_F(ProgramEnvTest, TestExecuteNoWaitDetached) {
+ using namespace llvm::sys;
+
+ if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED")) {
+ sleep_for(/*seconds=*/5);
+
+#if _WIN32
+ HWND ConsoleWnd = GetConsoleWindow();
+ if (ConsoleWnd == NULL)
+ exit(100);
+ else
+ exit(200);
+#else
+ int ParentSID = std::stoi(
+ std::string(getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_SID")));
+
+ pid_t ChildSID = ::getsid(0);
+ if (ChildSID == -1) {
+ llvm::errs() << "Could not get process SID: " << strerror(errno) << '\n';
+ exit(1);
+ }
+
+ char *Detached = getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_TRUE");
+ if (Detached && (ChildSID != ParentSID))
+ exit(100);
+ if (!Detached && (ChildSID == ParentSID))
+ exit(200);
+#endif
+ exit(0);
+ }
+
+ std::string Executable =
+ sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
+ StringRef argv[] = {
+ Executable, "--gtest_filter=ProgramEnvTest.TestExecuteNoWaitDetached"};
+ addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED=1");
+
+#ifndef _WIN32
+ pid_t SID = ::getsid(0);
+ ASSERT_NE(SID, -1);
+ std::string SIDEnvVar =
+ "LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_SID=" + std::to_string(SID);
+ addEnvVar(SIDEnvVar);
+#endif
+
+ // DetachProcess = true
+ {
+ std::string Error;
+ bool ExecutionFailed;
+ std::vector<llvm::StringRef> Env = getEnviron();
+ Env.emplace_back("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_TRUE=1");
+ ProcessInfo PI1 =
+ ExecuteNoWait(Executable, argv, Env, {}, 0, &Error, &ExecutionFailed,
+ nullptr, /*DetachedProcess=*/true);
+ ASSERT_FALSE(ExecutionFailed) << Error;
+ ASSERT_NE(PI1.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
+ ProcessInfo WaitResult = Wait(PI1, std::nullopt, &Error);
+ ASSERT_EQ(WaitResult.ReturnCode, 100);
+ }
+
+ // DetachProcess = false
+ {
+ std::string Error;
+ bool ExecutionFailed;
+ ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0,
+ &Error, &ExecutionFailed, nullptr);
----------------
aganea wrote:
I would add `, /*DetachedProcess=*/false` to make it explicit what we're testing, and not rely on a default value elsewhere.
https://github.com/llvm/llvm-project/pull/81708
More information about the llvm-commits
mailing list