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() and other similar system calls suffixed "at" are supported for
46 two reasons.
47
48 First, openat() allows an application to avoid race conditions that
49 could occur when using open(2) to open files in directories other than
50 the current working directory. These race conditions result from the
51 fact that some component of the directory prefix given to open() could
52 be changed in parallel with the call to open(). Such races can be
53 avoided by opening a file descriptor for the target directory, and then
54 specifying that file descriptor as the dirfd argument of openat().
55
56 Second, openat() allows the implementation of a per-thread "current
57 working directory", via file descriptor(s) maintained by the applica‐
58 tion. (This functionality can also be obtained by tricks based on the
59 use of /proc/self/fd/dirfd, but less efficiently.)
60
62 This system call is non-standard but is proposed for inclusion in a
63 future revision of POSIX.1. A similar system call exists on Solaris.
64
66 openat() was added to Linux in kernel 2.6.16.
67
69 faccessat(2), fchmodat(2), fchownat(2), fstatat(2), futimesat(2),
70 linkat(2), mkdirat(2), mknodat(2), open(2), path_resolution(2), read‐
71 linkat(2), renameat(2), symlinkat(2), unlinkat(2), mkfifoat(3).
72
73
74
75Linux 2.6.16 2006-03-06 OPENAT(2)