xref: /freebsd-13.1/lib/libc/sys/open.2 (revision ce35a3bc)
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 October 9, 2021
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.
79For
80.Fn openat
81and relative
82.Fa path ,
83the file to be opened is determined relative to the directory
84associated with the file descriptor
85.Fa fd
86instead of the current working directory.
87The
88.Fa flag
89parameter and the optional fourth parameter correspond exactly to
90the parameters of
91.Fn open .
92If
93.Fn openat
94is passed the special value
95.Dv AT_FDCWD
96in the
97.Fa fd
98parameter, the current working directory is used
99and the behavior is identical to a call to
100.Fn open .
101.Pp
102When
103.Fn openat
104is called with an absolute
105.Fa path ,
106it ignores the
107.Fa fd
108argument.
109.Pp
110In
111.Xr capsicum 4
112capability mode,
113.Fn open
114is not permitted.
115The
116.Fa path
117argument to
118.Fn openat
119must be strictly relative to a file descriptor
120.Fa fd .
121.Fa path
122must not be an absolute path and must not contain ".." components
123which cause the path resolution to escape the directory hierarchy
124starting at
125.Fa fd .
126Additionally, no symbolic link in
127.Fa path
128may target absolute path or contain escaping ".." components.
129.Fa fd
130must not be
131.Dv AT_FDCWD .
132.Pp
133If the
134.Dv vfs.lookup_cap_dotdot
135.Xr sysctl 3
136MIB is set to zero, ".." components in the paths,
137used in capability mode,
138are completely disabled.
139If the
140.Dv vfs.lookup_cap_dotdot_nonlocal
141MIB is set to zero, ".." is not allowed if found on non-local filesystem.
142.Pp
143The flags specified are formed by
144.Em or Ns 'ing
145the following values
146.Pp
147.Bd -literal -offset indent -compact
148O_RDONLY	open for reading only
149O_WRONLY	open for writing only
150O_RDWR		open for reading and writing
151O_EXEC		open for execute only
152O_SEARCH	open for search only, an alias for O_EXEC
153O_NONBLOCK	do not block on open
154O_APPEND	append on each write
155O_CREAT		create file if it does not exist
156O_TRUNC		truncate size to 0
157O_EXCL		error if create and file exists
158O_SHLOCK	atomically obtain a shared lock
159O_EXLOCK	atomically obtain an exclusive lock
160O_DIRECT	eliminate or reduce cache effects
161O_FSYNC		synchronous writes (historical synonym for O_SYNC)
162O_SYNC		synchronous writes
163O_DSYNC		synchronous data writes
164O_NOFOLLOW	do not follow symlinks
165O_NOCTTY	ignored
166O_TTY_INIT	ignored
167O_DIRECTORY	error if file is not a directory
168O_CLOEXEC	set FD_CLOEXEC upon open
169O_VERIFY	verify the contents of the file
170O_RESOLVE_BENEATH	path resolution must not cross the fd directory
171O_PATH		record only the target path in the opened descriptor
172O_EMPTY_PATH	openat, open file referenced by fd if path is empty
173.Ed
174.Pp
175Opening a file with
176.Dv O_APPEND
177set causes each write on the file
178to be appended to the end.
179If
180.Dv O_TRUNC
181is specified and the
182file exists, the file is truncated to zero length.
183If
184.Dv O_EXCL
185is set with
186.Dv O_CREAT
187and the file already
188exists,
189.Fn open
190returns an error.
191This may be used to
192implement a simple exclusive access locking mechanism.
193If
194.Dv O_EXCL
195is set and the last component of the pathname is
196a symbolic link,
197.Fn open
198will fail even if the symbolic
199link points to a non-existent name.
200If the
201.Dv O_NONBLOCK
202flag is specified and the
203.Fn open
204system call would result
205in the process being blocked for some reason (e.g., waiting for
206carrier on a dialup line),
207.Fn open
208returns immediately.
209The descriptor remains in non-blocking mode for subsequent operations.
210.Pp
211If
212.Dv O_SYNC
213is used in the mask, all writes will
214immediately and synchronously be written to disk.
215.Dv O_FSYNC
216is an historical synonym for
217.Dv O_SYNC .
218.Pp
219If
220.Dv O_DSYNC
221is used in the mask, all data and metadata required to read the data will be
222synchronously written to disk, but changes to metadata such as file access and
223modification timestamps may be written later.
224.Pp
225If
226.Dv O_NOFOLLOW
227is used in the mask and the target file passed to
228.Fn open
229is a symbolic link then the
230.Fn open
231will fail.
232.Pp
233When opening a file, a lock with
234.Xr flock 2
235semantics can be obtained by setting
236.Dv O_SHLOCK
237for a shared lock, or
238.Dv O_EXLOCK
239for an exclusive lock.
240If creating a file with
241.Dv O_CREAT ,
242the request for the lock will never fail
243(provided that the underlying file system supports locking).
244.Pp
245.Dv O_DIRECT
246may be used to minimize or eliminate the cache effects of reading and writing.
247The system will attempt to avoid caching the data you read or write.
248If it cannot avoid caching the data,
249it will minimize the impact the data has on the cache.
250Use of this flag can drastically reduce performance if not used with care.
251.Pp
252.Dv O_NOCTTY
253may be used to ensure the OS does not assign this file as the
254controlling terminal when it opens a tty device.
255This is the default on
256.Fx ,
257but is present for
258.Tn POSIX
259compatibility.
260The
261.Fn open
262system call will not assign controlling terminals on
263.Fx .
264.Pp
265.Dv O_TTY_INIT
266may be used to ensure the OS restores the terminal attributes when
267initially opening a TTY.
268This is the default on
269.Fx ,
270but is present for
271.Tn POSIX
272compatibility.
273The initial call to
274.Fn open
275on a TTY will always restore default terminal attributes on
276.Fx .
277.Pp
278.Dv O_DIRECTORY
279may be used to ensure the resulting file descriptor refers to a
280directory.
281This flag can be used to prevent applications with elevated privileges
282from opening files which are even unsafe to open with
283.Dv O_RDONLY ,
284such as device nodes.
285.Pp
286.Dv O_CLOEXEC
287may be used to set
288.Dv FD_CLOEXEC
289flag for the newly returned file descriptor.
290.Pp
291.Dv O_VERIFY
292may be used to indicate to the kernel that the contents of the file should
293be verified before allowing the open to proceed.
294The details of what
295.Dq verified
296means is implementation specific.
297The run-time linker (rtld) uses this flag to ensure shared objects have
298been verified before operating on them.
299.Pp
300.Dv O_RESOLVE_BENEATH
301returns
302.Er ENOTCAPABLE
303if any intermediate component of the specified relative path does not
304reside in the directory hierarchy beneath the starting directory.
305Absolute paths or even the temporal escape from beneath of the starting
306directory is not allowed.
307.Pp
308When
309.Fa fd
310is opened with
311.Dv O_SEARCH ,
312execute permissions are checked at open time.
313The
314.Fa fd
315may not be used for any read operations like
316.Xr getdirentries 2 .
317The primary use for this descriptor will be as the lookup descriptor for the
318.Fn *at
319family of functions.
320.Pp
321.Dv O_PATH
322returns a file descriptor that can be used as a directory file descriptor for
323.Xr openat 2
324and other system calls taking a file descriptor argument, like
325.Xr fstatat 2
326and others.
327The other functionality of the returned file descriptor is limited to
328the descriptor-level operations.
329It can be used for
330.Bl -tag -width readlinkat(2) -offset indent -compact
331.It Xr fcntl 2
332but advisory locking is not allowed
333.It Xr dup 2
334.It Xr close 2
335.It Xr fstat 2
336.It Xr fexecve 2
337.It Dv SCM_RIGHTS
338can be passed over a
339.Xr unix 4
340socket using a
341.Dv SCM_RIGHTS
342message
343.It Xr kqueue 2
344using for
345.Dv EVFILT_VNODE
346.It Xr readlinkat 2
347.El
348But operations like
349.Xr read 2 ,
350.Xr ftruncate 2 ,
351and any other that operate on file and not on file descriptor (except
352.Xr fstat 2 ),
353are not allowed.
354File opened with the
355.Dv O_PATH
356flag does not prevent non-forced unmount of the volume it belongs to.
357.Pp
358A file descriptor created with the
359.Dv O_PATH
360flag can be opened into normal (operable) file descriptor by
361specifying it as the
362.Fa fd
363argument to
364.Fn openat
365with empty
366.Fa path
367and flag
368.Dv O_EMPTY_PATH .
369Such an open behaves as if the current path of the file referenced by
370.Fa fd
371is passed, except that the path walk permissions are not checked.
372See also the description of
373.Dv AT_EMPTY_PATH
374flag for
375.Xr fstatat 2
376and related syscalls.
377.Pp
378If successful,
379.Fn open
380returns a non-negative integer, termed a file descriptor.
381It returns \-1 on failure.
382The file pointer used to mark the current position within the
383file is set to the beginning of the file.
384.Pp
385If a sleeping open of a device node from
386.Xr devfs 5
387is interrupted by a signal, the call always fails with
388.Er EINTR ,
389even if the
390.Dv SA_RESTART
391flag is set for the signal.
392A sleeping open of a fifo (see
393.Xr mkfifo 2 )
394is restarted as normal.
395.Pp
396When a new file is created it is given the group of the directory
397which contains it.
398.Pp
399Unless
400.Dv O_CLOEXEC
401flag was specified,
402the new descriptor is set to remain open across
403.Xr execve 2
404system calls; see
405.Xr close 2 ,
406.Xr fcntl 2
407and
408.Dv O_CLOEXEC
409description.
410.Pp
411The system imposes a limit on the number of file descriptors
412open simultaneously by one process.
413The
414.Xr getdtablesize 2
415system call returns the current system limit.
416.Sh RETURN VALUES
417If successful,
418.Fn open
419and
420.Fn openat
421return a non-negative integer, termed a file descriptor.
422They return \-1 on failure, and set
423.Va errno
424to indicate the error.
425.Sh ERRORS
426The named file is opened unless:
427.Bl -tag -width Er
428.It Bq Er ENOTDIR
429A component of the path prefix is not a directory.
430.It Bq Er ENAMETOOLONG
431A component of a pathname exceeded 255 characters,
432or an entire path name exceeded 1023 characters.
433.It Bq Er ENOENT
434.Dv O_CREAT
435is not set and the named file does not exist.
436.It Bq Er ENOENT
437A component of the path name that must exist does not exist.
438.It Bq Er EACCES
439Search permission is denied for a component of the path prefix.
440.It Bq Er EACCES
441The required permissions (for reading and/or writing)
442are denied for the given flags.
443.It Bq Er EACCES
444.Dv O_TRUNC
445is specified and write permission is denied.
446.It Bq Er EACCES
447.Dv O_CREAT
448is specified,
449the file does not exist,
450and the directory in which it is to be created
451does not permit writing.
452.It Bq Er EPERM
453.Dv O_CREAT
454is specified, the file does not exist, and the directory in which it is to be
455created has its immutable flag set, see the
456.Xr chflags 2
457manual page for more information.
458.It Bq Er EPERM
459The named file has its immutable flag set and the file is to be modified.
460.It Bq Er EPERM
461The named file has its append-only flag set, the file is to be modified, and
462.Dv O_TRUNC
463is specified or
464.Dv O_APPEND
465is not specified.
466.It Bq Er ELOOP
467Too many symbolic links were encountered in translating the pathname.
468.It Bq Er EISDIR
469The named file is a directory, and the arguments specify
470it is to be modified.
471.It Bq Er EISDIR
472The named file is a directory, and the flags specified
473.Dv O_CREAT
474without
475.Dv O_DIRECTORY .
476.It Bq Er EROFS
477The named file resides on a read-only file system,
478and the file is to be modified.
479.It Bq Er EROFS
480.Dv O_CREAT
481is specified and the named file would reside on a read-only file system.
482.It Bq Er EMFILE
483The process has already reached its limit for open file descriptors.
484.It Bq Er ENFILE
485The system file table is full.
486.It Bq Er EMLINK
487.Dv O_NOFOLLOW
488was specified and the target is a symbolic link.
489.It Bq Er ENXIO
490The named file is a character special or block
491special file, and the device associated with this special file
492does not exist.
493.It Bq Er ENXIO
494.Dv O_NONBLOCK
495is set, the named file is a fifo,
496.Dv O_WRONLY
497is set, and no process has the file open for reading.
498.It Bq Er EINTR
499The
500.Fn open
501operation was interrupted by a signal.
502.It Bq Er EOPNOTSUPP
503.Dv O_SHLOCK
504or
505.Dv O_EXLOCK
506is specified but the underlying file system does not support locking.
507.It Bq Er EOPNOTSUPP
508The named file is a special file mounted through a file system that
509does not support access to it (e.g.\& NFS).
510.It Bq Er EWOULDBLOCK
511.Dv O_NONBLOCK
512and one of
513.Dv O_SHLOCK
514or
515.Dv O_EXLOCK
516is specified and the file is locked.
517.It Bq Er ENOSPC
518.Dv O_CREAT
519is specified,
520the file does not exist,
521and the directory in which the entry for the new file is being placed
522cannot be extended because there is no space left on the file
523system containing the directory.
524.It Bq Er ENOSPC
525.Dv O_CREAT
526is specified,
527the file does not exist,
528and there are no free inodes on the file system on which the
529file is being created.
530.It Bq Er EDQUOT
531.Dv O_CREAT
532is specified,
533the file does not exist,
534and the directory in which the entry for the new file
535is being placed cannot be extended because the
536user's quota of disk blocks on the file system
537containing the directory has been exhausted.
538.It Bq Er EDQUOT
539.Dv O_CREAT
540is specified,
541the file does not exist,
542and the user's quota of inodes on the file system on
543which the file is being created has been exhausted.
544.It Bq Er EIO
545An I/O error occurred while making the directory entry or
546allocating the inode for
547.Dv O_CREAT .
548.It Bq Er EINTEGRITY
549Corrupted data was detected while reading from the file system.
550.It Bq Er ETXTBSY
551The file is a pure procedure (shared text) file that is being
552executed and the
553.Fn open
554system call requests write access.
555.It Bq Er EFAULT
556The
557.Fa path
558argument
559points outside the process's allocated address space.
560.It Bq Er EEXIST
561.Dv O_CREAT
562and
563.Dv O_EXCL
564were specified and the file exists.
565.It Bq Er EOPNOTSUPP
566An attempt was made to open a socket (not currently implemented).
567.It Bq Er EINVAL
568An attempt was made to open a descriptor with an illegal combination
569of
570.Dv O_RDONLY ,
571.Dv O_WRONLY ,
572or
573.Dv O_RDWR ,
574and
575.Dv O_EXEC
576or
577.Dv O_SEARCH .
578.It Bq Er EINVAL
579The
580.Dv O_RESOLVE_BENEATH
581flag is specified and
582.Dv path
583is absolute.
584.It Bq Er EBADF
585The
586.Fa path
587argument does not specify an absolute path and the
588.Fa fd
589argument is
590neither
591.Dv AT_FDCWD
592nor a valid file descriptor open for searching.
593.It Bq Er ENOTDIR
594The
595.Fa path
596argument is not an absolute path and
597.Fa fd
598is neither
599.Dv AT_FDCWD
600nor a file descriptor associated with a directory.
601.It Bq Er ENOTDIR
602.Dv O_DIRECTORY
603is specified and the file is not a directory.
604.It Bq Er ECAPMODE
605.Dv AT_FDCWD
606is specified and the process is in capability mode.
607.It Bq Er ECAPMODE
608.Fn open
609was called and the process is in capability mode.
610.It Bq Er ENOTCAPABLE
611.Fa path
612is an absolute path,
613or contained a ".." component leading to a
614directory outside of the directory hierarchy specified by
615.Fa fd ,
616and the process is in capability mode.
617.It Bq Er ENOTCAPABLE
618The
619.Dv O_RESOLVE_BENEATH
620flag was provided, and the relative
621.Fa path
622escapes the
623.Ar fd
624directory.
625.El
626.Sh SEE ALSO
627.Xr chmod 2 ,
628.Xr close 2 ,
629.Xr dup 2 ,
630.Xr fexecve 2 ,
631.Xr fhopen 2 ,
632.Xr getdtablesize 2 ,
633.Xr getfh 2 ,
634.Xr lgetfh 2 ,
635.Xr lseek 2 ,
636.Xr read 2 ,
637.Xr umask 2 ,
638.Xr write 2 ,
639.Xr fopen 3 ,
640.Xr capsicum 4
641.Sh STANDARDS
642These functions are specified by
643.St -p1003.1-2008 .
644.Fx
645sets
646.Va errno
647to
648.Er EMLINK instead of
649.Er ELOOP
650as specified by
651.Tn POSIX
652when
653.Dv O_NOFOLLOW
654is set in flags and the final component of pathname is a symbolic link
655to distinguish it from the case of too many symbolic link traversals
656in one of its non-final components.
657.Sh HISTORY
658The
659.Fn open
660function appeared in
661.At v1 .
662The
663.Fn openat
664function was introduced in
665.Fx 8.0 .
666.Dv O_DSYNC
667appeared in 13.0.
668.Sh BUGS
669The Open Group Extended API Set 2 specification requires that the test
670for whether
671.Fa fd
672is searchable is based on whether
673.Fa fd
674is open for searching, not whether the underlying directory currently
675permits searches.
676The present implementation of the
677.Fa openat
678checks the current permissions of directory instead.
679.Pp
680The
681.Fa mode
682argument is variadic and may result in different calling conventions
683than might otherwise be expected.
684