2 .\" Copyright 2015 Nexenta Systems, Inc. All rights reserved.
3 .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
4 .\" Copyright 1989 AT&T
5 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
6 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc. All Rights Reserved.
7 .\" 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
8 .\" http://www.opengroup.org/bookstore/.
9 .\" 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 Sun OS 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.
10 .\" This notice shall appear on any product containing this material.
11 .\" 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.
12 .\" 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.
13 .\" 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]
14 .TH OPEN 2 "Feb 14, 2015"
16 open, openat \- open a file
20 #include <sys/types.h>
24 \fBint\fR \fBopen\fR(\fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR, \fB/* mode_t\fR \fImode\fR */);
29 \fBint\fR \fBopenat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR,
30 \fB/* mode_t\fR \fImode\fR */);
35 The \fBopen()\fR function establishes the connection between a file and a file
36 descriptor. It creates an open file description that refers to a file and a
37 file descriptor that refers to that open file description. The file descriptor
38 is used by other I/O functions to refer to that file. The \fIpath\fR argument
39 points to a pathname naming the file.
42 The \fBopenat()\fR function is identical to the \fBopen()\fR function except
43 that the \fIpath\fR argument is interpreted relative to the starting point
44 implied by the \fIfildes\fR argument. If the \fIfildes\fR argument has the
45 special value \fBAT_FDCWD\fR, a relative path argument will be resolved
46 relative to the current working directory. If the \fIpath\fR argument is
47 absolute, the \fIfildes\fR argument is ignored.
50 The \fBopen()\fR function returns a file descriptor for the named file that is
51 the lowest file descriptor not currently open for that process. The open file
52 description is new, and therefore the file descriptor does not share it with
53 any other process in the system. The \fBFD_CLOEXEC\fR file descriptor flag
54 associated with the new file descriptor is cleared.
57 The file offset used to mark the current position within the file is set to the
58 beginning of the file.
61 The file status flags and file access modes of the open file description are
62 set according to the value of \fIoflag\fR. The \fImode\fR argument is used only
63 when \fBO_CREAT\fR is specified (see below.)
66 Values for \fIoflag\fR are constructed by a bitwise-inclusive-OR of flags from
67 the following list, defined in <\fBfcntl.h\fR>. Applications must specify
68 exactly one of the first three values (file access modes) below in the value of
76 Open for reading only.
85 Open for writing only.
94 Open for reading and writing. The result is undefined if this flag is applied
100 Any combination of the following may be used:
108 If set, the file offset is set to the end of the file prior to each write.
118 Create the file if it does not exist. This flag requires that the \fImode\fR
119 argument be specified.
121 If the file exists, this flag has no effect except as noted under \fBO_EXCL\fR
122 below. Otherwise, the file is created with the user \fBID\fR of the file set
123 to the effective user \fBID\fR of the process. The group \fBID\fR of the file
124 is set to the effective group \fBIDs\fR of the process, or if the \fBS_ISGID\fR
125 bit is set in the directory in which the file is being created, the file's
126 group \fBID\fR is set to the group \fBID\fR of its parent directory. If the
127 group \fBID\fR of the new file does not match the effective group \fBID\fR or
128 one of the supplementary groups IDs, the \fBS_ISGID\fR bit is cleared. The
129 access permission bits (see \fB<sys/stat.h>\fR) of the file mode are set to the
130 value of \fImode\fR, modified as follows (see \fBcreat\fR(2)): a bitwise-AND is
131 performed on the file-mode bits and the corresponding bits in the complement of
132 the process's file mode creation mask. Thus, all bits set in the process's file
133 mode creation mask (see \fBumask\fR(2)) are correspondingly cleared in the
134 file's permission mask. The "save text image after execution bit" of the mode
135 is cleared (see \fBchmod\fR(2)). When bits other than the file permission bits
136 are set, the effect is unspecified. The \fImode\fR argument does not affect
137 whether the file is open for reading, writing or for both.
147 Write I/O operations on the file descriptor complete as defined by synchronized
148 I/O data integrity completion.
158 If \fBO_CREAT\fR and \fBO_EXCL\fR are set, \fBopen()\fR fails if the file
159 exists. The check for the existence of the file and the creation of the file if
160 it does not exist is atomic with respect to other threads executing
161 \fBopen()\fR naming the same filename in the same directory with \fBO_EXCL\fR
162 and \fBO_CREAT\fR set. If \fBO_EXCL\fR and \fBO_CREAT\fR are set, and path
163 names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to \fBEEXIST\fR,
164 regardless of the contents of the symbolic link. If \fBO_EXCL\fR is set and
165 \fBO_CREAT\fR is not set, the result is undefined.
176 If set, indicates that the file should be opened for execute permission.
177 This option is only valid for regular files, an error will be returned
184 \fB\fBO_LARGEFILE\fR\fR
188 If set, the offset maximum in the open file description is the largest value
189 that can be represented correctly in an object of type \fBoff64_t\fR.
199 If set and \fIpath\fR identifies a terminal device, \fBopen()\fR does not cause
200 the terminal device to become the controlling terminal for the process.
206 \fB\fBO_NOFOLLOW\fR\fR
210 If the path names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to
217 \fB\fBO_NOLINKS\fR\fR
221 If the link count of the named file is greater than 1, \fBopen()\fR fails and
222 sets \fBerrno\fR to \fBEMLINK\fR.
228 \fB\fBO_CLOEXEC\fR\fR
232 If set, the file descriptor returned will be closed prior to any future
239 \fB\fBO_NONBLOCK\fR or \fBO_NDELAY\fR\fR
243 These flags can affect subsequent reads and writes (see \fBread\fR(2) and
244 \fBwrite\fR(2)). If both \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are set,
245 \fBO_NONBLOCK\fR takes precedence.
247 When opening a \fBFIFO\fR with \fBO_RDONLY\fR or \fBO_WRONLY\fR set:
252 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, an \fBopen()\fR for reading only
253 returns without delay. An \fBopen()\fR for writing only returns an error if no
254 process currently has the file open for reading.
260 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, an \fBopen()\fR for reading
261 only blocks until a thread opens the file for writing. An \fBopen()\fR for
262 writing only blocks the calling thread until a thread opens the file for
265 After both ends of a \fBFIFO\fR have been opened, there is no guarantee that
266 further calls to \fBopen()\fR \fBO_RDONLY\fR (\fBO_WRONLY\fR) will synchronize
267 with later calls to \fBopen()\fR \fBO_WRONLY\fR (\fBO_RDONLY\fR) until both
268 ends of the \fBFIFO\fR have been closed by all readers and writers. Any data
269 written into a \fBFIFO\fR will be lost if both ends of the \fBFIFO\fR are
270 closed before the data is read.
272 When opening a block special or character special file that supports
278 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, the \fBopen()\fR function returns
279 without blocking for the device to be ready or available. Subsequent behavior
280 of the device is device-specific.
286 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, the \fBopen()\fR function
287 blocks the calling thread until the device is ready or available before
290 Otherwise, the behavior of \fBO_NONBLOCK\fR and \fBO_NDELAY\fR is unspecified.
300 Read I/O operations on the file descriptor complete at the same level of
301 integrity as specified by the \fBO_DSYNC\fR and \fBO_SYNC\fR flags. If both
302 \fBO_DSYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all I/O operations on
303 the file descriptor complete as defined by synchronized I/O data integrity
304 completion. If both \fBO_SYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all
305 I/O operations on the file descriptor complete as defined by synchronized I/O
306 file integrity completion.
316 If set, indicates that the directory should be opened for searching.
317 This option is only valid for a directory, an error will be returned if
328 Write I/O operations on the file descriptor complete as defined by synchronized
329 I/O file integrity completion (see \fBfcntl.h\fR(3HEAD) definition of
340 If the file exists and is a regular file, and the file is successfully opened
341 \fBO_RDWR\fR or \fBO_WRONLY\fR, its length is truncated to 0 and the mode and
342 owner are unchanged. It has no effect on \fBFIFO\fR special files or terminal
343 device files. Its effect on other file types is implementation-dependent. The
344 result of using \fBO_TRUNC\fR with \fBO_RDONLY\fR is undefined.
354 If set in \fBopenat()\fR, a relative path argument is interpreted as a
355 reference to an extended attribute of the file associated with the supplied
356 file descriptor. This flag therefore requires the presence of a legal
357 \fIfildes\fR argument. If set in \fBopen()\fR, the implied file descriptor is
358 that for the current working directory. Extended attributes must be referenced
359 with a relative path; providing an absolute path results in a normal file
365 If \fBO_CREAT\fR is set and the file did not previously exist, upon successful
366 completion, \fBopen()\fR marks for update the \fBst_atime\fR, \fBst_ctime\fR,
367 and \fBst_mtime\fR fields of the file and the \fBst_ctime\fR and \fBst_mtime\fR
368 fields of the parent directory.
371 If \fBO_TRUNC\fR is set and the file did previously exist, upon successful
372 completion, \fBopen()\fR marks for update the \fBst_ctime\fR and \fBst_mtime\fR
376 If both the \fBO_SYNC\fR and \fBO_DSYNC\fR flags are set, the effect is as if
377 only the \fBO_SYNC\fR flag was set.
380 If \fIpath\fR refers to a \fBSTREAMS\fR file, \fIoflag\fR may be constructed
381 from \fBO_NONBLOCK\fR or \fBO_NODELAY\fR OR-ed with either \fBO_RDONLY\fR,
382 \fBO_WRONLY\fR, or \fBO_RDWR\fR. Other flag values are not applicable to
383 \fBSTREAMS\fR devices and have no effect on them. The values \fBO_NONBLOCK\fR
384 and \fBO_NODELAY\fR affect the operation of \fBSTREAMS\fR drivers and certain
385 functions (see \fBread\fR(2), \fBgetmsg\fR(2), \fBputmsg\fR(2), and
386 \fBwrite\fR(2)) applied to file descriptors associated with \fBSTREAMS\fR
387 files. For \fBSTREAMS\fR drivers, the implementation of \fBO_NONBLOCK\fR and
388 \fBO_NODELAY\fR is device-specific.
391 When \fBopen()\fR is invoked to open a named stream, and the \fBconnld\fR
392 module (see \fBconnld\fR(7M)) has been pushed on the pipe, \fBopen()\fR blocks
393 until the server process has issued an \fBI_RECVFD\fR \fBioctl()\fR (see
394 \fBstreamio\fR(7I)) to receive the file descriptor.
397 If \fIpath\fR names the master side of a pseudo-terminal device, then it is
398 unspecified whether \fBopen()\fR locks the slave side so that it cannot be
399 opened. Portable applications must call \fBunlockpt\fR(3C) before opening the
403 If the file is a regular file and the local file system is mounted with the
404 \fBnbmand\fR mount option, then a mandatory share reservation is automatically
405 obtained on the file. The share reservation is obtained as if \fBfcntl\fR(2)
406 were called with \fIcmd\fR \fBF_SHARE_NBMAND\fR and the \fBfshare_t\fR values
414 Set to the type of read/write access for which the file is opened.
432 The file descriptor value returned from \fBopen()\fR.
437 If \fIpath\fR is a symbolic link and \fBO_CREAT\fR and \fBO_EXCL\fR are set,
438 the link is not followed.
441 Certain flag values can be set following \fBopen()\fR as described in
445 The largest value that can be represented correctly in an object of type
446 \fBoff_t\fR is established as the offset maximum in the open file description.
449 Upon successful completion, both \fBopen()\fR and \fBopenat()\fR functions open
450 the file and return a non-negative integer representing the lowest numbered
451 unused file descriptor. Otherwise, \fB\(mi1\fR is returned, \fBerrno\fR is set
452 to indicate the error, and no files are created or modified.
455 The \fBopen()\fR and \fBopenat()\fR functions will fail if:
462 Search permission is denied on a component of the path prefix.
464 The file exists and the permissions specified by \fIoflag\fR are denied.
466 The file does not exist and write permission is denied for the parent directory
467 of the file to be created.
469 \fBO_TRUNC\fR is specified and write permission is denied.
471 The {\fBPRIV_FILE_DAC_SEARCH\fR} privilege allows processes to search
472 directories regardless of permission bits. The {\fBPRIV_FILE_DAC_WRITE\fR}
473 privilege allows processes to open files for writing regardless of permission
474 bits. See \fBprivileges\fR(5) for special considerations when opening files
475 owned by UID 0 for writing. The {\fBPRIV_FILE_DAC_READ\fR} privilege allows
476 processes to open files for reading regardless of permission bits.
485 A mandatory share reservation could not be obtained because the desired access
486 conflicts with an existing \fBf_deny\fR share reservation.
495 The file descriptor provided to \fBopenat()\fR is invalid.
504 The file does not exist, \fBO_CREAT\fR is specified, and either the directory
505 where the new file entry is being placed cannot be extended because the user's
506 quota of disk blocks on that file system has been exhausted, or the user's
507 quota of inodes on the file system where the file is being created has been
517 The \fBO_CREAT\fR and \fBO_EXCL\fR flags are set and the named file exists.
526 The \fIpath\fR argument includes non-UTF8 characters and the file system
527 accepts only file names where all characters are part of the UTF-8 character
537 A signal was caught during \fBopen()\fR.
546 The \fIpath\fR argument points to an illegal address.
555 The system does not support synchronized I/O for this file, or the
556 \fBO_XATTR\fR flag was supplied and the underlying file system does not support
557 extended file attributes.
566 The \fIpath\fR argument names a \fBSTREAMS\fR file and a hangup or error
567 occurred during the \fBopen()\fR.
576 The named file is a directory and \fIoflag\fR includes \fBO_WRONLY\fR or
586 Too many symbolic links were encountered in resolving \fIpath\fR.
588 A loop exists in symbolic links encountered during resolution of the \fIpath\fR
591 The \fBO_NOFOLLOW\fR flag is set and the final component of path is a symbolic
601 There are currently {\fBOPEN_MAX\fR} file descriptors open in the calling
611 The \fBO_NOLINKS\fR flag is set and the named file has a link count greater
618 \fB\fBEMULTIHOP\fR\fR
621 Components of \fIpath\fR require hopping to multiple remote machines and the
622 file system does not allow it.
628 \fB\fBENAMETOOLONG\fR\fR
631 The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR} or a pathname
632 component is longer than {\fBNAME_MAX\fR}.
641 The maximum allowable number of files is currently open in the system.
650 The \fBO_CREAT\fR flag is not set and the named file does not exist; or the
651 \fBO_CREAT\fR flag is set and either the path prefix does not exist or the
652 \fIpath\fR argument points to an empty string.
661 The \fBO_EXEC\fR flag is set and \fIpath\fR does not point to a regular
671 The \fIpath\fR argument points to a remote machine, and the link to that
672 machine is no longer active.
681 The \fIpath\fR argument names a STREAMS-based file and the system is unable to
691 The directory or file system that would contain the new file cannot be
692 expanded, the file does not exist, and \fBO_CREAT\fR is specified.
701 The device specified by \fIpath\fR does not support the open operation.
710 A component of the path prefix is not a directory or a relative path was
711 supplied to \fBopenat()\fR, the \fBO_XATTR\fR flag was not supplied, and the
712 file descriptor does not refer to a directory. The \fBO_SEARCH\fR flag
713 was passed and \fIpath\fR does not refer to a directory.
722 The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
723 flag is set, and no process has the file open for reading; or the named file is
724 a character special or block special file and the device associated with this
725 special file does not exist or has been retired by the fault management
732 \fB\fBEOPNOTSUPP\fR\fR
735 An attempt was made to open a path that corresponds to a \fBAF_UNIX\fR socket.
741 \fB\fBEOVERFLOW\fR\fR
744 The named file is a regular file and either \fBO_LARGEFILE\fR is not set and
745 the size of the file cannot be represented correctly in an object of type
746 \fBoff_t\fR or \fBO_LARGEFILE\fR is set and the size of the file cannot be
747 represented correctly in an object of type \fBoff64_t\fR.
756 The named file resides on a read-only file system and either \fBO_WRONLY\fR,
757 \fBO_RDWR\fR, \fBO_CREAT\fR (if file does not exist), or \fBO_TRUNC\fR is set
758 in the \fIoflag\fR argument.
763 The \fBopenat()\fR function will fail if:
770 The \fIfildes\fR argument is not a valid open file descriptor or is not
776 The \fBopen()\fR function may fail if:
783 The \fIpath\fR argument names the slave side of a pseudo-terminal device that
793 The value of the \fIoflag\fR argument is not valid.
799 \fB\fBENAMETOOLONG\fR\fR
802 Pathname resolution of a symbolic link produced an intermediate result whose
803 length exceeds {\fBPATH_MAX\fR}.
812 The \fIpath\fR argument names a \fBSTREAMS\fR file and the system is unable to
822 The file is a pure procedure (shared text) file that is being executed and
823 \fIoflag\fR is \fBO_WRONLY\fR or \fBO_RDWR\fR.
828 \fBExample 1 \fROpen a file for writing by the owner.
831 The following example opens the file \fB/tmp/file\fR, either by creating it if
832 it does not already exist, or by truncating its length to 0 if it does exist.
833 If the call creates a new file, the access permission bits in the file mode of
834 the file are set to permit reading and writing by the owner, and to permit
835 reading only by group members and others.
839 If the call to \fBopen()\fR is successful, the file is opened for writing.
847 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
848 char *filename = "/tmp/file";
850 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
856 \fBExample 2 \fROpen a file using an existence check.
859 The following example uses the \fBopen()\fR function to try to create the
860 \fBLOCKFILE\fR file and open it for writing. Since the \fBopen()\fR function
861 specifies the \fBO_EXCL\fR flag, the call fails if the file already exists. In
862 that case, the application assumes that someone else is updating the password
871 #define LOCKFILE "/etc/ptmp"
873 int pfd; /* Integer for file descriptor returned by open() call. */
875 if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
876 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
878 fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
886 \fBExample 3 \fROpen a file for writing.
889 The following example opens a file for writing, creating the file if it does
890 not already exist. If the file does exist, the system truncates the file to
899 #define LOCKFILE "/etc/ptmp"
902 char filename[PATH_MAX+1];
904 if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
905 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
907 perror("Cannot open output file\en"); exit(1);
915 The \fBopen()\fR function has a transitional interface for 64-bit file offsets.
916 See \fBlf64\fR(5). Note that using \fBopen64()\fR is equivalent to using
917 \fBopen()\fR with \fBO_LARGEFILE\fR set in \fIoflag\fR.
920 See \fBattributes\fR(5) for descriptions of the following attributes:
928 ATTRIBUTE TYPE ATTRIBUTE VALUE
930 Interface Stability Committed
932 MT-Level Async-Signal-Safe
934 Standard For \fBopen()\fR, see \fBstandards\fR(5).
939 \fBIntro\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2), \fBdup\fR(2),
940 \fBexec\fR(2), \fBfcntl\fR(2), \fBgetmsg\fR(2), \fBgetrlimit\fR(2),
941 \fBlseek\fR(2), \fBputmsg\fR(2), \fBread\fR(2), \fBstat\fR(2), \fBumask\fR(2),
942 \fBwrite\fR(2), \fBattropen\fR(3C), \fBfcntl.h\fR(3HEAD), \fBstat.h\fR(3HEAD),
943 \fBunlockpt\fR(3C), \fBattributes\fR(5), \fBlf64\fR(5), \fBprivileges\fR(5),
944 \fBstandards\fR(5), \fBconnld\fR(7M), \fBstreamio\fR(7I)
947 Hierarchical Storage Management (HSM) file systems can sometimes cause long
948 delays when opening a file, since HSM files must be recalled from secondary