xref: /freebsd-13.1/lib/libc/sys/open.2 (revision bdccf0bb)
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 June 14, 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 EROFS
423The named file resides on a read-only file system,
424and the file is to be modified.
425.It Bq Er EROFS
426.Dv O_CREAT
427is specified and the named file would reside on a read-only file system.
428.It Bq Er EMFILE
429The process has already reached its limit for open file descriptors.
430.It Bq Er ENFILE
431The system file table is full.
432.It Bq Er EMLINK
433.Dv O_NOFOLLOW
434was specified and the target is a symbolic link.
435.It Bq Er ENXIO
436The named file is a character special or block
437special file, and the device associated with this special file
438does not exist.
439.It Bq Er ENXIO
440.Dv O_NONBLOCK
441is set, the named file is a fifo,
442.Dv O_WRONLY
443is set, and no process has the file open for reading.
444.It Bq Er EINTR
445The
446.Fn open
447operation was interrupted by a signal.
448.It Bq Er EOPNOTSUPP
449.Dv O_SHLOCK
450or
451.Dv O_EXLOCK
452is specified but the underlying file system does not support locking.
453.It Bq Er EOPNOTSUPP
454The named file is a special file mounted through a file system that
455does not support access to it (e.g.\& NFS).
456.It Bq Er EWOULDBLOCK
457.Dv O_NONBLOCK
458and one of
459.Dv O_SHLOCK
460or
461.Dv O_EXLOCK
462is specified and the file is locked.
463.It Bq Er ENOSPC
464.Dv O_CREAT
465is specified,
466the file does not exist,
467and the directory in which the entry for the new file is being placed
468cannot be extended because there is no space left on the file
469system containing the directory.
470.It Bq Er ENOSPC
471.Dv O_CREAT
472is specified,
473the file does not exist,
474and there are no free inodes on the file system on which the
475file is being created.
476.It Bq Er EDQUOT
477.Dv O_CREAT
478is specified,
479the file does not exist,
480and the directory in which the entry for the new file
481is being placed cannot be extended because the
482user's quota of disk blocks on the file system
483containing the directory has been exhausted.
484.It Bq Er EDQUOT
485.Dv O_CREAT
486is specified,
487the file does not exist,
488and the user's quota of inodes on the file system on
489which the file is being created has been exhausted.
490.It Bq Er EIO
491An I/O error occurred while making the directory entry or
492allocating the inode for
493.Dv O_CREAT .
494.It Bq Er ETXTBSY
495The file is a pure procedure (shared text) file that is being
496executed and the
497.Fn open
498system call requests write access.
499.It Bq Er EFAULT
500The
501.Fa path
502argument
503points outside the process's allocated address space.
504.It Bq Er EEXIST
505.Dv O_CREAT
506and
507.Dv O_EXCL
508were specified and the file exists.
509.It Bq Er EOPNOTSUPP
510An attempt was made to open a socket (not currently implemented).
511.It Bq Er EINVAL
512An attempt was made to open a descriptor with an illegal combination
513of
514.Dv O_RDONLY ,
515.Dv O_WRONLY ,
516.Dv O_RDWR
517and
518.Dv O_EXEC .
519.It Bq Er EBADF
520The
521.Fa path
522argument does not specify an absolute path and the
523.Fa fd
524argument is
525neither
526.Dv AT_FDCWD
527nor a valid file descriptor open for searching.
528.It Bq Er ENOTDIR
529The
530.Fa path
531argument is not an absolute path and
532.Fa fd
533is neither
534.Dv AT_FDCWD
535nor a file descriptor associated with a directory.
536.It Bq Er ENOTDIR
537.Dv O_DIRECTORY
538is specified and the file is not a directory.
539.It Bq Er ECAPMODE
540.Dv AT_FDCWD
541is specified and the process is in capability mode.
542.It Bq Er ECAPMODE
543.Fn open
544was called and the process is in capability mode.
545.It Bq Er ENOTCAPABLE
546.Fa path
547is an absolute path,
548or contained a ".." component leading to a
549directory outside of the directory hierarchy specified by
550.Fa fd ,
551and the process is in capability mode.
552.It Bq Er ENOTCAPABLE
553The
554.Dv O_BENEATH
555flag was provided, and the absolute
556.Fa path
557does not have its tail fully contained under the topping directory,
558or the relative
559.Fa path
560escapes it.
561.El
562.Sh SEE ALSO
563.Xr chmod 2 ,
564.Xr close 2 ,
565.Xr dup 2 ,
566.Xr fexecve 2 ,
567.Xr fhopen 2 ,
568.Xr getdtablesize 2 ,
569.Xr getfh 2 ,
570.Xr lgetfh 2 ,
571.Xr lseek 2 ,
572.Xr read 2 ,
573.Xr umask 2 ,
574.Xr write 2 ,
575.Xr fopen 3 ,
576.Xr capsicum 4
577.Sh STANDARDS
578These functions are specified by
579.St -p1003.1-2008 .
580.Fx
581sets
582.Va errno
583to
584.Er EMLINK instead of
585.Er ELOOP
586as specified by
587.Tn POSIX
588when
589.Dv O_NOFOLLOW
590is set in flags and the final component of pathname is a symbolic link
591to distinguish it from the case of too many symbolic link traversals
592in one of its non-final components.
593.Sh HISTORY
594The
595.Fn open
596function appeared in
597.At v1 .
598The
599.Fn openat
600function was introduced in
601.Fx 8.0 .
602.Sh BUGS
603The Open Group Extended API Set 2 specification requires that the test
604for whether
605.Fa fd
606is searchable is based on whether
607.Fa fd
608is open for searching, not whether the underlying directory currently
609permits searches.
610The present implementation of the
611.Fa openat
612checks the current permissions of directory instead.
613