1execveat(2) System Calls Manual execveat(2)
2
3
4
6 execveat - execute program relative to a directory file descriptor
7
9 Standard C library (libc, -lc)
10
12 #include <linux/fcntl.h> /* Definition of AT_* constants */
13 #include <unistd.h>
14
15 int execveat(int dirfd, const char *pathname,
16 char *const _Nullable argv[],
17 char *const _Nullable envp[],
18 int flags);
19
21 The execveat() system call executes the program referred to by the com‐
22 bination of dirfd and pathname. It operates in exactly the same way as
23 execve(2), except for the differences described in this manual page.
24
25 If the pathname given in pathname is relative, then it is interpreted
26 relative to the directory referred to by the file descriptor dirfd
27 (rather than relative to the current working directory of the calling
28 process, as is done by execve(2) for a relative pathname).
29
30 If pathname is relative and dirfd is the special value AT_FDCWD, then
31 pathname is interpreted relative to the current working directory of
32 the calling process (like execve(2)).
33
34 If pathname is absolute, then dirfd is ignored.
35
36 If pathname is an empty string and the AT_EMPTY_PATH flag is specified,
37 then the file descriptor dirfd specifies the file to be executed (i.e.,
38 dirfd refers to an executable file, rather than a directory).
39
40 The flags argument is a bit mask that can include zero or more of the
41 following flags:
42
43 AT_EMPTY_PATH
44 If pathname is an empty string, operate on the file referred to
45 by dirfd (which may have been obtained using the open(2) O_PATH
46 flag).
47
48 AT_SYMLINK_NOFOLLOW
49 If the file identified by dirfd and a non-NULL pathname is a
50 symbolic link, then the call fails with the error ELOOP.
51
53 On success, execveat() does not return. On error, -1 is returned, and
54 errno is set to indicate the error.
55
57 The same errors that occur for execve(2) can also occur for execveat().
58 The following additional errors can occur for execveat():
59
60 pathname
61 is relative but dirfd is neither AT_FDCWD nor a valid file de‐
62 scriptor.
63
64 EINVAL Invalid flag specified in flags.
65
66 ELOOP flags includes AT_SYMLINK_NOFOLLOW and the file identified by
67 dirfd and a non-NULL pathname is a symbolic link.
68
69 ENOENT The program identified by dirfd and pathname requires the use of
70 an interpreter program (such as a script starting with "#!"),
71 but the file descriptor dirfd was opened with the O_CLOEXEC
72 flag, with the result that the program file is inaccessible to
73 the launched interpreter. See BUGS.
74
75 ENOTDIR
76 pathname is relative and dirfd is a file descriptor referring to
77 a file other than a directory.
78
80 Linux.
81
83 Linux 3.19, glibc 2.34.
84
86 In addition to the reasons explained in openat(2), the execveat() sys‐
87 tem call is also needed to allow fexecve(3) to be implemented on sys‐
88 tems that do not have the /proc filesystem mounted.
89
90 When asked to execute a script file, the argv[0] that is passed to the
91 script interpreter is a string of the form /dev/fd/N or /dev/fd/N/P,
92 where N is the number of the file descriptor passed via the dirfd argu‐
93 ment. A string of the first form occurs when AT_EMPTY_PATH is em‐
94 ployed. A string of the second form occurs when the script is speci‐
95 fied via both dirfd and pathname; in this case, P is the value given in
96 pathname.
97
98 For the same reasons described in fexecve(3), the natural idiom when
99 using execveat() is to set the close-on-exec flag on dirfd. (But see
100 BUGS.)
101
103 The ENOENT error described above means that it is not possible to set
104 the close-on-exec flag on the file descriptor given to a call of the
105 form:
106
107 execveat(fd, "", argv, envp, AT_EMPTY_PATH);
108
109 However, the inability to set the close-on-exec flag means that a file
110 descriptor referring to the script leaks through to the script itself.
111 As well as wasting a file descriptor, this leakage can lead to file-de‐
112 scriptor exhaustion in scenarios where scripts recursively employ ex‐
113 ecveat().
114
116 execve(2), openat(2), fexecve(3)
117
118
119
120Linux man-pages 6.05 2023-03-30 execveat(2)