S390: Use s390-64 specific ionv-modules on s390-32, too.
[glibc.git] / posix / execvpe.c
blobd933f9c92acf58c182af5994b226484dd4519713
1 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <unistd.h>
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <paths.h>
25 #include <confstr.h>
26 #include <sys/param.h>
28 #ifndef PATH_MAX
29 # ifdef MAXPATHLEN
30 # define PATH_MAX MAXPATHLEN
31 # else
32 # define PATH_MAX 1024
33 # endif
34 #endif
36 /* The file is accessible but it is not an executable file. Invoke
37 the shell to interpret it as a script. */
38 static void
39 maybe_script_execute (const char *file, char *const argv[], char *const envp[])
41 ptrdiff_t argc = 0;
42 while (argv[argc++] != NULL)
44 if (argc == INT_MAX - 1)
46 errno = E2BIG;
47 return;
51 /* Construct an argument list for the shell. */
52 char *new_argv[argc + 1];
53 new_argv[0] = (char *) _PATH_BSHELL;
54 new_argv[1] = (char *) file;
55 if (argc > 1)
56 memcpy (new_argv + 2, argv + 1, argc * sizeof(char *));
57 else
58 new_argv[2] = NULL;
60 /* Execute the shell. */
61 __execve (new_argv[0], new_argv, envp);
65 /* Execute FILE, searching in the `PATH' environment variable if it contains
66 no slashes, with arguments ARGV and environment from ENVP. */
67 int
68 __execvpe (const char *file, char *const argv[], char *const envp[])
70 /* We check the simple case first. */
71 if (*file == '\0')
73 __set_errno (ENOENT);
74 return -1;
77 /* Don't search when it contains a slash. */
78 if (strchr (file, '/') != NULL)
80 __execve (file, argv, envp);
82 if (errno == ENOEXEC)
83 maybe_script_execute (file, argv, envp);
85 return -1;
88 const char *path = getenv ("PATH");
89 if (!path)
90 path = CS_PATH;
91 /* Although GLIBC does not enforce NAME_MAX, we set it as the maximum
92 size to avoid unbounded stack allocation. Same applies for
93 PATH_MAX. */
94 size_t file_len = __strnlen (file, NAME_MAX + 1);
95 size_t path_len = __strnlen (path, PATH_MAX - 1) + 1;
97 if ((file_len > NAME_MAX)
98 || !__libc_alloca_cutoff (path_len + file_len + 1))
100 errno = ENAMETOOLONG;
101 return -1;
104 const char *subp;
105 bool got_eacces = false;
106 char buffer[path_len + file_len + 1];
107 for (const char *p = path; ; p = subp)
109 subp = __strchrnul (p, ':');
111 /* PATH is larger than PATH_MAX and thus potentially larger than
112 the stack allocation. */
113 if (subp - p >= path_len)
115 /* If there is only one path, bail out. */
116 if (*subp == '\0')
117 break;
118 /* Otherwise skip to next one. */
119 continue;
122 /* Use the current path entry, plus a '/' if nonempty, plus the file to
123 execute. */
124 char *pend = mempcpy (buffer, p, subp - p);
125 *pend = '/';
126 memcpy (pend + (p < subp), file, file_len + 1);
128 __execve (buffer, argv, envp);
130 if (errno == ENOEXEC)
131 /* This has O(P*C) behavior, where P is the length of the path and C
132 is the argument count. A better strategy would be allocate the
133 substitute argv and reuse it each time through the loop (so it
134 behaves as O(P+C) instead. */
135 maybe_script_execute (buffer, argv, envp);
137 switch (errno)
139 case EACCES:
140 /* Record that we got a 'Permission denied' error. If we end
141 up finding no executable we can use, we want to diagnose
142 that we did find one but were denied access. */
143 got_eacces = true;
144 case ENOENT:
145 case ESTALE:
146 case ENOTDIR:
147 /* Those errors indicate the file is missing or not executable
148 by us, in which case we want to just try the next path
149 directory. */
150 case ENODEV:
151 case ETIMEDOUT:
152 /* Some strange filesystems like AFS return even
153 stranger error numbers. They cannot reasonably mean
154 anything else so ignore those, too. */
155 break;
157 default:
158 /* Some other error means we found an executable file, but
159 something went wrong executing it; return the error to our
160 caller. */
161 return -1;
164 if (*subp++ == '\0')
165 break;
168 /* We tried every element and none of them worked. */
169 if (got_eacces)
170 /* At least one failure was due to permissions, so report that
171 error. */
172 __set_errno (EACCES);
174 return -1;
177 weak_alias (__execvpe, execvpe)