execvp.c revision 1.28 1 /* $NetBSD: execvp.c,v 1.28 2006/11/09 02:51:52 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: execvp.c,v 1.28 2006/11/09 02:51:52 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40
41 #include "namespace.h"
42 #include <assert.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <unistd.h>
49 #include <paths.h>
50 #if defined(__SSP__) || defined(__SSP_ALL__)
51 #include <sys/mman.h>
52 #endif
53 #include "reentrant.h"
54
55 #ifdef __weak_alias
56 __weak_alias(execvp,_execvp)
57 #endif
58
59 extern char **environ;
60
61 int
62 execvp(const char *name, char * const *argv)
63 {
64 const char **memp;
65 int cnt;
66 size_t lp, ln;
67 int eacces = 0;
68 unsigned int etxtbsy = 0;
69 char buf[PATH_MAX];
70 const char *bp, *path, *p;
71
72 _DIAGASSERT(name != NULL);
73
74 /* "" is not a valid filename; check this before traversing PATH. */
75 if (name[0] == '\0') {
76 errno = ENOENT;
77 goto done;
78 }
79 ln = strlen(name);
80 /* If it's an absolute or relative path name, it's easy. */
81 if (strchr(name, '/')) {
82 bp = name;
83 path = "";
84 goto retry;
85 }
86 bp = buf;
87
88 /* Get the path we're searching. */
89 if (!(path = getenv("PATH")))
90 path = _PATH_DEFPATH;
91
92 do {
93 /* Find the end of this path element. */
94 for (p = path; *path != 0 && *path != ':'; path++)
95 continue;
96 /*
97 * It's a SHELL path -- double, leading and trailing colons
98 * mean the current directory.
99 */
100 if (p == path) {
101 p = ".";
102 lp = 1;
103 } else
104 lp = path - p;
105
106 /*
107 * If the path is too long complain. This is a possible
108 * security issue; given a way to make the path too long
109 * the user may execute the wrong program.
110 */
111 if (lp + ln + 2 > sizeof(buf)) {
112 (void)write(STDERR_FILENO, "execvp: ", 8);
113 (void)write(STDERR_FILENO, p, lp);
114 (void)write(STDERR_FILENO, ": path too long\n", 16);
115 continue;
116 }
117 memcpy(buf, p, lp);
118 buf[lp] = '/';
119 memcpy(buf + lp + 1, name, ln);
120 buf[lp + ln + 1] = '\0';
121
122 retry: (void)execve(bp, argv, environ);
123 switch (errno) {
124 case EACCES:
125 eacces = 1;
126 break;
127 case ENOTDIR:
128 case ENOENT:
129 break;
130 case ENOEXEC:
131 for (cnt = 0; argv[cnt] != NULL; ++cnt)
132 continue;
133 ln = (cnt + 2) * sizeof(*memp);
134 #if defined(__SSP__) || defined(__SSP_ALL__)
135 if ((memp = mmap(NULL, ln, PROT_READ|PROT_WRITE,
136 MAP_ANON, -1, 0)) == MAP_FAILED)
137 goto done;
138 #else
139 if ((memp = alloca(ln)) == NULL)
140 goto done;
141 #endif
142 memp[0] = _PATH_BSHELL;
143 memp[1] = bp;
144 (void)memcpy(&memp[2], &argv[1], cnt * sizeof(*memp));
145 (void)execve(_PATH_BSHELL, __UNCONST(memp), environ);
146 #if defined(__SSP__) || defined(__SSP_ALL__)
147 eacces = errno;
148 (void)munmap(memp, ln);
149 errno = eacces;
150 #endif
151 goto done;
152 case ETXTBSY:
153 if (etxtbsy < 3)
154 (void)sleep(++etxtbsy);
155 goto retry;
156 default:
157 goto done;
158 }
159 } while (*path++ == ':'); /* Otherwise, *path was NUL */
160 if (eacces)
161 errno = EACCES;
162 else if (!errno)
163 errno = ENOENT;
164 done:
165 return (-1);
166 }
167