• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*	$OpenBSD: exec.c,v 1.18 2005/08/08 08:05:34 espie 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/param.h>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <paths.h>
41 #include <stdarg.h>
42 #include <alloca.h>
43 
44 extern char **environ;
45 
46 int
execl(const char * name,const char * arg,...)47 execl(const char *name, const char *arg, ...)
48 {
49 	va_list ap;
50 	char **argv;
51 	int n;
52 
53 	va_start(ap, arg);
54 	n = 1;
55 	while (va_arg(ap, char *) != NULL)
56 		n++;
57 	va_end(ap);
58 	argv = alloca((n + 1) * sizeof(*argv));
59 	if (argv == NULL) {
60 		errno = ENOMEM;
61 		return (-1);
62 	}
63 	va_start(ap, arg);
64 	n = 1;
65 	argv[0] = (char *)arg;
66 	while ((argv[n] = va_arg(ap, char *)) != NULL)
67 		n++;
68 	va_end(ap);
69 	return (execve(name, argv, environ));
70 }
71 
72 int
execle(const char * name,const char * arg,...)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
execlp(const char * name,const char * arg,...)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
execv(const char * name,char * const * argv)126 execv(const char *name, char * const *argv)
127 {
128 	(void)execve(name, argv, environ);
129 	return (-1);
130 }
131 
132 int
execvp(const char * name,char * const * argv)133 execvp(const char *name, char * const *argv)
134 {
135 	char **memp;
136 	int cnt, lp, ln, len;
137 	char *p;
138 	int eacces = 0;
139 	char *bp, *cur, *path, buf[MAXPATHLEN];
140 
141 	/*
142 	 * Do not allow null name
143 	 */
144 	if (name == NULL || *name == '\0') {
145 		errno = ENOENT;
146 		return (-1);
147  	}
148 
149 	/* If it's an absolute or relative path name, it's easy. */
150 	if (strchr(name, '/')) {
151 		bp = (char *)name;
152 		cur = path = NULL;
153 		goto retry;
154 	}
155 	bp = buf;
156 
157 	/* Get the path we're searching. */
158 	if (!(path = getenv("PATH")))
159 		path = _PATH_DEFPATH;
160 	len = strlen(path) + 1;
161 	cur = alloca(len);
162 	if (cur == NULL) {
163 		errno = ENOMEM;
164 		return (-1);
165 	}
166 	strlcpy(cur, path, len);
167 	path = cur;
168 	while ((p = strsep(&cur, ":"))) {
169 		/*
170 		 * It's a SHELL path -- double, leading and trailing colons
171 		 * mean the current directory.
172 		 */
173 		if (!*p) {
174 			p = ".";
175 			lp = 1;
176 		} else
177 			lp = strlen(p);
178 		ln = strlen(name);
179 
180 		/*
181 		 * If the path is too long complain.  This is a possible
182 		 * security issue; given a way to make the path too long
183 		 * the user may execute the wrong program.
184 		 */
185 		if (lp + ln + 2 > (int)sizeof(buf)) {
186 			struct iovec iov[3];
187 
188 			iov[0].iov_base = "execvp: ";
189 			iov[0].iov_len = 8;
190 			iov[1].iov_base = p;
191 			iov[1].iov_len = lp;
192 			iov[2].iov_base = ": path too long\n";
193 			iov[2].iov_len = 16;
194 			(void)writev(STDERR_FILENO, iov, 3);
195 			continue;
196 		}
197 		bcopy(p, buf, lp);
198 		buf[lp] = '/';
199 		bcopy(name, buf + lp + 1, ln);
200 		buf[lp + ln + 1] = '\0';
201 
202 retry:		(void)execve(bp, argv, environ);
203 		switch(errno) {
204 		case E2BIG:
205 			goto done;
206 		case EISDIR:
207 		case ELOOP:
208 		case ENAMETOOLONG:
209 		case ENOENT:
210 			break;
211 		case ENOEXEC:
212 			for (cnt = 0; argv[cnt]; ++cnt)
213 				;
214 			memp = alloca((cnt + 2) * sizeof(char *));
215 			if (memp == NULL)
216 				goto done;
217 			memp[0] = "sh";
218 			memp[1] = bp;
219 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
220 			(void)execve(_PATH_BSHELL, memp, environ);
221 			goto done;
222 		case ENOMEM:
223 			goto done;
224 		case ENOTDIR:
225 			break;
226 		case ETXTBSY:
227 			/*
228 			 * We used to retry here, but sh(1) doesn't.
229 			 */
230 			goto done;
231 		case EACCES:
232 			eacces = 1;
233 			break;
234 		default:
235 			goto done;
236 		}
237 	}
238 	if (eacces)
239 		errno = EACCES;
240 	else if (!errno)
241 		errno = ENOENT;
242 done:
243 	return (-1);
244 }
245