diff options
author | Connor Sughrue <55301806+cpsughrue@users.noreply.github.com> | 2024-03-06 20:45:11 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-06 23:45:11 -0500 |
commit | 2fcf248ec2614dd966f31d471a1a97989a76fd9d (patch) | |
tree | ceb41f60f2ee59e5738d9b109efb7b65da9e42cc /llvm/lib/Support/Unix/Program.inc | |
parent | ca7492fae42c5a21db96eccaca7a5e827ab1bf65 (diff) | |
download | llvm-2fcf248ec2614dd966f31d471a1a97989a76fd9d.zip llvm-2fcf248ec2614dd966f31d471a1a97989a76fd9d.tar.gz llvm-2fcf248ec2614dd966f31d471a1a97989a76fd9d.tar.bz2 |
Reland "[llvm][Support] Add support for executing a detached process (#81708)" (#83367)
Relands #81708, which was reverted by
f410f74cd5b26319b5796e0404c6a0f3b5cc00a5, now with a corrected unit
test. Origionally the test failed on Windows when run with lit as
`GetConsoleWindow` could not retrieve a window handle regardless of
whether `DetachProcess` was `true` or `false`. The test now uses
`GetStdHandle(STD_OUTPUT_HANDLE)` which does not rely on a console
window existing. Original commit message below.
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.
Diffstat (limited to 'llvm/lib/Support/Unix/Program.inc')
-rw-r--r-- | llvm/lib/Support/Unix/Program.inc | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc index 5d9757b..2742734 100644 --- a/llvm/lib/Support/Unix/Program.inc +++ b/llvm/lib/Support/Unix/Program.inc @@ -173,10 +173,11 @@ toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) { } static bool Execute(ProcessInfo &PI, StringRef Program, - ArrayRef<StringRef> Args, std::optional<ArrayRef<StringRef>> Env, + ArrayRef<StringRef> Args, + std::optional<ArrayRef<StringRef>> Env, ArrayRef<std::optional<StringRef>> Redirects, unsigned MemoryLimit, std::string *ErrMsg, - BitVector *AffinityMask) { + BitVector *AffinityMask, bool DetachProcess) { if (!llvm::sys::fs::exists(Program)) { if (ErrMsg) *ErrMsg = std::string("Executable \"") + Program.str() + @@ -202,7 +203,8 @@ static bool Execute(ProcessInfo &PI, StringRef Program, // If this OS has posix_spawn and there is no memory limit being implied, use // posix_spawn. It is more efficient than fork/exec. #ifdef HAVE_POSIX_SPAWN - if (MemoryLimit == 0) { + // Cannot use posix_spawn if you would like to detach the process + if (MemoryLimit == 0 && !DetachProcess) { posix_spawn_file_actions_t FileActionsStore; posix_spawn_file_actions_t *FileActions = nullptr; @@ -270,7 +272,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, return true; } -#endif +#endif // HAVE_POSIX_SPAWN // Create a child process. int child = fork(); @@ -307,6 +309,14 @@ static bool Execute(ProcessInfo &PI, StringRef Program, } } + if (DetachProcess) { + // Detach from controlling terminal + if (::setsid() == -1) { + MakeErrMsg(ErrMsg, "Could not detach process, ::setsid failed"); + return false; + } + } + // Set memory limits if (MemoryLimit != 0) { SetMemoryLimits(MemoryLimit); |