blob: fca92923825f01207a2b2843988aa27ca70aa545 [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
Daniel Dunbardefc8532009-08-03 05:02:46 +000037Program::Program() : Pid_(0) {}
38
39Program::~Program() {}
40
Reid Spencer52a7efa2004-08-29 19:20:41 +000041// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000042Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000043Program::FindProgramByName(const std::string& progName) {
44
45 // Check some degenerate cases
46 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000047 return Path();
48 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000049 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000050 return Path();
Benjamin Kramerb0facb12009-07-28 22:08:15 +000051 // Use the given path verbatim if it contains any slashes; this matches
52 // the behavior of sh(1) and friends.
Dan Gohmana87861e2009-07-28 23:25:18 +000053 if (progName.find('/') != std::string::npos)
Reid Spencer52a7efa2004-08-29 19:20:41 +000054 return temp;
55
Dan Gohman88abb892009-08-05 17:32:39 +000056 // At this point, the file name does not contain slashes. Search for it
57 // through the directories specified in the PATH environment variable.
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000058
Reid Spencer52a7efa2004-08-29 19:20:41 +000059 // Get the path. If its empty, we can't do anything to find it.
60 const char *PathStr = getenv("PATH");
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000061 if (PathStr == 0)
Reid Spencer25659432004-09-13 21:48:44 +000062 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000063
64 // Now we have a colon separated list of directories to search; try them.
Evan Cheng34cd4a42008-05-05 18:30:58 +000065 size_t PathLen = strlen(PathStr);
Reid Spencer52a7efa2004-08-29 19:20:41 +000066 while (PathLen) {
67 // Find the first colon...
68 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
69
70 // Check to see if this first directory contains the executable...
Reid Spencer25659432004-09-13 21:48:44 +000071 Path FilePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000072 if (FilePath.set(std::string(PathStr,Colon))) {
73 FilePath.appendComponent(progName);
Reid Spencerc7f08322005-07-07 18:21:42 +000074 if (FilePath.canExecute())
Reid Spencer52a7efa2004-08-29 19:20:41 +000075 return FilePath; // Found the executable!
76 }
77
78 // Nope it wasn't in this directory, check the next path in the list!
79 PathLen -= Colon-PathStr;
80 PathStr = Colon;
81
82 // Advance past duplicate colons
83 while (*PathStr == ':') {
84 PathStr++;
85 PathLen--;
86 }
87 }
Reid Spencer25659432004-09-13 21:48:44 +000088 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000089}
90
Matthijs Kooijman905261e2008-06-12 10:47:18 +000091static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
92 if (Path == 0)
93 // Noop
94 return false;
95 std::string File;
96 if (Path->isEmpty())
97 // Redirect empty paths to /dev/null
98 File = "/dev/null";
99 else
Chris Lattner74382b72009-08-23 22:45:37 +0000100 File = Path->str();
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000101
102 // Open the file
103 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
104 if (InFD == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000105 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +0000106 + (FD == 0 ? "input" : "output"));
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000107 return true;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000108 }
109
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000110 // Install it as the requested FD
111 if (-1 == dup2(InFD, FD)) {
112 MakeErrMsg(ErrMsg, "Cannot dup2");
113 return true;
114 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000115 close(InFD); // Close the original FD
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000116 return false;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000117}
118
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000119static void SetMemoryLimits (unsigned size)
120{
121#if HAVE_SYS_RESOURCE_H
122 struct rlimit r;
123 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
124
125 // Heap size
126 getrlimit (RLIMIT_DATA, &r);
127 r.rlim_cur = limit;
128 setrlimit (RLIMIT_DATA, &r);
Gabor Greifaa6b7fd2007-07-06 10:31:27 +0000129#ifdef RLIMIT_RSS
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000130 // Resident set size.
131 getrlimit (RLIMIT_RSS, &r);
132 r.rlim_cur = limit;
133 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000134#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000135#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000136 // Virtual memory.
137 getrlimit (RLIMIT_AS, &r);
138 r.rlim_cur = limit;
139 setrlimit (RLIMIT_AS, &r);
140#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000141#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000142}
143
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000144bool
145Program::Execute(const Path& path,
146 const char** args,
147 const char** envp,
148 const Path** redirects,
149 unsigned memoryLimit,
150 std::string* ErrMsg)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000151{
152 if (!path.canExecute()) {
153 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000154 *ErrMsg = path.str() + " is not executable";
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000155 return false;
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000156 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000157
Reid Spencer52a7efa2004-08-29 19:20:41 +0000158 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000159 int child = fork();
160 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000161 // An error occured: Return to the caller.
162 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000163 MakeErrMsg(ErrMsg, "Couldn't fork");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000164 return false;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000165
166 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000167 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000168 // Redirect file descriptors...
169 if (redirects) {
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000170 // Redirect stdin
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000171 if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000172 // Redirect stdout
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000173 if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000174 if (redirects[1] && redirects[2] &&
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000175 *(redirects[1]) == *(redirects[2])) {
176 // If stdout and stderr should go to the same place, redirect stderr
177 // to the FD already open for stdout.
178 if (-1 == dup2(1,2)) {
179 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000180 return false;
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000181 }
182 } else {
183 // Just redirect stderr
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000184 if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000185 }
186 }
187
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000188 // Set memory limits
189 if (memoryLimit!=0) {
190 SetMemoryLimits(memoryLimit);
191 }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000192
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000193 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000194 if (envp != 0)
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000195 execve(path.c_str(), (char**)args, (char**)envp);
Evan Cheng2fafaf12006-06-09 20:43:11 +0000196 else
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000197 execv(path.c_str(), (char**)args);
Dan Gohman33968522009-08-05 00:09:12 +0000198 // If the execve() failed, we should exit. Follow Unix protocol and
199 // return 127 if the executable was not found, and 126 otherwise.
200 // Use _exit rather than exit so that atexit functions and static
201 // object destructors cloned from the parent process aren't
202 // redundantly run, and so that any data buffered in stdio buffers
203 // cloned from the parent aren't redundantly written out.
204 _exit(errno == ENOENT ? 127 : 126);
Reid Spencere2e24112004-12-14 04:18:51 +0000205 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000206
207 // Parent process: Break out of the switch to do our processing.
208 default:
209 break;
210 }
211
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000212 Pid_ = child;
213
214 return true;
215}
216
217int
218Program::Wait(unsigned secondsToWait,
219 std::string* ErrMsg)
220{
221#ifdef HAVE_SYS_WAIT_H
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000222 struct sigaction Act, Old;
223
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000224 if (Pid_ == 0) {
225 MakeErrMsg(ErrMsg, "Process not started!");
226 return -1;
227 }
228
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000229 // Install a timeout handler.
230 if (secondsToWait) {
Duncan Sands3429c5e2009-09-07 05:58:25 +0000231 memset(&Act, 0, sizeof(Act));
232 Act.sa_handler = SIG_IGN;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000233 sigemptyset(&Act.sa_mask);
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000234 sigaction(SIGALRM, &Act, &Old);
235 alarm(secondsToWait);
236 }
237
Reid Spencer52a7efa2004-08-29 19:20:41 +0000238 // Parent process: Wait for the child process to terminate.
239 int status;
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000240 int child = this->Pid_;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000241 while (wait(&status) != child)
242 if (secondsToWait && errno == EINTR) {
243 // Kill the child.
244 kill(child, SIGKILL);
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000245
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000246 // Turn off the alarm and restore the signal handler
247 alarm(0);
248 sigaction(SIGALRM, &Old, 0);
249
250 // Wait for child to die
251 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000252 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Devang Patela1e4bba2008-02-04 20:57:54 +0000253 else
254 MakeErrMsg(ErrMsg, "Child timed out", 0);
255
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000256 return -1; // Timeout detected
Devang Patela1e4bba2008-02-04 20:57:54 +0000257 } else if (errno != EINTR) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000258 MakeErrMsg(ErrMsg, "Error waiting for child process");
259 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000260 }
261
262 // We exited normally without timeout, so turn off the timer.
263 if (secondsToWait) {
264 alarm(0);
265 sigaction(SIGALRM, &Old, 0);
266 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000267
Reid Spencerd555f412005-12-22 20:00:16 +0000268 // Return the proper exit status. 0=success, >0 is programs' exit status,
269 // <0 means a signal was returned, -9999999 means the program dumped core.
270 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000271 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000272 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000273 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000274 result = 0 - WTERMSIG(status);
275#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000276 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000277 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000278#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000279 return result;
280#else
281 return -99;
282#endif
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000283
Reid Spencer52a7efa2004-08-29 19:20:41 +0000284}
285
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000286bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000287 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000288 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000289}
290
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000291bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000292 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000293 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000294}
295
Reid Spencer52a7efa2004-08-29 19:20:41 +0000296}