Skip to content

Commit f410f74

Browse files
committed
Revert "[llvm][Support] Add support for executing a detached process (llvm#81708)"
This reverts commit 86f6caa. Unit test was failing on a few windows build bots
1 parent ef7417f commit f410f74

File tree

5 files changed

+21
-112
lines changed

5 files changed

+21
-112
lines changed

llvm/include/llvm/Support/Program.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,18 @@ namespace sys {
141141
/// program shall run on.
142142
);
143143

144-
/// Similar to \ref ExecuteAndWait, but returns immediately.
145-
/// \returns The \ref ProcessInfo of the newly launched process.
144+
/// Similar to ExecuteAndWait, but returns immediately.
145+
/// @returns The \see ProcessInfo of the newly launched process.
146146
/// \note On Microsoft Windows systems, users will need to either call
147-
/// \ref Wait until the process has finished executing or win32's CloseHandle
148-
/// API on ProcessInfo.ProcessHandle to avoid memory leaks.
149-
ProcessInfo ExecuteNoWait(
150-
StringRef Program, ArrayRef<StringRef> Args,
151-
std::optional<ArrayRef<StringRef>> Env,
152-
ArrayRef<std::optional<StringRef>> Redirects = {},
153-
unsigned MemoryLimit = 0, std::string *ErrMsg = nullptr,
154-
bool *ExecutionFailed = nullptr, BitVector *AffinityMask = nullptr,
155-
/// If true the executed program detatches from the controlling
156-
/// terminal. I/O streams such as llvm::outs, llvm::errs, and stdin will
157-
/// be closed until redirected to another output location
158-
bool DetachProcess = false);
147+
/// \see Wait until the process finished execution or win32 CloseHandle() API
148+
/// on ProcessInfo.ProcessHandle to avoid memory leaks.
149+
ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
150+
std::optional<ArrayRef<StringRef>> Env,
151+
ArrayRef<std::optional<StringRef>> Redirects = {},
152+
unsigned MemoryLimit = 0,
153+
std::string *ErrMsg = nullptr,
154+
bool *ExecutionFailed = nullptr,
155+
BitVector *AffinityMask = nullptr);
159156

160157
/// Return true if the given arguments fit within system-specific
161158
/// argument length limits.

llvm/lib/Support/Program.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
2727
std::optional<ArrayRef<StringRef>> Env,
2828
ArrayRef<std::optional<StringRef>> Redirects,
2929
unsigned MemoryLimit, std::string *ErrMsg,
30-
BitVector *AffinityMask, bool DetachProcess);
30+
BitVector *AffinityMask);
3131

3232
int sys::ExecuteAndWait(StringRef Program, ArrayRef<StringRef> Args,
3333
std::optional<ArrayRef<StringRef>> Env,
@@ -39,7 +39,7 @@ int sys::ExecuteAndWait(StringRef Program, ArrayRef<StringRef> Args,
3939
assert(Redirects.empty() || Redirects.size() == 3);
4040
ProcessInfo PI;
4141
if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
42-
AffinityMask, /*DetachProcess=*/false)) {
42+
AffinityMask)) {
4343
if (ExecutionFailed)
4444
*ExecutionFailed = false;
4545
ProcessInfo Result = Wait(
@@ -58,14 +58,13 @@ ProcessInfo sys::ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
5858
std::optional<ArrayRef<StringRef>> Env,
5959
ArrayRef<std::optional<StringRef>> Redirects,
6060
unsigned MemoryLimit, std::string *ErrMsg,
61-
bool *ExecutionFailed, BitVector *AffinityMask,
62-
bool DetachProcess) {
61+
bool *ExecutionFailed, BitVector *AffinityMask) {
6362
assert(Redirects.empty() || Redirects.size() == 3);
6463
ProcessInfo PI;
6564
if (ExecutionFailed)
6665
*ExecutionFailed = false;
6766
if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
68-
AffinityMask, DetachProcess))
67+
AffinityMask))
6968
if (ExecutionFailed)
7069
*ExecutionFailed = true;
7170

llvm/lib/Support/Unix/Program.inc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) {
173173
}
174174

