1 /*===- SystemUtils.h - Utilities to do low-level system stuff -------------===*\
3 * The LLVM Compiler Infrastructure
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.
8 *===----------------------------------------------------------------------===
10 * This file contains functions used to do a variety of low-level, often
11 * system-specific, tasks.
13 \*===----------------------------------------------------------------------===*/
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"
28 * isExecutable - This function returns true if given struct stat describes the
29 * file as being executable.
31 unsigned isExecutable(const struct stat *buf) {
32 if (!(buf->st_mode & S_IFREG))
33 return 0; // Not a regular file?
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;
44 * isExecutableFile - This function returns true if the filename specified
45 * exists and is executable.
47 unsigned isExecutableFile(const char *ExeFileName) {
49 if (stat(ExeFileName, &buf))
50 return 0; // Must not be executable!
52 return isExecutable(&buf);
57 * FindExecutable - Find a named executable in the directories listed in $PATH.
58 * If the executable cannot be found, returns NULL.
60 char *FindExecutable(const char *ExeName) {
61 /* Try to find the executable in the path */
62 const char *PathStr = getenv("PATH");
63 if (PathStr == 0) return 0;
65 /* Now we have a colon separated list of directories to search, try them. */
66 unsigned PathLen = strlen(PathStr);
68 /* Find the next colon */
69 const char *Colon = strchr(PathStr, ':');
71 /* Check to see if this first directory contains the executable... */
72 unsigned DirLen = Colon ? (unsigned)(Colon-PathStr) : strlen(PathStr);
73 char *FilePath = alloca(sizeof(char) * (DirLen+1+strlen(ExeName)+1));
75 for (i = 0; i != DirLen; ++i)
76 FilePath[i] = PathStr[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! */
84 /* If Colon is NULL, there are no more colon separators and no more dirs */
87 /* Nope, it wasn't in this directory, check the next range! */
90 while (*PathStr == ':') { /* Advance past colons */
95 /* Advance past the colon */
99 /* If we fell out, we ran out of directories to search, return failure. */
104 * The type of the execve() function is long and boring, but required.
106 typedef int(*execveTy)(const char*, char *const[], char *const[]);
109 * This method finds the real `execve' call in the C library and executes the
112 int executeProgram(const char *filename, char *const argv[], char *const envp[])
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.
119 execveTy execvePtr = (execveTy) dlsym(RTLD_NEXT, "execve");
120 if ((error = dlerror()) != NULL) {
121 fprintf(stderr, "%s\n", error);
125 /* Really execute the program */
126 return execvePtr(filename, argv, envp);