OPENAT(2) Linux Programmer's Manual OPENAT(2)

openat - open a file at a relative location

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);

The openat() system call operates in exactly the same way as open(2), except for the differences described in this manual page.

If the pathname given in pathname is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by open(2) for a relative pathname).

If the pathname given in pathname is relative and dirfd is the special value AT_FDCWD, then pathname is interpreted relative to the current working directory of the calling process (like open(2)).

If the pathname given in pathname is absolute, then dirfd is ignored.

openat() returns a new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

The same errors that occur for open(2) can also occur for openat(2). The following additional errors can occur for openat():

dirfd is not a valid file descriptor.
dirfd is a file descriptor referring to a file other than a directory.

The openat() system call (and similar system calls suffixed "at") provides functionality that can otherwise only be obtained less efficiently or at the risk of races in user applications. It allows the implementation of a per-thread "current working directory", via file descriptor(s) maintained by the application. (This functionality can also be obtained by tricks based on the use of /proc/self/fd/dirfd, but less efficiently.) It also allows the avoidance of various kinds of races that can occur as a result of directory deletions that may occur while an application is traversing a file system.

This system call is non-standard. A similar system call exists on Solaris.

openat() was added to Linux in kernel 2.6.16.

open(2), path_resolution(2)

2006-03-06 Linux 2.6.16

Different Versions of this Page: