xref: /openbsd/lib/libc/gen/exec.c (revision 0e3c6306)
1 /*	$OpenBSD: exec.c,v 1.22 2015/09/12 14:56:50 guenther Exp $ */
2 /*-
3  * Copyright (c) 1991, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 
34 #include <errno.h>
35 #include <limits.h>
36 #include <paths.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 extern char **environ;
44 
45 int
46 execl(const char *name, const char *arg, ...)
47 {
48 	va_list ap;
49 	char **argv;
50 	int n;
51 
52 	va_start(ap, arg);
53 	n = 1;
54 	while (va_arg(ap, char *) != NULL)
55 		n++;
56 	va_end(ap);
57 	argv = alloca((n + 1) * sizeof(*argv));
58 	if (argv == NULL) {
59 		errno = ENOMEM;
60 		return (-1);
61 	}
62 	va_start(ap, arg);
63 	n = 1;
64 	argv[0] = (char *)arg;
65 	while ((argv[n] = va_arg(ap, char *)) != NULL)
66 		n++;
67 	va_end(ap);
68 	return (execve(name, argv, environ));
69 }
70 DEF_WEAK(execl);
71 
72 int
73 execle(const char *name, const char *arg, ...)
74 {
75 	va_list ap;
76 	char **argv, **envp;
77 	int n;
78 
79 	va_start(ap, arg);
80 	n = 1;
81 	while (va_arg(ap, char *) != NULL)
82 		n++;
83 	va_end(ap);
84 	argv = alloca((n + 1) * sizeof(*argv));
85 	if (argv == NULL) {
86 		errno = ENOMEM;
87 		return (-1);
88 	}
89 	va_start(ap, arg);
90 	n = 1;
91 	argv[0] = (char *)arg;
92 	while ((argv[n] = va_arg(ap, char *)) != NULL)
93 		n++;
94 	envp = va_arg(ap, char **);
95 	va_end(ap);
96 	return (execve(name, argv, envp));
97 }
98 
99 int
100 execlp(const char *name, const char *arg, ...)
101 {
102 	va_list ap;
103 	char **argv;
104 	int n;
105 
106 	va_start(ap, arg);
107 	n = 1;
108 	while (va_arg(ap, char *) != NULL)
109 		n++;
110 	va_end(ap);
111 	argv = alloca((n + 1) * sizeof(*argv));
112 	if (argv == NULL) {
113 		errno = ENOMEM;
114 		return (-1);
115 	}
116 	va_start(ap, arg);
117 	n = 1;
118 	argv[0] = (char *)arg;
119 	while ((argv[n] = va_arg(ap, char *)) != NULL)
120 		n++;
121 	va_end(ap);
122 	return (execvp(name, argv));
123 }
124 
125 int
126 execv(const char *name, char *const *argv)
127 {
128 	(void)execve(name, argv, environ);
129 	return (-1);
130 }
131 
132 int
133 execvpe(const char *name, char *const *argv, char *const *envp)
134 {
135 	char **memp;
136 	int cnt;
137 	size_t lp, ln, len;
138 	char *p;
139 	int eacces = 0;
140 	char *bp, *cur, *path, buf[PATH_MAX];
141 
142 	/*
143 	 * Do not allow null name
144 	 */
145 	if (name == NULL || *name == '\0') {
146 		errno = ENOENT;
147 		return (-1);
148  	}
149 
150 	/* If it's an absolute or relative path name, it's easy. */
151 	if (strchr(name, '/')) {
152 		bp = (char *)name;
153 		cur = path = NULL;
154 		goto retry;
155 	}
156 	bp = buf;
157 
158 	/* Get the path we're searching. */
159 	if (!(path = getenv("PATH")))
160 		path = _PATH_DEFPATH;
161 	len = strlen(path) + 1;
162 	cur = alloca(len);
163 	if (cur == NULL) {
164 		errno = ENOMEM;
165 		return (-1);
166 	}
167 	strlcpy(cur, path, len);
168 	path = cur;
169 	while ((p = strsep(&cur, ":"))) {
170 		/*
171 		 * It's a SHELL path -- double, leading and trailing colons
172 		 * mean the current directory.
173 		 */
174 		if (!*p) {
175 			p = ".";
176 			lp = 1;
177 		} else
178 			lp = strlen(p);
179 		ln = strlen(name);
180 
181 		/*
182 		 * If the path is too long complain.  This is a possible
183 		 * security issue; given a way to make the path too long
184 		 * the user may execute the wrong program.
185 		 */
186 		if (lp + ln + 2 > sizeof(buf)) {
187 			struct iovec iov[3];
188 
189 			iov[0].iov_base = "execvp: ";
190 			iov[0].iov_len = 8;
191 			iov[1].iov_base = p;
192 			iov[1].iov_len = lp;
193 			iov[2].iov_base = ": path too long\n";
194 			iov[2].iov_len = 16;
195 			(void)writev(STDERR_FILENO, iov, 3);
196 			continue;
197 		}
198 		bcopy(p, buf, lp);
199 		buf[lp] = '/';
200 		bcopy(name, buf + lp + 1, ln);
201 		buf[lp + ln + 1] = '\0';
202 
203 retry:		(void)execve(bp, argv, envp);
204 		switch(errno) {
205 		case E2BIG:
206 			goto done;
207 		case EISDIR:
208 		case ELOOP:
209 		case ENAMETOOLONG:
210 		case ENOENT:
211 			break;
212 		case ENOEXEC:
213 			for (cnt = 0; argv[cnt]; ++cnt)
214 				;
215 			memp = alloca((cnt + 2) * sizeof(char *));
216 			if (memp == NULL)
217 				goto done;
218 			memp[0] = "sh";
219 			memp[1] = bp;
220 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
221 			(void)execve(_PATH_BSHELL, memp, envp);
222 			goto done;
223 		case ENOMEM:
224 			goto done;
225 		case ENOTDIR:
226 			break;
227 		case ETXTBSY:
228 			/*
229 			 * We used to retry here, but sh(1) doesn't.
230 			 */
231 			goto done;
232 		case EACCES:
233 			eacces = 1;
234 			break;
235 		default:
236 			goto done;
237 		}
238 	}
239 	if (eacces)
240 		errno = EACCES;
241 	else if (!errno)
242 		errno = ENOENT;
243 done:
244 	return (-1);
245 }
246 DEF_WEAK(execvpe);
247 
248 int
249 execvp(const char *name, char *const *argv)
250 {
251     return execvpe(name, argv, environ);
252 }
253 DEF_WEAK(execvp);
254 
255