Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 1 | //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Unix specific portion of the Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include <llvm/Config/config.h> |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 20 | #include "Unix.h" |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 23 | #ifdef HAVE_SYS_WAIT_H |
| 24 | #include <sys/wait.h> |
| 25 | #endif |
| 26 | |
Reid Spencer | c0854bf | 2004-08-29 20:10:07 +0000 | [diff] [blame] | 27 | extern char** environ; |
| 28 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 29 | namespace llvm { |
| 30 | using namespace sys; |
| 31 | |
| 32 | // This function just uses the PATH environment variable to find the program. |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 33 | Path |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 34 | Program::FindProgramByName(const std::string& progName) { |
| 35 | |
| 36 | // Check some degenerate cases |
| 37 | if (progName.length() == 0) // no program |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 38 | return Path(); |
| 39 | Path temp; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 40 | if (!temp.set_file(progName)) // invalid name |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 41 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 42 | if (temp.executable()) // already executable as is |
| 43 | return temp; |
| 44 | |
| 45 | // At this point, the file name is valid and its not executable |
| 46 | |
| 47 | // Get the path. If its empty, we can't do anything to find it. |
| 48 | const char *PathStr = getenv("PATH"); |
| 49 | if (PathStr == 0) |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 50 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 51 | |
| 52 | // Now we have a colon separated list of directories to search; try them. |
| 53 | unsigned PathLen = strlen(PathStr); |
| 54 | while (PathLen) { |
| 55 | // Find the first colon... |
| 56 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 57 | |
| 58 | // Check to see if this first directory contains the executable... |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 59 | Path FilePath; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 60 | if (FilePath.set_directory(std::string(PathStr,Colon))) { |
| 61 | FilePath.append_file(progName); |
| 62 | if (FilePath.executable()) |
| 63 | return FilePath; // Found the executable! |
| 64 | } |
| 65 | |
| 66 | // Nope it wasn't in this directory, check the next path in the list! |
| 67 | PathLen -= Colon-PathStr; |
| 68 | PathStr = Colon; |
| 69 | |
| 70 | // Advance past duplicate colons |
| 71 | while (*PathStr == ':') { |
| 72 | PathStr++; |
| 73 | PathLen--; |
| 74 | } |
| 75 | } |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 76 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // |
| 80 | int |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 81 | Program::ExecuteAndWait(const Path& path, |
| 82 | const std::vector<std::string>& args) { |
| 83 | if (!path.executable()) |
| 84 | throw path.get() + " is not executable"; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 85 | |
| 86 | #ifdef HAVE_SYS_WAIT_H |
| 87 | // Create local versions of the parameters that can be passed into execve() |
| 88 | // without creating const problems. |
| 89 | const char* argv[ args.size() + 2 ]; |
| 90 | unsigned index = 0; |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 91 | std::string progname(path.getLast()); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 92 | argv[index++] = progname.c_str(); |
| 93 | for (unsigned i = 0; i < args.size(); i++) |
| 94 | argv[index++] = args[i].c_str(); |
| 95 | argv[index] = 0; |
| 96 | |
| 97 | // Create a child process. |
| 98 | switch (fork()) { |
| 99 | // An error occured: Return to the caller. |
| 100 | case -1: |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 101 | ThrowErrno(std::string("Couldn't execute program '") + path.get() + "'"); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 102 | break; |
| 103 | |
| 104 | // Child process: Execute the program. |
| 105 | case 0: |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 106 | execve (path.c_str(), (char** const)argv, environ); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 107 | // If the execve() failed, we should exit and let the parent pick up |
| 108 | // our non-zero exit status. |
| 109 | exit (errno); |
| 110 | |
| 111 | // Parent process: Break out of the switch to do our processing. |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | // Parent process: Wait for the child process to terminate. |
| 117 | int status; |
| 118 | if ((::wait (&status)) == -1) |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 119 | ThrowErrno(std::string("Failed waiting for program '") + path.get() + "'"); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 120 | |
| 121 | // If the program exited normally with a zero exit status, return success! |
| 122 | if (WIFEXITED (status)) |
| 123 | return WEXITSTATUS(status); |
| 124 | else if (WIFSIGNALED(status)) |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 125 | throw std::string("Program '") + path.get() + "' received terminating signal."; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 126 | else |
| 127 | return 0; |
| 128 | |
| 129 | #else |
| 130 | throw std::string("Program::ExecuteAndWait not implemented on this platform!\n"); |
| 131 | #endif |
| 132 | |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |