Skip to content

Commit 86f6caa

Browse files
authored
[llvm][Support] Add support for executing a detached process (#81708)
Adds a new parameter, `bool DetachProcess` with a default option of `false`, to `llvm::sys::ExecuteNoWait`, which, when set to `true`, executes the specified program without a controlling terminal. Functionality added so that the module build daemon can be run without a controlling terminal.
1 parent 3892e8e commit 86f6caa

File tree

5 files changed

+112
-21
lines changed

5 files changed

+112
-21
lines changed

llvm/include/llvm/Support/Program.h

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

144-
/// Similar to ExecuteAndWait, but returns immediately.
145-
/// @returns The \see ProcessInfo of the newly launched process.
144+
/// Similar to \ref ExecuteAndWait, but returns immediately.
145+
/// \returns The \ref ProcessInfo of the newly launched process.
146146
/// \note On Microsoft Windows systems, users will need to either call
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);
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);
156159

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

llvm/lib/Support/Program.cpp

Lines changed: 5 additions & 4 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);
30+
BitVector *AffinityMask, bool DetachProcess);
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)) {
42+
AffinityMask, /*DetachProcess=*/false)) {
4343
if (ExecutionFailed)
4444
*ExecutionFailed = false;
4545
ProcessInfo Result = Wait(
@@ -58,13 +58,14 @@ 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) {
61+
bool *ExecutionFailed, BitVector *AffinityMask,
62+
bool DetachProcess) {
6263
assert(Redirects.empty() || Redirects.size() == 3);
6364
ProcessInfo PI;
6465
if (ExecutionFailed)
6566
*ExecutionFailed = false;
6667
if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
67-
AffinityMask))
68+
AffinityMask, DetachProcess))
6869
if (ExecutionFailed)
6970
*ExecutionFailed = true;
7071

llvm/lib/Support/Unix/Program.inc

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

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

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

271273
return true;
272274
}
273-
#endif
275+
#endif // HAVE_POSIX_SPAWN
274276

275277
// Create a child process.
276278
int child = fork();
@@ -307,6 +309,14 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
307309
}
308310
}
309311

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+
310320
// Set memory limits
311321
if (MemoryLimit != 0) {
312322
SetMemoryLimits(MemoryLimit);

llvm/lib/Support/Windows/Program.inc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ 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, std::optional<ArrayRef<StringRef>> Env,
175+
ArrayRef<StringRef> Args,
176+
std::optional<ArrayRef<StringRef>> Env,
176177
ArrayRef<std::optional<StringRef>> Redirects,
177178
unsigned MemoryLimit, std::string *ErrMsg,
178-
BitVector *AffinityMask) {
179+
BitVector *AffinityMask, bool DetachProcess) {
179180
if (!sys::fs::can_execute(Program)) {
180181
if (ErrMsg)
181182
*ErrMsg = "program not executable";
@@ -284,6 +285,8 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
284285
unsigned CreateFlags = CREATE_UNICODE_ENVIRONMENT;
285286
if (AffinityMask)
286287
CreateFlags |= CREATE_SUSPENDED;
288+
if (DetachProcess)
289+
CreateFlags |= DETACHED_PROCESS;
287290

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

llvm/unittests/Support/ProgramTest.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,80 @@ 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+
263337
TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
264338
using namespace llvm::sys;
265339

0 commit comments

Comments
 (0)