open(2) System Calls Manual open(2) open, openat, creat - , , LIBRARY Standard C library (libc, -lc) #include int open(const char *pathname, int flags, ... /* mode_t mode */ ); int creat(const char *pathname, mode_t mode); int openat(int dirfd, const char *pathname, int flags, ... /* mode_t mode */ ); /* Documented separately, in openat2(2): */ int openat2(int dirfd, const char *pathname, const struct open_how *how, size_t size); glibc (. feature_test_macros(7)): openat(): glibc 2.10: _POSIX_C_SOURCE >= 200809L glibc 2.10: _ATFILE_SOURCE open() , pathname. , open() ( flags O_CREAT). The return value of open() is a file descriptor, a small, nonnegative integer that is an index to an entry in the process's table of open file descriptors. The file descriptor is used in subsequent system calls ( read(2), write(2), lseek(2), fcntl(2), etc.) to refer to the open file. The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process. , execve(2) (. ., FD_CLOEXEC , fcntl(2), ; O_CLOEXEC, ). (. lseek(2)). open() -- . ( ). -- ; pathname . . flags : O_RDONLY ( ), O_WRONLY ( ) O_RDWR ( ). In addition, zero or more file creation flags and file status flags can be bitwise ORed in flags. The file creation flags are O_CLOEXEC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and O_TRUNC. The file status flags are all of the remaining flags listed below. The distinction between these two groups of flags is that the file creation flags affect the semantics of the open operation itself, while the file status flags affect the semantics of subsequent I/O operations. The file status flags can be retrieved and (in some cases) modified; see fcntl(2) for details. : O_APPEND . write(2), , lseek(2). , . O_APPEND NFS, . - , NFS , , . O_ASYNC -, : ( SIGIO, fcntl(2)), . , , , ( Linux 2.6) FIFO. fcntl(2). . O_CLOEXEC ( Linux 2.6.23) close-on-exec . fcntl(2) F_SETFD FD_CLOEXEC. , , fcntl(2) F_SETFD FD_CLOEXEC , , fork(2) execve(2). , , , open(), , fork(2) ( , , , , close-on-exec, Linux O_CLOEXEC, ). O_CREAT pathname , . (ID ) . (ID ) ( System V) ID ( BSD). Linux set-group-ID : , BSD; System V. bsdgroups sysvgroups, mount(8). The mode argument specifies the file mode bits to be applied when a new file is created. If neither O_CREAT nor O_TMPFILE is specified in flags, then mode is ignored (and can thus be specified as 0, or simply omitted). The mode argument must be supplied if O_CREAT or O_TMPFILE is specified in flags; if it is not supplied, some arbitrary bytes from the stack will be applied as the file mode. The effective mode is modified by the process's umask in the usual way: in the absence of a default ACL, the mode of the created file is (mode & ~umask). Note that mode applies only to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor. , mode: S_IRWXU 00700 ( ) , S_IRUSR 00400 S_IWUSR 00200 S_IXUSR 00100 S_IRWXG 00070 , S_IRGRP 00040 S_IWGRP 00020 S_IXGRP 00010 S_IRWXO 00007 , S_IROTH 00004 S_IWOTH 00002 S_IXOTH 00001 POSIX, , mode , . Linux mode : S_ISUID 0004000 set-user-ID S_ISGID 0002000 set-group-ID ( inode(7)). S_ISVTX 0001000 bit ( inode(7)). O_DIRECT ( Linux 2.4.10) - . , , , , . - / . O_DIRECT , , O_SYNC, . - O_DIRECT O_SYNC. . ( ) raw(8). O_DIRECTORY If pathname is not a directory, cause the open to fail. This flag was added in Linux 2.1.126, to avoid denial-of-service problems if opendir(3) is called on a FIFO or tape device. O_DSYNC - data. write(2) ( ) , (. ., write(2) fdatasync(2)). . O_EXCL , : O_CREAT pathname , open() EEXIST(). : pathname , open() , . , O_EXCL , O_CREAT. : Linux 2.6 O_EXCL O_CREAT, pathname . (, ), open() EBUSY. O_EXCL NFS , NFSv3 2.6 . , NFS O_EXCL, , , . , , NFS O_EXCL. (, PID ), link(2). link(2) 0, . stat(2), , . , . O_LARGEFILE (LFS) , off_t ( off64_t). _LARGEFILE64_SOURCE ( - ). _FILE_OFFSET_BITS 64 ( O_LARGEFILE) 32- (. feature_test_macros(7)). O_NOATIME ( Linux 2.6.8) (st_atime ) read(2) . , : o . o CAP_FOWNER UID . ; . . , NFS, . O_NOCTTY If pathname refers to a terminal device--see tty(4)--it will not become the process's controlling terminal even if the process does not have one. O_NOFOLLOW If the trailing component (i.e., basename) of pathname is a symbolic link, then the open fails, with the error ELOOP. Symbolic links in earlier components of the pathname will still be followed. (Note that the ELOOP error that can occur in this case is indistinguishable from the case where an open fails because there are too many symbolic links found while resolving components in the prefix part of the pathname.) This flag is a FreeBSD extension, which was added in Linux 2.1.126, and has subsequently been standardized in POSIX.1-2008. O_PATH. O_NONBLOCK O_NDELAY , . open(), - . , poll(2), select(2), epoll(7) , , <>, -, O_NONBLOCK, . , , - , , O_NONBLOCK. O_NONBLOCK - , . FIFO fifo(7). O_NONBLOCK (lease) fcntl(2). O_PATH ( Linux 2.6.39) , : , . (, read(2), write(2), fchmod(2), fchown(2), fgetxattr(2), ioctl(2), mmap(2)) EBADF. : o close(2). o fchdir(2), ( Linux 3.5). o fstat(2) ( Linux 3.6). o fstatfs(2) ( Linux 3.12). o (dup(2), fcntl(2) F_DUPFD ..). o (fcntl(2) F_GETFD F_SETFD). o fcntl(2) F_GETFL: O_PATH. o dirfd openat() <<*at()>>. linkat(2) AT_EMPTY_PATH ( procfs AT_SYMLINK_FOLLOW) , . o UNIX ( SCM_RIGHTS unix(7)). flags O_PATH, , O_CLOEXEC, O_DIRECTORY O_NOFOLLOW, . O_PATH ( ). (, fchdir(2) , ). , O_RDONLY , (, fchdir(2), fstat(2)) . pathname O_NOFOLLOW, , . dirfd fchownat(2), fstatat(2), linkat(2) readlinkat(2) , . pathname , , , , , . fstatfs(2) , (.f_type == AUTOFS_SUPER_MAGIC). O_PATH -- O_EXEC, POSIX.1. , , , : char buf[PATH_MAX]; fd = open("some_prog", O_PATH); snprintf(buf, PATH_MAX, "/proc/self/fd/%d", fd); execl(buf, "some_prog", (char *) NULL); O_PATH fexecve(3). O_SYNC - file ( - data, O_DSYNC). write(2) ( ) (. ., write(2) fsync(2)). . O_TMPFILE ( Linux 3.11) . pathname ; inode . , . O_TMPFILE O_RDWR O_WRONLY , , O_EXCL. O_EXCL , linkat(2) , : char path[PATH_MAX]; fd = open("/path/to/dir", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); /* File I/O on 'fd'... */ linkat(fd, "", AT_FDCWD, "/path/for/file", AT_EMPTY_PATH); /* If the caller doesn't have the CAP_DAC_READ_SEARCH capability (needed to use AT_EMPTY_PATH with linkat(2)), and there is a proc(5) filesystem mounted, then the linkat(2) call above can be replaced with: snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd); linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file", AT_SYMLINK_FOLLOW); */ mode open() O_CREAT. O_EXCL O_TMPFILE (, O_EXCL O_EXCL). O_TMPFILE: o tmpfile(3): , : ; ; ; . o , , (fchown(2), fchmod(2), fsetxattr(2) . .) ( linkat(2) ). O_TMPFILE ; Linux. ext2, ext3, ext4, UDF, Minix tmpfs. : XFS (Linux 3.15); Btrfs (Linux 3.16); F2FS (Linux 3.16); ubifs (Linux 4.9). O_TRUNC (.. O_RDWR O_WRONLY), . FIFO , . O_TRUNC . creat() creat() open() flags O_CREAT|O_WRONLY|O_TRUNC. openat() openat() open(), , . The dirfd argument is used in conjunction with the pathname argument as follows: o pathname , dirfd . o 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()). o 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() for a relative pathname). In this case, dirfd must be a directory that was opened for reading (O_RDONLY) or using the O_PATH flag. If the pathname given in pathname is relative, and dirfd is not a valid file descriptor, an error (EBADF) results. (Specifying an invalid file descriptor number in dirfd can be used as a means to ensure that pathname is absolute.) openat2(2) The openat2(2) system call is an extension of openat(), and provides a superset of the features of openat(). It is documented separately, in openat2(2). On success, open(), openat(), and creat() return the new file descriptor (a nonnegative integer). On error, -1 is returned and errno is set to indicate the error. open(), openat() creat() : EACCES , pathname , , (. path_resolution(7)). EACCES Where O_CREAT is specified, the protected_fifos or protected_regular sysctl is enabled, the file already exists and is a FIFO or regular file, the owner of the file is neither the current user nor the owner of the containing directory, and the containing directory is both world- or group-writable and sticky. For details, see the descriptions of /proc/sys/fs/protected_fifos and /proc/sys/fs/protected_regular in proc(5). EBADF (openat()) pathname is relative but dirfd is neither AT_FDCWD nor a valid file descriptor. EBUSY O_EXCL was specified in flags and pathname refers to a block device that is in use by the system (e.g., it is mounted). EDQUOT O_CREAT, inode . EEXIST pathname , O_CREAT O_EXCL. EFAULT pathname . EFBIG EOVERFLOW. EINTR (, FIFO; . fifo(7)), ; signal(7). EINVAL O_DIRECT. . EINVAL flags. EINVAL flags O_TMPFILE, O_WRONLY O_RDWR. EINVAL flags O_CREAT (<< >> (basename)) pathname (, ). EINVAL The final component ("basename") of pathname is invalid (e.g., it contains characters not permitted by the underlying filesystem). EISDIR pathname ( O_WRONLY O_RDWR). EISDIR pathname , flags O_TMPFILE O_WRONLY O_RDWR, O_TMPFILE. ELOOP pathname . ELOOP pathname flags O_NOFOLLOW, O_PATH. EMFILE ( RLIMIT_NOFILE getrlimit(2)). ENAMETOOLONG pathname . ENFILE . ENODEV pathname , ( Linux: ENXIO). ENOENT O_CREAT . ENOENT pathname . ENOENT pathname , flags O_TMPFILE O_WRONLY O_RDWR, O_TMPFILE. ENOMEM FIFO, FIFO , ; pipe(7). ENOMEM . ENOSPC pathname , . ENOTDIR , pathname, , O_DIRECTORY, pathname . ENOTDIR (openat()) pathname dirfd , , . ENXIO O_NONBLOCK | O_WRONLY , FIFO FIFO . ENXIO , . ENXIO UNIX. EOPNOTSUPP , pathname, O_TMPFILE. EOVERFLOW pathname refers to a regular file that is too large to be opened. The usual scenario here is that an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 tried to open a file whose size exceeds (1<<31)-1 bytes; see also O_LARGEFILE above. This is the error specified by POSIX.1; before Linux 2.6.24, Linux gave the error EFBIG for this case. EPERM O_NOATIME, ID . EPERM (file seal); fcntl(2). EROFS pathname , , . ETXTBSY pathname , , . ETXTBSY pathname , O_TRUNC. ETXTBSY pathname , (, /) . EWOULDBLOCK O_NONBLOCK, (lease) ( fcntl(2)). O_RDONLY | O_TRUNC ( ). . - POSIX.1-2008 << ->> - open() O_SYNC, O_DSYNC O_RSYNC. , , , , O_SYNC . Linux O_RSYNC O_DSYNC, O_RSYNC. glibc O_RSYNC O_SYNC (O_RSYNC Linux HP PA-RISC, ). O_SYNC - file, . O_DSYNC - data, , , . , , . : (st_mtime) . , , , . , . , O_DSYNC ( O_SYNC ). Linux 2.6.33 Linux O_SYNC open(). , , - data (. ., O_SYNC O_DSYNC). Since Linux 2.6.33, proper O_SYNC support is provided. However, to ensure backward binary compatibility, O_DSYNC was defined with the same value as the historical O_SYNC, and O_SYNC was defined as a new (two-bit) flag value that includes the O_DSYNC flag value. This ensures that applications compiled against new headers get at least O_DSYNC semantics before Linux 2.6.33. C Since glibc 2.26, the glibc wrapper function for open() employs the openat() system call, rather than the kernel's open() system call. For certain architectures, this is also true before glibc 2.26. open() creat() openat() POSIX.1-2008. openat2(2) Linux. O_DIRECT, O_NOATIME, O_PATH O_TMPFILE Linux. _GNU_SOURCE. O_CLOEXEC, O_DIRECTORY O_NOFOLLOW POSIX.1-2001, POSIX.1-2008. glibc 2.12, _POSIX_C_SOURCE 200809L, _XOPEN_SOURCE 700. glibc 2.11 _GNU_SOURCE. open() creat() SVr4, 4.3BSD, POSIX.1-2001. openat() POSIX.1-2008. Linux 2.6.16, glibc 2.4. Linux O_NONBLOCK , , . , , ioctl(2). , open() , creat() ; mknod(2). , st_atime, st_ctime, st_mtime ( , , ; . stat(2)) , st_ctime st_mtime . , - O_TRUNC, st_ctime st_mtime . The files in the /proc/pid/fd directory show the open file descriptors of the process with the PID pid. The files in the /proc/pid/fdinfo directory show even more information about these file descriptors. See proc(5) for further details of both of these directories. Linux O_ASYNC; FASYNC ( BSD). The term open file description is the one used by POSIX to refer to the entries in the system-wide table of open files. In other contexts, this object is variously also called an "open file object", a "file handle", an "open file table entry", or--in kernel-developer parlance--a struct file. ( dup(2) ), , , , . : -, fork(2), . open() ; , , inode . , ( ) , Linux kcmp(2) KCMP_FILE. NFS , NFS, , , O_SYNC O_NDELAY. NFS UID, open() , , , read(2) EACCES. - , open() , UID . FIFO FIFO , ( ). fifo(7). , flags, O_RDONLY, O_WRONLY O_RDWR, . , flags, 0, 1 2, . , O_RDONLY | O_WRONLY O_RDWR. Linux 3 (11 ) flags, : , . Linux , ioctl(2) . openat() openat() and the other system calls and library functions that take a directory file descriptor argument (i.e., execveat(2), faccessat(2), fanotify_mark(2), fchmodat(2), fchownat(2), fspick(2), fstatat(2), futimesat(2), linkat(2), mkdirat(2), mknodat(2), mount_setattr(2), move_mount(2), name_to_handle_at(2), open_tree(2), openat2(2), readlinkat(2), renameat(2), renameat2(2), statx(2), symlinkat(2), unlinkat(2), utimensat(2), mkfifoat(3), and scandirat(3)) address two problems with the older interfaces that preceded them. Here, the explanation is in terms of the openat() call, but the rationale is analogous for the other interfaces. -, openat() , , open() , . - , , open(), open(). , , dir1/dir2/xxx.dep dir1/dir2/xxx. , dir1 dir2 ( ) . , dirfd () fstatat(2) openat(). , dirfd : o -- , ; o , . -, openat() << >> , . /proc/self/fd/dirfd, . The dirfd argument for these APIs can be obtained by using open() or openat() to open a directory (with either the O_RDONLY or the O_PATH flag). Alternatively, such a file descriptor can be obtained by applying dirfd(3) to a directory stream created using opendir(3). When these APIs are given a dirfd argument of AT_FDCWD or the specified pathname is absolute, then they handle their pathname argument in the same way as the corresponding conventional APIs. However, in this case, several of the APIs have a flags argument that provides access to functionality that is not available with the corresponding conventional APIs. O_DIRECT The O_DIRECT flag may impose alignment restrictions on the length and address of user-space buffers and the file offset of I/Os. In Linux alignment restrictions vary by filesystem and kernel version and might be absent entirely. The handling of misaligned O_DIRECT I/Os also varies; they can either fail with EINVAL or fall back to buffered I/O. Since Linux 6.1, O_DIRECT support and alignment restrictions for a file can be queried using statx(2), using the STATX_DIOALIGN flag. Support for STATX_DIOALIGN varies by filesystem; see statx(2). Some filesystems provide their own interfaces for querying O_DIRECT alignment restrictions, for example the XFS_IOC_DIOINFO operation in xfsctl(3). STATX_DIOALIGN should be used instead when it is available. If none of the above is available, then direct I/O support and alignment restrictions can only be assumed from known characteristics of the filesystem, the individual file, the underlying storage device(s), and the kernel version. In Linux 2.4, most filesystems based on block devices require that the file offset and the length and memory address of all I/O segments be multiples of the filesystem block size (typically 4096 bytes). In Linux 2.6.0, this was relaxed to the logical block size of the block device (typically 512 bytes). A block device's logical block size can be determined using the ioctl(2) BLKSSZGET operation or from the shell using the command: blockdev --getss - O_DIRECT fork(2), (. ., , mmap(2) MAP_PRIVATE; , ). -, , fork(2). , . , - O_DIRECT shmat(2) mmap(2) MAP_SHARED. , (advised) MADV_DONTFORK madvise(2), , fork(2). O_DIRECT SGI IRIX, Linux 2.4. IRIX fcntl(2) . FreeBSD 4.x , . O_DIRECT support was added in Linux 2.4.10. Older Linux kernels simply ignore this flag. Some filesystems may not implement the flag, in which case open() fails with the error EINVAL if it is used. O_DIRECT - . , -, , - . mmap(2) - . O_DIRECT NFS . , , . NFS , - O_DIRECT ; -. - O_DIRECT. , - . , - ; , . Linux NFS - O_DIRECT. O_DIRECT , . , O_DIRECT , . -, O_ASYNC open(); fcntl(2). O_TMPFILE -- EISDIR ENOENT. O_CREAT O_DIRECTORY flags, pathname , open() ( O_DIRECTORY ). . chmod(2), chown(2), close(2), dup(2), fcntl(2), link(2), lseek(2), mknod(2), mmap(2), mount(2), open_by_handle_at(2), openat2(2), read(2), socket(2), stat(2), umask(2), unlink(2), write(2), fopen(3), acl(5), fifo(7), inode(7), path_resolution(7), symlink(7) Azamat Hackimov , Konstantin Shvaykovskiy , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 16 2024 . open(2)