1 //===- SystemUtils.h - Utilities to do low-level system stuff --*- C++ -*--===//
3 // This file contains functions used to do a variety of low-level, often
4 // system-specific, tasks.
6 //===----------------------------------------------------------------------===//
9 #include "Config/dlfcn.h"
10 #include "Config/errno.h"
11 #include "Config/fcntl.h"
12 #include "Config/unistd.h"
13 #include "Config/sys/stat.h"
14 #include "Config/sys/types.h"
15 #include "Config/sys/wait.h"
21 * isExecutable - This function returns true if given struct stat describes the
22 * file as being executable.
24 unsigned isExecutable(const struct stat *buf) {
25 if (!(buf->st_mode & S_IFREG))
26 return 0; // Not a regular file?
28 if (buf->st_uid == getuid()) // Owner of file?
29 return buf->st_mode & S_IXUSR;
30 else if (buf->st_gid == getgid()) // In group of file?
31 return buf->st_mode & S_IXGRP;
32 else // Unrelated to file?
33 return buf->st_mode & S_IXOTH;
37 * isExecutableFile - This function returns true if the filename specified
38 * exists and is executable.
40 unsigned isExecutableFile(const char *ExeFileName) {
42 if (stat(ExeFileName, &buf))
43 return 0; // Must not be executable!
45 return isExecutable(&buf);
50 * FindExecutable - Find a named executable in the directories listed in $PATH.
51 * If the executable cannot be found, returns NULL.
53 char *FindExecutable(const char *ExeName) {
54 /* Try to find the executable in the path */
55 const char *PathStr = getenv("PATH");
56 if (PathStr == 0) return 0;
58 /* Now we have a colon separated list of directories to search, try them. */
59 unsigned PathLen = strlen(PathStr);
61 /* Find the next colon */
62 const char *Colon = strchr(PathStr, ':');
64 /* Check to see if this first directory contains the executable... */
65 unsigned DirLen = Colon ? (unsigned)(Colon-PathStr) : strlen(PathStr);
66 char *FilePath = alloca(sizeof(char) * (DirLen+1+strlen(ExeName)+1));
68 for (i = 0; i != DirLen; ++i)
69 FilePath[i] = PathStr[i];
71 for (i = 0, e = strlen(ExeName); i != e; ++i)
72 FilePath[DirLen + 1 + i] = ExeName[i];
73 FilePath[DirLen + 1 + i] = '\0';
74 if (isExecutableFile(FilePath))
75 return strdup(FilePath); /* Found the executable! */
77 /* If Colon is NULL, there are no more colon separators and no more dirs */
80 /* Nope, it wasn't in this directory, check the next range! */
83 while (*PathStr == ':') { /* Advance past colons */
88 /* Advance past the colon */
92 /* If we fell out, we ran out of directories to search, return failure. */
97 * The type of the execve() function is long and boring, but required.
99 typedef int(*execveTy)(const char*, char *const[], char *const[]);
102 * This method finds the real `execve' call in the C library and executes the
105 int executeProgram(const char *filename, char *const argv[], char *const envp[])
108 * Find a pointer to the *real* execve() function starting the search in the
109 * next library and forward, to avoid finding the one defined in this file.
112 execveTy execvePtr = (execveTy) dlsym(RTLD_NEXT, "execve");
113 if ((error = dlerror()) != NULL) {
114 fprintf(stderr, "%s\n", error);
118 /* Really execute the program */
119 return execvePtr(filename, argv, envp);