2004-06-18 15:38:49 +00:00
|
|
|
//===- SystemUtils.cpp - Utilities for low-level system tasks -------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-23 23:50:16 +00:00
|
|
|
//
|
|
|
|
// This file contains functions used to do a variety of low-level, often
|
|
|
|
// system-specific, tasks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2004-12-13 23:41:37 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2004-12-14 04:18:15 +00:00
|
|
|
#include "llvm/Config/fcntl.h"
|
|
|
|
#include "llvm/Config/sys/wait.h"
|
2004-01-10 19:15:14 +00:00
|
|
|
#include <algorithm>
|
2004-06-18 15:38:49 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
2004-01-10 19:15:14 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2004-07-25 07:34:00 +00:00
|
|
|
#include <signal.h>
|
2004-12-14 04:18:15 +00:00
|
|
|
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-04-02 05:04:03 +00:00
|
|
|
/// isStandardOutAConsole - Return true if we can tell that the standard output
|
|
|
|
/// stream goes to a terminal window or console.
|
|
|
|
bool llvm::isStandardOutAConsole() {
|
2004-04-02 21:26:04 +00:00
|
|
|
#if HAVE_ISATTY
|
2004-04-02 05:04:03 +00:00
|
|
|
return isatty(1);
|
2004-04-02 21:26:04 +00:00
|
|
|
#endif
|
|
|
|
// If we don't have isatty, just return false.
|
|
|
|
return false;
|
2004-04-02 05:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-07 21:34:25 +00:00
|
|
|
/// FindExecutable - Find a named executable, giving the argv[0] of program
|
2003-09-29 22:40:07 +00:00
|
|
|
/// being executed. This allows us to find another LLVM tool if it is built
|
|
|
|
/// into the same directory, but that directory is neither the current
|
|
|
|
/// directory, nor in the PATH. If the executable cannot be found, return an
|
|
|
|
/// empty string.
|
2003-08-07 21:34:25 +00:00
|
|
|
///
|
2004-05-28 01:20:58 +00:00
|
|
|
#undef FindExecutable // needed on windows :(
|
2004-12-13 23:41:37 +00:00
|
|
|
sys::Path llvm::FindExecutable(const std::string &ExeName,
|
2003-12-14 21:35:53 +00:00
|
|
|
const std::string &ProgramPath) {
|
2004-12-13 23:41:37 +00:00
|
|
|
// First check the directory that the calling program is in. We can do this
|
|
|
|
// if ProgramPath contains at least one / character, indicating that it is a
|
2002-12-23 23:50:16 +00:00
|
|
|
// relative path to bugpoint itself.
|
|
|
|
//
|
2004-12-13 23:41:37 +00:00
|
|
|
sys::Path Result ( ProgramPath );
|
|
|
|
Result.elideFile();
|
2002-12-23 23:50:16 +00:00
|
|
|
|
2004-12-13 23:41:37 +00:00
|
|
|
if (!Result.isEmpty()) {
|
|
|
|
Result.appendFile(ExeName);
|
|
|
|
if (Result.executable()) return Result;
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|
|
|
|
|
2004-12-13 23:41:37 +00:00
|
|
|
return sys::Program::FindProgramByName(ExeName);
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RedirectFD(const std::string &File, int FD) {
|
|
|
|
if (File.empty()) return; // Noop
|
|
|
|
|
|
|
|
// Open the file
|
|
|
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
|
|
|
if (InFD == -1) {
|
|
|
|
std::cerr << "Error opening file '" << File << "' for "
|
2003-09-29 22:40:07 +00:00
|
|
|
<< (FD == 0 ? "input" : "output") << "!\n";
|
2002-12-23 23:50:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(InFD, FD); // Install it as the requested FD
|
|
|
|
close(InFD); // Close the original FD
|
|
|
|
}
|
|
|
|
|
2004-07-24 07:41:31 +00:00
|
|
|
static bool Timeout = false;
|
|
|
|
static void TimeOutHandler(int Sig) {
|
|
|
|
Timeout = true;
|
|
|
|
}
|
|
|
|
|
2002-12-23 23:50:16 +00:00
|
|
|
/// RunProgramWithTimeout - This function executes the specified program, with
|
|
|
|
/// the specified null-terminated argument array, with the stdin/out/err fd's
|
2004-07-24 07:41:31 +00:00
|
|
|
/// redirected, with a timeout specified by the last argument. This terminates
|
2002-12-23 23:50:16 +00:00
|
|
|
/// the calling program if there is an error executing the specified program.
|
|
|
|
/// It returns the return value of the program, or -1 if a timeout is detected.
|
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
int llvm::RunProgramWithTimeout(const std::string &ProgramPath,
|
|
|
|
const char **Args,
|
|
|
|
const std::string &StdInFile,
|
|
|
|
const std::string &StdOutFile,
|
2004-07-24 07:41:31 +00:00
|
|
|
const std::string &StdErrFile,
|
|
|
|
unsigned NumSeconds) {
|
2004-05-27 01:20:55 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2002-12-23 23:50:16 +00:00
|
|
|
int Child = fork();
|
|
|
|
switch (Child) {
|
|
|
|
case -1:
|
|
|
|
std::cerr << "ERROR forking!\n";
|
|
|
|
exit(1);
|
|
|
|
case 0: // Child
|
|
|
|
RedirectFD(StdInFile, 0); // Redirect file descriptors...
|
|
|
|
RedirectFD(StdOutFile, 1);
|
2004-04-16 05:35:58 +00:00
|
|
|
if (StdOutFile != StdErrFile)
|
|
|
|
RedirectFD(StdErrFile, 2);
|
|
|
|
else
|
|
|
|
dup2(1, 2);
|
2002-12-23 23:50:16 +00:00
|
|
|
|
|
|
|
execv(ProgramPath.c_str(), (char *const *)Args);
|
2003-10-15 20:46:58 +00:00
|
|
|
std::cerr << "Error executing program: '" << ProgramPath;
|
2002-12-23 23:50:16 +00:00
|
|
|
for (; *Args; ++Args)
|
|
|
|
std::cerr << " " << *Args;
|
2003-10-15 20:46:58 +00:00
|
|
|
std::cerr << "'\n";
|
2002-12-23 23:50:16 +00:00
|
|
|
exit(1);
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all output has been written while waiting
|
|
|
|
std::cout << std::flush;
|
|
|
|
|
2004-07-24 07:41:31 +00:00
|
|
|
// Install a timeout handler.
|
|
|
|
Timeout = false;
|
|
|
|
struct sigaction Act, Old;
|
|
|
|
Act.sa_sigaction = 0;
|
|
|
|
Act.sa_handler = TimeOutHandler;
|
2004-07-25 07:34:00 +00:00
|
|
|
sigemptyset(&Act.sa_mask);
|
|
|
|
Act.sa_flags = 0;
|
2004-07-24 07:41:31 +00:00
|
|
|
sigaction(SIGALRM, &Act, &Old);
|
|
|
|
|
|
|
|
// Set the timeout if one is set.
|
|
|
|
if (NumSeconds)
|
|
|
|
alarm(NumSeconds);
|
|
|
|
|
2002-12-23 23:50:16 +00:00
|
|
|
int Status;
|
2004-07-24 07:50:48 +00:00
|
|
|
while (wait(&Status) != Child)
|
2002-12-23 23:50:16 +00:00
|
|
|
if (errno == EINTR) {
|
2004-07-24 07:41:31 +00:00
|
|
|
if (Timeout) {
|
2004-07-24 07:50:48 +00:00
|
|
|
// Kill the child.
|
|
|
|
kill(Child, SIGKILL);
|
|
|
|
|
|
|
|
if (wait(&Status) != Child)
|
|
|
|
std::cerr << "Something funny happened waiting for the child!\n";
|
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &Old, 0);
|
|
|
|
return -1; // Timeout detected
|
|
|
|
} else {
|
|
|
|
std::cerr << "Error waiting for child process!\n";
|
|
|
|
exit(1);
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-24 07:41:31 +00:00
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &Old, 0);
|
2002-12-23 23:50:16 +00:00
|
|
|
return Status;
|
2004-05-27 01:20:55 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
std::cerr << "RunProgramWithTimeout not implemented on this platform!\n";
|
|
|
|
return -1;
|
|
|
|
#endif
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|