1OPENAT(2) Linux Programmer's Manual OPENAT(2)
2
3
4
6 openat - open a file relative to a directory file descriptor
7
9 #define _ATFILE_SOURCE
10 #include <fcntl.h>
11
12 int openat(int dirfd, const char *pathname, int flags);
13 int openat(int dirfd, const char *pathname, int flags, mode_t mode);
14
16 The openat() system call operates in exactly the same way as open(2),
17 except for the differences described in this manual page.
18
19 If the pathname given in pathname is relative, then it is interpreted
20 relative to the directory referred to by the file descriptor dirfd
21 (rather than relative to the current working directory of the calling
22 process, as is done by open(2) for a relative pathname).
23
24 If pathname is relative and dirfd is the special value AT_FDCWD, then
25 pathname is interpreted relative to the current working directory of
26 the calling process (like open(2)).
27
28 If pathname is absolute, then dirfd is ignored.
29
31 On success, openat() returns a new file descriptor. On error, -1 is
32 returned and errno is set to indicate the error.
33
35 The same errors that occur for open(2) can also occur for openat().
36 The following additional errors can occur for openat():
37
38 EBADF dirfd is not a valid file descriptor.
39
40 ENOTDIR
41 pathname is relative and dirfd is a file descriptor referring to
42 a file other than a directory.
43
45 openat() was added to Linux in kernel 2.6.16.
46
48 POSIX.1-2008. A similar system call exists on Solaris.
49
51 openat() and other similar system calls suffixed "at" are supported for
52 two reasons.
53
54 First, openat() allows an application to avoid race conditions that
55 could occur when using open(2) to open files in directories other than
56 the current working directory. These race conditions result from the
57 fact that some component of the directory prefix given to open(2) could
58 be changed in parallel with the call to open(2). Such races can be
59 avoided by opening a file descriptor for the target directory, and then
60 specifying that file descriptor as the dirfd argument of openat().
61
62 Second, openat() allows the implementation of a per-thread "current
63 working directory", via file descriptor(s) maintained by the applica‐
64 tion. (This functionality can also be obtained by tricks based on the
65 use of /proc/self/fd/dirfd, but less efficiently.)
66
68 faccessat(2), fchmodat(2), fchownat(2), fstatat(2), futimesat(2),
69 linkat(2), mkdirat(2), mknodat(2), open(2), readlinkat(2), renameat(2),
70 symlinkat(2), unlinkat(2), utimensat(2), mkfifoat(3), path_resolu‐
71 tion(7)
72
74 This page is part of release 3.22 of the Linux man-pages project. A
75 description of the project, and information about reporting bugs, can
76 be found at http://www.kernel.org/doc/man-pages/.
77
78
79
80Linux 2008-08-21 OPENAT(2)