blob: ce757105f88627fcb233a06f3d29a61542157418 [file] [log] [blame]
Reid Spencer52a7efa2004-08-29 19:20:41 +00001//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
Mikhail Glushenkov234f6892009-07-17 20:38:17 +00002//
Reid Spencer52a7efa2004-08-29 19:20:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkov234f6892009-07-17 20:38:17 +00007//
Reid Spencer52a7efa2004-08-29 19:20:41 +00008//===----------------------------------------------------------------------===//
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
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include <llvm/Config/config.h>
Reid Spencer52a7efa2004-08-29 19:20:41 +000020#include "Unix.h"
Reid Spencercdf54d02004-12-27 06:16:52 +000021#if HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000024#if HAVE_SYS_RESOURCE_H
25#include <sys/resource.h>
26#endif
Reid Spencercdf54d02004-12-27 06:16:52 +000027#if HAVE_SIGNAL_H
28#include <signal.h>
29#endif
30#if HAVE_FCNTL_H
31#include <fcntl.h>
32#endif
Reid Spencer52a7efa2004-08-29 19:20:41 +000033
Reid Spencer52a7efa2004-08-29 19:20:41 +000034namespace llvm {
35using namespace sys;
36
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +000037Program::Program() : Data_(0) {}
Daniel Dunbardefc8532009-08-03 05:02:46 +000038
39Program::~Program() {}
40
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +000041unsigned Program::GetPid() {
42 return reinterpret_cast<unsigned>(Data_);
43}
44
Reid Spencer52a7efa2004-08-29 19:20:41 +000045// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000046Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000047Program::FindProgramByName(const std::string& progName) {
48
49 // Check some degenerate cases
50 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000051 return Path();
52 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000053 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000054 return Path();
Benjamin Kramerb0facb12009-07-28 22:08:15 +000055 // Use the given path verbatim if it contains any slashes; this matches
56 // the behavior of sh(1) and friends.
Dan Gohmana87861e2009-07-28 23:25:18 +000057 if (progName.find('/') != std::string::npos)
Reid Spencer52a7efa2004-08-29 19:20:41 +000058 return temp;
59
Dan Gohman88abb892009-08-05 17:32:39 +000060 // At this point, the file name does not contain slashes. Search for it
61 // through the directories specified in the PATH environment variable.
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000062
Reid Spencer52a7efa2004-08-29 19:20:41 +000063 // Get the path. If its empty, we can't do anything to find it.
64 const char *PathStr = getenv("PATH");
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000065 if (PathStr == 0)
Reid Spencer25659432004-09-13 21:48:44 +000066 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000067
68 // Now we have a colon separated list of directories to search; try them.
Evan Cheng34cd4a42008-05-05 18:30:58 +000069 size_t PathLen = strlen(PathStr);
Reid Spencer52a7efa2004-08-29 19:20:41 +000070 while (PathLen) {
71 // Find the first colon...
72 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
73
74 // Check to see if this first directory contains the executable...
Reid Spencer25659432004-09-13 21:48:44 +000075 Path FilePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000076 if (FilePath.set(std::string(PathStr,Colon))) {
77 FilePath.appendComponent(progName);
Reid Spencerc7f08322005-07-07 18:21:42 +000078 if (FilePath.canExecute())
Reid Spencer52a7efa2004-08-29 19:20:41 +000079 return FilePath; // Found the executable!
80 }
81
82 // Nope it wasn't in this directory, check the next path in the list!
83 PathLen -= Colon-PathStr;
84 PathStr = Colon;
85
86 // Advance past duplicate colons
87 while (*PathStr == ':') {
88 PathStr++;
89 PathLen--;
90 }
91 }
Reid Spencer25659432004-09-13 21:48:44 +000092 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000093}
94
Matthijs Kooijman905261e2008-06-12 10:47:18 +000095static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
96 if (Path == 0)
97 // Noop
98 return false;
99 std::string File;
100 if (Path->isEmpty())
101 // Redirect empty paths to /dev/null
102 File = "/dev/null";
103 else
Chris Lattner74382b72009-08-23 22:45:37 +0000104 File = Path->str();
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000105
106 // Open the file
107 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
108 if (InFD == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000109 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +0000110 + (FD == 0 ? "input" : "output"));
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000111 return true;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000112 }
113
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000114 // Install it as the requested FD
115 if (-1 == dup2(InFD, FD)) {
116 MakeErrMsg(ErrMsg, "Cannot dup2");
117 return true;
118 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000119 close(InFD); // Close the original FD
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000120 return false;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000121}
122
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000123static void SetMemoryLimits (unsigned size)
124{
125#if HAVE_SYS_RESOURCE_H
126 struct rlimit r;
127 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
128
129 // Heap size
130 getrlimit (RLIMIT_DATA, &r);
131 r.rlim_cur = limit;
132 setrlimit (RLIMIT_DATA, &r);
Gabor Greifaa6b7fd2007-07-06 10:31:27 +0000133#ifdef RLIMIT_RSS
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000134 // Resident set size.
135 getrlimit (RLIMIT_RSS, &r);
136 r.rlim_cur = limit;
137 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000138#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000139#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000140 // Virtual memory.
141 getrlimit (RLIMIT_AS, &r);
142 r.rlim_cur = limit;
143 setrlimit (RLIMIT_AS, &r);
144#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000145#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000146}
147
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000148bool
149Program::Execute(const Path& path,
150 const char** args,
151 const char** envp,
152 const Path** redirects,
153 unsigned memoryLimit,
154 std::string* ErrMsg)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000155{
156 if (!path.canExecute()) {
157 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000158 *ErrMsg = path.str() + " is not executable";
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000159 return false;
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000160 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000161
Reid Spencer52a7efa2004-08-29 19:20:41 +0000162 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000163 int child = fork();
164 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000165 // An error occured: Return to the caller.
166 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000167 MakeErrMsg(ErrMsg, "Couldn't fork");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000168 return false;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000169
170 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000171 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000172 // Redirect file descriptors...
173 if (redirects) {
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000174 // Redirect stdin
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000175 if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000176 // Redirect stdout
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000177 if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000178 if (redirects[1] && redirects[2] &&
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000179 *(redirects[1]) == *(redirects[2])) {
180 // If stdout and stderr should go to the same place, redirect stderr
181 // to the FD already open for stdout.
182 if (-1 == dup2(1,2)) {
183 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000184 return false;
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000185 }
186 } else {
187 // Just redirect stderr
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000188 if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000189 }
190 }
191
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000192 // Set memory limits
193 if (memoryLimit!=0) {
194 SetMemoryLimits(memoryLimit);
195 }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000196
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000197 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000198 if (envp != 0)
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000199 execve(path.c_str(), (char**)args, (char**)envp);
Evan Cheng2fafaf12006-06-09 20:43:11 +0000200 else
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000201 execv(path.c_str(), (char**)args);
Dan Gohman33968522009-08-05 00:09:12 +0000202 // If the execve() failed, we should exit. Follow Unix protocol and
203 // return 127 if the executable was not found, and 126 otherwise.
204 // Use _exit rather than exit so that atexit functions and static
205 // object destructors cloned from the parent process aren't
206 // redundantly run, and so that any data buffered in stdio buffers
207 // cloned from the parent aren't redundantly written out.
208 _exit(errno == ENOENT ? 127 : 126);
Reid Spencere2e24112004-12-14 04:18:51 +0000209 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000210
211 // Parent process: Break out of the switch to do our processing.
212 default:
213 break;
214 }
215
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +0000216 Data_ = reinterpret_cast<void*>(child);
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000217
218 return true;
219}
220
221int
222Program::Wait(unsigned secondsToWait,
223 std::string* ErrMsg)
224{
225#ifdef HAVE_SYS_WAIT_H
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000226 struct sigaction Act, Old;
227
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +0000228 if (Data_ == 0) {
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000229 MakeErrMsg(ErrMsg, "Process not started!");
230 return -1;
231 }
232
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000233 // Install a timeout handler.
234 if (secondsToWait) {
Duncan Sands3429c5e2009-09-07 05:58:25 +0000235 memset(&Act, 0, sizeof(Act));
236 Act.sa_handler = SIG_IGN;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000237 sigemptyset(&Act.sa_mask);
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000238 sigaction(SIGALRM, &Act, &Old);
239 alarm(secondsToWait);
240 }
241
Reid Spencer52a7efa2004-08-29 19:20:41 +0000242 // Parent process: Wait for the child process to terminate.
243 int status;
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +0000244 pid_t child = reinterpret_cast<pid_t>(Data_);
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000245 while (wait(&status) != child)
246 if (secondsToWait && errno == EINTR) {
247 // Kill the child.
248 kill(child, SIGKILL);
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000249
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000250 // Turn off the alarm and restore the signal handler
251 alarm(0);
252 sigaction(SIGALRM, &Old, 0);
253
254 // Wait for child to die
255 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000256 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Devang Patela1e4bba2008-02-04 20:57:54 +0000257 else
258 MakeErrMsg(ErrMsg, "Child timed out", 0);
259
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000260 return -1; // Timeout detected
Devang Patela1e4bba2008-02-04 20:57:54 +0000261 } else if (errno != EINTR) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000262 MakeErrMsg(ErrMsg, "Error waiting for child process");
263 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000264 }
265
266 // We exited normally without timeout, so turn off the timer.
267 if (secondsToWait) {
268 alarm(0);
269 sigaction(SIGALRM, &Old, 0);
270 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000271
Reid Spencerd555f412005-12-22 20:00:16 +0000272 // Return the proper exit status. 0=success, >0 is programs' exit status,
273 // <0 means a signal was returned, -9999999 means the program dumped core.
274 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000275 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000276 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000277 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000278 result = 0 - WTERMSIG(status);
279#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000280 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000281 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000282#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000283 return result;
284#else
285 return -99;
286#endif
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000287
Reid Spencer52a7efa2004-08-29 19:20:41 +0000288}
289
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000290bool
291Program::Kill(std::string* ErrMsg) {
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +0000292 if (Data_ == 0) {
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000293 MakeErrMsg(ErrMsg, "Process not started!");
294 return true;
295 }
296
Mikhail Glushenkovedf8e482009-09-08 19:50:55 +0000297 pid_t pid = reinterpret_cast<pid_t>(Data_);
298 return (kill(pid, SIGKILL) == 0);
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000299}
300
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000301bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000302 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000303 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000304}
305
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000306bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000307 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000308 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000309}
310
Reid Spencer52a7efa2004-08-29 19:20:41 +0000311}