.\" .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for .\" permission to reproduce portions of its copyrighted documentation. .\" Original documentation from The Open Group can be obtained online at .\" http://www.opengroup.org/bookstore/. .\" .\" The Institute of Electrical and Electronics Engineers and The Open .\" Group, have given us permission to reprint portions of their .\" documentation. .\" .\" In the following statement, the phrase ``this text'' refers to portions .\" of the system documentation. .\" .\" Portions of this text are reprinted and reproduced in electronic form .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, .\" Standard for Information Technology -- Portable Operating System .\" Interface (POSIX), The Open Group Base Specifications Issue 6, .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics .\" Engineers, Inc and The Open Group. In the event of any discrepancy .\" between these versions and the original IEEE and The Open Group .\" Standard, the original IEEE and The Open Group Standard is the referee .\" document. The original Standard can be obtained online at .\" http://www.opengroup.org/unix/online.html. .\" .\" This notice shall appear on any product containing this material. .\" .\" The contents of this file are subject to the terms of the .\" Common Development and Distribution License (the "License"). .\" You may not use this file except in compliance with the License. .\" .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE .\" or http://www.opensolaris.org/os/licensing. .\" See the License for the specific language governing permissions .\" and limitations under the License. .\" .\" When distributing Covered Code, include this CDDL HEADER in each .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. .\" If applicable, add the following below this CDDL HEADER, with the .\" fields enclosed by brackets "[]" replaced with your own identifying .\" information: Portions Copyright [yyyy] [name of copyright owner] .\" .\" .\" Copyright 1989 AT&T .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved. .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc. .\" All Rights Reserved. .\" Copyright 2015 Nexenta Systems, Inc. All rights reserved. .\" Copyright 2020 Joyent, Inc. .\" Copyright 2022 Oxide Computer Company .\" .Dd February 5, 2022 .Dt OPEN 2 .Os .Sh NAME .Nm open , .Nm openat .Nd open a file .Sh SYNOPSIS .In sys/types.h .In sys/stat.h .In fcntl.h .Ft int .Fo open .Fa "const char *path" .Fa "int oflag" .Op , Fa "mode_t mode" .Fc .Ft int .Fo openat .Fa "int fildes" .Fa "const char *path" .Fa "int oflag" .Op , Fa "mode_t mode" .Fc .Sh DESCRIPTION The .Fn open function establishes the connection between a file and a file descriptor. It creates an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The .Fa path argument points to a pathname naming the file. .Pp The .Fn openat function is identical to the .Fn open function except that the .Fa path argument is interpreted relative to the starting point implied by the .Fa fildes argument. If the .Fa fildes argument has the special value .Dv AT_FDCWD , a relative path argument will be resolved relative to the current working directory. If the .Fa path argument is absolute, the .Fa fildes argument is ignored. .Pp The .Fn open function returns a file descriptor for the named file that is the lowest file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not share it with any other process in the system. The .Dv FD_CLOEXEC file descriptor flag associated with the new file descriptor is cleared. .Pp The file offset used to mark the current position within the file is set to the beginning of the file. .Pp The file status flags and file access modes of the open file description are set according to the value of .Fa oflag . The .Fa mode argument is used only when .Dv O_CREAT is specified .Pq "see below" . .Pp Values for .Fa oflag are constructed by a bitwise-inclusive-OR of flags from the following list, defined in .Xr fcntl.h 3HEAD . Applications must specify exactly one of the first three values (file access modes) below in the value of .Fa oflag : .Bl -tag -width Ds .It Dv O_RDONLY Open for reading only. .It Dv O_WRONLY Open for writing only. .It Dv O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO. .El .Pp Any combination of the following may be used: .Bl -tag -width Ds .It Dv O_APPEND If set, the file offset is set to the end of the file prior to each write. .It Dv O_CREAT Create the file if it does not exist. This flag requires that the .Fa mode argument be specified. .Pp If the file exists, this flag has no effect except as noted under .Dv O_EXCL below. Otherwise, the file is created with the user ID of the file set to the effective user ID of the process. The group ID of the file is set to the effective group IDs of the process, or if the .Dv S_ISGID bit is set in the directory in which the file is being created, the file's group ID is set to the group ID of its parent directory. If the group ID of the new file does not match the effective group ID or one of the supplementary groups IDs, the .Dv S_ISGID bit is cleared. .Pp The access permission bits .Po see .Xr stat.h 3HEAD .Pc of the file mode are set to the value of .Fa mode , modified as follows .Po see .Xr creat 2 .Pc : a bitwise-AND is performed on the file-mode bits and the corresponding bits in the complement of the process's file mode creation mask. Thus, all bits set in the process's file mode creation mask .Po see .Xr umask 2 .Pc are correspondingly cleared in the file's permission mask. The .Dq save text image after execution bit of the mode is cleared .Po see .Xr chmod 2 .Pc . When bits other than the file permission bits are set, the effect is unspecified. The .Fa mode argument does not affect whether the file is open for reading, writing or for both. .It Dv O_DIRECT Indicates that the file data is not going to be reused in the near future. When possible, data is read or written directly between the application's memory and the device when the data is accessed with .Xr read 2 and .Xr write 2 operations. See .Xr directio 3C for more details. .It Dv O_DIRECTORY Indicates that attempts to open .Fa path should fail unless .Fa path is a directory. If both .Dv O_CREAT and .Dv O_DIRECTORY are specified then the call will fail if it would result in a file being created. If a directory already exists at .Fa path then it will behave as if the .Dv O_DIRECTORY flag had not been present. If the .Dv O_EXCL and .Dv O_CREAT flags are specified, then the call will always fail as they imply a file should always be created. .It Dv O_DSYNC Write I/O operations on the file descriptor complete as defined by synchronized I/O data integrity completion. .It Dv O_EXCL If .Dv O_CREAT and .Dv O_EXCL are set, .Fn open fails if the file exists. The check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other threads executing .Fn open naming the same filename in the same directory with .Dv O_EXCL and .Dv O_CREAT set. If .Dv O_EXCL and .Dv O_CREAT are set, and .Fa path names a symbolic link, .Fn open fails and sets .Va errno to .Er EEXIST , regardless of the contents of the symbolic link. If .Dv O_EXCL is set and .Dv O_CREAT is not set, the result is undefined. .It Dv O_EXEC If set, indicates that the file should be opened for execute permission. This option is only valid for regular files; an error will be returned if the target is not a regular file. .It Dv O_LARGEFILE If set, the offset maximum in the open file description is the largest value that can be represented correctly in an object of type .Vt off64_t . .It Dv O_NOCTTY If set and .Fa path identifies a terminal device, .Fn open does not cause the terminal device to become the controlling terminal for the process. .It Dv O_NOFOLLOW If the path names a symbolic link, .Fn open fails and sets .Va errno to .Er ELOOP . .It Dv O_NOLINKS If the link count of the named file is greater than .Sy 1 , .Fn open fails and sets .Va errno to .Er EMLINK . .It Dv O_CLOEXEC If set, the file descriptor returned will be closed prior to any future .Xr exec 2 calls. .It Dv O_NONBLOCK O_NDELAY These flags can affect subsequent reads and writes .Po see .Xr read 2 and .Xr write 2 .Pc . If both .Dv O_NDELAY and .Dv O_NONBLOCK are set, .Dv O_NONBLOCK takes precedence. .Pp When opening a FIFO with .Dv O_RDONLY or .Dv O_WRONLY set: .Bl -bullet .It If .Dv O_NONBLOCK or .Dv O_NDELAY is set, an .Fn open for reading only returns without delay. An .Fn open for writing only returns an error if no process currently has the file open for reading. .It If .Dv O_NONBLOCK and .Dv O_NDELAY are clear, an .Fn open for reading only blocks until a thread opens the file for writing. An .Fn open for writing only blocks the calling thread until a thread opens the file for reading. .El .Pp After both ends of a FIFO have been opened once, there is no guarantee that further calls to .Fn open .Dv O_RDONLY .Pq Dv O_WRONLY will synchronize with later calls to .Fn open .Dv O_WRONLY .Pq Dv O_RDONLY until both ends of the FIFO have been closed by all readers and writers. Any data written into a FIFO will be lost if both ends of the FIFO are closed before the data is read. .Pp When opening a block special or character special file that supports non-blocking opens: .Bl -bullet .It If .Dv O_NONBLOCK or .Dv O_NDELAY is set, the .Fn open function returns without blocking for the device to be ready or available. Subsequent behavior of the device is device-specific. .It If .Dv O_NONBLOCK and .Dv O_NDELAY are clear, the .Fn open function blocks the calling thread until the device is ready or available before returning. .El .Pp Otherwise, the behavior of .Dv O_NONBLOCK and .Dv O_NDELAY is unspecified. .It Dv O_RSYNC Read I/O operations on the file descriptor complete at the same level of integrity as specified by the .Dv O_DSYNC and .Dv O_SYNC flags. If both .Dv O_DSYNC and .Dv O_RSYNC are set in .Fa oflag , all I/O operations on the file descriptor complete as defined by synchronized I/O data integrity completion. If both .Dv O_SYNC and .Dv O_RSYNC are set in .Fa oflag , all I/O operations on the file descriptor complete as defined by synchronized I/O file integrity completion. .It Dv O_SEARCH If set, indicates that the directory should be opened for searching. This option is only valid for a directory; an error will be returned if the target is not a directory. .It Dv O_SYNC Write I/O operations on the file descriptor complete as defined by synchronized I/O file integrity completion .Po see .Xr fcntl.h 3HEAD .Pc definition of .Dv O_SYNC . .It Dv O_TRUNC If the file exists and is a regular file, and the file is successfully opened .Dv O_RDWR or .Dv O_WRONLY , its length is truncated to .Sy 0 and the mode and owner are unchanged. It has no effect on FIFO special files or terminal device files. Its effect on other file types is implementation-dependent. The result of using .Dv O_TRUNC with .Dv O_RDONLY is undefined. .It Dv O_XATTR If set in .Fn openat , a relative path argument is interpreted as a reference to an extended attribute of the file associated with the supplied file descriptor. This flag therefore requires the presence of a legal .Fa fildes argument. If set in .Fn open , the implied file descriptor is that for the current working directory. Extended attributes must be referenced with a relative path; providing an absolute path results in a normal file reference. .El .Pp If .Dv O_CREAT is set and the file did not previously exist, upon successful completion, .Fn open marks for update the .Fa st_atime , .Fa st_ctime , and .Fa st_mtime fields of the file and the .Fa st_ctime and .Fa st_mtime fields of the parent directory. .Pp If .Dv O_TRUNC is set and the file did previously exist, upon successful completion, .Fn open marks for update the .Fa st_ctime and .Fa st_mtime fields of the file. .Pp If both the .Dv O_SYNC and .Dv O_DSYNC flags are set, the effect is as if only the .Dv O_SYNC flag was set. .Pp If .Fa path refers to a STREAMS file, .Fa oflag may be constructed from .Dv O_NONBLOCK or .Dv O_NODELAY OR-ed with either .Dv O_RDONLY , .Dv O_WRONLY , or .Dv O_RDWR . Other flag values are not applicable to STREAMS devices and have no effect on them. The values .Dv O_NONBLOCK and .Dv O_NODELAY affect the operation of STREAMS drivers and certain functions .Po see .Xr read 2 , .Xr getmsg 2 , .Xr putmsg 2 , and .Xr write 2 .Pc applied to file descriptors associated with STREAMS files. For STREAMS drivers, the implementation of .Dv O_NONBLOCK and .Dv O_NODELAY is device-specific. .Pp When .Fn open is invoked to open a named stream, and the .Xr connld 4M module has been pushed on the pipe, .Fn open blocks until the server process has issued an .Dv I_RECVFD .Xr ioctl 2 .Po see .Xr streamio 4I .Pc to receive the file descriptor. .Pp If .Fa path names the manager side of a pseudo-terminal device, then it is unspecified whether .Fn open locks the subsidiary side so that it cannot be opened. Portable applications must call .Xr unlockpt 3C before opening the subsidiary side. .Pp If the file is a regular file and the local file system is mounted with the .Cm nbmand mount option, then a mandatory share reservation is automatically obtained on the file. The share reservation is obtained as if .Xr fcntl 2 were called with .Fa cmd .Dv F_SHARE_NBMAND and the .Vt fshare_t values set as follows: .Bl -tag -width Ds -offset Ds .It Fa f_access Set to the type of read/write access for which the file is opened. .It Fa f_deny .Dv F_NODNY .It Fa f_id The file descriptor value returned from .Fn open . .El .Pp If .Fa path is a symbolic link and .Dv O_CREAT and .Dv O_EXCL are set, the link is not followed. .Pp Certain flag values can be set following .Fn open as described in .Xr fcntl 2 . .Pp The largest value that can be represented correctly in an object of type .Vt off_t is established as the offset maximum in the open file description. .Sh RETURN VALUES The .Fn open and .Fn openat functions open the file and, if successful, return a non-negative integer representing the lowest numbered unused file descriptor; otherwise the value .Sy -1 is returned and the global variable .Va errno is set to indicate the error and no files are created or modified. .Sh EXAMPLES .Sy Example 1 Open a file for writing by the owner. .Pp The following example opens the file .Pa /tmp/file , either by creating it if it does not already exist, or by truncating its length to .Sy 0 if it does exist. If the call creates a new file, the access permission bits in the file mode of the file are set to permit reading and writing by the owner, and to permit reading only by group members and others. .Pp If the call to .Fn open is successful, the file is opened for writing. .Bd -literal -offset Ds #include \&... int fd; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char *filename = "/tmp/file"; \&... fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); \&... .Ed .Pp .Sy Example 2 Open a file using an existence check. .Pp The following example uses the .Fn open function to try to create the .Dv LOCKFILE file and open it for writing. Since the .Fn open function specifies the .Dv O_EXCL flag, the call fails if the file already exists. In that case, the application assumes that someone else is updating the password file and exits. .Bd -literal -offset Ds #include #include #include #include \&... #define LOCKFILE "/etc/ptmp" \&... int pfd; /* Integer for file descriptor returned by open() call. */ \&... if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { err(1, "Cannot open %s. Try again later.", LOCKFILE); } \&... .Ed .Pp .Sy Example 3 Open a file for writing. .Pp The following example opens a file for writing, creating the file if it does not already exist. If the file does exist, the system truncates the file to zero bytes. .Bd -literal -offset Ds #include #include #include #include \&... int pfd; char filename[PATH_MAX+1]; \&... if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { err(1, "Cannot open output file"); } \&... .Ed .Sh ERRORS The .Fn open and .Fn openat functions will fail if: .Bl -tag -width Er .It Er EACCES Search permission is denied on a component of the path prefix. .Pp The file exists and the permissions specified by .Fa oflag are denied. .Pp The file does not exist and write permission is denied for the parent directory of the file to be created. .Pp .Dv O_TRUNC is specified and write permission is denied. .Pp The .Brq Dv PRIV_FILE_DAC_SEARCH privilege allows processes to search directories regardless of permission bits. The .Brq Dv PRIV_FILE_DAC_WRITE privilege allows processes to open files for writing regardless of permission bits. See .Xr privileges 7 for special considerations when opening files owned by user ID .Sy 0 for writing. The .Brq Dv PRIV_FILE_DAC_READ privilege allows processes to open files for reading regardless of permission bits. .It Er EAGAIN A mandatory share reservation could not be obtained because the desired access conflicts with an existing .Fa f_deny share reservation .Po see .Xr fcntl 2 .Pc . .It Er EDQUOT The file does not exist, .Dv O_CREAT is specified, and either the directory where the new file entry is being placed cannot be extended because the user's quota of disk blocks on that file system has been exhausted, or the user's quota of inodes on the file system where the file is being created has been exhausted. .It Er EEXIST The .Dv O_CREAT and .Dv O_EXCL flags are set and the named file already exists. .It Er EILSEQ The .Fa path argument includes bytes that are not valid UTF-8 characters, and the file system accepts only file names where all characters are part of the UTF-8 character codeset. .It Er EINTR A signal was caught during .Fn open . .It Er EFAULT The .Fa path argument points to an illegal address. .It Er EINVAL Either the system does not support synchronized or direct I/O for this file, or the .Dv O_XATTR flag was supplied and the underlying file system does not support extended file attributes. .It Er EIO The .Fa path argument names a STREAMS file and a hangup or error occurred during the .Fn open . .It Er EISDIR The named file is a directory and .Fa oflag includes .Dv O_WRONLY or .Dv O_RDWR . .It Er ELOOP Too many symbolic links were encountered in resolving .Fa path . .Pp A loop exists in symbolic links encountered during resolution of the .Fa path argument. .Pp The .Dv O_NOFOLLOW flag is set and the final component of path is a symbolic link. .It Er EMFILE There are currently .Brq Dv OPEN_MAX file descriptors open in the calling process. .It Er EMLINK The .Dv O_NOLINKS flag is set and the named file has a link count greater than .Sy 1 . .It Er EMULTIHOP Components of .Fa path require hopping to multiple remote machines and the file system does not allow it. .It Er ENAMETOOLONG The length of the .Fa path argument exceeds .Brq Dv PATH_MAX or a pathname component is longer than .Brq Dv NAME_MAX . .It Er ENFILE The maximum allowable number of files is currently open in the system. .It Er ENOENT The .Dv O_CREAT flag is not set and the named file does not exist; or the .Dv O_CREAT flag is set and either the path prefix does not exist or the .Fa path argument points to an empty string. .Pp The .Dv O_CREAT and .Dv O_DIRECTORY flags were both set and .Fa path did not point to a file. .It Er ENOEXEC The .Dv O_EXEC flag is set and .Fa path does not point to a regular file. .It Er ENOLINK The .Fa path argument points to a remote machine, and the link to that machine is no longer active. .It Er ENOSR Th .Fa path argument names a STREAMS-based file and the system is unable to allocate a STREAM. .It Er ENOSPC The directory or file system that would contain the new file cannot be expanded, the file does not exist, and .Dv O_CREAT is specified. .It Er ENOSYS The device specified by .Fa path does not support the open operation. .It Er ENOTDIR A component of the path prefix is not a directory or a relative path was supplied to .Fn openat , the .Dv O_XATTR flag was not supplied, and the file descriptor does not refer to a directory. The .Dv O_SEARCH flag was passed and .Fa path does not refer to a directory. .Pp The .Dv O_DIRECTORY flag was set and the file was not a directory. .It Er ENXIO The .Dv O_NONBLOCK flag is set, the named file is a FIFO, the .Dv O_WRONLY flag is set, and no process has the file open for reading; or the named file is a character special or block special file and the device associated with this special file does not exist or has been retired by the fault management framework. .It Er EOPNOTSUPP An attempt was made to open a path that corresponds to an .Dv AF_UNIX socket. .It Er EOVERFLOW The named file is a regular file and either .Dv O_LARGEFILE is not set and the size of the file cannot be represented correctly in an object of type .Vt off_t or .Dv O_LARGEFILE is set and the size of the file cannot be represented correctly in an object of type .Vt off64_t . .It Er EROFS The named file resides on a read-only file system and either .Dv O_WRONLY , .Dv O_RDWR , .Dv O_CREAT (if file does not exist), or .Dv O_TRUNC is set in the .Fa oflag argument. .El .Pp The .Fn openat function will fail if: .Bl -tag -width Er .It Er EBADF The .Fa fildes argument is not a valid open file descriptor or is not .Dv AT_FTCWD . .El .Pp The .Fn open function may fail if: .Bl -tag -width Er .It Er EAGAIN The .Fa path argument names the subsidiary side of a pseudo-terminal device that is locked. .It Er EINVAL The value of the .Fa oflag argument is not valid. .It Er ENAMETOOLONG Pathname resolution of a symbolic link produced an intermediate result whose length exceeds .Brq Dv PATH_MAX . .It Er ENOMEM The .Fa path argument names a STREAMS file and the system is unable to allocate resources. .It Er ETXTBSY The file is a pure procedure (shared text) file that is being executed and .Fa oflag is .Dv O_WRONLY or .Dv O_RDWR . .El .Sh USAGE The .Fn open function has a transitional interface for 64-bit file offsets. See .Xr lf64 7 . Note that using .Fn open64 is equivalent to using .Fn open with .Dv O_LARGEFILE set in .Fa oflag . .Sh INTERFACE STABILITY .Sy Committed .Sh MT LEVEL .Sy Async-Signal-Safe .Sh SEE ALSO .Xr chmod 2 , .Xr close 2 , .Xr creat 2 , .Xr dup 2 , .Xr exec 2 , .Xr fcntl 2 , .Xr getmsg 2 , .Xr getrlimit 2 , .Xr Intro 2 , .Xr lseek 2 , .Xr putmsg 2 , .Xr read 2 , .Xr stat 2 , .Xr umask 2 , .Xr write 2 , .Xr attropen 3C , .Xr directio 3C , .Xr unlockpt 3C , .Xr fcntl.h 3HEAD , .Xr stat.h 3HEAD , .Xr streamio 4I , .Xr connld 4M , .Xr attributes 7 , .Xr lf64 7 , .Xr privileges 7 , .Xr standards 7 .Sh NOTES Hierarchical Storage Management .Pq HSM file systems can sometimes cause long delays when opening a file, since HSM files must be recalled from secondary storage.