blob: 77d74a18c67735b123078758a4f57b39e2cecadd [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);
122 // Resident set size.
123 getrlimit (RLIMIT_RSS, &r);
124 r.rlim_cur = limit;
125 setrlimit (RLIMIT_RSS, &r);
126 // Virtual memory.
127 getrlimit (RLIMIT_AS, &r);
128 r.rlim_cur = limit;
129 setrlimit (RLIMIT_AS, &r);
130#endif
131}
132
Reid Spencer52a7efa2004-08-29 19:20:41 +0000133int
Reid Spencer25659432004-09-13 21:48:44 +0000134Program::ExecuteAndWait(const Path& path,
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000135 const char** args,
136 const char** envp,
137 const Path** redirects,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000138 unsigned secondsToWait,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000139 unsigned memoryLimit,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000140 std::string* ErrMsg)
141{
142 if (!path.canExecute()) {
143 if (ErrMsg)
144 *ErrMsg = path.toString() + " is not executable";
145 return -1;
146 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000147
148#ifdef HAVE_SYS_WAIT_H
Reid Spencer52a7efa2004-08-29 19:20:41 +0000149 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000150 int child = fork();
151 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000152 // An error occured: Return to the caller.
153 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000154 MakeErrMsg(ErrMsg, "Couldn't fork");
155 return -1;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000156
157 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000158 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000159 // Redirect file descriptors...
160 if (redirects) {
Reid Spencerad42ea72006-08-22 15:56:52 +0000161 if (redirects[0]) {
162 if (redirects[0]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000163 if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000164 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000165 if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000166 }
167 }
168 if (redirects[1]) {
169 if (redirects[1]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000170 if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000171 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000172 if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000173 }
174 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000175 if (redirects[1] && redirects[2] &&
176 *(redirects[1]) != *(redirects[2])) {
Reid Spencerad42ea72006-08-22 15:56:52 +0000177 if (redirects[2]->isEmpty()) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000178 if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000179 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000180 if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
Reid Spencerad42ea72006-08-22 15:56:52 +0000181 }
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000182 } else if (-1 == dup2(1,2)) {
183 MakeErrMsg(ErrMsg, "Can't redirect");
184 return -1;
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 }
192
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000193 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000194 if (envp != 0)
195 execve (path.c_str(), (char** const)args, (char**)envp);
196 else
197 execv (path.c_str(), (char** const)args);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000198 // If the execve() failed, we should exit and let the parent pick up
199 // our non-zero exit status.
200 exit (errno);
Reid Spencere2e24112004-12-14 04:18:51 +0000201 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000202
203 // Parent process: Break out of the switch to do our processing.
204 default:
205 break;
206 }
207
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000208 // Make sure stderr and stdout have been flushed
209 std::cerr << std::flush;
210 std::cout << std::flush;
211 fsync(1);
212 fsync(2);
213
214 struct sigaction Act, Old;
215
216 // Install a timeout handler.
217 if (secondsToWait) {
218 Timeout = false;
219 Act.sa_sigaction = 0;
220 Act.sa_handler = TimeOutHandler;
221 sigemptyset(&Act.sa_mask);
222 Act.sa_flags = 0;
223 sigaction(SIGALRM, &Act, &Old);
224 alarm(secondsToWait);
225 }
226
Reid Spencer52a7efa2004-08-29 19:20:41 +0000227 // Parent process: Wait for the child process to terminate.
228 int status;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000229 while (wait(&status) != child)
230 if (secondsToWait && errno == EINTR) {
231 // Kill the child.
232 kill(child, SIGKILL);
233
234 // Turn off the alarm and restore the signal handler
235 alarm(0);
236 sigaction(SIGALRM, &Old, 0);
237
238 // Wait for child to die
239 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000240 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000241
242 return -1; // Timeout detected
243 } else {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000244 MakeErrMsg(ErrMsg, "Error waiting for child process");
245 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000246 }
247
248 // We exited normally without timeout, so turn off the timer.
249 if (secondsToWait) {
250 alarm(0);
251 sigaction(SIGALRM, &Old, 0);
252 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000253
Reid Spencerd555f412005-12-22 20:00:16 +0000254 // Return the proper exit status. 0=success, >0 is programs' exit status,
255 // <0 means a signal was returned, -9999999 means the program dumped core.
256 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000257 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000258 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000259 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000260 result = 0 - WTERMSIG(status);
261#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000262 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000263 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000264#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000265 return result;
266#else
267 return -99;
268#endif
269
Reid Spencer52a7efa2004-08-29 19:20:41 +0000270}
271
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000272bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000273 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000274 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000275}
276
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000277bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000278 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000279 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000280}
281
Reid Spencer52a7efa2004-08-29 19:20:41 +0000282}