xref: /freebsd-14.2/lib/libc/sys/open.2 (revision 052d3c12)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
29.\" $FreeBSD$
30.\"
31.Dd December 1, 2017
32.Dt OPEN 2
33.Os
34.Sh NAME
35.Nm open , openat
36.Nd open or create a file for reading, writing or executing
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In fcntl.h
41.Ft int
42.Fn open "const char *path" "int flags" "..."
43.Ft int
44.Fn openat "int fd" "const char *path" "int flags" "..."
45.Sh DESCRIPTION
46The file name specified by
47.Fa path
48is opened
49for either execution or reading and/or writing as specified by the
50argument
51.Fa flags
52and the file descriptor returned to the calling process.
53The
54.Fa flags
55argument may indicate the file is to be
56created if it does not exist (by specifying the
57.Dv O_CREAT
58flag).
59In this case
60.Fn open
61and
62.Fn openat
63require an additional argument
64.Fa "mode_t mode" ,
65and the file is created with mode
66.Fa mode
67as described in
68.Xr chmod 2
69and modified by the process' umask value (see
70.Xr umask 2 ) .
71.Pp
72The
73.Fn openat
74function is equivalent to the
75.Fn open
76function except in the case where the
77.Fa path
78specifies a relative path.
79In this case the file to be opened is determined relative to the directory
80associated with the file descriptor
81.Fa fd
82instead of the current working directory.
83The
84.Fa flag
85parameter and the optional fourth parameter correspond exactly to
86the parameters of
87.Fn open .
88If
89.Fn openat
90is passed the special value
91.Dv AT_FDCWD
92in the
93.Fa fd
94parameter, the current working directory is used
95and the behavior is identical to a call to
96.Fn open .
97.Pp
98In
99.Xr capsicum 4
100capability mode,
101.Fn open
102is not permitted.
103The
104.Fa path
105argument to
106.Fn openat
107must be strictly relative to a file descriptor
108.Fa fd ,
109as defined in
110.Pa sys/kern/vfs_lookup.c .
111.Fa path
112must not be an absolute path and must not contain ".." components.
113Additionally, no symbolic link in
114.Fa path
115may contain ".." components either.
116.Fa fd
117must not be
118.Dv AT_FDCWD .
119.Pp
120The flags specified are formed by
121.Em or Ns 'ing
122the following values
123.Pp
124.Bd -literal -offset indent -compact
125O_RDONLY	open for reading only
126O_WRONLY	open for writing only
127O_RDWR		open for reading and writing
128O_EXEC		open for execute only
129O_NONBLOCK	do not block on open
130O_APPEND	append on each write
131O_CREAT		create file if it does not exist
132O_TRUNC		truncate size to 0
133O_EXCL		error if create and file exists
134O_SHLOCK	atomically obtain a shared lock
135O_EXLOCK	atomically obtain an exclusive lock
136O_DIRECT	eliminate or reduce cache effects
137O_FSYNC		synchronous writes
138O_SYNC		synchronous writes
139O_NOFOLLOW	do not follow symlinks
140O_NOCTTY	ignored
141O_TTY_INIT	ignored
142O_DIRECTORY	error if file is not a directory
143O_CLOEXEC	set FD_CLOEXEC upon open
144O_VERIFY	verify the contents of the file
145.Ed
146.Pp
147Opening a file with
148.Dv O_APPEND
149set causes each write on the file
150to be appended to the end.
151If
152.Dv O_TRUNC
153is specified and the
154file exists, the file is truncated to zero length.
155If
156.Dv O_EXCL
157is set with
158.Dv O_CREAT
159and the file already
160exists,
161.Fn open
162returns an error.
163This may be used to
164implement a simple exclusive access locking mechanism.
165If
166.Dv O_EXCL
167is set and the last component of the pathname is
168a symbolic link,
169.Fn open
170will fail even if the symbolic
171link points to a non-existent name.
172If the
173.Dv O_NONBLOCK
174flag is specified and the
175.Fn open
176system call would result
177in the process being blocked for some reason (e.g., waiting for
178carrier on a dialup line),
179.Fn open
180returns immediately.
181The descriptor remains in non-blocking mode for subsequent operations.
182.Pp
183If
184.Dv O_FSYNC
185is used in the mask, all writes will
186immediately be written to disk,
187the kernel will not cache written data
188and all writes on the descriptor will not return until
189the data to be written completes.
190.Pp
191.Dv O_SYNC
192is a synonym for
193.Dv O_FSYNC
194required by
195.Tn POSIX .
196.Pp
197If
198.Dv O_NOFOLLOW
199is used in the mask and the target file passed to
200.Fn open
201is a symbolic link then the
202.Fn open
203will fail.
204.Pp
205When opening a file, a lock with
206.Xr flock 2
207semantics can be obtained by setting
208.Dv O_SHLOCK
209for a shared lock, or
210.Dv O_EXLOCK
211for an exclusive lock.
212If creating a file with
213.Dv O_CREAT ,
214the request for the lock will never fail
215(provided that the underlying file system supports locking).
216.Pp
217.Dv O_DIRECT
218may be used to minimize or eliminate the cache effects of reading and writing.
219The system will attempt to avoid caching the data you read or write.
220If it cannot avoid caching the data,
221it will minimize the impact the data has on the cache.
222Use of this flag can drastically reduce performance if not used with care.
223.Pp
224.Dv O_NOCTTY
225may be used to ensure the OS does not assign this file as the
226controlling terminal when it opens a tty device.
227This is the default on
228.Fx ,
229but is present for
230.Tn POSIX
231compatibility.
232The
233.Fn open
234system call will not assign controlling terminals on
235.Fx .
236.Pp
237.Dv O_TTY_INIT
238may be used to ensure the OS restores the terminal attributes when
239initially opening a TTY.
240This is the default on
241.Fx ,
242but is present for
243.Tn POSIX
244compatibility.
245The initial call to
246.Fn open
247on a TTY will always restore default terminal attributes on
248.Fx .
249.Pp
250.Dv O_DIRECTORY
251may be used to ensure the resulting file descriptor refers to a
252directory.
253This flag can be used to prevent applications with elevated privileges
254from opening files which are even unsafe to open with
255.Dv O_RDONLY ,
256such as device nodes.
257.Pp
258.Dv O_CLOEXEC
259may be used to set
260.Dv FD_CLOEXEC
261flag for the newly returned file descriptor.
262.Pp
263.Dv O_VERIFY
264may be used to indicate to the kernel that the contents of the file should
265be verified before allowing the open to proceed.
266The details of what
267.Dq verified
268means is implementation specific.
269The run-time linker (rtld) uses this flag to ensure shared objects have
270been verified before operating on them.
271.Pp
272If successful,
273.Fn open
274returns a non-negative integer, termed a file descriptor.
275It returns \-1 on failure.
276The file pointer used to mark the current position within the
277file is set to the beginning of the file.
278.Pp
279If a sleeping open of a device node from
280.Xr devfs 5
281is interrupted by a signal, the call always fails with
282.Er EINTR ,
283even if the
284.Dv SA_RESTART
285flag is set for the signal.
286A sleeping open of a fifo (see
287.Xr mkfifo 2 )
288is restarted as normal.
289.Pp
290When a new file is created it is given the group of the directory
291which contains it.
292.Pp
293Unless
294.Dv O_CLOEXEC
295flag was specified,
296the new descriptor is set to remain open across
297.Xr execve 2
298system calls; see
299.Xr close 2 ,
300.Xr fcntl 2
301and
302.Dv O_CLOEXEC
303description.
304.Pp
305The system imposes a limit on the number of file descriptors
306open simultaneously by one process.
307The
308.Xr getdtablesize 2
309system call returns the current system limit.
310.Sh RETURN VALUES
311If successful,
312.Fn open
313and
314.Fn openat
315return a non-negative integer, termed a file descriptor.
316They return \-1 on failure, and set
317.Va errno
318to indicate the error.
319.Sh ERRORS
320The named file is opened unless:
321.Bl -tag -width Er
322.It Bq Er ENOTDIR
323A component of the path prefix is not a directory.
324.It Bq Er ENAMETOOLONG
325A component of a pathname exceeded 255 characters,
326or an entire path name exceeded 1023 characters.
327.It Bq Er ENOENT
328.Dv O_CREAT
329is not set and the named file does not exist.
330.It Bq Er ENOENT
331A component of the path name that must exist does not exist.
332.It Bq Er EACCES
333Search permission is denied for a component of the path prefix.
334.It Bq Er EACCES
335The required permissions (for reading and/or writing)
336are denied for the given flags.
337.It Bq Er EACCES
338.Dv O_TRUNC
339is specified and write permission is denied.
340.It Bq Er EACCES
341.Dv O_CREAT
342is specified,
343the file does not exist,
344and the directory in which it is to be created
345does not permit writing.
346.It Bq Er EPERM
347.Dv O_CREAT
348is specified, the file does not exist, and the directory in which it is to be
349created has its immutable flag set, see the
350.Xr chflags 2
351manual page for more information.
352.It Bq Er EPERM
353The named file has its immutable flag set and the file is to be modified.
354.It Bq Er EPERM
355The named file has its append-only flag set, the file is to be modified, and
356.Dv O_TRUNC
357is specified or
358.Dv O_APPEND
359is not specified.
360.It Bq Er ELOOP
361Too many symbolic links were encountered in translating the pathname.
362.It Bq Er EISDIR
363The named file is a directory, and the arguments specify
364it is to be modified.
365.It Bq Er EROFS
366The named file resides on a read-only file system,
367and the file is to be modified.
368.It Bq Er EROFS
369.Dv O_CREAT
370is specified and the named file would reside on a read-only file system.
371.It Bq Er EMFILE
372The process has already reached its limit for open file descriptors.
373.It Bq Er ENFILE
374The system file table is full.
375.It Bq Er EMLINK
376.Dv O_NOFOLLOW
377was specified and the target is a symbolic link.
378.It Bq Er ENXIO
379The named file is a character special or block
380special file, and the device associated with this special file
381does not exist.
382.It Bq Er ENXIO
383.Dv O_NONBLOCK
384is set, the named file is a fifo,
385.Dv O_WRONLY
386is set, and no process has the file open for reading.
387.It Bq Er EINTR
388The
389.Fn open
390operation was interrupted by a signal.
391.It Bq Er EOPNOTSUPP
392.Dv O_SHLOCK
393or
394.Dv O_EXLOCK
395is specified but the underlying file system does not support locking.
396.It Bq Er EOPNOTSUPP
397The named file is a special file mounted through a file system that
398does not support access to it (e.g.\& NFS).
399.It Bq Er EWOULDBLOCK
400.Dv O_NONBLOCK
401and one of
402.Dv O_SHLOCK
403or
404.Dv O_EXLOCK
405is specified and the file is locked.
406.It Bq Er ENOSPC
407.Dv O_CREAT
408is specified,
409the file does not exist,
410and the directory in which the entry for the new file is being placed
411cannot be extended because there is no space left on the file
412system containing the directory.
413.It Bq Er ENOSPC
414.Dv O_CREAT
415is specified,
416the file does not exist,
417and there are no free inodes on the file system on which the
418file is being created.
419.It Bq Er EDQUOT
420.Dv O_CREAT
421is specified,
422the file does not exist,
423and the directory in which the entry for the new file
424is being placed cannot be extended because the
425user's quota of disk blocks on the file system
426containing the directory has been exhausted.
427.It Bq Er EDQUOT
428.Dv O_CREAT
429is specified,
430the file does not exist,
431and the user's quota of inodes on the file system on
432which the file is being created has been exhausted.
433.It Bq Er EIO
434An I/O error occurred while making the directory entry or
435allocating the inode for
436.Dv O_CREAT .
437.It Bq Er ETXTBSY
438The file is a pure procedure (shared text) file that is being
439executed and the
440.Fn open
441system call requests write access.
442.It Bq Er EFAULT
443The
444.Fa path
445argument
446points outside the process's allocated address space.
447.It Bq Er EEXIST
448.Dv O_CREAT
449and
450.Dv O_EXCL
451were specified and the file exists.
452.It Bq Er EOPNOTSUPP
453An attempt was made to open a socket (not currently implemented).
454.It Bq Er EINVAL
455An attempt was made to open a descriptor with an illegal combination
456of
457.Dv O_RDONLY ,
458.Dv O_WRONLY ,
459.Dv O_RDWR
460and
461.Dv O_EXEC .
462.It Bq Er EBADF
463The
464.Fa path
465argument does not specify an absolute path and the
466.Fa fd
467argument is
468neither
469.Dv AT_FDCWD
470nor a valid file descriptor open for searching.
471.It Bq Er ENOTDIR
472The
473.Fa path
474argument is not an absolute path and
475.Fa fd
476is neither
477.Dv AT_FDCWD
478nor a file descriptor associated with a directory.
479.It Bq Er ENOTDIR
480.Dv O_DIRECTORY
481is specified and the file is not a directory.
482.It Bq Er ECAPMODE
483.Dv AT_FDCWD
484is specified and the process is in capability mode.
485.It Bq Er ECAPMODE
486.Fn open
487was called and the process is in capability mode.
488.It Bq Er ENOTCAPABLE
489.Fa path
490is an absolute path or contained a ".." component leading to a
491directory outside of the directory hierarchy specified by
492.Fa fd .
493.El
494.Sh SEE ALSO
495.Xr chmod 2 ,
496.Xr close 2 ,
497.Xr dup 2 ,
498.Xr fexecve 2 ,
499.Xr fhopen 2 ,
500.Xr getdtablesize 2 ,
501.Xr getfh 2 ,
502.Xr lgetfh 2 ,
503.Xr lseek 2 ,
504.Xr read 2 ,
505.Xr umask 2 ,
506.Xr write 2 ,
507.Xr fopen 3 ,
508.Xr capsicum 4
509.Sh STANDARDS
510These functions are specified by
511.St -p1003.1-2008 .
512.Fx
513sets
514.Va errno
515to
516.Er EMLINK instead of
517.Er ELOOP
518as specified by
519.Tn POSIX
520when
521.Dv O_NOFOLLOW
522is set in flags and the final component of pathname is a symbolic link
523to distinguish it from the case of too many symbolic link traversals
524in one of its non-final components.
525.Sh HISTORY
526The
527.Fn open
528function appeared in
529.At v1 .
530The
531.Fn openat
532function was introduced in
533.Fx 8.0 .
534.Sh BUGS
535The Open Group Extended API Set 2 specification requires that the test
536for whether
537.Fa fd
538is searchable is based on whether
539.Fa fd
540is open for searching, not whether the underlying directory currently
541permits searches.
542The present implementation of the
543.Fa openat
544checks the current permissions of directory instead.
545