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>
32 extern char** environ;
37 // This function just uses the PATH environment variable to find the program.
39 Program::FindProgramByName(const std::string& progName) {
41 // Check some degenerate cases
42 if (progName.length() == 0) // no program
45 if (!temp.set(progName)) // invalid name
47 // FIXME: have to check for absolute filename - we cannot assume anything
48 // about "." being in $PATH
49 if (temp.canExecute()) // already executable as is
52 // At this point, the file name is valid and its not executable
54 // Get the path. If its empty, we can't do anything to find it.
55 const char *PathStr = getenv("PATH");
59 // Now we have a colon separated list of directories to search; try them.
60 unsigned PathLen = strlen(PathStr);
62 // Find the first colon...
63 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
65 // Check to see if this first directory contains the executable...
67 if (FilePath.set(std::string(PathStr,Colon))) {
68 FilePath.appendComponent(progName);
69 if (FilePath.canExecute())
70 return FilePath; // Found the executable!
73 // Nope it wasn't in this directory, check the next path in the list!
74 PathLen -= Colon-PathStr;
77 // Advance past duplicate colons
78 while (*PathStr == ':') {
86 static void RedirectFD(const std::string &File, int FD) {
87 if (File.empty()) return; // Noop
90 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
92 ThrowErrno("Cannot open file '" + File + "' for "
93 + (FD == 0 ? "input" : "output") + "!\n");
96 dup2(InFD, FD); // Install it as the requested FD
97 close(InFD); // Close the original FD
100 static bool Timeout = false;
101 static void TimeOutHandler(int Sig) {
106 Program::ExecuteAndWait(const Path& path,
109 const Path** redirects,
110 unsigned secondsToWait
112 if (!path.canExecute())
113 throw path.toString() + " is not executable";
115 #ifdef HAVE_SYS_WAIT_H
116 // Create a child process.
119 // An error occured: Return to the caller.
121 ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
125 // Child process: Execute the program.
127 // Redirect file descriptors...
130 if (redirects[0]->isEmpty())
131 RedirectFD("/dev/null",0);
133 RedirectFD(redirects[0]->toString(), 0);
135 if (redirects[1]->isEmpty())
136 RedirectFD("/dev/null",1);
138 RedirectFD(redirects[1]->toString(), 1);
139 if (redirects[1] && redirects[2] &&
140 *(redirects[1]) != *(redirects[2])) {
141 if (redirects[2]->isEmpty())
142 RedirectFD("/dev/null",2);
144 RedirectFD(redirects[2]->toString(), 2);
150 // Set up the environment
151 char** env = environ;
156 execve (path.c_str(), (char** const)args, env);
157 // If the execve() failed, we should exit and let the parent pick up
158 // our non-zero exit status.
162 // Parent process: Break out of the switch to do our processing.
167 // Make sure stderr and stdout have been flushed
168 std::cerr << std::flush;
169 std::cout << std::flush;
173 struct sigaction Act, Old;
175 // Install a timeout handler.
178 Act.sa_sigaction = 0;
179 Act.sa_handler = TimeOutHandler;
180 sigemptyset(&Act.sa_mask);
182 sigaction(SIGALRM, &Act, &Old);
183 alarm(secondsToWait);
186 // Parent process: Wait for the child process to terminate.
188 while (wait(&status) != child)
189 if (secondsToWait && errno == EINTR) {
191 kill(child, SIGKILL);
193 // Turn off the alarm and restore the signal handler
195 sigaction(SIGALRM, &Old, 0);
197 // Wait for child to die
198 if (wait(&status) != child)
199 ThrowErrno("Child timedout but wouldn't die");
201 return -1; // Timeout detected
203 ThrowErrno("Error waiting for child process");
206 // We exited normally without timeout, so turn off the timer.
209 sigaction(SIGALRM, &Old, 0);
212 // Return the proper exit status. 0=success, >0 is programs' exit status,
213 // <0 means a signal was returned, -9999999 means the program dumped core.
215 if (WIFEXITED (status))
216 result = WEXITSTATUS(status);
217 else if (WIFSIGNALED(status))
218 result = 0 - WTERMSIG(status);
220 if (WCOREDUMP(status))
221 result |= 0x01000000;