17#include "llvm/Support/Errno.h"
22#include <sys/ptrace.h>
29#if defined(__FreeBSD__)
34#include <sys/personality.h>
45 int r = write(error_fd, str, strlen(str));
49[[noreturn]]
static void ExitWithError(
int error_fd,
const char *operation) {
60 const unsigned long personality_get_current = 0xffffffff;
61 int value = personality(personality_get_current);
65 value = personality(ADDR_NO_RANDOMIZE | value);
71static void DupDescriptor(
int error_fd,
const char *file,
int fd,
int flags) {
80 if (::dup2(target_fd, fd) == -1)
87struct ForkFileAction {
88 ForkFileAction(
const FileAction &act);
96struct ForkLaunchInfo {
97 ForkLaunchInfo(
const ProcessLaunchInfo &info,
int error_fd);
99 bool separate_process_group;
103 std::string executable;
105 Environment::Envp envp;
106 std::vector<ForkFileAction> actions;
107 std::vector<int> preserved_fds;
109 bool keep_fd(
int fd)
const {
110 return std::binary_search(preserved_fds.begin(), preserved_fds.end(), fd);
116#if defined(__FreeBSD__) && __FreeBSD_version >= 1202000
121 for (
int fd : info.preserved_fds) {
122 if (first <
static_cast<unsigned>(fd) &&
123 close_range(first, fd - 1, 0) != 0) {
127 first =
static_cast<unsigned>(fd) + 1;
129 if (closed && close_range(first, UINT_MAX, 0) == 0)
137 const llvm::StringRef proc_fd_path =
"/proc/self/fd";
140 ec = llvm::sys::fs::is_directory(proc_fd_path, result);
142 std::vector<int> files_to_close;
144 for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
145 iter != file_end && !ec; iter.increment(ec)) {
146 int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
148 if (!info.keep_fd(fd))
149 files_to_close.push_back(fd);
151 for (
int file_to_close : files_to_close)
152 close(file_to_close);
155 int max_fd = sysconf(_SC_OPEN_MAX);
156 for (
int fd = 0; fd < max_fd; ++fd)
157 if (!info.keep_fd(fd))
162[[noreturn]]
static void ChildFunc(
int error_fd,
const ForkLaunchInfo &info) {
163 if (info.separate_process_group) {
164 if (setpgid(0, 0) != 0)
168 for (
const ForkFileAction &action : info.actions) {
169 switch (action.action) {
171 if (close(action.fd) != 0)
175 if (action.fd != action.arg) {
176 if (dup2(action.fd, action.arg) == -1)
179 if (fcntl(action.fd, F_SETFD,
180 fcntl(action.fd, F_GETFD) & ~FD_CLOEXEC) == -1)
185 DupDescriptor(error_fd, action.path.c_str(), action.fd, action.arg);
193 if (!info.wd.empty() && 0 != ::chdir(info.wd.c_str()))
196 if (info.disable_aslr)
202 if (sigemptyset(&set) != 0 ||
203 pthread_sigmask(SIG_SETMASK, &set,
nullptr) != 0)
208 if (setgid(getgid()) != 0)
219 if (ptrace64(PT_TRACE_ME, 0, 0, 0,
nullptr) == -1)
221 if (ptrace(PT_TRACE_ME, 0,
nullptr, 0) == -1)
227 execve(info.executable.c_str(),
const_cast<char *
const *
>(info.argv),
230#if defined(__linux__)
231 if (errno == ETXTBSY) {
241 execve(info.executable.c_str(),
const_cast<char *
const *
>(info.argv),
253ForkFileAction::ForkFileAction(
const FileAction &act)
254 : action(act.GetAction()), fd(act.GetFD()),
255 path(act.GetFileSpec().GetPath()), arg(act.GetActionArgument()) {}
257static std::vector<ForkFileAction>
259 std::vector<ForkFileAction> result;
266 : separate_process_group(
267 info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)),
268 debug(info.GetFlags().Test(eLaunchFlagDebug)),
269 disable_aslr(info.GetFlags().Test(eLaunchFlagDisableASLR)),
270 wd(info.GetWorkingDirectory().GetPath()),
271 executable(info.GetExecutableFile().GetPath()),
272 argv(info.GetArguments().GetConstArgumentVector()),
273 envp(info.GetEnvironment().getEnvp()), actions(
MakeForkActions(info)) {
278 preserved_fds = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
279 auto preserve = [&](
int fd) {
281 preserved_fds.push_back(fd);
284 for (
const ForkFileAction &action : actions) {
287 preserve(action.arg);
289 std::sort(preserved_fds.begin(), preserved_fds.end());
290 preserved_fds.erase(std::unique(preserved_fds.begin(), preserved_fds.end()),
291 preserved_fds.end());
303 const ForkLaunchInfo fork_launch_info(launch_info,
310 "Fork failed with error message: {0}", llvm::sys::StrError());
322 llvm::SmallString<0> buf;
327 buf.resize_for_overwrite(pos + 100);
329 buf.begin() + pos, buf.size() - pos);
339 llvm::sys::RetryAfterSignal(-1, waitpid, pid,
nullptr, 0);
static llvm::raw_ostream & error(Stream &strm)
static void ExitWithError(int error_fd, const char *operation)
static void DisableASLR(int error_fd)
static std::vector< ForkFileAction > MakeForkActions(const ProcessLaunchInfo &info)
static void DupDescriptor(int error_fd, const char *file, int fd, int flags)
static void write_string(int error_fd, const char *str)
static void ChildFunc(int error_fd, const ForkLaunchInfo &info)
static void CloseFileDescriptors(const ForkLaunchInfo &info)
Represents a file descriptor action to be performed during process launch.
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
A posix-based implementation of Pipe, a class that abtracts unix style pipes.
int GetReadFileDescriptor() const override
void CloseWriteFileDescriptor() override
Status CreateNew() override
int ReleaseWriteFileDescriptor() override
void CloseReadFileDescriptor() override
int GetWriteFileDescriptor() const override
const FileAction * GetFileActionAtIndex(size_t idx) const
size_t GetNumFileActions() const
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.