1OPENAT(2)                  Linux Programmer's Manual                 OPENAT(2)
2
3
4

NAME

6       openat - open a file relative to a directory file descriptor
7

SYNOPSIS

9       #include <fcntl.h>
10
11       int openat(int dirfd, const char *pathname, int flags);
12       int openat(int dirfd, const char *pathname, int flags, mode_t mode);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       openat():
17           Since glibc 2.10:
18               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
19           Before glibc 2.10:
20               _ATFILE_SOURCE
21

DESCRIPTION

23       The  openat()  system call operates in exactly the same way as open(2),
24       except for the differences described in this manual page.
25
26       If the pathname given in pathname is relative, then it  is  interpreted
27       relative  to  the  directory  referred  to by the file descriptor dirfd
28       (rather than relative to the current working directory of  the  calling
29       process, as is done by open(2) for a relative pathname).
30
31       If  pathname  is relative and dirfd is the special value AT_FDCWD, then
32       pathname is interpreted relative to the current  working  directory  of
33       the calling process (like open(2)).
34
35       If pathname is absolute, then dirfd is ignored.
36

RETURN VALUE

38       On  success,  openat()  returns a new file descriptor.  On error, -1 is
39       returned and errno is set to indicate the error.
40

ERRORS

42       The same errors that occur for open(2) can  also  occur  for  openat().
43       The following additional errors can occur for openat():
44
45       EBADF  dirfd is not a valid file descriptor.
46
47       ENOTDIR
48              pathname is relative and dirfd is a file descriptor referring to
49              a file other than a directory.
50

VERSIONS

52       openat() was added to Linux in kernel 2.6.16; library support was added
53       to glibc in version 2.4.
54

CONFORMING TO

56       POSIX.1-2008.  A similar system call exists on Solaris.
57

NOTES

59       openat() and other similar system calls suffixed "at" are supported for
60       two reasons.
61
62       First, openat() allows an application to  avoid  race  conditions  that
63       could  occur when using open(2) to open files in directories other than
64       the current working directory.  These race conditions result  from  the
65       fact that some component of the directory prefix given to open(2) could
66       be changed in parallel with the call to open(2).   Such  races  can  be
67       avoided by opening a file descriptor for the target directory, and then
68       specifying that file descriptor as the dirfd argument of openat().
69
70       Second, openat() allows the implementation  of  a  per-thread  "current
71       working  directory",  via file descriptor(s) maintained by the applica‐
72       tion.  (This functionality can also be obtained by tricks based on  the
73       use of /proc/self/fd/dirfd, but less efficiently.)
74

SEE ALSO

76       faccessat(2),   fchmodat(2),   fchownat(2),  fstatat(2),  futimesat(2),
77       linkat(2), mkdirat(2), mknodat(2), open(2), readlinkat(2), renameat(2),
78       symlinkat(2),   unlinkat(2),  utimensat(2),  mkfifoat(3),  path_resolu‐
79       tion(7)
80

COLOPHON

82       This page is part of release 3.53 of the Linux  man-pages  project.   A
83       description  of  the project, and information about reporting bugs, can
84       be found at http://www.kernel.org/doc/man-pages/.
85
86
87
88Linux                             2012-05-04                         OPENAT(2)
Impressum