Debian Codegraph @

src/posix_spawnp.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010-2015 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 #ifdef HAVE_POSIX_SPAWNP
24 #define _GNU_SOURCE
25 #define _DEFAULT_SOURCE
26 #include <errno.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <spawn.h>
31 #include <alloca.h>
32 #include "strchrnul.h"
33 #include "libfakechroot.h"
34
35 #define DEFAULT_PATH ":/usr/bin:/bin"
36
37
38 /*
39 Based off of implementation of execvp in execvp.c
40 That was taken from the execvp function in the GNU C Library.
41 Copyright (C) 1991,92, 1995-99, 2002, 2004, 2005, 2007, 2009
42 Free Software Foundation, Inc.
43 */
44 wrapper(posix_spawnp, int, (pid_t* pid, const char* file,
45 const posix_spawn_file_actions_t* file_actions,
46 const posix_spawnattr_t* attrp, char *const argv[],
47 char *const envp[]))
48 {
49 debug("posix_spawnp(\"%s\", {\"%s\", ...})", file, argv[0]);
50 if (*file == '\0') {
51 /* We check the simple case first. */
52 __set_errno(ENOENT);
53 return errno;
54 }
55
56 if (strchr(file, '/') != NULL) {
57 /* Don't search when it contains a slash. */
58 return posix_spawn(pid, file, file_actions, attrp, argv, envp);
59 } else {
60 int got_eacces = 0;
61 char *path, *p, *name;
62 size_t len;
63 size_t pathlen;
64
65 path = getenv("PATH");
66 if (path == NULL) {
67 /* There is no `PATH' in the environment.
68 The default search path is the current directory
69 followed by the path `confstr' returns for `_CS_PATH'. */
70 #ifdef _CS_PATH
71 len = confstr(_CS_PATH, (char *) NULL, 0);
72 path = (char *) alloca(1 + len);
73 path[0] = ':';
74 (void) confstr(_CS_PATH, path + 1, len);
75 #else
76 path = (char *) alloca(sizeof(DEFAULT_PATH));
77 strcpy(path, DEFAULT_PATH);
78 #endif
79 }
80
81 len = strlen(file) + 1;
82 pathlen = strlen(path);
83 name = alloca(pathlen + len + 1);
84 /* Copy the file name at the top. */
85 name = (char *) memcpy(name + pathlen + 1, file, len);
86 /* And add the slash. */
87 *--name = '/';
88
89 p = path;
90 do {
91 char *startp;
92
93 path = p;
94 p = strchrnul(path, ':');
95
96 if (p == path)
97 /* Two adjacent colons, or a colon at the beginning or the end
98 of `PATH' means to search the current directory. */
99 startp = name + 1;
100 else
101 startp = (char *) memcpy(name - (p - path), path, p - path);
102
103 /* Try to execute this name. If it works, return. */
104 if (!posix_spawn(pid, startp, file_actions, attrp, argv, envp))
105 return 0;
106
107 switch (errno) {
108 case EACCES:
109 /* Record the we got a `Permission denied' error. If we end
110 up finding no executable we can use, we want to diagnose
111 that we did find one but were denied access. */
112 got_eacces = 1;
113 break;
114 case ENOENT:
115 case ESTALE:
116 case ENOTDIR:
117 /* Those errors indicate the file is missing or not executable
118 by us, in which case we want to just try the next path
119 directory. */
120 break;
121
122 default:
123 /* Some other error means we found an executable file, but
124 something went wrong executing it; return the error to our
125 caller. */
126 return errno;
127 }
128 } while (*p++ != '\0');
129
130 /* We tried every element and none of them worked. */
131 if (got_eacces)
132 /* At least one failure was due to permissions, so report that
133 error. */
134 __set_errno(EACCES);
135 }
136
137 /* Return the error from the last attempt (probably ENOENT). */
138 return errno;
139 }
140 #endif