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