1 //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific portion of the Program class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
19 #include <llvm/Config/config.h>
35 // This function just uses the PATH environment variable to find the program.
37 Program::FindProgramByName(const std::string& progName) {
39 // Check some degenerate cases
40 if (progName.length() == 0) // no program
43 if (!temp.set(progName)) // invalid name
45 // FIXME: have to check for absolute filename - we cannot assume anything
46 // about "." being in $PATH
47 if (temp.canExecute()) // already executable as is
50 // At this point, the file name is valid and its not executable
52 // Get the path. If its empty, we can't do anything to find it.
53 const char *PathStr = getenv("PATH");
57 // Now we have a colon separated list of directories to search; try them.
58 unsigned PathLen = strlen(PathStr);
60 // Find the first colon...
61 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
63 // Check to see if this first directory contains the executable...
65 if (FilePath.set(std::string(PathStr,Colon))) {
66 FilePath.appendComponent(progName);
67 if (FilePath.canExecute())
68 return FilePath; // Found the executable!
71 // Nope it wasn't in this directory, check the next path in the list!
72 PathLen -= Colon-PathStr;
75 // Advance past duplicate colons
76 while (*PathStr == ':') {
84 static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
85 if (File.empty()) return false; // Noop
88 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
90 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
91 + (FD == 0 ? "input" : "output") + "!\n");
95 // Install it as the requested FD
96 if (-1 == dup2(InFD, FD)) {
97 MakeErrMsg(ErrMsg, "Cannot dup2");
100 close(InFD); // Close the original FD
104 static bool Timeout = false;
105 static void TimeOutHandler(int Sig) {
110 Program::ExecuteAndWait(const Path& path,
113 const Path** redirects,
114 unsigned secondsToWait,
117 if (!path.canExecute()) {
119 *ErrMsg = path.toString() + " is not executable";
123 #ifdef HAVE_SYS_WAIT_H
124 // Create a child process.
127 // An error occured: Return to the caller.
129 MakeErrMsg(ErrMsg, "Couldn't fork");
132 // Child process: Execute the program.
134 // Redirect file descriptors...
137 if (redirects[0]->isEmpty()) {
138 if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
140 if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
144 if (redirects[1]->isEmpty()) {
145 if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
147 if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
150 if (redirects[1] && redirects[2] &&
151 *(redirects[1]) != *(redirects[2])) {
152 if (redirects[2]->isEmpty()) {
153 if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
155 if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
157 } else if (-1 == dup2(1,2)) {
158 MakeErrMsg(ErrMsg, "Can't redirect");
165 execve (path.c_str(), (char** const)args, (char**)envp);
167 execv (path.c_str(), (char** const)args);
168 // If the execve() failed, we should exit and let the parent pick up
169 // our non-zero exit status.
173 // Parent process: Break out of the switch to do our processing.
178 // Make sure stderr and stdout have been flushed
179 std::cerr << std::flush;
180 std::cout << std::flush;
184 struct sigaction Act, Old;
186 // Install a timeout handler.
189 Act.sa_sigaction = 0;
190 Act.sa_handler = TimeOutHandler;
191 sigemptyset(&Act.sa_mask);
193 sigaction(SIGALRM, &Act, &Old);
194 alarm(secondsToWait);
197 // Parent process: Wait for the child process to terminate.
199 while (wait(&status) != child)
200 if (secondsToWait && errno == EINTR) {
202 kill(child, SIGKILL);
204 // Turn off the alarm and restore the signal handler
206 sigaction(SIGALRM, &Old, 0);
208 // Wait for child to die
209 if (wait(&status) != child)
210 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
212 return -1; // Timeout detected
214 MakeErrMsg(ErrMsg, "Error waiting for child process");
218 // We exited normally without timeout, so turn off the timer.
221 sigaction(SIGALRM, &Old, 0);
224 // Return the proper exit status. 0=success, >0 is programs' exit status,
225 // <0 means a signal was returned, -9999999 means the program dumped core.
227 if (WIFEXITED(status))
228 result = WEXITSTATUS(status);
229 else if (WIFSIGNALED(status))
230 result = 0 - WTERMSIG(status);
232 else if (WCOREDUMP(status))
233 result |= 0x01000000;
242 bool Program::ChangeStdinToBinary(){
243 // Do nothing, as Unix doesn't differentiate between text and binary.
247 bool Program::ChangeStdoutToBinary(){
248 // Do nothing, as Unix doesn't differentiate between text and binary.