1 /* Copyright (C) 1994-2019 Free Software Foundation, Inc.
2
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with the GNU C Library; if not, see
15 <https://www.gnu.org/licenses/>. */
16
17 #include <errno.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23
24 int
fexecve(int fd,char * const argv[],char * const envp[])25 fexecve (int fd, char *const argv[], char *const envp[])
26 {
27 if (fd < 0 || argv == NULL || envp == NULL)
28 {
29 __set_errno (EINVAL);
30 return -1;
31 }
32 /* We use the /proc filesystem to get the information. If it is not
33 mounted we fail. */
34 char buf[sizeof "/proc/self/fd/" + sizeof (int) * 3];
35 snprintf (buf, sizeof (buf), "/proc/self/fd/%d", fd);
36
37 /* We do not need the return value. */
38 execve (buf, argv, envp);
39
40 int save = errno;
41
42 /* We come here only if the 'execve' call fails. Determine whether
43 /proc is mounted. If not we return ENOSYS. */
44 struct stat st;
45 if (stat ("/proc/self/fd", &st) != 0 && errno == ENOENT)
46 save = ENOSYS;
47
48 __set_errno (save);
49
50 return -1;
51 }
52 libc_hidden_def(fexecve)
53