175175
static bool Execute(ProcessInfo &PI, StringRef Program,
176-
ArrayRef<StringRef> Args,
177-
std::optional<ArrayRef<StringRef>> Env,
176+
ArrayRef<StringRef> Args, std::optional<ArrayRef<StringRef>> Env,
178177
ArrayRef<std::optional<StringRef>> Redirects,
179178
unsigned MemoryLimit, std::string *ErrMsg,
180-
BitVector *AffinityMask, bool DetachProcess) {
179+
BitVector *AffinityMask) {
181180
if (!llvm::sys::fs::exists(Program)) {
182181
if (ErrMsg)
183182
*ErrMsg = std::string("Executable \"") + Program.str() +
@@ -203,8 +202,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
203202
// If this OS has posix_spawn and there is no memory limit being implied, use
204203
// posix_spawn. It is more efficient than fork/exec.
205204
#ifdef HAVE_POSIX_SPAWN
206-
// Cannot use posix_spawn if you would like to detach the process
207-
if (MemoryLimit == 0 && !DetachProcess) {
205+
if (MemoryLimit == 0) {
208206
posix_spawn_file_actions_t FileActionsStore;
209207
posix_spawn_file_actions_t *FileActions = nullptr;
210208

@@ -272,7 +270,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
272270

273271
return true;
274272
}
275-
#endif // HAVE_POSIX_SPAWN
273+
#endif
276274

277275
// Create a child process.
278276
int child = fork();
@@ -309,14 +307,6 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
309307
}
310308
}
311309

312-
if (DetachProcess) {
313-
// Detach from controlling terminal
314-
if (::setsid() == -1) {
315-
MakeErrMsg(ErrMsg, "Could not detach process, ::setsid failed");
316-
return false;
317-
}
318-
}
319-
320310
// Set memory limits
321311
if (MemoryLimit != 0) {
322312
SetMemoryLimits(MemoryLimit);

llvm/lib/Support/Windows/Program.inc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,10 @@ static HANDLE RedirectIO(std::optional<StringRef> Path, int fd,
172172
} // namespace llvm
173173

174174
static bool Execute(ProcessInfo &PI, StringRef Program,
175-
ArrayRef<StringRef> Args,
176-
std::optional<ArrayRef<StringRef>> Env,
175+
ArrayRef<StringRef> Args, std::optional<ArrayRef<StringRef>> Env,
177176
ArrayRef<std::optional<StringRef>> Redirects,
178177
unsigned MemoryLimit, std::string *ErrMsg,
179-
BitVector *AffinityMask, bool DetachProcess) {
178+
BitVector *AffinityMask) {
180179
if (!sys::fs::can_execute(Program)) {
181180
if (ErrMsg)
182181
*ErrMsg = "program not executable";
@@ -285,8 +284,6 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
285284
unsigned CreateFlags = CREATE_UNICODE_ENVIRONMENT;
286285
if (AffinityMask)
287286
CreateFlags |= CREATE_SUSPENDED;
288-
if (DetachProcess)
289-
CreateFlags |= DETACHED_PROCESS;
290287

291288
std::vector<wchar_t> CommandUtf16(Command.size() + 1, 0);
292289
std::copy(Command.begin(), Command.end(), CommandUtf16.begin());

llvm/unittests/Support/ProgramTest.cpp

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -260,80 +260,6 @@ TEST_F(ProgramEnvTest, TestExecuteNoWait) {
260260
ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
261261
}
262262

263-
TEST_F(ProgramEnvTest, TestExecuteNoWaitDetached) {
264-
using namespace llvm::sys;
265-
266-
if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED")) {
267-
sleep_for(/*seconds=*/5);
268-
269-
#if _WIN32
270-
HWND ConsoleWnd = GetConsoleWindow();
271-
if (ConsoleWnd == NULL)
272-
exit(100);
273-
else
274-
exit(200);
275-
#else
276-
int ParentSID = std::stoi(
277-
std::string(getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_SID")));
278-
279-
pid_t ChildSID = ::getsid(0);
280-
if (ChildSID == -1) {
281-
llvm::errs() << "Could not get process SID: " << strerror(errno) << '\n';
282-
exit(1);
283-
}
284-
285-
char *Detached = getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_TRUE");
286-
if (Detached && (ChildSID != ParentSID))
287-
exit(100);
288-
if (!Detached && (ChildSID == ParentSID))
289-
exit(200);
290-
#endif
291-
exit(0);
292-
}
293-
294-
std::string Executable =
295-
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
296-
StringRef argv[] = {
297-
Executable, "--gtest_filter=ProgramEnvTest.TestExecuteNoWaitDetached"};
298-
addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED=1");
299-
300-
#ifndef _WIN32
301-
pid_t SID = ::getsid(0);
302-
ASSERT_NE(SID, -1);
303-
std::string SIDEnvVar =
304-
"LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_SID=" + std::to_string(SID);
305-
addEnvVar(SIDEnvVar);
306-
#endif
307-
308-
// DetachProcess = true
309-
{
310-
std::string Error;
311-
bool ExecutionFailed;
312-
std::vector<llvm::StringRef> Env = getEnviron();
313-
Env.emplace_back("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT_DETACHED_TRUE=1");
314-
ProcessInfo PI1 =
315-
ExecuteNoWait(Executable, argv, Env, {}, 0, &Error, &ExecutionFailed,
316-
nullptr, /*DetachProcess=*/true);
317-
ASSERT_FALSE(ExecutionFailed) << Error;
318-
ASSERT_NE(PI1.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
319-
ProcessInfo WaitResult = Wait(PI1, std::nullopt, &Error);
320-
ASSERT_EQ(WaitResult.ReturnCode, 100);
321-
}
322-
323-
// DetachProcess = false
324-
{
325-
std::string Error;
326-
bool ExecutionFailed;
327-
ProcessInfo PI2 =
328-
ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
329-
&ExecutionFailed, nullptr, /*DetachProcess=*/false);
330-
ASSERT_FALSE(ExecutionFailed) << Error;
331-
ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
332-
ProcessInfo WaitResult = Wait(PI2, std::nullopt, &Error);
333-
ASSERT_EQ(WaitResult.ReturnCode, 200);
334-
}
335-
}
336-
337263
TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
338264
using namespace llvm::sys;
339265

0 commit comments

Comments
 (0)