blob: 3a562e45d41d5df83e5c2b506929c3fa5d370ffb [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"
Bill Wendling64455372008-05-29 22:02:08 +000021#include <iostream>
Reid Spencercdf54d02004-12-27 06:16:52 +000022#if HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000025#if HAVE_SYS_RESOURCE_H
26#include <sys/resource.h>
27#endif
Reid Spencercdf54d02004-12-27 06:16:52 +000028#if HAVE_SIGNAL_H
29#include <signal.h>
30#endif
31#if HAVE_FCNTL_H
32#include <fcntl.h>
33#endif
Reid Spencer52a7efa2004-08-29 19:20:41 +000034
Reid Spencer52a7efa2004-08-29 19:20:41 +000035namespace llvm {
36using namespace sys;
37
Daniel Dunbardefc8532009-08-03 05:02:46 +000038Program::Program() : Pid_(0) {}
39
40Program::~Program() {}
41
Reid Spencer52a7efa2004-08-29 19:20:41 +000042// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000043Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000044Program::FindProgramByName(const std::string& progName) {
45
46 // Check some degenerate cases
47 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000048 return Path();
49 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000050 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000051 return Path();
Benjamin Kramerb0facb12009-07-28 22:08:15 +000052 // Use the given path verbatim if it contains any slashes; this matches
53 // the behavior of sh(1) and friends.
Dan Gohmana87861e2009-07-28 23:25:18 +000054 if (progName.find('/') != std::string::npos)
Reid Spencer52a7efa2004-08-29 19:20:41 +000055 return temp;
56
57 // At this point, the file name is valid and its not executable
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
100 File = Path->toString();
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
119static bool Timeout = false;
120static void TimeOutHandler(int Sig) {
121 Timeout = true;
122}
123
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000124static void SetMemoryLimits (unsigned size)
125{
126#if HAVE_SYS_RESOURCE_H
127 struct rlimit r;
128 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
129
130 // Heap size
131 getrlimit (RLIMIT_DATA, &r);
132 r.rlim_cur = limit;
133 setrlimit (RLIMIT_DATA, &r);
Gabor Greifaa6b7fd2007-07-06 10:31:27 +0000134#ifdef RLIMIT_RSS
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000135 // Resident set size.
136 getrlimit (RLIMIT_RSS, &r);
137 r.rlim_cur = limit;
138 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000139#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000140#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000141 // Virtual memory.
142 getrlimit (RLIMIT_AS, &r);
143 r.rlim_cur = limit;
144 setrlimit (RLIMIT_AS, &r);
145#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000146#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000147}
148
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000149bool
150Program::Execute(const Path& path,
151 const char** args,
152 const char** envp,
153 const Path** redirects,
154 unsigned memoryLimit,
155 std::string* ErrMsg)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000156{
157 if (!path.canExecute()) {
158 if (ErrMsg)
159 *ErrMsg = path.toString() + " is not executable";
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000160 return false;
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000161 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000162
Reid Spencer52a7efa2004-08-29 19:20:41 +0000163 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000164 int child = fork();
165 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000166 // An error occured: Return to the caller.
167 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000168 MakeErrMsg(ErrMsg, "Couldn't fork");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000169 return false;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000170
171 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000172 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000173 // Redirect file descriptors...
174 if (redirects) {
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000175 // Redirect stdin
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000176 if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000177 // Redirect stdout
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000178 if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000179 if (redirects[1] && redirects[2] &&
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000180 *(redirects[1]) == *(redirects[2])) {
181 // If stdout and stderr should go to the same place, redirect stderr
182 // to the FD already open for stdout.
183 if (-1 == dup2(1,2)) {
184 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000185 return false;
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000186 }
187 } else {
188 // Just redirect stderr
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000189 if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000190 }
191 }
192
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000193 // Set memory limits
194 if (memoryLimit!=0) {
195 SetMemoryLimits(memoryLimit);
196 }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000197
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000198 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000199 if (envp != 0)
Dan Gohmancb648f92007-09-14 20:08:19 +0000200 execve (path.c_str(), (char**)args, (char**)envp);
Evan Cheng2fafaf12006-06-09 20:43:11 +0000201 else
Dan Gohmancb648f92007-09-14 20:08:19 +0000202 execv (path.c_str(), (char**)args);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000203 // If the execve() failed, we should exit and let the parent pick up
204 // our non-zero exit status.
205 exit (errno);
Reid Spencere2e24112004-12-14 04:18:51 +0000206 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000207
208 // Parent process: Break out of the switch to do our processing.
209 default:
210 break;
211 }
212
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000213 // Make sure stderr and stdout have been flushed
Bill Wendling64455372008-05-29 22:02:08 +0000214 std::cerr << std::flush;
215 std::cout << std::flush;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000216 fsync(1);
217 fsync(2);
218
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000219 Pid_ = child;
220
221 return true;
222}
223
224int
225Program::Wait(unsigned secondsToWait,
226 std::string* ErrMsg)
227{
228#ifdef HAVE_SYS_WAIT_H
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000229 struct sigaction Act, Old;
230
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000231 if (Pid_ == 0) {
232 MakeErrMsg(ErrMsg, "Process not started!");
233 return -1;
234 }
235
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000236 // Install a timeout handler.
237 if (secondsToWait) {
238 Timeout = false;
239 Act.sa_sigaction = 0;
240 Act.sa_handler = TimeOutHandler;
241 sigemptyset(&Act.sa_mask);
242 Act.sa_flags = 0;
243 sigaction(SIGALRM, &Act, &Old);
244 alarm(secondsToWait);
245 }
246
Reid Spencer52a7efa2004-08-29 19:20:41 +0000247 // Parent process: Wait for the child process to terminate.
248 int status;
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000249 int child = this->Pid_;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000250 while (wait(&status) != child)
251 if (secondsToWait && errno == EINTR) {
252 // Kill the child.
253 kill(child, SIGKILL);
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000254
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000255 // Turn off the alarm and restore the signal handler
256 alarm(0);
257 sigaction(SIGALRM, &Old, 0);
258
259 // Wait for child to die
260 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000261 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Devang Patela1e4bba2008-02-04 20:57:54 +0000262 else
263 MakeErrMsg(ErrMsg, "Child timed out", 0);
264
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000265 return -1; // Timeout detected
Devang Patela1e4bba2008-02-04 20:57:54 +0000266 } else if (errno != EINTR) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000267 MakeErrMsg(ErrMsg, "Error waiting for child process");
268 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000269 }
270
271 // We exited normally without timeout, so turn off the timer.
272 if (secondsToWait) {
273 alarm(0);
274 sigaction(SIGALRM, &Old, 0);
275 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000276
Reid Spencerd555f412005-12-22 20:00:16 +0000277 // Return the proper exit status. 0=success, >0 is programs' exit status,
278 // <0 means a signal was returned, -9999999 means the program dumped core.
279 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000280 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000281 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000282 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000283 result = 0 - WTERMSIG(status);
284#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000285 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000286 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000287#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000288 return result;
289#else
290 return -99;
291#endif
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000292
Reid Spencer52a7efa2004-08-29 19:20:41 +0000293}
294
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000295bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000296 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000297 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000298}
299
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000300bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000301 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000302 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000303}
304
Reid Spencer52a7efa2004-08-29 19:20:41 +0000305}