blob: 1cf3a589f718b207307480821d65ffb61606e2e7 [file] [log] [blame]
Elliott Hughes1b40aaf2016-08-18 10:11:36 -07001/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/types.h>
31#include <sys/uio.h>
32
33#include <errno.h>
34#include <limits.h>
35#include <paths.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
Elliott Hughes4d215aa2017-10-18 15:54:56 -070042#include "private/FdPath.h"
43
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070044extern "C" char** environ;
45
46enum ExecVariant { kIsExecL, kIsExecLE, kIsExecLP };
47
48static int __execl(const char* name, const char* argv0, ExecVariant variant, va_list ap) {
49 // Count the arguments.
50 va_list count_ap;
51 va_copy(count_ap, ap);
52 size_t n = 1;
53 while (va_arg(count_ap, char*) != nullptr) {
54 ++n;
55 }
56 va_end(count_ap);
57
58 // Construct the new argv.
59 char* argv[n + 1];
60 argv[0] = const_cast<char*>(argv0);
61 n = 1;
62 while ((argv[n] = va_arg(ap, char*)) != nullptr) {
63 ++n;
64 }
65
66 // Collect the argp too.
67 char** argp = (variant == kIsExecLE) ? va_arg(ap, char**) : environ;
68
69 va_end(ap);
70
71 return (variant == kIsExecLP) ? execvp(name, argv) : execve(name, argv, argp);
72}
73
74int execl(const char* name, const char* arg, ...) {
75 va_list ap;
76 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010077 int result = __execl(name, arg, kIsExecL, ap);
78 va_end(ap);
79 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070080}
81
82int execle(const char* name, const char* arg, ...) {
83 va_list ap;
84 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010085 int result = __execl(name, arg, kIsExecLE, ap);
86 va_end(ap);
87 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070088}
89
90int execlp(const char* name, const char* arg, ...) {
91 va_list ap;
92 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010093 int result = __execl(name, arg, kIsExecLP, ap);
94 va_end(ap);
95 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070096}
97
98int execv(const char* name, char* const* argv) {
99 return execve(name, argv, environ);
100}
101
102int execvp(const char* name, char* const* argv) {
103 return execvpe(name, argv, environ);
104}
105
Elliott Hughes3c115902016-08-24 19:27:04 -0700106static int __exec_as_script(const char* buf, char* const* argv, char* const* envp) {
107 size_t arg_count = 1;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700108 while (argv[arg_count] != nullptr) ++arg_count;
109
Elliott Hughes3c115902016-08-24 19:27:04 -0700110 const char* script_argv[arg_count + 2];
111 script_argv[0] = "sh";
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700112 script_argv[1] = buf;
Elliott Hughes3c115902016-08-24 19:27:04 -0700113 memcpy(script_argv + 2, argv + 1, arg_count * sizeof(char*));
114 return execve(_PATH_BSHELL, const_cast<char**>(script_argv), envp);
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700115}
116
117int execvpe(const char* name, char* const* argv, char* const* envp) {
118 // Do not allow null name.
119 if (name == nullptr || *name == '\0') {
120 errno = ENOENT;
121 return -1;
122 }
123
124 // If it's an absolute or relative path name, it's easy.
Elliott Hughes3c115902016-08-24 19:27:04 -0700125 if (strchr(name, '/') && execve(name, argv, envp) == -1) {
Elliott Hughes63615062016-08-25 17:40:27 -0700126 if (errno == ENOEXEC) return __exec_as_script(name, argv, envp);
127 return -1;
Elliott Hughes3c115902016-08-24 19:27:04 -0700128 }
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700129
130 // Get the path we're searching.
131 const char* path = getenv("PATH");
132 if (path == nullptr) path = _PATH_DEFPATH;
133
134 // Make a writable copy.
135 size_t len = strlen(path) + 1;
136 char writable_path[len];
137 memcpy(writable_path, path, len);
138
139 bool saw_EACCES = false;
140
141 // Try each element of $PATH in turn...
142 char* strsep_buf = writable_path;
143 const char* dir;
144 while ((dir = strsep(&strsep_buf, ":"))) {
145 // It's a shell path: double, leading and trailing colons
146 // mean the current directory.
147 if (*dir == '\0') dir = const_cast<char*>(".");
148
149 size_t dir_len = strlen(dir);
150 size_t name_len = strlen(name);
151
152 char buf[dir_len + 1 + name_len + 1];
153 mempcpy(mempcpy(mempcpy(buf, dir, dir_len), "/", 1), name, name_len + 1);
154
155 execve(buf, argv, envp);
156 switch (errno) {
157 case EISDIR:
158 case ELOOP:
159 case ENAMETOOLONG:
160 case ENOENT:
161 case ENOTDIR:
162 break;
163 case ENOEXEC:
164 return __exec_as_script(buf, argv, envp);
165 case EACCES:
166 saw_EACCES = true;
167 break;
168 default:
169 return -1;
170 }
171 }
172 if (saw_EACCES) errno = EACCES;
173 return -1;
174}
Elliott Hughes4d215aa2017-10-18 15:54:56 -0700175
176int fexecve(int fd, char* const* argv, char* const* envp) {
177 // execveat with AT_EMPTY_PATH (>= 3.19) seems to offer no advantages.
178 execve(FdPath(fd).c_str(), argv, envp);
179 if (errno == ENOENT) errno = EBADF;
180 return -1;
181}