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