xref: /freebsd/lib/libc/gen/exec.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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 
38 #include <sys/param.h>
39 #include <sys/types.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 #if __STDC__
48 #include <stdarg.h>
49 #else
50 #include <varargs.h>
51 #endif
52 
53 extern char **environ;
54 
55 static char **
56 buildargv(ap, arg, envpp)
57 	va_list ap;
58 	const char *arg;
59 	char ***envpp;
60 {
61 	static int memsize;
62 	static char **argv;
63 	register int off;
64 
65 	argv = NULL;
66 	for (off = 0;; ++off) {
67 		if (off >= memsize) {
68 			memsize += 50;	/* Starts out at 0. */
69 			memsize *= 2;	/* Ramp up fast. */
70 			if (!(argv = realloc(argv, memsize * sizeof(char *)))) {
71 				memsize = 0;
72 				return (NULL);
73 			}
74 			if (off == 0) {
75 				argv[0] = (char *)arg;
76 				off = 1;
77 			}
78 		}
79 		if (!(argv[off] = va_arg(ap, char *)))
80 			break;
81 	}
82 	/* Get environment pointer if user supposed to provide one. */
83 	if (envpp)
84 		*envpp = va_arg(ap, char **);
85 	return (argv);
86 }
87 
88 int
89 #if __STDC__
90 execl(const char *name, const char *arg, ...)
91 #else
92 execl(name, arg, va_alist)
93 	const char *name;
94 	const char *arg;
95 	va_dcl
96 #endif
97 {
98 	va_list ap;
99 	int sverrno;
100 	char **argv;
101 
102 #if __STDC__
103 	va_start(ap, arg);
104 #else
105 	va_start(ap);
106 #endif
107 	if (argv = buildargv(ap, arg, NULL))
108 		(void)execve(name, argv, environ);
109 	va_end(ap);
110 	sverrno = errno;
111 	free(argv);
112 	errno = sverrno;
113 	return (-1);
114 }
115 
116 int
117 #if __STDC__
118 execle(const char *name, const char *arg, ...)
119 #else
120 execle(name, arg, va_alist)
121 	const char *name;
122 	const char *arg;
123 	va_dcl
124 #endif
125 {
126 	va_list ap;
127 	int sverrno;
128 	char **argv, **envp;
129 
130 #if __STDC__
131 	va_start(ap, arg);
132 #else
133 	va_start(ap);
134 #endif
135 	if (argv = buildargv(ap, arg, &envp))
136 		(void)execve(name, argv, envp);
137 	va_end(ap);
138 	sverrno = errno;
139 	free(argv);
140 	errno = sverrno;
141 	return (-1);
142 }
143 
144 int
145 #if __STDC__
146 execlp(const char *name, const char *arg, ...)
147 #else
148 execlp(name, arg, va_alist)
149 	const char *name;
150 	const char *arg;
151 	va_dcl
152 #endif
153 {
154 	va_list ap;
155 	int sverrno;
156 	char **argv;
157 
158 #if __STDC__
159 	va_start(ap, arg);
160 #else
161 	va_start(ap);
162 #endif
163 	if (argv = buildargv(ap, arg, NULL))
164 		(void)execvp(name, argv);
165 	va_end(ap);
166 	sverrno = errno;
167 	free(argv);
168 	errno = sverrno;
169 	return (-1);
170 }
171 
172 int
173 execv(name, argv)
174 	const char *name;
175 	char * const *argv;
176 {
177 	(void)execve(name, argv, environ);
178 	return (-1);
179 }
180 
181 int
182 execvp(name, argv)
183 	const char *name;
184 	char * const *argv;
185 {
186 	static int memsize;
187 	static char **memp;
188 	register int cnt, lp, ln;
189 	register char *p;
190 	int eacces, etxtbsy;
191 	char *bp, *cur, *path, buf[MAXPATHLEN];
192 
193 	/* If it's an absolute or relative path name, it's easy. */
194 	if (index(name, '/')) {
195 		bp = (char *)name;
196 		cur = path = NULL;
197 		goto retry;
198 	}
199 	bp = buf;
200 
201 	/* Get the path we're searching. */
202 	if (!(path = getenv("PATH")))
203 		path = _PATH_DEFPATH;
204 	cur = path = strdup(path);
205 
206 	eacces = etxtbsy = 0;
207 	while (p = strsep(&cur, ":")) {
208 		/*
209 		 * It's a SHELL path -- double, leading and trailing colons
210 		 * mean the current directory.
211 		 */
212 		if (!*p) {
213 			p = ".";
214 			lp = 1;
215 		} else
216 			lp = strlen(p);
217 		ln = strlen(name);
218 
219 		/*
220 		 * If the path is too long complain.  This is a possible
221 		 * security issue; given a way to make the path too long
222 		 * the user may execute the wrong program.
223 		 */
224 		if (lp + ln + 2 > sizeof(buf)) {
225 			(void)write(STDERR_FILENO, "execvp: ", 8);
226 			(void)write(STDERR_FILENO, p, lp);
227 			(void)write(STDERR_FILENO, ": path too long\n", 16);
228 			continue;
229 		}
230 		bcopy(p, buf, lp);
231 		buf[lp] = '/';
232 		bcopy(name, buf + lp + 1, ln);
233 		buf[lp + ln + 1] = '\0';
234 
235 retry:		(void)execve(bp, argv, environ);
236 		switch(errno) {
237 		case EACCES:
238 			eacces = 1;
239 			break;
240 		case ENOENT:
241 			break;
242 		case ENOEXEC:
243 			for (cnt = 0; argv[cnt]; ++cnt);
244 			if ((cnt + 2) * sizeof(char *) > memsize) {
245 				memsize = (cnt + 2) * sizeof(char *);
246 				if ((memp = realloc(memp, memsize)) == NULL) {
247 					memsize = 0;
248 					goto done;
249 				}
250 			}
251 			memp[0] = "sh";
252 			memp[1] = bp;
253 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
254 			(void)execve(_PATH_BSHELL, memp, environ);
255 			goto done;
256 		case ETXTBSY:
257 			if (etxtbsy < 3)
258 				(void)sleep(++etxtbsy);
259 			goto retry;
260 		default:
261 			goto done;
262 		}
263 	}
264 	if (eacces)
265 		errno = EACCES;
266 	else if (!errno)
267 		errno = ENOENT;
268 done:	if (path)
269 		free(path);
270 	return (-1);
271 }
272