Remove FindProgramByName. NFC.
[oota-llvm.git] / lib / Support / Unix / Program.inc
1 //===- llvm/Support/Unix/Program.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 // This file implements the Unix specific portion of the Program class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //===          is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
18
19 #include "Unix.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <llvm/Config/config.h>
25 #if HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif
28 #if HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
30 #endif
31 #if HAVE_SIGNAL_H
32 #include <signal.h>
33 #endif
34 #if HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #ifdef HAVE_POSIX_SPAWN
41 #ifdef __sun__
42 #define  _RESTRICT_KYWD
43 #endif
44 #include <spawn.h>
45 #if !defined(__APPLE__)
46   extern char **environ;
47 #else
48 #include <crt_externs.h> // _NSGetEnviron
49 #endif
50 #endif
51
52 namespace llvm {
53
54 using namespace sys;
55
56 ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
57
58 ErrorOr<std::string> sys::findProgramByName(StringRef Name,
59                                             ArrayRef<StringRef> Paths) {
60   assert(!Name.empty() && "Must have a name!");
61   // Use the given path verbatim if it contains any slashes; this matches
62   // the behavior of sh(1) and friends.
63   if (Name.find('/') != StringRef::npos)
64     return std::string(Name);
65
66   if (Paths.empty()) {
67     SmallVector<StringRef, 16> SearchPaths;
68     SplitString(std::getenv("PATH"), SearchPaths, ":");
69     return findProgramByName(Name, SearchPaths);
70   }
71
72   for (auto Path : Paths) {
73     if (Path.empty())
74       continue;
75
76     // Check to see if this first directory contains the executable...
77     SmallString<128> FilePath(Path);
78     sys::path::append(FilePath, Name);
79     if (sys::fs::can_execute(FilePath.c_str()))
80       return std::string(FilePath.str()); // Found the executable!
81   }
82   return std::errc::no_such_file_or_directory;
83 }
84
85 static bool RedirectIO(const StringRef *Path, int FD, std::string* ErrMsg) {
86   if (!Path) // Noop
87     return false;
88   std::string File;
89   if (Path->empty())
90     // Redirect empty paths to /dev/null
91     File = "/dev/null";
92   else
93     File = *Path;
94
95   // Open the file
96   int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
97   if (InFD == -1) {
98     MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
99               + (FD == 0 ? "input" : "output"));
100     return true;
101   }
102
103   // Install it as the requested FD
104   if (dup2(InFD, FD) == -1) {
105     MakeErrMsg(ErrMsg, "Cannot dup2");
106     close(InFD);
107     return true;
108   }
109   close(InFD);      // Close the original FD
110   return false;
111 }
112
113 #ifdef HAVE_POSIX_SPAWN
114 static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
115                           posix_spawn_file_actions_t *FileActions) {
116   if (!Path) // Noop
117     return false;
118   const char *File;
119   if (Path->empty())
120     // Redirect empty paths to /dev/null
121     File = "/dev/null";
122   else
123     File = Path->c_str();
124
125   if (int Err = posix_spawn_file_actions_addopen(
126           FileActions, FD, File,
127           FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
128     return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
129   return false;
130 }
131 #endif
132
133 static void TimeOutHandler(int Sig) {
134 }
135
136 static void SetMemoryLimits (unsigned size)
137 {
138 #if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
139   struct rlimit r;
140   __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
141
142   // Heap size
143   getrlimit (RLIMIT_DATA, &r);
144   r.rlim_cur = limit;
145   setrlimit (RLIMIT_DATA, &r);
146 #ifdef RLIMIT_RSS
147   // Resident set size.
148   getrlimit (RLIMIT_RSS, &r);
149   r.rlim_cur = limit;
150   setrlimit (RLIMIT_RSS, &r);
151 #endif
152 #ifdef RLIMIT_AS  // e.g. NetBSD doesn't have it.
153   // Don't set virtual memory limit if built with any Sanitizer. They need 80Tb
154   // of virtual memory for shadow memory mapping.
155 #if !LLVM_MEMORY_SANITIZER_BUILD && !LLVM_ADDRESS_SANITIZER_BUILD
156   // Virtual memory.
157   getrlimit (RLIMIT_AS, &r);
158   r.rlim_cur = limit;
159   setrlimit (RLIMIT_AS, &r);
160 #endif
161 #endif
162 #endif
163 }
164
165 }
166
167 static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
168                     const char **envp, const StringRef **redirects,
169                     unsigned memoryLimit, std::string *ErrMsg) {
170   if (!llvm::sys::fs::exists(Program)) {
171     if (ErrMsg)
172       *ErrMsg = std::string("Executable \"") + Program.str() +
173                 std::string("\" doesn't exist!");
174     return false;
175   }
176
177   // If this OS has posix_spawn and there is no memory limit being implied, use
178   // posix_spawn.  It is more efficient than fork/exec.
179 #ifdef HAVE_POSIX_SPAWN
180   if (memoryLimit == 0) {
181     posix_spawn_file_actions_t FileActionsStore;
182     posix_spawn_file_actions_t *FileActions = nullptr;
183
184     // If we call posix_spawn_file_actions_addopen we have to make sure the
185     // c strings we pass to it stay alive until the call to posix_spawn,
186     // so we copy any StringRefs into this variable.
187     std::string RedirectsStorage[3];
188
189     if (redirects) {
190       std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
191       for (int I = 0; I < 3; ++I) {
192         if (redirects[I]) {
193           RedirectsStorage[I] = *redirects[I];
194           RedirectsStr[I] = &RedirectsStorage[I];
195         }
196       }
197
198       FileActions = &FileActionsStore;
199       posix_spawn_file_actions_init(FileActions);
200
201       // Redirect stdin/stdout.
202       if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
203           RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
204         return false;
205       if (redirects[1] == nullptr || redirects[2] == nullptr ||
206           *redirects[1] != *redirects[2]) {
207         // Just redirect stderr
208         if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
209           return false;
210       } else {
211         // If stdout and stderr should go to the same place, redirect stderr
212         // to the FD already open for stdout.
213         if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
214           return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
215       }
216     }
217
218     if (!envp)
219 #if !defined(__APPLE__)
220       envp = const_cast<const char **>(environ);
221 #else
222       // environ is missing in dylibs.
223       envp = const_cast<const char **>(*_NSGetEnviron());
224 #endif
225
226     // Explicitly initialized to prevent what appears to be a valgrind false
227     // positive.
228     pid_t PID = 0;
229     int Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
230                           /*attrp*/nullptr, const_cast<char **>(args),
231                           const_cast<char **>(envp));
232
233     if (FileActions)
234       posix_spawn_file_actions_destroy(FileActions);
235
236     if (Err)
237      return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
238
239     PI.Pid = PID;
240
241     return true;
242   }
243 #endif
244
245   // Create a child process.
246   int child = fork();
247   switch (child) {
248     // An error occurred:  Return to the caller.
249     case -1:
250       MakeErrMsg(ErrMsg, "Couldn't fork");
251       return false;
252
253     // Child process: Execute the program.
254     case 0: {
255       // Redirect file descriptors...
256       if (redirects) {
257         // Redirect stdin
258         if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
259         // Redirect stdout
260         if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
261         if (redirects[1] && redirects[2] &&
262             *(redirects[1]) == *(redirects[2])) {
263           // If stdout and stderr should go to the same place, redirect stderr
264           // to the FD already open for stdout.
265           if (-1 == dup2(1,2)) {
266             MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
267             return false;
268           }
269         } else {
270           // Just redirect stderr
271           if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
272         }
273       }
274
275       // Set memory limits
276       if (memoryLimit!=0) {
277         SetMemoryLimits(memoryLimit);
278       }
279
280       // Execute!
281       std::string PathStr = Program;
282       if (envp != nullptr)
283         execve(PathStr.c_str(),
284                const_cast<char **>(args),
285                const_cast<char **>(envp));
286       else
287         execv(PathStr.c_str(),
288               const_cast<char **>(args));
289       // If the execve() failed, we should exit. Follow Unix protocol and
290       // return 127 if the executable was not found, and 126 otherwise.
291       // Use _exit rather than exit so that atexit functions and static
292       // object destructors cloned from the parent process aren't
293       // redundantly run, and so that any data buffered in stdio buffers
294       // cloned from the parent aren't redundantly written out.
295       _exit(errno == ENOENT ? 127 : 126);
296     }
297
298     // Parent process: Break out of the switch to do our processing.
299     default:
300       break;
301   }
302
303   PI.Pid = child;
304
305   return true;
306 }
307
308 namespace llvm {
309
310 ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
311                       bool WaitUntilTerminates, std::string *ErrMsg) {
312 #ifdef HAVE_SYS_WAIT_H
313   struct sigaction Act, Old;
314   assert(PI.Pid && "invalid pid to wait on, process not started?");
315
316   int WaitPidOptions = 0;
317   pid_t ChildPid = PI.Pid;
318   if (WaitUntilTerminates) {
319     SecondsToWait = 0;
320   } else if (SecondsToWait) {
321     // Install a timeout handler.  The handler itself does nothing, but the
322     // simple fact of having a handler at all causes the wait below to return
323     // with EINTR, unlike if we used SIG_IGN.
324     memset(&Act, 0, sizeof(Act));
325     Act.sa_handler = TimeOutHandler;
326     sigemptyset(&Act.sa_mask);
327     sigaction(SIGALRM, &Act, &Old);
328     alarm(SecondsToWait);
329   } else if (SecondsToWait == 0)
330     WaitPidOptions = WNOHANG;
331
332   // Parent process: Wait for the child process to terminate.
333   int status;
334   ProcessInfo WaitResult;
335
336   do {
337     WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions);
338   } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
339
340   if (WaitResult.Pid != PI.Pid) {
341     if (WaitResult.Pid == 0) {
342       // Non-blocking wait.
343       return WaitResult;
344     } else {
345       if (SecondsToWait && errno == EINTR) {
346         // Kill the child.
347         kill(PI.Pid, SIGKILL);
348
349         // Turn off the alarm and restore the signal handler
350         alarm(0);
351         sigaction(SIGALRM, &Old, nullptr);
352
353         // Wait for child to die
354         if (wait(&status) != ChildPid)
355           MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
356         else
357           MakeErrMsg(ErrMsg, "Child timed out", 0);
358
359         WaitResult.ReturnCode = -2; // Timeout detected
360         return WaitResult;
361       } else if (errno != EINTR) {
362         MakeErrMsg(ErrMsg, "Error waiting for child process");
363         WaitResult.ReturnCode = -1;
364         return WaitResult;
365       }
366     }
367   }
368
369   // We exited normally without timeout, so turn off the timer.
370   if (SecondsToWait && !WaitUntilTerminates) {
371     alarm(0);
372     sigaction(SIGALRM, &Old, nullptr);
373   }
374
375   // Return the proper exit status. Detect error conditions
376   // so we can return -1 for them and set ErrMsg informatively.
377   int result = 0;
378   if (WIFEXITED(status)) {
379     result = WEXITSTATUS(status);
380     WaitResult.ReturnCode = result;
381
382     if (result == 127) {
383       if (ErrMsg)
384         *ErrMsg = llvm::sys::StrError(ENOENT);
385       WaitResult.ReturnCode = -1;
386       return WaitResult;
387     }
388     if (result == 126) {
389       if (ErrMsg)
390         *ErrMsg = "Program could not be executed";
391       WaitResult.ReturnCode = -1;
392       return WaitResult;
393     }
394   } else if (WIFSIGNALED(status)) {
395     if (ErrMsg) {
396       *ErrMsg = strsignal(WTERMSIG(status));
397 #ifdef WCOREDUMP
398       if (WCOREDUMP(status))
399         *ErrMsg += " (core dumped)";
400 #endif
401     }
402     // Return a special value to indicate that the process received an unhandled
403     // signal during execution as opposed to failing to execute.
404     WaitResult.ReturnCode = -2;
405   }
406 #else
407   if (ErrMsg)
408     *ErrMsg = "Program::Wait is not implemented on this platform yet!";
409   ProcessInfo WaitResult;
410   WaitResult.ReturnCode = -2;
411 #endif
412   return WaitResult;
413 }
414
415   std::error_code sys::ChangeStdinToBinary(){
416   // Do nothing, as Unix doesn't differentiate between text and binary.
417     return std::error_code();
418 }
419
420   std::error_code sys::ChangeStdoutToBinary(){
421   // Do nothing, as Unix doesn't differentiate between text and binary.
422     return std::error_code();
423 }
424
425 std::error_code
426 llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
427                                  WindowsEncodingMethod Encoding /*unused*/) {
428   std::error_code EC;
429   llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text);
430
431   if (EC)
432     return EC;
433
434   OS << Contents;
435
436   if (OS.has_error())
437     return std::make_error_code(std::errc::io_error);
438
439   return EC;
440 }
441
442 bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
443   static long ArgMax = sysconf(_SC_ARG_MAX);
444
445   // System says no practical limit.
446   if (ArgMax == -1)
447     return true;
448
449   // Conservatively account for space required by environment variables.
450   long HalfArgMax = ArgMax / 2;
451
452   size_t ArgLength = 0;
453   for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
454        I != E; ++I) {
455     ArgLength += strlen(*I) + 1;
456     if (ArgLength > size_t(HalfArgMax)) {
457       return false;
458     }
459   }
460   return true;
461 }
462 }