1 //===-- ProcessLauncherPosixFork.cpp ----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
10 #include "lldb/Host/Host.h"
11 #include "lldb/Host/HostProcess.h"
12 #include "lldb/Host/Pipe.h"
13 #include "lldb/Host/ProcessLaunchInfo.h"
14 #include "lldb/Utility/FileSpec.h"
15 #include "lldb/Utility/Log.h"
16 #include "llvm/Support/Errno.h"
19 #include <sys/ptrace.h>
27 #include <android/api-level.h>
28 #define PT_TRACE_ME PTRACE_TRACEME
31 #if defined(__ANDROID_API__) && __ANDROID_API__ < 15
32 #include <linux/personality.h>
33 #elif defined(__linux__)
34 #include <sys/personality.h>
38 using namespace lldb_private;
40 static void FixupEnvironment(Environment &env) {
42 // If there is no PATH variable specified inside the environment then set the
43 // path to /system/bin. It is required because the default path used by
44 // execve() is wrong on android.
45 env.try_emplace("PATH", "/system/bin");
49 static void LLVM_ATTRIBUTE_NORETURN ExitWithError(int error_fd,
50 const char *operation) {
52 llvm::raw_fd_ostream os(error_fd, true);
53 os << operation << " failed: " << llvm::sys::StrError(err);
58 static void DisableASLRIfRequested(int error_fd, const ProcessLaunchInfo &info) {
59 #if defined(__linux__)
60 if (info.GetFlags().Test(lldb::eLaunchFlagDisableASLR)) {
61 const unsigned long personality_get_current = 0xffffffff;
62 int value = personality(personality_get_current);
64 ExitWithError(error_fd, "personality get");
66 value = personality(ADDR_NO_RANDOMIZE | value);
68 ExitWithError(error_fd, "personality set");
73 static void DupDescriptor(int error_fd, const FileSpec &file_spec, int fd,
75 int target_fd = llvm::sys::RetryAfterSignal(-1, ::open,
76 file_spec.GetCString(), flags, 0666);
79 ExitWithError(error_fd, "DupDescriptor-open");
84 if (::dup2(target_fd, fd) == -1)
85 ExitWithError(error_fd, "DupDescriptor-dup2");
91 static void LLVM_ATTRIBUTE_NORETURN ChildFunc(int error_fd,
92 const ProcessLaunchInfo &info) {
93 if (info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)) {
94 if (setpgid(0, 0) != 0)
95 ExitWithError(error_fd, "setpgid");
98 for (size_t i = 0; i < info.GetNumFileActions(); ++i) {
99 const FileAction &action = *info.GetFileActionAtIndex(i);
100 switch (action.GetAction()) {
101 case FileAction::eFileActionClose:
102 if (close(action.GetFD()) != 0)
103 ExitWithError(error_fd, "close");
105 case FileAction::eFileActionDuplicate:
106 if (dup2(action.GetFD(), action.GetActionArgument()) == -1)
107 ExitWithError(error_fd, "dup2");
109 case FileAction::eFileActionOpen:
110 DupDescriptor(error_fd, action.GetFileSpec(), action.GetFD(),
111 action.GetActionArgument());
113 case FileAction::eFileActionNone:
118 const char **argv = info.GetArguments().GetConstArgumentVector();
120 // Change working directory
121 if (info.GetWorkingDirectory() &&
122 0 != ::chdir(info.GetWorkingDirectory().GetCString()))
123 ExitWithError(error_fd, "chdir");
125 DisableASLRIfRequested(error_fd, info);
126 Environment env = info.GetEnvironment();
127 FixupEnvironment(env);
128 Environment::Envp envp = env.getEnvp();
130 // Clear the signal mask to prevent the child from being affected by any
131 // masking done by the parent.
133 if (sigemptyset(&set) != 0 ||
134 pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0)
135 ExitWithError(error_fd, "pthread_sigmask");
137 if (info.GetFlags().Test(eLaunchFlagDebug)) {
138 // Do not inherit setgid powers.
139 if (setgid(getgid()) != 0)
140 ExitWithError(error_fd, "setgid");
143 // Close everything besides stdin, stdout, and stderr that has no file
144 // action to avoid leaking. Only do this when debugging, as elsewhere we
145 // actually rely on passing open descriptors to child processes.
146 for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
147 if (!info.GetFileActionForFD(fd) && fd != error_fd)
150 // Start tracing this child that is about to exec.
151 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
152 ExitWithError(error_fd, "ptrace");
155 // Execute. We should never return...
156 execve(argv[0], const_cast<char *const *>(argv), envp);
158 #if defined(__linux__)
159 if (errno == ETXTBSY) {
160 // On android M and earlier we can get this error because the adb daemon
161 // can hold a write handle on the executable even after it has finished
162 // uploading it. This state lasts only a short time and happens only when
163 // there are many concurrent adb commands being issued, such as when
164 // running the test suite. (The file remains open when someone does an "adb
165 // shell" command in the fork() child before it has had a chance to exec.)
166 // Since this state should clear up quickly, wait a while and then give it
169 execve(argv[0], const_cast<char *const *>(argv), envp);
173 // ...unless exec fails. In which case we definitely need to end the child
175 ExitWithError(error_fd, "execve");
179 ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info,
181 char exe_path[PATH_MAX];
182 launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
184 // A pipe used by the child process to report errors.
186 const bool child_processes_inherit = false;
187 error = pipe.CreateNew(child_processes_inherit);
189 return HostProcess();
191 ::pid_t pid = ::fork();
194 error.SetErrorStringWithFormatv("Fork failed with error message: {0}",
195 llvm::sys::StrError());
196 return HostProcess(LLDB_INVALID_PROCESS_ID);
200 pipe.CloseReadFileDescriptor();
201 ChildFunc(pipe.ReleaseWriteFileDescriptor(), launch_info);
206 pipe.CloseWriteFileDescriptor();
208 int r = read(pipe.GetReadFileDescriptor(), buf, sizeof buf);
211 return HostProcess(pid); // No error. We're done.
213 error.SetErrorString(buf);
215 llvm::sys::RetryAfterSignal(-1, waitpid, pid, nullptr, 0);
217 return HostProcess();