blob: 86a54316179ce8411c56f33a310b56a842c7ab63 [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
25#if HAVE_SIGNAL_H
26#include <signal.h>
27#endif
28#if HAVE_FCNTL_H
29#include <fcntl.h>
30#endif
Reid Spencer52a7efa2004-08-29 19:20:41 +000031
Reid Spencer52a7efa2004-08-29 19:20:41 +000032namespace llvm {
33using namespace sys;
34
35// This function just uses the PATH environment variable to find the program.
Reid Spencer25659432004-09-13 21:48:44 +000036Path
Reid Spencer52a7efa2004-08-29 19:20:41 +000037Program::FindProgramByName(const std::string& progName) {
38
39 // Check some degenerate cases
40 if (progName.length() == 0) // no program
Reid Spencer25659432004-09-13 21:48:44 +000041 return Path();
42 Path temp;
Reid Spencerdd04df02005-07-07 23:21:43 +000043 if (!temp.set(progName)) // invalid name
Reid Spencer25659432004-09-13 21:48:44 +000044 return Path();
Misha Brukman22c46da2005-04-20 15:42:11 +000045 // FIXME: have to check for absolute filename - we cannot assume anything
46 // about "." being in $PATH
Reid Spencerc7f08322005-07-07 18:21:42 +000047 if (temp.canExecute()) // already executable as is
Reid Spencer52a7efa2004-08-29 19:20:41 +000048 return temp;
49
50 // At this point, the file name is valid and its not executable
51
52 // Get the path. If its empty, we can't do anything to find it.
53 const char *PathStr = getenv("PATH");
54 if (PathStr == 0)
Reid Spencer25659432004-09-13 21:48:44 +000055 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000056
57 // Now we have a colon separated list of directories to search; try them.
58 unsigned PathLen = strlen(PathStr);
59 while (PathLen) {
60 // Find the first colon...
61 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
62
63 // Check to see if this first directory contains the executable...
Reid Spencer25659432004-09-13 21:48:44 +000064 Path FilePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000065 if (FilePath.set(std::string(PathStr,Colon))) {
66 FilePath.appendComponent(progName);
Reid Spencerc7f08322005-07-07 18:21:42 +000067 if (FilePath.canExecute())
Reid Spencer52a7efa2004-08-29 19:20:41 +000068 return FilePath; // Found the executable!
69 }
70
71 // Nope it wasn't in this directory, check the next path in the list!
72 PathLen -= Colon-PathStr;
73 PathStr = Colon;
74
75 // Advance past duplicate colons
76 while (*PathStr == ':') {
77 PathStr++;
78 PathLen--;
79 }
80 }
Reid Spencer25659432004-09-13 21:48:44 +000081 return Path();
Reid Spencer52a7efa2004-08-29 19:20:41 +000082}
83
Reid Spencer2a7d9e92004-12-19 18:00:44 +000084static void RedirectFD(const std::string &File, int FD) {
85 if (File.empty()) return; // Noop
86
87 // Open the file
88 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
89 if (InFD == -1) {
90 ThrowErrno("Cannot open file '" + File + "' for "
91 + (FD == 0 ? "input" : "output") + "!\n");
92 }
93
94 dup2(InFD, FD); // Install it as the requested FD
95 close(InFD); // Close the original FD
96}
97
98static bool Timeout = false;
99static void TimeOutHandler(int Sig) {
100 Timeout = true;
101}
102
Reid Spencer52a7efa2004-08-29 19:20:41 +0000103int
Reid Spencer25659432004-09-13 21:48:44 +0000104Program::ExecuteAndWait(const Path& path,
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000105 const char** args,
106 const char** envp,
107 const Path** redirects,
108 unsigned secondsToWait
109) {
Reid Spencerc7f08322005-07-07 18:21:42 +0000110 if (!path.canExecute())
Reid Spencer1fce0912004-12-11 00:14:15 +0000111 throw path.toString() + " is not executable";
Reid Spencer52a7efa2004-08-29 19:20:41 +0000112
113#ifdef HAVE_SYS_WAIT_H
Reid Spencer52a7efa2004-08-29 19:20:41 +0000114 // Create a child process.
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000115 int child = fork();
116 switch (child) {
Reid Spencer52a7efa2004-08-29 19:20:41 +0000117 // An error occured: Return to the caller.
118 case -1:
Reid Spencer1fce0912004-12-11 00:14:15 +0000119 ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
120 "'");
Reid Spencer52a7efa2004-08-29 19:20:41 +0000121 break;
122
123 // Child process: Execute the program.
Reid Spencere2e24112004-12-14 04:18:51 +0000124 case 0: {
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000125 // Redirect file descriptors...
126 if (redirects) {
127 if (redirects[0])
128 if (redirects[0]->isEmpty())
129 RedirectFD("/dev/null",0);
130 else
131 RedirectFD(redirects[0]->toString(), 0);
132 if (redirects[1])
133 if (redirects[1]->isEmpty())
134 RedirectFD("/dev/null",1);
135 else
136 RedirectFD(redirects[1]->toString(), 1);
137 if (redirects[1] && redirects[2] &&
138 *(redirects[1]) != *(redirects[2])) {
139 if (redirects[2]->isEmpty())
140 RedirectFD("/dev/null",2);
141 else
142 RedirectFD(redirects[2]->toString(), 2);
143 } else {
144 dup2(1, 2);
145 }
146 }
147
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000148 // Execute!
Evan Cheng2fafaf12006-06-09 20:43:11 +0000149 if (envp != 0)
150 execve (path.c_str(), (char** const)args, (char**)envp);
151 else
152 execv (path.c_str(), (char** const)args);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000153 // If the execve() failed, we should exit and let the parent pick up
154 // our non-zero exit status.
155 exit (errno);
Reid Spencere2e24112004-12-14 04:18:51 +0000156 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000157
158 // Parent process: Break out of the switch to do our processing.
159 default:
160 break;
161 }
162
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000163 // Make sure stderr and stdout have been flushed
164 std::cerr << std::flush;
165 std::cout << std::flush;
166 fsync(1);
167 fsync(2);
168
169 struct sigaction Act, Old;
170
171 // Install a timeout handler.
172 if (secondsToWait) {
173 Timeout = false;
174 Act.sa_sigaction = 0;
175 Act.sa_handler = TimeOutHandler;
176 sigemptyset(&Act.sa_mask);
177 Act.sa_flags = 0;
178 sigaction(SIGALRM, &Act, &Old);
179 alarm(secondsToWait);
180 }
181
Reid Spencer52a7efa2004-08-29 19:20:41 +0000182 // Parent process: Wait for the child process to terminate.
183 int status;
Reid Spencer2a7d9e92004-12-19 18:00:44 +0000184 while (wait(&status) != child)
185 if (secondsToWait && errno == EINTR) {
186 // Kill the child.
187 kill(child, SIGKILL);
188
189 // Turn off the alarm and restore the signal handler
190 alarm(0);
191 sigaction(SIGALRM, &Old, 0);
192
193 // Wait for child to die
194 if (wait(&status) != child)
195 ThrowErrno("Child timedout but wouldn't die");
196
197 return -1; // Timeout detected
198 } else {
199 ThrowErrno("Error waiting for child process");
200 }
201
202 // We exited normally without timeout, so turn off the timer.
203 if (secondsToWait) {
204 alarm(0);
205 sigaction(SIGALRM, &Old, 0);
206 }
Reid Spencer52a7efa2004-08-29 19:20:41 +0000207
Reid Spencerd555f412005-12-22 20:00:16 +0000208 // Return the proper exit status. 0=success, >0 is programs' exit status,
209 // <0 means a signal was returned, -9999999 means the program dumped core.
210 int result = 0;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000211 if (WIFEXITED (status))
Reid Spencerd555f412005-12-22 20:00:16 +0000212 result = WEXITSTATUS(status);
Reid Spencer52a7efa2004-08-29 19:20:41 +0000213 else if (WIFSIGNALED(status))
Reid Spencerd555f412005-12-22 20:00:16 +0000214 result = 0 - WTERMSIG(status);
215#ifdef WCOREDUMP
216 if (WCOREDUMP(status))
217 result |= 0x01000000;
Reid Spencer52a7efa2004-08-29 19:20:41 +0000218#endif
Reid Spencerd555f412005-12-22 20:00:16 +0000219 return result;
220#else
221 return -99;
222#endif
223
Reid Spencer52a7efa2004-08-29 19:20:41 +0000224}
225
Reid Spencer32f55532006-06-07 23:18:34 +0000226void Program::ChangeStdinToBinary(){
227 // Do nothing, as Unix doesn't differentiate between text and binary.
228}
229
230void Program::ChangeStdoutToBinary(){
231 // Do nothing, as Unix doesn't differentiate between text and binary.
232}
233
Reid Spencer52a7efa2004-08-29 19:20:41 +0000234}