2004-08-29 19:20:41 +00:00
|
|
|
//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Unix specific portion of the Program class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include <llvm/Config/config.h>
|
2004-08-29 19:20:41 +00:00
|
|
|
#include "Unix.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2004-08-29 20:10:07 +00:00
|
|
|
extern char** environ;
|
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
// This function just uses the PATH environment variable to find the program.
|
2004-09-13 21:48:44 +00:00
|
|
|
Path
|
2004-08-29 19:20:41 +00:00
|
|
|
Program::FindProgramByName(const std::string& progName) {
|
|
|
|
|
|
|
|
// Check some degenerate cases
|
|
|
|
if (progName.length() == 0) // no program
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
|
|
|
Path temp;
|
2004-11-05 22:15:36 +00:00
|
|
|
if (!temp.setFile(progName)) // invalid name
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
2004-08-29 19:20:41 +00:00
|
|
|
if (temp.executable()) // already executable as is
|
|
|
|
return temp;
|
|
|
|
|
|
|
|
// At this point, the file name is valid and its not executable
|
|
|
|
|
|
|
|
// Get the path. If its empty, we can't do anything to find it.
|
|
|
|
const char *PathStr = getenv("PATH");
|
|
|
|
if (PathStr == 0)
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Now we have a colon separated list of directories to search; try them.
|
|
|
|
unsigned PathLen = strlen(PathStr);
|
|
|
|
while (PathLen) {
|
|
|
|
// Find the first colon...
|
|
|
|
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
|
|
|
|
|
|
|
// Check to see if this first directory contains the executable...
|
2004-09-13 21:48:44 +00:00
|
|
|
Path FilePath;
|
2004-11-05 22:15:36 +00:00
|
|
|
if (FilePath.setDirectory(std::string(PathStr,Colon))) {
|
|
|
|
FilePath.appendFile(progName);
|
2004-08-29 19:20:41 +00:00
|
|
|
if (FilePath.executable())
|
|
|
|
return FilePath; // Found the executable!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nope it wasn't in this directory, check the next path in the list!
|
|
|
|
PathLen -= Colon-PathStr;
|
|
|
|
PathStr = Colon;
|
|
|
|
|
|
|
|
// Advance past duplicate colons
|
|
|
|
while (*PathStr == ':') {
|
|
|
|
PathStr++;
|
|
|
|
PathLen--;
|
|
|
|
}
|
|
|
|
}
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
int
|
2004-09-13 21:48:44 +00:00
|
|
|
Program::ExecuteAndWait(const Path& path,
|
2004-12-14 04:18:51 +00:00
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const char ** envp ) {
|
2004-09-13 21:48:44 +00:00
|
|
|
if (!path.executable())
|
2004-12-11 00:14:15 +00:00
|
|
|
throw path.toString() + " is not executable";
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
// Create local versions of the parameters that can be passed into execve()
|
|
|
|
// without creating const problems.
|
|
|
|
const char* argv[ args.size() + 2 ];
|
|
|
|
unsigned index = 0;
|
2004-09-13 21:48:44 +00:00
|
|
|
std::string progname(path.getLast());
|
2004-08-29 19:20:41 +00:00
|
|
|
argv[index++] = progname.c_str();
|
|
|
|
for (unsigned i = 0; i < args.size(); i++)
|
|
|
|
argv[index++] = args[i].c_str();
|
|
|
|
argv[index] = 0;
|
|
|
|
|
|
|
|
// Create a child process.
|
|
|
|
switch (fork()) {
|
|
|
|
// An error occured: Return to the caller.
|
|
|
|
case -1:
|
2004-12-11 00:14:15 +00:00
|
|
|
ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
|
|
|
|
"'");
|
2004-08-29 19:20:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Child process: Execute the program.
|
2004-12-14 04:18:51 +00:00
|
|
|
case 0: {
|
|
|
|
char** env = environ;
|
|
|
|
if (envp != 0)
|
|
|
|
env = (char**) envp;
|
|
|
|
execve (path.c_str(), (char** const)argv, env);
|
2004-08-29 19:20:41 +00:00
|
|
|
// If the execve() failed, we should exit and let the parent pick up
|
|
|
|
// our non-zero exit status.
|
|
|
|
exit (errno);
|
2004-12-14 04:18:51 +00:00
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Parent process: Break out of the switch to do our processing.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent process: Wait for the child process to terminate.
|
|
|
|
int status;
|
|
|
|
if ((::wait (&status)) == -1)
|
2004-12-11 00:14:15 +00:00
|
|
|
ThrowErrno(std::string("Failed waiting for program '") + path.toString()
|
|
|
|
+ "'");
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// If the program exited normally with a zero exit status, return success!
|
|
|
|
if (WIFEXITED (status))
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
else if (WIFSIGNALED(status))
|
2004-12-11 00:14:15 +00:00
|
|
|
throw std::string("Program '") + path.toString() +
|
|
|
|
"' received terminating signal.";
|
2004-08-29 19:20:41 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
throw std::string("Program::ExecuteAndWait not implemented on this platform!\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
|