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