blob: 1fe25861e1e74fe5851f7fb2caf826e2b2341a3c [file] [log] [blame]
Reid Spencer52a7efa2004-08-29 19:20:41 +00001//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source 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
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 Spencer2a7d9e92004-12-19 18:00:44 +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.
61 unsigned PathLen = strlen(PathStr);
62 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
Reid Spencer4ce5dc62006-08-21 06:02:44 +000087static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
88 if (File.empty()) return false; // Noop
Reid Spencer2a7d9e92004-12-19 18:00:44 +000089
90 // Open the file
91 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
92 if (InFD == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +000093 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Reid Spencer2a7d9e92004-12-19 18:00:44 +000094 + (FD == 0 ? "input" : "output") + "!\n");
Reid Spencer4ce5dc62006-08-21 06:02:44 +000095 return true;
Reid Spencer2a7d9e92004-12-19 18:00:44 +000096 }
97
Reid Spencer4ce5dc62006-08-21 06:02:44 +000098 // Install it as the requested FD
99 if (-1 == dup2(InFD, FD)) {
100 MakeErrMsg(ErrMsg, "Cannot dup2");
101 return true;
102 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000103 close(InFD); // Close the original FD
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000104 return false;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000105}
106
107static bool Timeout = false;
108static void TimeOutHandler(int Sig) {
109 Timeout = true;
110}
111
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000112static void SetMemoryLimits (unsigned size)
113{
114#if HAVE_SYS_RESOURCE_H
115 struct rlimit r;
116 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
117
118 // Heap size
119 getrlimit (RLIMIT_DATA, &r);
120 r.rlim_cur = limit;
121 setrlimit (RLIMIT_DATA, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000122#ifndef __CYGWIN__
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000123 // Resident set size.
124 getrlimit (RLIMIT_RSS, &r);
125 r.rlim_cur = limit;
126 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000127#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000128 // Virtual memory.
129 getrlimit (RLIMIT_AS, &r);
130 r.rlim_cur = limit;
131 setrlimit (RLIMIT_AS, &r);
132#endif
133}
134
Reid Spencer52a7efa2004-08-29 19:20:41 +0000135int
Reid Spencer25659432004-09-13 21:48:44 +0000136Program::ExecuteAndWait(const Path& path,
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000137 const char** args,
138 const char** envp,
139 const Path** redirects,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000140 unsigned secondsToWait,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000141 unsigned memoryLimit,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000142 std::string* ErrMsg)
143{
144 if (!path.canExecute()) {
145 if (ErrMsg)
146 *ErrMsg = path.toString() + " is not executable";
147 return -1;
148 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000149
150#ifdef HAVE_SYS_WAIT_H
Reid Spencer52a7efa2004-08-29 19:20:41 +0000151 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000152 int child = fork();
153 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000154 // An error occured: Return to the caller.
155 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000156 MakeErrMsg(ErrMsg, "Couldn't fork");
157 return -1;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000158
159 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000160 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000161 // Redirect file descriptors...
162 if (redirects) {
Reid Spencerad42ea72006-08-22 15:56:52 +0000163 if (redirects[0]) {
164 if (redirects[0]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000165 if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000166 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000167 if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000168 }
169 }
170 if (redirects[1]) {
171 if (redirects[1]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000172 if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000173 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000174 if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000175 }
176 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000177 if (redirects[1] && redirects[2] &&
178 *(redirects[1]) != *(redirects[2])) {
Reid Spencerad42ea72006-08-22 15:56:52 +0000179 if (redirects[2]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000180 if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000181 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000182 if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000183 }
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000184 } else if (-1 == dup2(1,2)) {
185 MakeErrMsg(ErrMsg, "Can't redirect");
186 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000187 }
188 }
189
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000190 // Set memory limits
191 if (memoryLimit!=0) {
192 SetMemoryLimits(memoryLimit);
193 }
194
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000195 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000196 if (envp != 0)
197 execve (path.c_str(), (char** const)args, (char**)envp);
198 else
199 execv (path.c_str(), (char** const)args);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000200 // If the execve() failed, we should exit and let the parent pick up
201 // our non-zero exit status.
202 exit (errno);
Reid Spencere2e24112004-12-14 04:18:51 +0000203 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000204
205 // Parent process: Break out of the switch to do our processing.
206 default:
207 break;
208 }
209
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000210 // Make sure stderr and stdout have been flushed
211 std::cerr << std::flush;
212 std::cout << std::flush;
213 fsync(1);
214 fsync(2);
215
216 struct sigaction Act, Old;
217
218 // Install a timeout handler.
219 if (secondsToWait) {
220 Timeout = false;
221 Act.sa_sigaction = 0;
222 Act.sa_handler = TimeOutHandler;
223 sigemptyset(&Act.sa_mask);
224 Act.sa_flags = 0;
225 sigaction(SIGALRM, &Act, &Old);
226 alarm(secondsToWait);
227 }
228
Reid Spencer52a7efa2004-08-29 19:20:41 +0000229 // Parent process: Wait for the child process to terminate.
230 int status;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000231 while (wait(&status) != child)
232 if (secondsToWait && errno == EINTR) {
233 // Kill the child.
234 kill(child, SIGKILL);
235
236 // Turn off the alarm and restore the signal handler
237 alarm(0);
238 sigaction(SIGALRM, &Old, 0);
239
240 // Wait for child to die
241 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000242 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000243
244 return -1; // Timeout detected
245 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000246 MakeErrMsg(ErrMsg, "Error waiting for child process");
247 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000248 }
249
250 // We exited normally without timeout, so turn off the timer.
251 if (secondsToWait) {
252 alarm(0);
253 sigaction(SIGALRM, &Old, 0);
254 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000255
Reid Spencerd555f412005-12-22 20:00:16 +0000256 // Return the proper exit status. 0=success, >0 is programs' exit status,
257 // <0 means a signal was returned, -9999999 means the program dumped core.
258 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000259 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000260 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000261 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000262 result = 0 - WTERMSIG(status);
263#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000264 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000265 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000266#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000267 return result;
268#else
269 return -99;
270#endif
271
Reid Spencer52a7efa2004-08-29 19:20:41 +0000272}
273
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000274bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000275 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000276 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000277}
278
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000279bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000280 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000281 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000282}
283
Reid Spencer52a7efa2004-08-29 19:20:41 +0000284}