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>
24 #ifdef HAVE_SYS_WAIT_H
29 extern char** environ;
34 // This function just uses the PATH environment variable to find the program.
36 Program::FindProgramByName(const std::string& progName) {
38 // Check some degenerate cases
39 if (progName.length() == 0) // no program
42 if (!temp.setFile(progName)) // invalid name
44 if (temp.executable()) // already executable as is
47 // At this point, the file name is valid and its not executable
49 // Get the path. If its empty, we can't do anything to find it.
50 const char *PathStr = getenv("PATH");
54 // Now we have a colon separated list of directories to search; try them.
55 unsigned PathLen = strlen(PathStr);
57 // Find the first colon...
58 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
60 // Check to see if this first directory contains the executable...
62 if (FilePath.setDirectory(std::string(PathStr,Colon))) {
63 FilePath.appendFile(progName);
64 if (FilePath.executable())
65 return FilePath; // Found the executable!
68 // Nope it wasn't in this directory, check the next path in the list!
69 PathLen -= Colon-PathStr;
72 // Advance past duplicate colons
73 while (*PathStr == ':') {
81 static void RedirectFD(const std::string &File, int FD) {
82 if (File.empty()) return; // Noop
85 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
87 ThrowErrno("Cannot open file '" + File + "' for "
88 + (FD == 0 ? "input" : "output") + "!\n");
91 dup2(InFD, FD); // Install it as the requested FD
92 close(InFD); // Close the original FD
95 static bool Timeout = false;
96 static void TimeOutHandler(int Sig) {
101 Program::ExecuteAndWait(const Path& path,
104 const Path** redirects,
105 unsigned secondsToWait
107 if (!path.executable())
108 throw path.toString() + " is not executable";
110 #ifdef HAVE_SYS_WAIT_H
111 // Create a child process.
114 // An error occured: Return to the caller.
116 ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
120 // Child process: Execute the program.
122 // Redirect file descriptors...
125 if (redirects[0]->isEmpty())
126 RedirectFD("/dev/null",0);
128 RedirectFD(redirects[0]->toString(), 0);
130 if (redirects[1]->isEmpty())
131 RedirectFD("/dev/null",1);
133 RedirectFD(redirects[1]->toString(), 1);
134 if (redirects[1] && redirects[2] &&
135 *(redirects[1]) != *(redirects[2])) {
136 if (redirects[2]->isEmpty())
137 RedirectFD("/dev/null",2);
139 RedirectFD(redirects[2]->toString(), 2);
145 // Set up the environment
146 char** env = environ;
151 execve (path.c_str(), (char** const)args, env);
152 // If the execve() failed, we should exit and let the parent pick up
153 // our non-zero exit status.
157 // Parent process: Break out of the switch to do our processing.
162 // Make sure stderr and stdout have been flushed
163 std::cerr << std::flush;
164 std::cout << std::flush;
168 struct sigaction Act, Old;
170 // Install a timeout handler.
173 Act.sa_sigaction = 0;
174 Act.sa_handler = TimeOutHandler;
175 sigemptyset(&Act.sa_mask);
177 sigaction(SIGALRM, &Act, &Old);
178 alarm(secondsToWait);
181 // Parent process: Wait for the child process to terminate.
183 while (wait(&status) != child)
184 if (secondsToWait && errno == EINTR) {
186 kill(child, SIGKILL);
188 // Turn off the alarm and restore the signal handler
190 sigaction(SIGALRM, &Old, 0);
192 // Wait for child to die
193 if (wait(&status) != child)
194 ThrowErrno("Child timedout but wouldn't die");
196 return -1; // Timeout detected
198 ThrowErrno("Error waiting for child process");
201 // We exited normally without timeout, so turn off the timer.
204 sigaction(SIGALRM, &Old, 0);
207 // If the program exited normally with a zero exit status, return success!
208 if (WIFEXITED (status))
209 return WEXITSTATUS(status);
210 else if (WIFSIGNALED(status))
211 throw std::string("Program '") + path.toString() +
212 "' received terminating signal.";
216 "Program::ExecuteAndWait not implemented on this platform!\n");
222 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab