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