1 .\" $NetBSD: open.2,v 1.56 2013/07/29 18:37:16 njoly Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
38 .Nd open or create a file for reading or writing
44 .Fn open "const char *path" "int flags" "..."
46 .Fn openat "int fd" "const char *path" "int flags" "..."
48 The file name specified by
51 for reading and/or writing as specified by the
54 and the file descriptor returned to the calling process.
62 In that case, it is looked up from a directory whose file
63 descriptor was passed as
65 Search permission is required on this directory.
66 .\" (These alternatives await a decision about the semantics of O_SEARCH)
67 .\" Search permission is required on this directory
70 .\" was opened with the
74 .\" This file descriptor must have been opened with the
80 in order to specify the current directory.
86 the values listed below.
87 Applications must specify exactly one of the first three values
88 (file access methods):
89 .Bl -tag -offset indent -width O_DIRECTORY
91 Open for reading only.
93 Open for writing only.
95 Open for reading and writing.
98 Any combination of the following may be used:
99 .Bl -tag -offset indent -width O_DIRECTORY
101 Do not block on open or for data to become available.
103 Append to the file on each write.
105 Create the file if it does not exist.
106 The third argument of type
108 is used to compute the mode bits of the file as described in
110 and modified by the process' umask value (see
117 and the file already exists.
119 Atomically obtain a shared lock.
121 Atomically obtain an exclusive lock.
123 If last path element is a symlink, don't follow it.
124 This option is provided for compatibility with other operating
125 systems, but its security value is questionable.
138 If set, write operations will be performed according to synchronized
139 I/O data integrity completion:
140 each write will wait for the file data to be committed to stable
143 If set, write operations will be performed according to synchronized
144 I/O file integrity completion:
145 each write will wait for both the file data and file status to be
146 committed to stable storage.
148 If set, read operations will complete at the same level of
149 integrity which is in effect for write operations:
150 if specified together with
152 each read will wait for the file status to be committed to stable
159 only, or specifying it without any other synchronized I/O integrity
160 completion flag set, has no further effect.
162 Alternate I/O semantics will be used for read and write operations
163 on the file descriptor.
164 Alternate semantics are defined by the underlying layers and will not
165 have any alternate effect in most cases.
167 If the file is a terminal device, the opened device is not
168 made the controlling terminal for the session.
169 This flag has no effect on
171 since the system defaults to the abovementioned behaviour.
172 The flag is present only for standards conformance.
174 If set on a regular file, data I/O operations will not buffer the data
175 being transferred in the kernel's cache, but rather transfer the data
176 directly between user memory and the underlying device driver if possible.
177 This flag is advisory; the request may be performed in the normal
178 buffered fashion if certain conditions are not met, e.g. if the request
179 is not sufficiently aligned or if the file is mapped.
181 To meet the alignment requirements for direct I/O, the file offset,
182 the length of the I/O and the address of the buffer in memory must all
186 If the I/O request is made
187 using an interface that supports scatter/gather via struct iovec, each
188 element of the request must meet the above alignment constraints.
190 Fail if the file is not a directory.
194 signal to be sent to the process group
195 when I/O is possible, e.g.,
196 upon availability of data to be read.
197 .\" (This block awaits a decision about the semantics of O_SEARCH)
199 .\" If opening a directory, search permission checks will not be performed on
200 .\" subsequent usage of the file descriptor for looking up relative paths by
201 .\" .Xr faccessat 2 ,
210 .\" .Xr readlinkat 2 ,
211 .\" .Xr symlinkat 2 ,
214 .\" .Xr utimensat 2 .
219 set causes each write on the file
220 to be appended to the end.
224 file exists, the file is truncated to zero length.
234 This may be used to implement a simple exclusive access locking mechanism.
237 is set and the last component of the pathname is
240 will fail even if the symbolic
241 link points to a non-existent name.
245 flag is specified, do not wait for the device or file to be ready or
250 in the process being blocked for some reason (e.g., waiting for
251 carrier on a dialup line),
254 This flag also has the effect of making all subsequent I/O on the open file non-blocking.
256 When opening a file, a lock with
258 semantics can be obtained by setting
260 for a shared lock, or
262 for an exclusive lock.
263 If creating a file with
265 the request for the lock will never fail
266 (provided that the underlying filesystem supports locking).
270 is successful, the file pointer used to mark the current position within
271 the file is set to the beginning of the file.
273 When a new file is created it is given the group of the directory
276 The new descriptor is set to remain open across
283 The system imposes a limit on the number of file descriptors
284 open simultaneously by one process.
287 returns the current system limit.
293 returns a non-negative integer, termed a file descriptor.
294 Otherwise, a value of \-1 is returned and
296 is set to indicate the error.
298 The named file is opened unless:
301 Search permission is denied for a component of the path prefix,
302 the required permissions (for reading and/or writing)
303 are denied for the given flags, or
306 the file does not exist,
307 and the directory in which it is to be created
308 does not permit writing.
312 the file does not exist,
313 and the directory in which the entry for the new file
314 is being placed cannot be extended because the
315 user's quota of disk blocks on the file system
316 containing the directory has been exhausted; or
319 the file does not exist,
320 and the user's quota of inodes on the file system on
321 which the file is being created has been exhausted.
326 were specified and the file exists.
329 points outside the process's allocated address space.
332 was specified, but the last path component is a symlink.
341 operation was interrupted by a signal.
343 An I/O error occurred while making the directory entry or
344 allocating the inode for
347 The named file is a directory, and the arguments specify
348 it is to be opened for writing.
350 Too many symbolic links were encountered in translating the pathname.
352 The process has already reached its limit for open file descriptors.
353 .It Bq Er ENAMETOOLONG
354 A component of a pathname exceeded
356 characters, or an entire path name exceeded
360 The system file table is full.
363 is not set and the named file does not exist, or
364 a component of the path name that must exist does not exist.
368 the file does not exist,
369 and the directory in which the entry for the new file is being placed
370 cannot be extended because there is no space left on the file
371 system containing the directory; or
374 the file does not exist,
375 and there are no free inodes on the file system on which the
376 file is being created.
378 A component of the path prefix is not a directory; or
380 is specified and the last path component is not a directory.
382 The named file is a character special or block
383 special file, and the device associated with this special file
390 is set and no process has the file open for reading.
395 is specified but the underlying filesystem does not support locking; or
396 an attempt was made to open a socket (not currently implemented).
398 The file's flags (see
400 don't allow the file to be opened.
402 The named file resides on a read-only file system,
403 and the file is to be modified.
405 The file is a pure procedure (shared text) file that is being
408 call requests write access.
417 does not specify an absolute path and
421 nor a valid file descriptor open for reading or searching.
424 is not an absolute path and
426 is a file descriptor associated with a non-directory file.
465 are extensions defined in
467 .\" (This block awaits a decision about the semantics of O_SEARCH)
470 .\" .St -p1003.1-2008 .
476 flags are non-standard extensions and should not be used if portability
481 function call appeared in