Debian Codegraph @

src/execvp.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #define _GNU_SOURCE
24 #include <errno.h>
25 #include <stddef.h>
26 #ifdef HAVE_ALLOCA_H
27 # include <alloca.h>
28 #endif
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include "strchrnul.h"
32 #include "libfakechroot.h"
33
34 #ifndef __GLIBC__
35 extern char **environ;
36 #endif
37
38 #define DEFAULT_PATH ":/usr/bin:/bin"
39
40
41 /*
42 execvp function taken from GNU C Library.
43 Copyright (C) 1991,92, 1995-99, 2002, 2004, 2005, 2007, 2009
44 Free Software Foundation, Inc.
45 */
46 wrapper(execvp, int, (const char * file, char * const argv[]))
47 {
48 debug("execvp(\"%s\", {\"%s\", ...})", file, argv[0]);
49 if (*file == '\0') {
50 /* We check the simple case first. */
51 __set_errno(ENOENT);
52 return -1;
53 }
54
55 if (strchr(file, '/') != NULL) {
56 /* Don't search when it contains a slash. */
57 return execve(file, argv, environ);
58 } else {
59 int got_eacces = 0;
60 char *path, *p, *name;
61 size_t len;
62 size_t pathlen;
63
64 path = getenv("PATH");
65 if (path == NULL) {
66 /* There is no `PATH' in the environment.
67 The default search path is the current directory
68 followed by the path `confstr' returns for `_CS_PATH'. */
69 #ifdef _CS_PATH
70 len = confstr(_CS_PATH, (char *) NULL, 0);
71 path = (char *) alloca(1 + len);
72 path[0] = ':';
73 (void) confstr(_CS_PATH, path + 1, len);
74 #else
75 path = (char *) alloca(sizeof(DEFAULT_PATH));
76 strcpy(path, DEFAULT_PATH);
77 #endif
78 }
79
80 len = strlen(file) + 1;
81 pathlen = strlen(path);
82 name = alloca(pathlen + len + 1);
83 /* Copy the file name at the top. */
84 name = (char *) memcpy(name + pathlen + 1, file, len);
85 /* And add the slash. */
86 *--name = '/';
87
88 p = path;
89 do {
90 char *startp;
91
92 path = p;
93 p = strchrnul(path, ':');
94
95 if (p == path)
96 /* Two adjacent colons, or a colon at the beginning or the end
97 of `PATH' means to search the current directory. */
98 startp = name + 1;
99 else
100 startp = (char *) memcpy(name - (p - path), path, p - path);
101
102 /* Try to execute this name. If it works, execv will not return. */
103 execve(startp, argv, environ);
104
105 switch (errno) {
106 case EACCES:
107 /* Record the we got a `Permission denied' error. If we end
108 up finding no executable we can use, we want to diagnose
109 that we did find one but were denied access. */
110 got_eacces = 1;
111 break;
112 case ENOENT:
113 case ESTALE:
114 case ENOTDIR:
115 /* Those errors indicate the file is missing or not executable
116 by us, in which case we want to just try the next path
117 directory. */
118 break;
119
120 default:
121 /* Some other error means we found an executable file, but
122 something went wrong executing it; return the error to our
123 caller. */
124 return -1;
125 }
126 } while (*p++ != '\0');
127
128 /* We tried every element and none of them worked. */
129 if (got_eacces)
130 /* At least one failure was due to permissions, so report that
131 error. */
132 __set_errno(EACCES);
133 }
134
135 /* Return the error from the last attempt (probably ENOENT). */
136 return -1;
137 }