blob: 3309585d5be801e51aa4cb6802b0a3bbdb7b1667 [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 Hughes886370c2019-03-21 21:11:41 -070042#include "private/__bionic_get_shell_path.h"
Elliott Hughes4d215aa2017-10-18 15:54:56 -070043#include "private/FdPath.h"
44
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070045extern "C" char** environ;
46
47enum ExecVariant { kIsExecL, kIsExecLE, kIsExecLP };
48
49static int __execl(const char* name, const char* argv0, ExecVariant variant, va_list ap) {
50 // Count the arguments.
51 va_list count_ap;
52 va_copy(count_ap, ap);
53 size_t n = 1;
54 while (va_arg(count_ap, char*) != nullptr) {
55 ++n;
56 }
57 va_end(count_ap);
58
59 // Construct the new argv.
60 char* argv[n + 1];
61 argv[0] = const_cast<char*>(argv0);
62 n = 1;
63 while ((argv[n] = va_arg(ap, char*)) != nullptr) {
64 ++n;
65 }
66
67 // Collect the argp too.
68 char** argp = (variant == kIsExecLE) ? va_arg(ap, char**) : environ;
69
70 va_end(ap);
71
72 return (variant == kIsExecLP) ? execvp(name, argv) : execve(name, argv, argp);
73}
74
75int execl(const char* name, const char* arg, ...) {
76 va_list ap;
77 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010078 int result = __execl(name, arg, kIsExecL, ap);
79 va_end(ap);
80 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070081}
82
83int execle(const char* name, const char* arg, ...) {
84 va_list ap;
85 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010086 int result = __execl(name, arg, kIsExecLE, ap);
87 va_end(ap);
88 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070089}
90
91int execlp(const char* name, const char* arg, ...) {
92 va_list ap;
93 va_start(ap, arg);
Mikhail Lappo13ec1cf2017-03-25 19:02:55 +010094 int result = __execl(name, arg, kIsExecLP, ap);
95 va_end(ap);
96 return result;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -070097}
98
99int execv(const char* name, char* const* argv) {
100 return execve(name, argv, environ);
101}
102
103int execvp(const char* name, char* const* argv) {
104 return execvpe(name, argv, environ);
105}
106
Elliott Hughes3c115902016-08-24 19:27:04 -0700107static int __exec_as_script(const char* buf, char* const* argv, char* const* envp) {
108 size_t arg_count = 1;
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700109 while (argv[arg_count] != nullptr) ++arg_count;
110
Elliott Hughes3c115902016-08-24 19:27:04 -0700111 const char* script_argv[arg_count + 2];
112 script_argv[0] = "sh";
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700113 script_argv[1] = buf;
Elliott Hughes3c115902016-08-24 19:27:04 -0700114 memcpy(script_argv + 2, argv + 1, arg_count * sizeof(char*));
Elliott Hughes886370c2019-03-21 21:11:41 -0700115 return execve(__bionic_get_shell_path(), const_cast<char**>(script_argv), envp);
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700116}
117
118int execvpe(const char* name, char* const* argv, char* const* envp) {
119 // Do not allow null name.
120 if (name == nullptr || *name == '\0') {
121 errno = ENOENT;
122 return -1;
123 }
124
125 // If it's an absolute or relative path name, it's easy.
Elliott Hughes3c115902016-08-24 19:27:04 -0700126 if (strchr(name, '/') && execve(name, argv, envp) == -1) {
Elliott Hughes63615062016-08-25 17:40:27 -0700127 if (errno == ENOEXEC) return __exec_as_script(name, argv, envp);
128 return -1;
Elliott Hughes3c115902016-08-24 19:27:04 -0700129 }
Elliott Hughes1b40aaf2016-08-18 10:11:36 -0700130
131 // Get the path we're searching.
132 const char* path = getenv("PATH");
133 if (path == nullptr) path = _PATH_DEFPATH;
134
135 // Make a writable copy.
136 size_t len = strlen(path) + 1;
137 char writable_path[len];
138 memcpy(writable_path, path, len);
139
140 bool saw_EACCES = false;
141
142 // Try each element of $PATH in turn...
143 char* strsep_buf = writable_path;
144 const char* dir;
145 while ((dir = strsep(&strsep_buf, ":"))) {
146 // It's a shell path: double, leading and trailing colons
147 // mean the current directory.
148 if (*dir == '\0') dir = const_cast<char*>(".");
149
150 size_t dir_len = strlen(dir);
151 size_t name_len = strlen(name);
152
153 char buf[dir_len + 1 + name_len + 1];
154 mempcpy(mempcpy(mempcpy(buf, dir, dir_len), "/", 1), name, name_len + 1);
155
156 execve(buf, argv, envp);
157 switch (errno) {
158 case EISDIR:
159 case ELOOP:
160 case ENAMETOOLONG:
161 case ENOENT:
162 case ENOTDIR:
163 break;
164 case ENOEXEC:
165 return __exec_as_script(buf, argv, envp);
166 case EACCES:
167 saw_EACCES = true;
168 break;
169 default:
170 return -1;
171 }
172 }
173 if (saw_EACCES) errno = EACCES;
174 return -1;
175}
Elliott Hughes4d215aa2017-10-18 15:54:56 -0700176
177int fexecve(int fd, char* const* argv, char* const* envp) {
178 // execveat with AT_EMPTY_PATH (>= 3.19) seems to offer no advantages.
179 execve(FdPath(fd).c_str(), argv, envp);
180 if (errno == ENOENT) errno = EBADF;
181 return -1;
182}