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