xref: /freebsd/lib/libc/gen/exec.c (revision 3ff369fed2a08f32dda232c10470b949bef9489f)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <paths.h>
50 
51 #include <stdarg.h>
52 #include "un-namespace.h"
53 
54 extern char **environ;
55 
56 int
57 execl(const char *name, const char *arg, ...)
58 {
59 	va_list ap;
60 	char **argv;
61 	int n;
62 
63 	va_start(ap, arg);
64 	n = 1;
65 	while (va_arg(ap, char *) != NULL)
66 		n++;
67 	va_end(ap);
68 	argv = alloca((n + 1) * sizeof(*argv));
69 	if (argv == NULL) {
70 		errno = ENOMEM;
71 		return (-1);
72 	}
73 	va_start(ap, arg);
74 	n = 1;
75 	argv[0] = (char *)arg;
76 	while ((argv[n] = va_arg(ap, char *)) != NULL)
77 		n++;
78 	va_end(ap);
79 	return (_execve(name, argv, environ));
80 }
81 
82 int
83 execle(const char *name, const char *arg, ...)
84 {
85 	va_list ap;
86 	char **argv, **envp;
87 	int n;
88 
89 	va_start(ap, arg);
90 	n = 1;
91 	while (va_arg(ap, char *) != NULL)
92 		n++;
93 	va_end(ap);
94 	argv = alloca((n + 1) * sizeof(*argv));
95 	if (argv == NULL) {
96 		errno = ENOMEM;
97 		return (-1);
98 	}
99 	va_start(ap, arg);
100 	n = 1;
101 	argv[0] = (char *)arg;
102 	while ((argv[n] = va_arg(ap, char *)) != NULL)
103 		n++;
104 	envp = va_arg(ap, char **);
105 	va_end(ap);
106 	return (_execve(name, argv, envp));
107 }
108 
109 int
110 execlp(const char *name, const char *arg, ...)
111 {
112 	va_list ap;
113 	int sverrno;
114 	char **argv;
115 	int n;
116 
117 	va_start(ap, arg);
118 	n = 1;
119 	while (va_arg(ap, char *) != NULL)
120 		n++;
121 	va_end(ap);
122 	argv = alloca((n + 1) * sizeof(*argv));
123 	if (argv == NULL) {
124 		errno = ENOMEM;
125 		return (-1);
126 	}
127 	va_start(ap, arg);
128 	n = 1;
129 	argv[0] = (char *)arg;
130 	while ((argv[n] = va_arg(ap, char *)) != NULL)
131 		n++;
132 	va_end(ap);
133 	return (execvp(name, argv));
134 }
135 
136 int
137 execv(name, argv)
138 	const char *name;
139 	char * const *argv;
140 {
141 	(void)_execve(name, argv, environ);
142 	return (-1);
143 }
144 
145 int
146 execvp(name, argv)
147 	const char *name;
148 	char * const *argv;
149 {
150 	char **memp;
151 	int cnt, lp, ln;
152 	char *p;
153 	int eacces, save_errno;
154 	char *bp, *cur, *path, buf[MAXPATHLEN];
155 	struct stat sb;
156 
157 	eacces = 0;
158 
159 	/* If it's an absolute or relative path name, it's easy. */
160 	if (index(name, '/')) {
161 		bp = (char *)name;
162 		cur = path = NULL;
163 		goto retry;
164 	}
165 	bp = buf;
166 
167 	/* If it's an empty path name, fail in the usual POSIX way. */
168 	if (*name == '\0') {
169 		errno = ENOENT;
170 		return (-1);
171 	}
172 
173 	/* Get the path we're searching. */
174 	if (!(path = getenv("PATH")))
175 		path = _PATH_DEFPATH;
176 	cur = alloca(strlen(path) + 1);
177 	if (cur == NULL) {
178 		errno = ENOMEM;
179 		return (-1);
180 	}
181 	strcpy(cur, path);
182 	path = cur;
183 	while ( (p = strsep(&cur, ":")) ) {
184 		/*
185 		 * It's a SHELL path -- double, leading and trailing colons
186 		 * mean the current directory.
187 		 */
188 		if (!*p) {
189 			p = ".";
190 			lp = 1;
191 		} else
192 			lp = strlen(p);
193 		ln = strlen(name);
194 
195 		/*
196 		 * If the path is too long complain.  This is a possible
197 		 * security issue; given a way to make the path too long
198 		 * the user may execute the wrong program.
199 		 */
200 		if (lp + ln + 2 > sizeof(buf)) {
201 			(void)_write(STDERR_FILENO, "execvp: ", 8);
202 			(void)_write(STDERR_FILENO, p, lp);
203 			(void)_write(STDERR_FILENO, ": path too long\n",
204 			    16);
205 			continue;
206 		}
207 		bcopy(p, buf, lp);
208 		buf[lp] = '/';
209 		bcopy(name, buf + lp + 1, ln);
210 		buf[lp + ln + 1] = '\0';
211 
212 retry:		(void)_execve(bp, argv, environ);
213 		switch(errno) {
214 		case E2BIG:
215 			goto done;
216 		case ELOOP:
217 		case ENAMETOOLONG:
218 		case ENOENT:
219 			break;
220 		case ENOEXEC:
221 			for (cnt = 0; argv[cnt]; ++cnt)
222 				;
223 			memp = alloca((cnt + 2) * sizeof(char *));
224 			if (memp == NULL) {
225 				/* errno = ENOMEM; XXX override ENOEXEC? */
226 				goto done;
227 			}
228 			memp[0] = "sh";
229 			memp[1] = bp;
230 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
231 			(void)_execve(_PATH_BSHELL, memp, environ);
232 			goto done;
233 		case ENOMEM:
234 			goto done;
235 		case ENOTDIR:
236 			break;
237 		case ETXTBSY:
238 			/*
239 			 * We used to retry here, but sh(1) doesn't.
240 			 */
241 			goto done;
242 		default:
243 			/*
244 			 * EACCES may be for an inaccessible directory or
245 			 * a non-executable file.  Call stat() to decide
246 			 * which.  This also handles ambiguities for EFAULT
247 			 * and EIO, and undocumented errors like ESTALE.
248 			 * We hope that the race for a stat() is unimportant.
249 			 */
250 			save_errno = errno;
251 			if (stat(bp, &sb) != 0)
252 				break;
253 			if (save_errno == EACCES) {
254 				eacces = 1;
255 				continue;
256 			}
257 			errno = save_errno;
258 			goto done;
259 		}
260 	}
261 	if (eacces)
262 		errno = EACCES;
263 	else
264 		errno = ENOENT;
265 done:
266 	return (-1);
267 }
268