blob: 7e196b08fed02d4070009d009c7d6e372f5848ea [file] [log] [blame]
Reid Spencer52a7efa2004-08-29 19:20:41 +00001//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
2//
3// 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.
Reid Spencer52a7efa2004-08-29 19:20:41 +00007//
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
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
38// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000039Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000040Program::FindProgramByName(const std::string& progName) {
41
42 // Check some degenerate cases
43 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000044 return Path();
45 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000046 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000047 return Path();
Misha Brukman22c46da2005-04-20 15:42:11 +000048 // FIXME: have to check for absolute filename - we cannot assume anything
49 // about "." being in $PATH
Reid Spencerc7f08322005-07-07 18:21:42 +000050 if (temp.canExecute()) // already executable as is
Reid Spencer52a7efa2004-08-29 19:20:41 +000051 return temp;
52
53 // At this point, the file name is valid and its not executable
54
55 // Get the path. If its empty, we can't do anything to find it.
56 const char *PathStr = getenv("PATH");
57 if (PathStr == 0)
Reid Spencer25659432004-09-13 21:48:44 +000058 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000059
60 // Now we have a colon separated list of directories to search; try them.
Evan Cheng34cd4a42008-05-05 18:30:58 +000061 size_t PathLen = strlen(PathStr);
Reid Spencer52a7efa2004-08-29 19:20:41 +000062 while (PathLen) {
63 // Find the first colon...
64 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
65
66 // Check to see if this first directory contains the executable...
Reid Spencer25659432004-09-13 21:48:44 +000067 Path FilePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000068 if (FilePath.set(std::string(PathStr,Colon))) {
69 FilePath.appendComponent(progName);
Reid Spencerc7f08322005-07-07 18:21:42 +000070 if (FilePath.canExecute())
Reid Spencer52a7efa2004-08-29 19:20:41 +000071 return FilePath; // Found the executable!
72 }
73
74 // Nope it wasn't in this directory, check the next path in the list!
75 PathLen -= Colon-PathStr;
76 PathStr = Colon;
77
78 // Advance past duplicate colons
79 while (*PathStr == ':') {
80 PathStr++;
81 PathLen--;
82 }
83 }
Reid Spencer25659432004-09-13 21:48:44 +000084 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000085}
86
Matthijs Kooijman905261e2008-06-12 10:47:18 +000087static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
88 if (Path == 0)
89 // Noop
90 return false;
91 std::string File;
92 if (Path->isEmpty())
93 // Redirect empty paths to /dev/null
94 File = "/dev/null";
95 else
96 File = Path->toString();
Reid Spencer2a7d9e92004-12-19 18:00:44 +000097
98 // Open the file
99 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
100 if (InFD == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000101 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +0000102 + (FD == 0 ? "input" : "output"));
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000103 return true;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000104 }
105
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000106 // Install it as the requested FD
107 if (-1 == dup2(InFD, FD)) {
108 MakeErrMsg(ErrMsg, "Cannot dup2");
109 return true;
110 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000111 close(InFD); // Close the original FD
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000112 return false;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000113}
114
115static bool Timeout = false;
116static void TimeOutHandler(int Sig) {
117 Timeout = true;
118}
119
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000120static void SetMemoryLimits (unsigned size)
121{
122#if HAVE_SYS_RESOURCE_H
123 struct rlimit r;
124 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
125
126 // Heap size
127 getrlimit (RLIMIT_DATA, &r);
128 r.rlim_cur = limit;
129 setrlimit (RLIMIT_DATA, &r);
Gabor Greifaa6b7fd2007-07-06 10:31:27 +0000130#ifdef RLIMIT_RSS
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000131 // Resident set size.
132 getrlimit (RLIMIT_RSS, &r);
133 r.rlim_cur = limit;
134 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000135#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000136#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000137 // Virtual memory.
138 getrlimit (RLIMIT_AS, &r);
139 r.rlim_cur = limit;
140 setrlimit (RLIMIT_AS, &r);
141#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000142#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000143}
144
Reid Spencer52a7efa2004-08-29 19:20:41 +0000145int
Reid Spencer25659432004-09-13 21:48:44 +0000146Program::ExecuteAndWait(const Path& path,
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000147 const char** args,
148 const char** envp,
149 const Path** redirects,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000150 unsigned secondsToWait,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000151 unsigned memoryLimit,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000152 std::string* ErrMsg)
153{
154 if (!path.canExecute()) {
155 if (ErrMsg)
156 *ErrMsg = path.toString() + " is not executable";
157 return -1;
158 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000159
160#ifdef HAVE_SYS_WAIT_H
Reid Spencer52a7efa2004-08-29 19:20:41 +0000161 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000162 int child = fork();
163 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000164 // An error occured: Return to the caller.
165 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000166 MakeErrMsg(ErrMsg, "Couldn't fork");
167 return -1;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000168
169 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000170 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000171 // Redirect file descriptors...
172 if (redirects) {
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000173 // Redirect stdin
Matthijs Kooijman905261e2008-06-12 10:47:18 +0000174 if (RedirectIO(redirects[0], 0, ErrMsg)) { return -1; }
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000175 // Redirect stdout
Matthijs Kooijman905261e2008-06-12 10:47:18 +0000176 if (RedirectIO(redirects[1], 1, ErrMsg)) { return -1; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000177 if (redirects[1] && redirects[2] &&
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000178 *(redirects[1]) == *(redirects[2])) {
179 // If stdout and stderr should go to the same place, redirect stderr
180 // to the FD already open for stdout.
181 if (-1 == dup2(1,2)) {
182 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
183 return -1;
184 }
185 } else {
186 // Just redirect stderr
Matthijs Kooijman905261e2008-06-12 10:47:18 +0000187 if (RedirectIO(redirects[2], 2, ErrMsg)) { return -1; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000188 }
189 }
190
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000191 // Set memory limits
192 if (memoryLimit!=0) {
193 SetMemoryLimits(memoryLimit);
194 }
195
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000196 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000197 if (envp != 0)
Dan Gohmancb648f92007-09-14 20:08:19 +0000198 execve (path.c_str(), (char**)args, (char**)envp);
Evan Cheng2fafaf12006-06-09 20:43:11 +0000199 else
Dan Gohmancb648f92007-09-14 20:08:19 +0000200 execv (path.c_str(), (char**)args);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000201 // If the execve() failed, we should exit and let the parent pick up
202 // our non-zero exit status.
203 exit (errno);
Reid Spencere2e24112004-12-14 04:18:51 +0000204 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000205
206 // Parent process: Break out of the switch to do our processing.
207 default:
208 break;
209 }
210
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000211 // Make sure stderr and stdout have been flushed
Bill Wendling64455372008-05-29 22:02:08 +0000212 std::cerr << std::flush;
213 std::cout << std::flush;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000214 fsync(1);
215 fsync(2);
216
217 struct sigaction Act, Old;
218
219 // Install a timeout handler.
220 if (secondsToWait) {
221 Timeout = false;
222 Act.sa_sigaction = 0;
223 Act.sa_handler = TimeOutHandler;
224 sigemptyset(&Act.sa_mask);
225 Act.sa_flags = 0;
226 sigaction(SIGALRM, &Act, &Old);
227 alarm(secondsToWait);
228 }
229
Reid Spencer52a7efa2004-08-29 19:20:41 +0000230 // Parent process: Wait for the child process to terminate.
231 int status;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000232 while (wait(&status) != child)
233 if (secondsToWait && errno == EINTR) {
234 // Kill the child.
235 kill(child, SIGKILL);
236
237 // Turn off the alarm and restore the signal handler
238 alarm(0);
239 sigaction(SIGALRM, &Old, 0);
240
241 // Wait for child to die
242 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000243 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Devang Patela1e4bba2008-02-04 20:57:54 +0000244 else
245 MakeErrMsg(ErrMsg, "Child timed out", 0);
246
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000247 return -1; // Timeout detected
Devang Patela1e4bba2008-02-04 20:57:54 +0000248 } else if (errno != EINTR) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000249 MakeErrMsg(ErrMsg, "Error waiting for child process");
250 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000251 }
252
253 // We exited normally without timeout, so turn off the timer.
254 if (secondsToWait) {
255 alarm(0);
256 sigaction(SIGALRM, &Old, 0);
257 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000258
Reid Spencerd555f412005-12-22 20:00:16 +0000259 // Return the proper exit status. 0=success, >0 is programs' exit status,
260 // <0 means a signal was returned, -9999999 means the program dumped core.
261 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000262 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000263 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000264 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000265 result = 0 - WTERMSIG(status);
266#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000267 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000268 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000269#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000270 return result;
271#else
272 return -99;
273#endif
274
Reid Spencer52a7efa2004-08-29 19:20:41 +0000275}
276
David Greene59629c12009-07-08 21:46:40 +0000277void
278Program::ExecuteNoWait(const Path& path,
279 const char** args,
280 const char** envp,
281 const Path** redirects,
282 unsigned memoryLimit,
283 std::string* ErrMsg)
284{
285 if (!path.canExecute()) {
286 if (ErrMsg)
287 *ErrMsg = path.toString() + " is not executable";
288 return;
289 }
290
291 // Create a child process.
292 int child = fork();
293 switch (child) {
294 // An error occured: Return to the caller.
295 case -1:
296 MakeErrMsg(ErrMsg, "Couldn't fork");
297 return;
298
299 // Child process: Execute the program.
300 case 0: {
301 // Redirect file descriptors...
302 if (redirects) {
303 // Redirect stdin
304 if (RedirectIO(redirects[0], 0, ErrMsg)) { return; }
305 // Redirect stdout
306 if (RedirectIO(redirects[1], 1, ErrMsg)) { return; }
307 if (redirects[1] && redirects[2] &&
308 *(redirects[1]) == *(redirects[2])) {
309 // If stdout and stderr should go to the same place, redirect stderr
310 // to the FD already open for stdout.
311 if (-1 == dup2(1,2)) {
312 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
313 return;
314 }
315 } else {
316 // Just redirect stderr
317 if (RedirectIO(redirects[2], 2, ErrMsg)) { return; }
318 }
319 }
320
321 // Set memory limits
322 if (memoryLimit!=0) {
323 SetMemoryLimits(memoryLimit);
324 }
325
326 // Execute!
327 if (envp != 0)
328 execve (path.c_str(), (char**)args, (char**)envp);
329 else
330 execv (path.c_str(), (char**)args);
331 // If the execve() failed, we should exit and let the parent pick up
332 // our non-zero exit status.
333 exit (errno);
334 }
335
336 // Parent process: Break out of the switch to do our processing.
337 default:
338 break;
339 }
340
341 // Make sure stderr and stdout have been flushed
342 std::cerr << std::flush;
343 std::cout << std::flush;
344 fsync(1);
345 fsync(2);
346
347}
348
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000349bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000350 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000351 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000352}
353
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000354bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000355 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000356 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000357}
358
Reid Spencer52a7efa2004-08-29 19:20:41 +0000359}