Misha Brukman | 3b87f21 | 2004-08-04 21:19:49 +0000 | [diff] [blame] | 1 | /*===- SystemUtils.h - Utilities to do low-level system stuff -------------===*\ |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file was developed by the LLVM research group and is distributed under |
| 6 | * the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | *===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file contains functions used to do a variety of low-level, often |
| 11 | * system-specific, tasks. |
| 12 | * |
| 13 | \*===----------------------------------------------------------------------===*/ |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 14 | |
| 15 | #include "SysUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Config/dlfcn.h" |
| 17 | #include "llvm/Config/fcntl.h" |
| 18 | #include "llvm/Config/unistd.h" |
| 19 | #include "llvm/Config/sys/stat.h" |
| 20 | #include "llvm/Config/sys/types.h" |
| 21 | #include "llvm/Config/sys/wait.h" |
Chris Lattner | e275fe8 | 2004-01-10 19:15:38 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 27 | /* |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 28 | * isExecutable - This function returns true if given struct stat describes the |
| 29 | * file as being executable. |
| 30 | */ |
| 31 | unsigned isExecutable(const struct stat *buf) { |
| 32 | if (!(buf->st_mode & S_IFREG)) |
| 33 | return 0; // Not a regular file? |
| 34 | |
| 35 | if (buf->st_uid == getuid()) // Owner of file? |
| 36 | return buf->st_mode & S_IXUSR; |
| 37 | else if (buf->st_gid == getgid()) // In group of file? |
| 38 | return buf->st_mode & S_IXGRP; |
| 39 | else // Unrelated to file? |
| 40 | return buf->st_mode & S_IXOTH; |
| 41 | } |
| 42 | |
| 43 | /* |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 44 | * isExecutableFile - This function returns true if the filename specified |
| 45 | * exists and is executable. |
| 46 | */ |
| 47 | unsigned isExecutableFile(const char *ExeFileName) { |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 48 | struct stat buf; |
| 49 | if (stat(ExeFileName, &buf)) |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 50 | return 0; // Must not be executable! |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 51 | |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 52 | return isExecutable(&buf); |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 55 | |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 56 | /* |
| 57 | * FindExecutable - Find a named executable in the directories listed in $PATH. |
| 58 | * If the executable cannot be found, returns NULL. |
| 59 | */ |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 60 | char *FindExecutable(const char *ExeName) { |
| 61 | /* Try to find the executable in the path */ |
| 62 | const char *PathStr = getenv("PATH"); |
Misha Brukman | 00ce840 | 2003-09-27 22:26:37 +0000 | [diff] [blame] | 63 | if (PathStr == 0) return 0; |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 64 | |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 65 | /* Now we have a colon separated list of directories to search, try them. */ |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 66 | unsigned PathLen = strlen(PathStr); |
| 67 | while (PathLen) { |
| 68 | /* Find the next colon */ |
| 69 | const char *Colon = strchr(PathStr, ':'); |
| 70 | |
| 71 | /* Check to see if this first directory contains the executable... */ |
Misha Brukman | 00ce840 | 2003-09-27 22:26:37 +0000 | [diff] [blame] | 72 | unsigned DirLen = Colon ? (unsigned)(Colon-PathStr) : strlen(PathStr); |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 73 | char *FilePath = alloca(sizeof(char) * (DirLen+1+strlen(ExeName)+1)); |
| 74 | unsigned i, e; |
| 75 | for (i = 0; i != DirLen; ++i) |
| 76 | FilePath[i] = PathStr[i]; |
| 77 | FilePath[i] = '/'; |
| 78 | for (i = 0, e = strlen(ExeName); i != e; ++i) |
| 79 | FilePath[DirLen + 1 + i] = ExeName[i]; |
| 80 | FilePath[DirLen + 1 + i] = '\0'; |
| 81 | if (isExecutableFile(FilePath)) |
| 82 | return strdup(FilePath); /* Found the executable! */ |
| 83 | |
| 84 | /* If Colon is NULL, there are no more colon separators and no more dirs */ |
| 85 | if (!Colon) break; |
| 86 | |
| 87 | /* Nope, it wasn't in this directory, check the next range! */ |
| 88 | PathLen -= DirLen; |
| 89 | PathStr = Colon; |
| 90 | while (*PathStr == ':') { /* Advance past colons */ |
| 91 | PathStr++; |
| 92 | PathLen--; |
| 93 | } |
| 94 | |
| 95 | /* Advance past the colon */ |
| 96 | ++Colon; |
| 97 | } |
| 98 | |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 99 | /* If we fell out, we ran out of directories to search, return failure. */ |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * The type of the execve() function is long and boring, but required. |
| 105 | */ |
| 106 | typedef int(*execveTy)(const char*, char *const[], char *const[]); |
| 107 | |
| 108 | /* |
| 109 | * This method finds the real `execve' call in the C library and executes the |
| 110 | * given program. |
| 111 | */ |
| 112 | int executeProgram(const char *filename, char *const argv[], char *const envp[]) |
| 113 | { |
| 114 | /* |
| 115 | * Find a pointer to the *real* execve() function starting the search in the |
| 116 | * next library and forward, to avoid finding the one defined in this file. |
| 117 | */ |
Reid Spencer | e6bd638 | 2004-09-14 15:46:13 +0000 | [diff] [blame] | 118 | const char *error; |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 119 | execveTy execvePtr = (execveTy) dlsym(RTLD_NEXT, "execve"); |
| 120 | if ((error = dlerror()) != NULL) { |
| 121 | fprintf(stderr, "%s\n", error); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | /* Really execute the program */ |
| 126 | return execvePtr(filename, argv, envp); |
| 127 | } |