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