xref: /netbsd-src/lib/libc/sys/open.2 (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1.\"	$NetBSD: open.2,v 1.65 2021/03/17 08:13:29 wiz Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
17.\"
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
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
31.\"
32.Dd March 17, 2021
33.Dt OPEN 2
34.Os
35.Sh NAME
36.Nm open ,
37.Nm openat
38.Nd open or create a file for reading, writing or executing
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In fcntl.h
43.Ft int
44.Fn open "const char *path" "int flags" "..."
45.Ft int
46.Fn openat "int fd" "const char *path" "int flags" "..."
47.Sh DESCRIPTION
48The file name specified by
49.Fa path
50is opened
51for either execution or reading and/or writing as specified by the
52argument
53.Fa flags
54and the file descriptor returned to the calling process.
55The
56.Fa flags
57argument may indicate the file is to be
58created if it does not exist (by specifying the
59.Dv O_CREAT
60flag).
61In this case
62.Fn open
63and
64.Fn openat
65require an additional argument
66.Fa "mode_t mode" ,
67and the file is created with mode
68.Fa mode
69as described in
70.Xr chmod 2
71and modified by the process' umask value (see
72.Xr umask 2 ) .
73.Pp
74The
75.Fn openat
76function is equivalent to the
77.Fn open
78function except in the case where the
79.Fa path
80is relative.
81In that case, it is looked up from a directory whose file
82descriptor was passed as
83.Fa fd .
84Search permission is required on this directory.
85.\"    (These alternatives await a decision about the semantics of O_SEARCH)
86.\" Search permission is required on this directory
87.\" except if
88.\" .Fa fd
89.\" was opened with the
90.\" .Dv O_SEARCH
91.\" flag.
92.\"    - or -
93.\" This file descriptor must have been opened with the
94.\" .Dv O_SEARCH
95.\" flag.
96.Fa fd
97can be set to
98.Dv AT_FDCWD
99in order to specify the current directory.
100.Pp
101The
102.Fa flags
103are specified by
104.Em or Ns 'ing
105the values listed below.
106Applications must specify exactly one of these four values
107(file access methods):
108.Bl -tag -offset indent -width O_DIRECTORY
109.It Dv O_RDONLY
110Open for reading only.
111.It Dv O_WRONLY
112Open for writing only.
113.It Dv O_EXEC
114Open for execute only.
115.It Dv O_RDWR
116Open for reading and writing.
117.El
118.Pp
119Any combination of the following may be used as well:
120.Bl -tag -offset indent -width O_DIRECTORY
121.It Dv O_NONBLOCK
122Do not block on open or for data to become available.
123.It Dv O_APPEND
124Append to the file on each write.
125.It Dv O_CREAT
126Create the file if it does not exist.
127The third argument of type
128.Ft mode_t
129is used to compute the mode bits of the file as described in
130.Xr chmod 2
131and modified by the process' umask value (see
132.Xr umask 2 ) .
133.It Dv O_TRUNC
134Truncate size to 0.
135.It Dv O_EXCL
136Error if
137.Dv O_CREAT
138and the file already exists.
139.It Dv O_SHLOCK
140Atomically obtain a shared lock.
141.It Dv O_EXLOCK
142Atomically obtain an exclusive lock.
143.It Dv O_NOFOLLOW
144If last path element is a symlink, don't follow it.
145This option is provided for compatibility with other operating
146systems, but its security value is questionable.
147.It Dv O_CLOEXEC
148Set the
149.Xr close 2
150on
151.Xr exec 3
152flag.
153.It Dv O_NOSIGPIPE
154Return
155.Er EPIPE
156instead of raising
157.Dv SIGPIPE .
158.It Dv O_DSYNC
159If set, write operations will be performed according to synchronized
160I/O data integrity completion:
161each write will wait for the file data to be committed to stable
162storage.
163.It Dv O_SYNC
164If set, write operations will be performed according to synchronized
165I/O file integrity completion:
166each write will wait for both the file data and file status to be
167committed to stable storage.
168.It Dv O_RSYNC
169If set, read operations will complete at the same level of
170integrity which is in effect for write operations:
171if specified together with
172.Dv O_SYNC ,
173each read will wait for the file status to be committed to stable
174storage.
175.Pp
176Combining
177.Dv O_RSYNC
178with
179.Dv O_DSYNC
180only, or specifying it without any other synchronized I/O integrity
181completion flag set, has no further effect.
182.It Dv O_ALT_IO
183Alternate I/O semantics will be used for read and write operations
184on the file descriptor.
185Alternate semantics are defined by the underlying layers and will not
186have any alternate effect in most cases.
187.It Dv O_NOCTTY
188If the file is a terminal device, the opened device is not
189made the controlling terminal for the session.
190This flag has no effect on
191.Nx ,
192since the system defaults to the abovementioned behaviour.
193The flag is present only for standards conformance.
194.It Dv O_DIRECT
195If set on a regular file, data I/O operations will not buffer the data
196being transferred in the kernel's cache, but rather transfer the data
197directly between user memory and the underlying device driver if possible.
198This flag is advisory; the request may be performed in the normal
199buffered fashion if certain conditions are not met, e.g. if the request
200is not sufficiently aligned or if the file is mapped.
201.Pp
202To meet the alignment requirements for direct I/O, the file offset,
203the length of the I/O and the address of the buffer in memory must all
204be multiples of
205.Dv DEV_BSIZE
206(512 bytes).
207If the I/O request is made
208using an interface that supports scatter/gather via struct iovec, each
209element of the request must meet the above alignment constraints.
210.It Dv O_DIRECTORY
211Fail if the file is not a directory.
212.It Dv O_REGULAR
213Fail if the path does not refer to a regular file.
214.It Dv O_ASYNC
215Enable the
216.Dv SIGIO
217signal to be sent to the process group
218when I/O is possible, e.g.,
219upon availability of data to be read.
220.Pp
221Note: This is broken in
222.Fn open ;
223it must be set explicitly with the
224.Dv F_SETFL
225command to
226.Xr fcntl 2 .
227.\"    (This block awaits a decision about the semantics of O_SEARCH)
228.\" .It Dv O_SEARCH
229.\" If opening a directory, search permission checks will not be performed on
230.\" subsequent usage of the file descriptor for looking up relative paths by
231.\" .Xr faccessat 2 ,
232.\" .Xr fchmodat 2 ,
233.\" .Xr fchownat 2 ,
234.\" .Xr fstatat 2 ,
235.\" .Xr linkat 2 ,
236.\" .Xr mkdirat 2 ,
237.\" .Xr mkfifoat 2 ,
238.\" .Xr mknodat 2 ,
239.\" .Xr openat 2 ,
240.\" .Xr readlinkat 2 ,
241.\" .Xr symlinkat 2 ,
242.\" .Xr unlinkat 2 ,
243.\" and
244.\" .Xr utimensat 2 .
245.El
246.Pp
247Opening a file with
248.Dv O_APPEND
249set causes each write on the file
250to be appended to the end.
251If
252.Dv O_TRUNC
253is specified and the
254file exists, the file is truncated to zero length.
255.Pp
256If
257.Dv O_EXCL
258is set with
259.Dv O_CREAT
260and the file already
261exists,
262.Fn open
263returns an error.
264This may be used to implement a simple exclusive access locking mechanism.
265If
266.Dv O_EXCL
267is set and the last component of the pathname is
268a symbolic link,
269.Fn open
270will fail even if the symbolic
271link points to a non-existent name.
272.Pp
273If the
274.Dv O_NONBLOCK
275flag is specified, do not wait for the device or file to be ready or
276available.
277If the
278.Fn open
279call would result
280in the process being blocked for some reason (e.g., waiting for
281carrier on a dialup line),
282.Fn open
283returns immediately.
284This flag also has the effect of making all subsequent I/O on the open file non-blocking.
285.Pp
286When opening a file, a lock with
287.Xr flock 2
288semantics can be obtained by setting
289.Dv O_SHLOCK
290for a shared lock, or
291.Dv O_EXLOCK
292for an exclusive lock.
293If creating a file with
294.Dv O_CREAT ,
295the request for the lock will never fail
296(provided that the underlying file system supports locking).
297.Pp
298If
299.Fn open
300is successful, the file pointer used to mark the current position within
301the file is set to the beginning of the file.
302.Pp
303When a new file is created it is given the group of the directory
304which contains it.
305.Pp
306The new descriptor is set to remain open across
307.Xr execve 2
308system calls; see
309.Xr close 2
310and
311.Xr fcntl 2 .
312.Pp
313The system imposes a limit on the number of file descriptors
314open simultaneously by one process.
315Calling
316.Xr getdtablesize 3
317returns the current system limit.
318.Sh RETURN VALUES
319If successful,
320.Fn open
321and
322.Fn openat
323returns a non-negative integer, termed a file descriptor.
324Otherwise, a value of \-1 is returned and
325.Va errno
326is set to indicate the error.
327.Sh ERRORS
328The named file is opened unless:
329.Bl -tag -width Er
330.It Bq Er EACCES
331Search permission is denied for a component of the path prefix,
332the required permissions (for reading and/or writing)
333are denied for the given flags, or
334.Dv O_CREAT
335is specified,
336the file does not exist,
337and the directory in which it is to be created
338does not permit writing.
339.It Bq Er EDQUOT
340.Dv O_CREAT
341is specified,
342the file does not exist,
343and the directory in which the entry for the new file
344is being placed cannot be extended because the
345user's quota of disk blocks on the file system
346containing the directory has been exhausted; or
347.Dv O_CREAT
348is specified,
349the file does not exist,
350and the user's quota of inodes on the file system on
351which the file is being created has been exhausted.
352.It Bq Er EEXIST
353.Dv O_CREAT
354and
355.Dv O_EXCL
356were specified and the file exists.
357.It Bq Er EFAULT
358.Fa path
359points outside the process's allocated address space.
360.It Bq Er EFTYPE
361.Dv O_NOFOLLOW
362was specified, but the last path component is a symlink.
363.Em Note :
364.St -p1003.1-2008
365specifies returning
366.Bq Er ELOOP
367for this case.
368.It Bq Er EINTR
369The
370.Fn open
371operation was interrupted by a signal.
372.It Bq Er EIO
373An I/O error occurred while making the directory entry or
374allocating the inode for
375.Dv O_CREAT .
376.It Bq Er EISDIR
377The named file is a directory, and the arguments specify
378it is to be opened for writing.
379.It Bq Er ELOOP
380Too many symbolic links were encountered in translating the pathname.
381.It Bq Er EMFILE
382The process has already reached its limit for open file descriptors.
383.It Bq Er ENAMETOOLONG
384A component of a pathname exceeded
385.Brq Dv NAME_MAX
386characters, or an entire path name exceeded
387.Brq Dv PATH_MAX
388characters.
389.It Bq Er ENFILE
390The system file table is full.
391.It Bq Er ENOENT
392.Dv O_CREAT
393is not set and the named file does not exist, or
394a component of the path name that must exist does not exist.
395.It Bq Er ENOSPC
396.Dv O_CREAT
397is specified,
398the file does not exist,
399and the directory in which the entry for the new file is being placed
400cannot be extended because there is no space left on the file
401system containing the directory; or
402.Dv O_CREAT
403is specified,
404the file does not exist,
405and there are no free inodes on the file system on which the
406file is being created.
407.It Bq Er ENOTDIR
408A component of the path prefix is not a directory; or
409.Dv O_DIRECTORY
410is specified and the last path component is not a directory.
411.It Bq Er ENXIO
412The named file is a character special or block
413special file, and the device associated with this special file
414does not exist, or the named file is a FIFO,
415.Dv O_NONBLOCK
416and
417.Dv O_WRONLY
418is set and no process has the file open for reading.
419.It Bq Er EOPNOTSUPP
420.Dv O_SHLOCK
421or
422.Dv O_EXLOCK
423is specified but the underlying file system does not support locking; or
424an attempt was made to open a socket (not currently implemented).
425.It Bq Er EPERM
426The file's flags (see
427.Xr chflags 2 )
428don't allow the file to be opened.
429.It Bq Er EROFS
430The named file resides on a read-only file system,
431and the file is to be modified.
432.It Bq Er ETXTBSY
433The file is a pure procedure (shared text) file that is being
434executed and the
435.Fn open
436call requests write access.
437.El
438.Pp
439In addition,
440.Fn openat
441will fail if:
442.Bl -tag -width Er
443.It Bq Er EBADF
444.Fa path
445does not specify an absolute path and
446.Fa fd
447is neither
448.Dv AT_FDCWD
449nor a valid file descriptor open for reading or searching.
450.It Bq Er EINVAL
451An attempt was made to open a descriptor with an illegal combination
452of
453.Dv O_RDONLY ,
454.Dv O_WRONLY ,
455.Dv O_RDWR ,
456and
457.Dv O_EXEC .
458.It Bq Er ENOTDIR
459.Fa path
460is not an absolute path and
461.Fa fd
462is a file descriptor associated with a non-directory file.
463.El
464.Sh SEE ALSO
465.Xr chmod 2 ,
466.Xr close 2 ,
467.Xr dup 2 ,
468.Xr faccessat 2 ,
469.Xr fchmodat 2 ,
470.Xr fchownat 2 ,
471.Xr fstatat 2 ,
472.Xr linkat 2 ,
473.Xr lseek 2 ,
474.Xr mkdirat 2 ,
475.Xr mkfifoat 2 ,
476.Xr mknodat 2 ,
477.Xr read 2 ,
478.Xr readlinkat 2 ,
479.Xr symlinkat 2 ,
480.Xr umask 2 ,
481.Xr unlinkat 2 ,
482.Xr utimensat 2 ,
483.Xr write 2 ,
484.Xr getdtablesize 3
485.Sh STANDARDS
486The
487.Fn open
488function conforms to
489.St -p1003.1-90 .
490.Fn openat
491conforms to
492.St -p1003.1-2008 .
493.Pp
494The
495.Fa flags
496values
497.Dv O_DSYNC ,
498.Dv O_SYNC
499and
500.Dv O_RSYNC
501are extensions defined in
502.St -p1003.1b-93 .
503.\"    (This block awaits a decision about the semantics of O_SEARCH)
504.\" .Dv O_SEARCH
505.\" is defined in
506.\" .St -p1003.1-2008 .
507.Pp
508The
509.Dv O_SHLOCK
510and
511.Dv O_EXLOCK
512flags are non-standard extensions and should not be used if portability
513is of concern.
514.Sh HISTORY
515An
516.Fn open
517function call appeared in
518.At v1 .
519.Sh BUGS
520.Dv O_ASYNC
521doesn't actually work as advertised with
522.Nm ;
523you must set
524.Dv O_ASYNC
525explicitly with the
526.Dv F_SETFL
527command to
528.Xr fcntl 2 .
529