xref: /freebsd/lib/libc/gen/exec.c (revision c713eaa60353b6f1bc562075efcbe0720233249e)
1 /*-
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  * 4. 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 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <paths.h>
46 
47 #include <stdarg.h>
48 #include "un-namespace.h"
49 
50 extern char **environ;
51 
52 int
53 execl(const char *name, const char *arg, ...)
54 {
55 	va_list ap;
56 	char **argv;
57 	int n;
58 
59 	va_start(ap, arg);
60 	n = 1;
61 	while (va_arg(ap, char *) != NULL)
62 		n++;
63 	va_end(ap);
64 	argv = alloca((n + 1) * sizeof(*argv));
65 	if (argv == NULL) {
66 		errno = ENOMEM;
67 		return (-1);
68 	}
69 	va_start(ap, arg);
70 	n = 1;
71 	argv[0] = (char *)arg;
72 	while ((argv[n] = va_arg(ap, char *)) != NULL)
73 		n++;
74 	va_end(ap);
75 	return (_execve(name, argv, environ));
76 }
77 
78 int
79 execle(const char *name, const char *arg, ...)
80 {
81 	va_list ap;
82 	char **argv, **envp;
83 	int n;
84 
85 	va_start(ap, arg);
86 	n = 1;
87 	while (va_arg(ap, char *) != NULL)
88 		n++;
89 	va_end(ap);
90 	argv = alloca((n + 1) * sizeof(*argv));
91 	if (argv == NULL) {
92 		errno = ENOMEM;
93 		return (-1);
94 	}
95 	va_start(ap, arg);
96 	n = 1;
97 	argv[0] = (char *)arg;
98 	while ((argv[n] = va_arg(ap, char *)) != NULL)
99 		n++;
100 	envp = va_arg(ap, char **);
101 	va_end(ap);
102 	return (_execve(name, argv, envp));
103 }
104 
105 int
106 execlp(const char *name, const char *arg, ...)
107 {
108 	va_list ap;
109 	char **argv;
110 	int n;
111 
112 	va_start(ap, arg);
113 	n = 1;
114 	while (va_arg(ap, char *) != NULL)
115 		n++;
116 	va_end(ap);
117 	argv = alloca((n + 1) * sizeof(*argv));
118 	if (argv == NULL) {
119 		errno = ENOMEM;
120 		return (-1);
121 	}
122 	va_start(ap, arg);
123 	n = 1;
124 	argv[0] = (char *)arg;
125 	while ((argv[n] = va_arg(ap, char *)) != NULL)
126 		n++;
127 	va_end(ap);
128 	return (execvp(name, argv));
129 }
130 
131 int
132 execv(name, argv)
133 	const char *name;
134 	char * const *argv;
135 {
136 	(void)_execve(name, argv, environ);
137 	return (-1);
138 }
139 
140 int
141 execvp(const char *name, char * const *argv)
142 {
143 	return (execvpe(name, argv, environ));
144 }
145 
146 static int
147 execvPe(name, path, argv, envp)
148 	const char *name;
149 	const char *path;
150 	char * const *argv;
151 	char * const *envp;
152 {
153 	char **memp;
154 	int cnt, lp, ln;
155 	char *p;
156 	int eacces, save_errno;
157 	char *bp, *cur, buf[MAXPATHLEN];
158 	struct stat sb;
159 
160 	eacces = 0;
161 
162 	/* If it's an absolute or relative path name, it's easy. */
163 	if (index(name, '/')) {
164 		bp = (char *)name;
165 		cur = NULL;
166 		goto retry;
167 	}
168 	bp = buf;
169 
170 	/* If it's an empty path name, fail in the usual POSIX way. */
171 	if (*name == '\0') {
172 		errno = ENOENT;
173 		return (-1);
174 	}
175 
176 	cur = alloca(strlen(path) + 1);
177 	if (cur == NULL) {
178 		errno = ENOMEM;
179 		return (-1);
180 	}
181 	strcpy(cur, path);
182 	while ((p = strsep(&cur, ":")) != NULL) {
183 		/*
184 		 * It's a SHELL path -- double, leading and trailing colons
185 		 * mean the current directory.
186 		 */
187 		if (*p == '\0') {
188 			p = ".";
189 			lp = 1;
190 		} else
191 			lp = strlen(p);
192 		ln = strlen(name);
193 
194 		/*
195 		 * If the path is too long complain.  This is a possible
196 		 * security issue; given a way to make the path too long
197 		 * the user may execute the wrong program.
198 		 */
199 		if (lp + ln + 2 > sizeof(buf)) {
200 			(void)_write(STDERR_FILENO, "execvP: ", 8);
201 			(void)_write(STDERR_FILENO, p, lp);
202 			(void)_write(STDERR_FILENO, ": path too long\n",
203 			    16);
204 			continue;
205 		}
206 		bcopy(p, buf, lp);
207 		buf[lp] = '/';
208 		bcopy(name, buf + lp + 1, ln);
209 		buf[lp + ln + 1] = '\0';
210 
211 retry:		(void)_execve(bp, argv, environ);
212 		switch (errno) {
213 		case E2BIG:
214 			goto done;
215 		case ELOOP:
216 		case ENAMETOOLONG:
217 		case ENOENT:
218 			break;
219 		case ENOEXEC:
220 			for (cnt = 0; argv[cnt]; ++cnt)
221 				;
222 			memp = alloca((cnt + 2) * sizeof(char *));
223 			if (memp == NULL) {
224 				/* errno = ENOMEM; XXX override ENOEXEC? */
225 				goto done;
226 			}
227 			memp[0] = "sh";
228 			memp[1] = bp;
229 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
230 			(void)_execve(_PATH_BSHELL, memp, environ);
231 			goto done;
232 		case ENOMEM:
233 			goto done;
234 		case ENOTDIR:
235 			break;
236 		case ETXTBSY:
237 			/*
238 			 * We used to retry here, but sh(1) doesn't.
239 			 */
240 			goto done;
241 		default:
242 			/*
243 			 * EACCES may be for an inaccessible directory or
244 			 * a non-executable file.  Call stat() to decide
245 			 * which.  This also handles ambiguities for EFAULT
246 			 * and EIO, and undocumented errors like ESTALE.
247 			 * We hope that the race for a stat() is unimportant.
248 			 */
249 			save_errno = errno;
250 			if (stat(bp, &sb) != 0)
251 				break;
252 			if (save_errno == EACCES) {
253 				eacces = 1;
254 				continue;
255 			}
256 			errno = save_errno;
257 			goto done;
258 		}
259 	}
260 	if (eacces)
261 		errno = EACCES;
262 	else
263 		errno = ENOENT;
264 done:
265 	return (-1);
266 }
267 
268 int
269 execvP(const char *name, const char *path, char * const argv[])
270 {
271 	return execvPe(name, path, argv, environ);
272 }
273 
274 int
275 execvpe(const char *name, char * const argv[], char * const envp[])
276 {
277 	const char *path;
278 
279 	/* Get the path we're searching. */
280 	if ((path = getenv("PATH")) == NULL)
281 		path = _PATH_DEFPATH;
282 
283 	return (execvPe(name, path, argv, envp));
284 }
285