blob: b4cc875df8c7765e691e0e39bef893e174834aab [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 Dunbar57d69032009-09-22 04:44:56 +000037Program::Program() : Data_(0) {}
38
39Program::~Program() {}
40
41unsigned Program::GetPid() const {
42 uint64_t pid = reinterpret_cast<uint64_t>(Data_);
43 return static_cast<unsigned>(pid);
44}
45
Reid Spencer52a7efa2004-08-29 19:20:41 +000046// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000047Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000048Program::FindProgramByName(const std::string& progName) {
49
50 // Check some degenerate cases
51 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000052 return Path();
53 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000054 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000055 return Path();
Benjamin Kramerb0facb12009-07-28 22:08:15 +000056 // Use the given path verbatim if it contains any slashes; this matches
57 // the behavior of sh(1) and friends.
Dan Gohmana87861e2009-07-28 23:25:18 +000058 if (progName.find('/') != std::string::npos)
Reid Spencer52a7efa2004-08-29 19:20:41 +000059 return temp;
60
Dan Gohman88abb892009-08-05 17:32:39 +000061 // At this point, the file name does not contain slashes. Search for it
62 // through the directories specified in the PATH environment variable.
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000063
Reid Spencer52a7efa2004-08-29 19:20:41 +000064 // Get the path. If its empty, we can't do anything to find it.
65 const char *PathStr = getenv("PATH");
Mikhail Glushenkov234f6892009-07-17 20:38:17 +000066 if (PathStr == 0)
Reid Spencer25659432004-09-13 21:48:44 +000067 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000068
69 // Now we have a colon separated list of directories to search; try them.
Evan Cheng34cd4a42008-05-05 18:30:58 +000070 size_t PathLen = strlen(PathStr);
Reid Spencer52a7efa2004-08-29 19:20:41 +000071 while (PathLen) {
72 // Find the first colon...
73 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
74
75 // Check to see if this first directory contains the executable...
Reid Spencer25659432004-09-13 21:48:44 +000076 Path FilePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000077 if (FilePath.set(std::string(PathStr,Colon))) {
78 FilePath.appendComponent(progName);
Reid Spencerc7f08322005-07-07 18:21:42 +000079 if (FilePath.canExecute())
Reid Spencer52a7efa2004-08-29 19:20:41 +000080 return FilePath; // Found the executable!
81 }
82
83 // Nope it wasn't in this directory, check the next path in the list!
84 PathLen -= Colon-PathStr;
85 PathStr = Colon;
86
87 // Advance past duplicate colons
88 while (*PathStr == ':') {
89 PathStr++;
90 PathLen--;
91 }
92 }
Reid Spencer25659432004-09-13 21:48:44 +000093 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000094}
95
Matthijs Kooijman905261e2008-06-12 10:47:18 +000096static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
97 if (Path == 0)
98 // Noop
99 return false;
100 std::string File;
101 if (Path->isEmpty())
102 // Redirect empty paths to /dev/null
103 File = "/dev/null";
104 else
Chris Lattner74382b72009-08-23 22:45:37 +0000105 File = Path->str();
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000106
107 // Open the file
108 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
109 if (InFD == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000110 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +0000111 + (FD == 0 ? "input" : "output"));
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000112 return true;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000113 }
114
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000115 // Install it as the requested FD
Chris Lattnercdff5f72010-03-14 23:16:45 +0000116 if (dup2(InFD, FD) == -1) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000117 MakeErrMsg(ErrMsg, "Cannot dup2");
Chris Lattnercdff5f72010-03-14 23:16:45 +0000118 close(InFD);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000119 return true;
120 }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000121 close(InFD); // Close the original FD
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000122 return false;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000123}
124
Duncan Sands68d29d02009-11-08 20:55:48 +0000125static void TimeOutHandler(int Sig) {
126}
127
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000128static void SetMemoryLimits (unsigned size)
129{
Chris Lattner6ae7bbb2010-02-12 00:37:46 +0000130#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000131 struct rlimit r;
132 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
133
134 // Heap size
135 getrlimit (RLIMIT_DATA, &r);
136 r.rlim_cur = limit;
137 setrlimit (RLIMIT_DATA, &r);
Gabor Greifaa6b7fd2007-07-06 10:31:27 +0000138#ifdef RLIMIT_RSS
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000139 // Resident set size.
140 getrlimit (RLIMIT_RSS, &r);
141 r.rlim_cur = limit;
142 setrlimit (RLIMIT_RSS, &r);
Reid Spencer2e40d032007-04-23 07:22:51 +0000143#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000144#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000145 // Virtual memory.
146 getrlimit (RLIMIT_AS, &r);
147 r.rlim_cur = limit;
148 setrlimit (RLIMIT_AS, &r);
149#endif
Devang Patel69bdf682007-06-04 15:28:57 +0000150#endif
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000151}
152
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000153bool
154Program::Execute(const Path& path,
155 const char** args,
156 const char** envp,
157 const Path** redirects,
158 unsigned memoryLimit,
159 std::string* ErrMsg)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000160{
161 if (!path.canExecute()) {
162 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000163 *ErrMsg = path.str() + " is not executable";
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000164 return false;
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000165 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000166
Reid Spencer52a7efa2004-08-29 19:20:41 +0000167 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000168 int child = fork();
169 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000170 // An error occured: Return to the caller.
171 case -1:
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000172 MakeErrMsg(ErrMsg, "Couldn't fork");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000173 return false;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000174
175 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000176 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000177 // Redirect file descriptors...
178 if (redirects) {
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000179 // Redirect stdin
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000180 if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000181 // Redirect stdout
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000182 if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000183 if (redirects[1] && redirects[2] &&
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000184 *(redirects[1]) == *(redirects[2])) {
185 // If stdout and stderr should go to the same place, redirect stderr
186 // to the FD already open for stdout.
187 if (-1 == dup2(1,2)) {
188 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000189 return false;
Matthijs Kooijmancf45ca02008-06-12 12:53:35 +0000190 }
191 } else {
192 // Just redirect stderr
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000193 if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000194 }
195 }
196
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000197 // Set memory limits
198 if (memoryLimit!=0) {
199 SetMemoryLimits(memoryLimit);
200 }
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000201
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000202 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000203 if (envp != 0)
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000204 execve(path.c_str(), (char**)args, (char**)envp);
Evan Cheng2fafaf12006-06-09 20:43:11 +0000205 else
Daniel Dunbar6db0a8b2009-08-04 20:32:25 +0000206 execv(path.c_str(), (char**)args);
Dan Gohman33968522009-08-05 00:09:12 +0000207 // If the execve() failed, we should exit. Follow Unix protocol and
208 // return 127 if the executable was not found, and 126 otherwise.
209 // Use _exit rather than exit so that atexit functions and static
210 // object destructors cloned from the parent process aren't
211 // redundantly run, and so that any data buffered in stdio buffers
212 // cloned from the parent aren't redundantly written out.
213 _exit(errno == ENOENT ? 127 : 126);
Reid Spencere2e24112004-12-14 04:18:51 +0000214 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000215
216 // Parent process: Break out of the switch to do our processing.
217 default:
218 break;
219 }
220
Daniel Dunbar57d69032009-09-22 04:44:56 +0000221 Data_ = reinterpret_cast<void*>(child);
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000222
223 return true;
224}
225
226int
227Program::Wait(unsigned secondsToWait,
228 std::string* ErrMsg)
229{
230#ifdef HAVE_SYS_WAIT_H
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000231 struct sigaction Act, Old;
232
Daniel Dunbar57d69032009-09-22 04:44:56 +0000233 if (Data_ == 0) {
Mikhail Glushenkov31406192009-07-18 21:43:12 +0000234 MakeErrMsg(ErrMsg, "Process not started!");
235 return -1;
236 }
237
Duncan Sands68d29d02009-11-08 20:55:48 +0000238 // Install a timeout handler. The handler itself does nothing, but the simple
239 // fact of having a handler at all causes the wait below to return with EINTR,
240 // unlike if we used SIG_IGN.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000241 if (secondsToWait) {
Duncan Sands68d29d02009-11-08 20:55:48 +0000242 Act.sa_sigaction = 0;
243 Act.sa_handler = TimeOutHandler;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000244 sigemptyset(&Act.sa_mask);
Duncan Sands68d29d02009-11-08 20:55:48 +0000245 Act.sa_flags = 0;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000246 sigaction(SIGALRM, &Act, &Old);
247 alarm(secondsToWait);
248 }
249
Reid Spencer52a7efa2004-08-29 19:20:41 +0000250 // Parent process: Wait for the child process to terminate.
251 int status;
Daniel Dunbar57d69032009-09-22 04:44:56 +0000252 uint64_t pid = reinterpret_cast<uint64_t>(Data_);
253 pid_t child = static_cast<pid_t>(pid);
Ted Kremenek8201ebd2009-10-22 22:16:17 +0000254 while (waitpid(pid, &status, 0) != child)
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000255 if (secondsToWait && errno == EINTR) {
256 // Kill the child.
257 kill(child, SIGKILL);
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000258
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000259 // Turn off the alarm and restore the signal handler
260 alarm(0);
261 sigaction(SIGALRM, &Old, 0);
262
263 // Wait for child to die
264 if (wait(&status) != child)
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000265 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
Devang Patela1e4bba2008-02-04 20:57:54 +0000266 else
267 MakeErrMsg(ErrMsg, "Child timed out", 0);
268
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000269 return -1; // Timeout detected
Devang Patela1e4bba2008-02-04 20:57:54 +0000270 } else if (errno != EINTR) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000271 MakeErrMsg(ErrMsg, "Error waiting for child process");
272 return -1;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000273 }
274
275 // We exited normally without timeout, so turn off the timer.
276 if (secondsToWait) {
277 alarm(0);
278 sigaction(SIGALRM, &Old, 0);
279 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000280
Reid Spencerd555f412005-12-22 20:00:16 +0000281 // Return the proper exit status. 0=success, >0 is programs' exit status,
282 // <0 means a signal was returned, -9999999 means the program dumped core.
283 int result = 0;
Chris Lattner0228c0f2006-07-12 22:37:18 +0000284 if (WIFEXITED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000285 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000286 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000287 result = 0 - WTERMSIG(status);
288#ifdef WCOREDUMP
Chris Lattner0228c0f2006-07-12 22:37:18 +0000289 else if (WCOREDUMP(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000290 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000291#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000292 return result;
293#else
294 return -99;
295#endif
Mikhail Glushenkov234f6892009-07-17 20:38:17 +0000296
Reid Spencer52a7efa2004-08-29 19:20:41 +0000297}
298
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000299bool
300Program::Kill(std::string* ErrMsg) {
Daniel Dunbar57d69032009-09-22 04:44:56 +0000301 if (Data_ == 0) {
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000302 MakeErrMsg(ErrMsg, "Process not started!");
303 return true;
304 }
305
Daniel Dunbar57d69032009-09-22 04:44:56 +0000306 uint64_t pid64 = reinterpret_cast<uint64_t>(Data_);
307 pid_t pid = static_cast<pid_t>(pid64);
308
309 if (kill(pid, SIGKILL) != 0) {
Mikhail Glushenkov8add2692009-09-09 09:51:47 +0000310 MakeErrMsg(ErrMsg, "The process couldn't be killed!");
311 return true;
312 }
313
314 return false;
Mikhail Glushenkova6072022009-09-08 19:50:27 +0000315}
316
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000317bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000318 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000319 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000320}
321
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000322bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000323 // Do nothing, as Unix doesn't differentiate between text and binary.
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000324 return false;
Reid Spencer32f55532006-06-07 23:18:34 +0000325}
326
Douglas Gregor21569cd2010-01-28 06:42:08 +0000327bool Program::ChangeStderrToBinary(){
328 // Do nothing, as Unix doesn't differentiate between text and binary.
329 return false;
330}
331
Reid Spencer52a7efa2004-08-29 19:20:41 +0000332}