1 //===-- ProcessLauncherLinux.cpp --------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 11 #include "lldb/Host/Host.h" 12 #include "lldb/Host/HostProcess.h" 13 #include "lldb/Host/Pipe.h" 14 #include "lldb/Target/ProcessLaunchInfo.h" 15 #include "lldb/Utility/FileSpec.h" 16 #include "lldb/Utility/Log.h" 17 #include "llvm/Support/Errno.h" 18 19 #include <limits.h> 20 #include <sys/ptrace.h> 21 #include <sys/wait.h> 22 23 #include <sstream> 24 25 #ifdef __ANDROID__ 26 #include <android/api-level.h> 27 #define PT_TRACE_ME PTRACE_TRACEME 28 #endif 29 30 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21 31 #include <linux/personality.h> 32 #elif defined(__linux__) 33 #include <sys/personality.h> 34 #endif 35 36 using namespace lldb; 37 using namespace lldb_private; 38 39 static void FixupEnvironment(Args &env) { 40 #ifdef __ANDROID__ 41 // If there is no PATH variable specified inside the environment then set the 42 // path to /system/bin. It is required because the default path used by 43 // execve() is wrong on android. 44 static const char *path = "PATH="; 45 for (auto &entry : env.entries()) { 46 if (entry.ref.startswith(path)) 47 return; 48 } 49 env.AppendArgument(llvm::StringRef("PATH=/system/bin")); 50 #endif 51 } 52 53 static void LLVM_ATTRIBUTE_NORETURN ExitWithError(int error_fd, 54 const char *operation) { 55 int err = errno; 56 llvm::raw_fd_ostream os(error_fd, true); 57 os << operation << " failed: " << llvm::sys::StrError(err); 58 os.flush(); 59 _exit(1); 60 } 61 62 static void DisableASLRIfRequested(int error_fd, const ProcessLaunchInfo &info) { 63 #if defined(__linux__) 64 if (info.GetFlags().Test(lldb::eLaunchFlagDisableASLR)) { 65 const unsigned long personality_get_current = 0xffffffff; 66 int value = personality(personality_get_current); 67 if (value == -1) 68 ExitWithError(error_fd, "personality get"); 69 70 value = personality(ADDR_NO_RANDOMIZE | value); 71 if (value == -1) 72 ExitWithError(error_fd, "personality set"); 73 } 74 #endif 75 } 76 77 static void DupDescriptor(int error_fd, const FileSpec &file_spec, int fd, 78 int flags) { 79 int target_fd = ::open(file_spec.GetCString(), flags, 0666); 80 81 if (target_fd == -1) 82 ExitWithError(error_fd, "DupDescriptor-open"); 83 84 if (target_fd == fd) 85 return; 86 87 if (::dup2(target_fd, fd) == -1) 88 ExitWithError(error_fd, "DupDescriptor-dup2"); 89 90 ::close(target_fd); 91 return; 92 } 93 94 static void LLVM_ATTRIBUTE_NORETURN ChildFunc(int error_fd, 95 const ProcessLaunchInfo &info) { 96 // First, make sure we disable all logging. If we are logging to stdout, our 97 // logs can be mistaken for inferior output. 98 Log::DisableAllLogChannels(); 99 100 // Do not inherit setgid powers. 101 if (setgid(getgid()) != 0) 102 ExitWithError(error_fd, "setgid"); 103 104 if (info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)) { 105 if (setpgid(0, 0) != 0) 106 ExitWithError(error_fd, "setpgid"); 107 } 108 109 for (size_t i = 0; i < info.GetNumFileActions(); ++i) { 110 const FileAction &action = *info.GetFileActionAtIndex(i); 111 switch (action.GetAction()) { 112 case FileAction::eFileActionClose: 113 if (close(action.GetFD()) != 0) 114 ExitWithError(error_fd, "close"); 115 break; 116 case FileAction::eFileActionDuplicate: 117 if (dup2(action.GetFD(), action.GetActionArgument()) == -1) 118 ExitWithError(error_fd, "dup2"); 119 break; 120 case FileAction::eFileActionOpen: 121 DupDescriptor(error_fd, action.GetFileSpec(), action.GetFD(), 122 action.GetActionArgument()); 123 break; 124 case FileAction::eFileActionNone: 125 break; 126 } 127 } 128 129 const char **argv = info.GetArguments().GetConstArgumentVector(); 130 131 // Change working directory 132 if (info.GetWorkingDirectory() && 133 0 != ::chdir(info.GetWorkingDirectory().GetCString())) 134 ExitWithError(error_fd, "chdir"); 135 136 DisableASLRIfRequested(error_fd, info); 137 Args env = info.GetEnvironmentEntries(); 138 FixupEnvironment(env); 139 const char **envp = env.GetConstArgumentVector(); 140 141 // Clear the signal mask to prevent the child from being affected by 142 // any masking done by the parent. 143 sigset_t set; 144 if (sigemptyset(&set) != 0 || 145 pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0) 146 ExitWithError(error_fd, "pthread_sigmask"); 147 148 if (info.GetFlags().Test(eLaunchFlagDebug)) { 149 // HACK: 150 // Close everything besides stdin, stdout, and stderr that has no file 151 // action to avoid leaking. Only do this when debugging, as elsewhere we 152 // actually rely on 153 // passing open descriptors to child processes. 154 for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) 155 if (!info.GetFileActionForFD(fd) && fd != error_fd) 156 close(fd); 157 158 // Start tracing this child that is about to exec. 159 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1) 160 ExitWithError(error_fd, "ptrace"); 161 } 162 163 // Execute. We should never return... 164 execve(argv[0], const_cast<char *const *>(argv), 165 const_cast<char *const *>(envp)); 166 167 #if defined(__linux__) 168 if (errno == ETXTBSY) { 169 // On android M and earlier we can get this error because the adb deamon can 170 // hold a write 171 // handle on the executable even after it has finished uploading it. This 172 // state lasts 173 // only a short time and happens only when there are many concurrent adb 174 // commands being 175 // issued, such as when running the test suite. (The file remains open when 176 // someone does 177 // an "adb shell" command in the fork() child before it has had a chance to 178 // exec.) Since 179 // this state should clear up quickly, wait a while and then give it one 180 // more go. 181 usleep(50000); 182 execve(argv[0], const_cast<char *const *>(argv), 183 const_cast<char *const *>(envp)); 184 } 185 #endif 186 187 // ...unless exec fails. In which case we definitely need to end the child 188 // here. 189 ExitWithError(error_fd, "execve"); 190 } 191 192 HostProcess 193 ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info, 194 Status &error) { 195 char exe_path[PATH_MAX]; 196 launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path)); 197 198 // A pipe used by the child process to report errors. 199 PipePosix pipe; 200 const bool child_processes_inherit = false; 201 error = pipe.CreateNew(child_processes_inherit); 202 if (error.Fail()) 203 return HostProcess(); 204 205 ::pid_t pid = ::fork(); 206 if (pid == -1) { 207 // Fork failed 208 error.SetErrorStringWithFormatv("Fork failed with error message: {0}", 209 llvm::sys::StrError()); 210 return HostProcess(LLDB_INVALID_PROCESS_ID); 211 } 212 if (pid == 0) { 213 // child process 214 pipe.CloseReadFileDescriptor(); 215 ChildFunc(pipe.ReleaseWriteFileDescriptor(), launch_info); 216 } 217 218 // parent process 219 220 pipe.CloseWriteFileDescriptor(); 221 char buf[1000]; 222 int r = read(pipe.GetReadFileDescriptor(), buf, sizeof buf); 223 224 if (r == 0) 225 return HostProcess(pid); // No error. We're done. 226 227 error.SetErrorString(buf); 228 229 waitpid(pid, nullptr, 0); 230 231 return HostProcess(); 232 } 233