xref: /freebsd/lib/libsys/open.2 (revision e2dd73cf45a6a8d97131bdbe512ab1ff63121d85)
18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
28*e2dd73cfSKonstantin Belousov.Dd February 28, 2025
298269e767SBrooks Davis.Dt OPEN 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm open , openat
338269e767SBrooks Davis.Nd open or create a file for reading, writing or executing
348269e767SBrooks Davis.Sh LIBRARY
358269e767SBrooks Davis.Lb libc
368269e767SBrooks Davis.Sh SYNOPSIS
378269e767SBrooks Davis.In fcntl.h
388269e767SBrooks Davis.Ft int
398269e767SBrooks Davis.Fn open "const char *path" "int flags" "..."
408269e767SBrooks Davis.Ft int
418269e767SBrooks Davis.Fn openat "int fd" "const char *path" "int flags" "..."
428269e767SBrooks Davis.Sh DESCRIPTION
438269e767SBrooks DavisThe file name specified by
448269e767SBrooks Davis.Fa path
458269e767SBrooks Davisis opened
468269e767SBrooks Davisfor either execution or reading and/or writing as specified by the
478269e767SBrooks Davisargument
488269e767SBrooks Davis.Fa flags
498269e767SBrooks Davisand the file descriptor returned to the calling process.
508269e767SBrooks DavisThe
518269e767SBrooks Davis.Fa flags
528269e767SBrooks Davisargument may indicate the file is to be
538269e767SBrooks Daviscreated if it does not exist (by specifying the
548269e767SBrooks Davis.Dv O_CREAT
558269e767SBrooks Davisflag).
568269e767SBrooks DavisIn this case
578269e767SBrooks Davis.Fn open
588269e767SBrooks Davisand
598269e767SBrooks Davis.Fn openat
608269e767SBrooks Davisrequire an additional argument
618269e767SBrooks Davis.Fa "mode_t mode" ,
628269e767SBrooks Davisand the file is created with mode
638269e767SBrooks Davis.Fa mode
648269e767SBrooks Davisas described in
658269e767SBrooks Davis.Xr chmod 2
668269e767SBrooks Davisand modified by the process' umask value (see
678269e767SBrooks Davis.Xr umask 2 ) .
688269e767SBrooks Davis.Pp
698269e767SBrooks DavisThe
708269e767SBrooks Davis.Fn openat
718269e767SBrooks Davisfunction is equivalent to the
728269e767SBrooks Davis.Fn open
738269e767SBrooks Davisfunction except in the case where the
748269e767SBrooks Davis.Fa path
758269e767SBrooks Davisspecifies a relative path.
768269e767SBrooks DavisFor
778269e767SBrooks Davis.Fn openat
788269e767SBrooks Davisand relative
798269e767SBrooks Davis.Fa path ,
808269e767SBrooks Davisthe file to be opened is determined relative to the directory
818269e767SBrooks Davisassociated with the file descriptor
828269e767SBrooks Davis.Fa fd
838269e767SBrooks Davisinstead of the current working directory.
848269e767SBrooks DavisThe
858269e767SBrooks Davis.Fa flag
868269e767SBrooks Davisparameter and the optional fourth parameter correspond exactly to
878269e767SBrooks Davisthe parameters of
888269e767SBrooks Davis.Fn open .
898269e767SBrooks DavisIf
908269e767SBrooks Davis.Fn openat
918269e767SBrooks Davisis passed the special value
928269e767SBrooks Davis.Dv AT_FDCWD
938269e767SBrooks Davisin the
948269e767SBrooks Davis.Fa fd
958269e767SBrooks Davisparameter, the current working directory is used
968269e767SBrooks Davisand the behavior is identical to a call to
978269e767SBrooks Davis.Fn open .
988269e767SBrooks Davis.Pp
998269e767SBrooks DavisWhen
1008269e767SBrooks Davis.Fn openat
1018269e767SBrooks Davisis called with an absolute
1028269e767SBrooks Davis.Fa path ,
1038269e767SBrooks Davisit ignores the
1048269e767SBrooks Davis.Fa fd
1058269e767SBrooks Davisargument.
1068269e767SBrooks Davis.Pp
1078269e767SBrooks DavisIn
1088269e767SBrooks Davis.Xr capsicum 4
1098269e767SBrooks Daviscapability mode,
1108269e767SBrooks Davis.Fn open
1118269e767SBrooks Davisis not permitted.
1128269e767SBrooks DavisThe
1138269e767SBrooks Davis.Fa path
1148269e767SBrooks Davisargument to
1158269e767SBrooks Davis.Fn openat
1168269e767SBrooks Davismust be strictly relative to a file descriptor
117826509a3SJohn Baldwin.Fa fd ;
118826509a3SJohn Baldwinthat is,
1198269e767SBrooks Davis.Fa path
1208269e767SBrooks Davismust not be an absolute path and must not contain ".." components
1218269e767SBrooks Daviswhich cause the path resolution to escape the directory hierarchy
1228269e767SBrooks Davisstarting at
1238269e767SBrooks Davis.Fa fd .
1248269e767SBrooks DavisAdditionally, no symbolic link in
1258269e767SBrooks Davis.Fa path
1268269e767SBrooks Davismay target absolute path or contain escaping ".." components.
1278269e767SBrooks Davis.Fa fd
1288269e767SBrooks Davismust not be
1298269e767SBrooks Davis.Dv AT_FDCWD .
1308269e767SBrooks Davis.Pp
1318269e767SBrooks DavisIf the
1328269e767SBrooks Davis.Dv vfs.lookup_cap_dotdot
1338269e767SBrooks Davis.Xr sysctl 3
1348269e767SBrooks DavisMIB is set to zero, ".." components in the paths,
1358269e767SBrooks Davisused in capability mode,
1368269e767SBrooks Davisare completely disabled.
1378269e767SBrooks DavisIf the
1388269e767SBrooks Davis.Dv vfs.lookup_cap_dotdot_nonlocal
1398269e767SBrooks DavisMIB is set to zero, ".." is not allowed if found on non-local filesystem.
1408269e767SBrooks Davis.Pp
141826509a3SJohn BaldwinThe
142826509a3SJohn Baldwin.Fa flags
143826509a3SJohn Baldwinare formed by
1448269e767SBrooks Davis.Em or Ns 'ing
145826509a3SJohn Baldwinthe following values:
1468269e767SBrooks Davis.Pp
147826509a3SJohn Baldwin.Bl -tag -width O_RESOLVE_BENEATH
148826509a3SJohn Baldwin.It Dv O_RDONLY
149826509a3SJohn Baldwinopen for reading only
150826509a3SJohn Baldwin.It Dv O_WRONLY
151826509a3SJohn Baldwinopen for writing only
152826509a3SJohn Baldwin.It Dv O_RDWR
153826509a3SJohn Baldwinopen for reading and writing
154826509a3SJohn Baldwin.It Dv O_EXEC
155826509a3SJohn Baldwinopen for execute only
156826509a3SJohn Baldwin.It Dv O_SEARCH
157826509a3SJohn Baldwinopen for search only
158826509a3SJohn Baldwin(an alias for
159826509a3SJohn Baldwin.Dv O_EXEC
160826509a3SJohn Baldwintypically used with
161826509a3SJohn Baldwin.Dv O_DIRECTORY )
162826509a3SJohn Baldwin.It Dv O_NONBLOCK
163826509a3SJohn Baldwindo not block on open
164826509a3SJohn Baldwin.It Dv O_APPEND
165826509a3SJohn Baldwinset file pointer to the end of the file before each write
166826509a3SJohn Baldwin.It Dv O_CREAT
167826509a3SJohn Baldwincreate file if it does not exist
168826509a3SJohn Baldwin.It Dv O_TRUNC
169826509a3SJohn Baldwintruncate size to 0
170826509a3SJohn Baldwin.It Dv O_EXCL
171826509a3SJohn Baldwinfail if
172826509a3SJohn Baldwin.Dv O_CREAT
173826509a3SJohn Baldwinis set and the file exists
174826509a3SJohn Baldwin.It Dv O_SHLOCK
175826509a3SJohn Baldwinatomically obtain a shared lock
176826509a3SJohn Baldwin.It Dv O_EXLOCK
177826509a3SJohn Baldwinatomically obtain an exclusive lock
178826509a3SJohn Baldwin.It Dv O_DIRECT
179826509a3SJohn Baldwinread and write directly from the backing store
180826509a3SJohn Baldwin.It Dv O_FSYNC
181826509a3SJohn Baldwinsynchronous data and metadata writes
182826509a3SJohn Baldwin.Pq historical synonym for Dv O_SYNC
183826509a3SJohn Baldwin.It Dv O_SYNC
184826509a3SJohn Baldwinsynchronous data and metadata writes
185826509a3SJohn Baldwin.It Dv O_DSYNC
186826509a3SJohn Baldwinsynchronous data writes
187826509a3SJohn Baldwin.It Dv O_NOFOLLOW
188826509a3SJohn Baldwindo not follow symlinks
189826509a3SJohn Baldwin.It Dv O_NOCTTY
190826509a3SJohn Baldwinignored
191826509a3SJohn Baldwin.It Dv O_TTY_INIT
192826509a3SJohn Baldwinignored
193826509a3SJohn Baldwin.It Dv O_DIRECTORY
194826509a3SJohn Baldwinerror if file is not a directory
195826509a3SJohn Baldwin.It Dv O_CLOEXEC
196826509a3SJohn Baldwinautomatically close file on
197826509a3SJohn Baldwin.Xr execve 2
198826509a3SJohn Baldwin.It Dv O_VERIFY
199826509a3SJohn Baldwinverify the contents of the file with
200826509a3SJohn Baldwin.Xr mac_veriexec 4
201826509a3SJohn Baldwin.It Dv O_RESOLVE_BENEATH
202826509a3SJohn Baldwin.Pq Xr openat 2 only
203826509a3SJohn Baldwinpath resolution must not cross the
204826509a3SJohn Baldwin.Fa fd
205826509a3SJohn Baldwindirectory
206826509a3SJohn Baldwin.It Dv O_PATH
207826509a3SJohn Baldwinrecord only the target path in the opened descriptor
208826509a3SJohn Baldwin.It Dv O_EMPTY_PATH
209826509a3SJohn Baldwin.Pq Xr openat 2 only
210826509a3SJohn Baldwinopen file referenced by
211826509a3SJohn Baldwin.Fa fd
212826509a3SJohn Baldwinif path is empty
213826509a3SJohn Baldwin.El
214826509a3SJohn Baldwin.Pp
215826509a3SJohn BaldwinExactly one of the flags
216826509a3SJohn Baldwin.Dv O_RDONLY ,
217826509a3SJohn Baldwin.Dv O_WRONLY ,
218826509a3SJohn Baldwin.Dv O_RDWR ,
219826509a3SJohn Baldwinor
220826509a3SJohn Baldwin.Dv O_EXEC
221826509a3SJohn Baldwinmust be provided.
2228269e767SBrooks Davis.Pp
2238269e767SBrooks DavisOpening a file with
2248269e767SBrooks Davis.Dv O_APPEND
225826509a3SJohn Baldwinset causes each write on the resulting file descriptor
226826509a3SJohn Baldwinto be appended to the end of the file.
227826509a3SJohn Baldwin.Pp
2288269e767SBrooks DavisIf
2298269e767SBrooks Davis.Dv O_TRUNC
2308269e767SBrooks Davisis specified and the
2318269e767SBrooks Davisfile exists, the file is truncated to zero length.
232826509a3SJohn Baldwin.Pp
2338269e767SBrooks DavisIf
2348269e767SBrooks Davis.Dv O_CREAT
2358cebb063Sartembunichevis set, but file already exists,
2368cebb063Sartembunichevthis flag has no effect except when
2378cebb063Sartembunichev.Dv O_EXCL
2388cebb063Sartembunichevis set too, in this case
2398269e767SBrooks Davis.Fn open
240826509a3SJohn Baldwinfails with
241826509a3SJohn Baldwin.Er EEXIST .
2428269e767SBrooks DavisThis may be used to
2438269e767SBrooks Davisimplement a simple exclusive access locking mechanism.
2448cebb063SartembunichevIn all other cases, the file is created
2458cebb063Sartembunichevand the access permission bits (see
2468cebb063Sartembunichev.Xr chmod 2)
2478cebb063Sartembunichevof the file mode
2488cebb063Sartembunichevare set to the value of the third argument taken as
2498cebb063Sartembunichev.Fa "mode_t mode"
2508cebb063Sartembunichevand passed through the
2518cebb063Sartembunichev.Xr umask 2 .
2528cebb063SartembunichevThis argument does not affect whether the file is opened
2538cebb063Sartembunichevfor reading, writing, or for both.
2548cebb063SartembunichevThe open' request for a lock on the file, created with
2558cebb063Sartembunichev.Dv O_CREAT ,
2568cebb063Sartembunichevwill never fail
257*e2dd73cfSKonstantin Belousovprovided that the underlying file system supports locking;
2588cebb063Sartembunichevsee also
2598cebb063Sartembunichev.Dv O_SHLOCK
2608cebb063Sartembunichevand
2618cebb063Sartembunichev.Dv O_EXLOCK
262*e2dd73cfSKonstantin Belousovbelow.
2638cebb063Sartembunichev.Pp
2648269e767SBrooks DavisIf
2658269e767SBrooks Davis.Dv O_EXCL
2668269e767SBrooks Davisis set and the last component of the pathname is
2678269e767SBrooks Davisa symbolic link,
2688269e767SBrooks Davis.Fn open
2698269e767SBrooks Daviswill fail even if the symbolic
2708269e767SBrooks Davislink points to a non-existent name.
271826509a3SJohn Baldwin.Pp
272826509a3SJohn BaldwinIf
2738269e767SBrooks Davis.Dv O_NONBLOCK
274826509a3SJohn Baldwinis specified and the
2758269e767SBrooks Davis.Fn open
276826509a3SJohn Baldwinsystem call would
277826509a3SJohn Baldwinblock for some reason (for example, waiting for
2788269e767SBrooks Daviscarrier on a dialup line),
2798269e767SBrooks Davis.Fn open
2808269e767SBrooks Davisreturns immediately.
2818269e767SBrooks DavisThe descriptor remains in non-blocking mode for subsequent operations.
2828269e767SBrooks Davis.Pp
2838269e767SBrooks DavisIf
2848269e767SBrooks Davis.Dv O_SYNC
2858269e767SBrooks Davisis used in the mask, all writes will
2868269e767SBrooks Davisimmediately and synchronously be written to disk.
2878269e767SBrooks Davis.Dv O_FSYNC
2888269e767SBrooks Davisis an historical synonym for
2898269e767SBrooks Davis.Dv O_SYNC .
2908269e767SBrooks Davis.Pp
2918269e767SBrooks DavisIf
2928269e767SBrooks Davis.Dv O_DSYNC
2938269e767SBrooks Davisis used in the mask, all data and metadata required to read the data will be
2948269e767SBrooks Davissynchronously written to disk, but changes to metadata such as file access and
2958269e767SBrooks Davismodification timestamps may be written later.
2968269e767SBrooks Davis.Pp
2978269e767SBrooks DavisIf
2988269e767SBrooks Davis.Dv O_NOFOLLOW
2998269e767SBrooks Davisis used in the mask and the target file passed to
3008269e767SBrooks Davis.Fn open
3018269e767SBrooks Davisis a symbolic link then the
3028269e767SBrooks Davis.Fn open
3038269e767SBrooks Daviswill fail.
3048269e767SBrooks Davis.Pp
3058269e767SBrooks DavisWhen opening a file, a lock with
3068269e767SBrooks Davis.Xr flock 2
3078269e767SBrooks Davissemantics can be obtained by setting
3088269e767SBrooks Davis.Dv O_SHLOCK
3098269e767SBrooks Davisfor a shared lock, or
3108269e767SBrooks Davis.Dv O_EXLOCK
3118269e767SBrooks Davisfor an exclusive lock.
3128269e767SBrooks Davis.Pp
3138269e767SBrooks Davis.Dv O_DIRECT
3148269e767SBrooks Davismay be used to minimize or eliminate the cache effects of reading and writing.
3158269e767SBrooks DavisThe system will attempt to avoid caching the data you read or write.
3168269e767SBrooks DavisIf it cannot avoid caching the data,
3178269e767SBrooks Davisit will minimize the impact the data has on the cache.
3188269e767SBrooks DavisUse of this flag can drastically reduce performance if not used with care.
319826509a3SJohn BaldwinThe semantics of this flag are filesystem dependent,
320826509a3SJohn Baldwinand some filesystems may ignore it entirely.
3218269e767SBrooks Davis.Pp
3228269e767SBrooks Davis.Dv O_NOCTTY
3238269e767SBrooks Davismay be used to ensure the OS does not assign this file as the
3248269e767SBrooks Daviscontrolling terminal when it opens a tty device.
3258269e767SBrooks DavisThis is the default on
3268269e767SBrooks Davis.Fx ,
3278269e767SBrooks Davisbut is present for
328f5a37f8dSJose Luis DuranPOSIX
3298269e767SBrooks Daviscompatibility.
3308269e767SBrooks DavisThe
3318269e767SBrooks Davis.Fn open
3328269e767SBrooks Davissystem call will not assign controlling terminals on
3338269e767SBrooks Davis.Fx .
3348269e767SBrooks Davis.Pp
3358269e767SBrooks Davis.Dv O_TTY_INIT
3368269e767SBrooks Davismay be used to ensure the OS restores the terminal attributes when
3378269e767SBrooks Davisinitially opening a TTY.
3388269e767SBrooks DavisThis is the default on
3398269e767SBrooks Davis.Fx ,
3408269e767SBrooks Davisbut is present for
341f5a37f8dSJose Luis DuranPOSIX
3428269e767SBrooks Daviscompatibility.
3438269e767SBrooks DavisThe initial call to
3448269e767SBrooks Davis.Fn open
3458269e767SBrooks Davison a TTY will always restore default terminal attributes on
3468269e767SBrooks Davis.Fx .
3478269e767SBrooks Davis.Pp
3488269e767SBrooks Davis.Dv O_DIRECTORY
3498269e767SBrooks Davismay be used to ensure the resulting file descriptor refers to a
3508269e767SBrooks Davisdirectory.
3518269e767SBrooks DavisThis flag can be used to prevent applications with elevated privileges
3528269e767SBrooks Davisfrom opening files which are even unsafe to open with
3538269e767SBrooks Davis.Dv O_RDONLY ,
3548269e767SBrooks Davissuch as device nodes.
3558269e767SBrooks Davis.Pp
3568269e767SBrooks Davis.Dv O_CLOEXEC
3578269e767SBrooks Davismay be used to set
3588269e767SBrooks Davis.Dv FD_CLOEXEC
3598269e767SBrooks Davisflag for the newly returned file descriptor.
3608269e767SBrooks Davis.Pp
3618269e767SBrooks Davis.Dv O_VERIFY
3628269e767SBrooks Davismay be used to indicate to the kernel that the contents of the file should
3638269e767SBrooks Davisbe verified before allowing the open to proceed.
3648269e767SBrooks DavisThe details of what
3658269e767SBrooks Davis.Dq verified
3668269e767SBrooks Davismeans is implementation specific.
3678269e767SBrooks DavisThe run-time linker (rtld) uses this flag to ensure shared objects have
3688269e767SBrooks Davisbeen verified before operating on them.
3698269e767SBrooks Davis.Pp
3708269e767SBrooks Davis.Dv O_RESOLVE_BENEATH
3718269e767SBrooks Davisreturns
3728269e767SBrooks Davis.Er ENOTCAPABLE
3738269e767SBrooks Davisif any intermediate component of the specified relative path does not
3748269e767SBrooks Davisreside in the directory hierarchy beneath the starting directory.
3758269e767SBrooks DavisAbsolute paths or even the temporal escape from beneath of the starting
3768269e767SBrooks Davisdirectory is not allowed.
3778269e767SBrooks Davis.Pp
378826509a3SJohn BaldwinWhen a directory
3798269e767SBrooks Davisis opened with
3808269e767SBrooks Davis.Dv O_SEARCH ,
3818269e767SBrooks Davisexecute permissions are checked at open time.
382826509a3SJohn BaldwinThe returned file descriptor
3838269e767SBrooks Davismay not be used for any read operations like
3848269e767SBrooks Davis.Xr getdirentries 2 .
385826509a3SJohn BaldwinThe primary use of this descriptor is as the lookup descriptor for the
3868269e767SBrooks Davis.Fn *at
3878269e767SBrooks Davisfamily of functions.
3888269e767SBrooks DavisIf
3898269e767SBrooks Davis.Dv O_SEARCH
3908269e767SBrooks Daviswas not requested at open time, then the
3918269e767SBrooks Davis.Fn *at
3928269e767SBrooks Davisfunctions use the current directory permissions for the directory referenced
393826509a3SJohn Baldwinby the descriptor at the time of the
394826509a3SJohn Baldwin.Fn *at
395826509a3SJohn Baldwincall.
3968269e767SBrooks Davis.Pp
3978269e767SBrooks Davis.Dv O_PATH
3988269e767SBrooks Davisreturns a file descriptor that can be used as a directory file descriptor for
399d3de1bd4SChristopher Davidson.Fn openat
4008269e767SBrooks Davisand other system calls taking a file descriptor argument, like
4018269e767SBrooks Davis.Xr fstatat 2
4028269e767SBrooks Davisand others.
4038269e767SBrooks DavisThe other functionality of the returned file descriptor is limited to
404826509a3SJohn Baldwinthe following descriptor-level operations:
405826509a3SJohn Baldwin.Pp
406826509a3SJohn Baldwin.Bl -tag -width __acl_aclcheck_fd -offset indent -compact
4078269e767SBrooks Davis.It Xr fcntl 2
4088269e767SBrooks Davisbut advisory locking is not allowed
4098269e767SBrooks Davis.It Xr dup 2
4108269e767SBrooks Davis.It Xr close 2
4118269e767SBrooks Davis.It Xr fstat 2
412c8141728SCismonX.It Xr fstatfs 2
413c8141728SCismonX.It Xr fchdir 2
414c8141728SCismonX.It Xr fchroot 2
4158269e767SBrooks Davis.It Xr fexecve 2
416c8141728SCismonX.It Xr funlinkat 2
417c8141728SCismonXcan be passed as the third argument
4188269e767SBrooks Davis.It Dv SCM_RIGHTS
4198269e767SBrooks Daviscan be passed over a
4208269e767SBrooks Davis.Xr unix 4
4218269e767SBrooks Davissocket using a
4228269e767SBrooks Davis.Dv SCM_RIGHTS
4238269e767SBrooks Davismessage
4248269e767SBrooks Davis.It Xr kqueue 2
425826509a3SJohn Baldwinonly with
4268269e767SBrooks Davis.Dv EVFILT_VNODE
427826509a3SJohn Baldwin.It Xr __acl_get_fd 2
428826509a3SJohn Baldwin.It Xr __acl_aclcheck_fd 2
429c8141728SCismonX.It Xr extattr 2
430c8141728SCismonX.It Xr capsicum 4
431c8141728SCismonXcan be passed to
432c8141728SCismonX.Fn cap_*_limit
433c8141728SCismonXand
434c8141728SCismonX.Fn cap_*_get
435c8141728SCismonXsystem calls (such as
436c8141728SCismonX.Xr cap_rights_limit 2 ) .
4378269e767SBrooks Davis.El
438826509a3SJohn Baldwin.Pp
439826509a3SJohn BaldwinOther operations like
4408269e767SBrooks Davis.Xr read 2 ,
4418269e767SBrooks Davis.Xr ftruncate 2 ,
4428269e767SBrooks Davisand any other that operate on file and not on file descriptor (except
4438269e767SBrooks Davis.Xr fstat 2 ) ,
4448269e767SBrooks Davisare not allowed.
4458269e767SBrooks Davis.Pp
4468269e767SBrooks DavisA file descriptor created with the
4478269e767SBrooks Davis.Dv O_PATH
448826509a3SJohn Baldwinflag can be opened as a normal (operable) file descriptor by
4498269e767SBrooks Davisspecifying it as the
4508269e767SBrooks Davis.Fa fd
4518269e767SBrooks Davisargument to
4528269e767SBrooks Davis.Fn openat
453826509a3SJohn Baldwinwith an empty
4548269e767SBrooks Davis.Fa path
455826509a3SJohn Baldwinand the
456826509a3SJohn Baldwin.Dv O_EMPTY_PATH
457826509a3SJohn Baldwinflag.
4588269e767SBrooks DavisSuch an open behaves as if the current path of the file referenced by
4598269e767SBrooks Davis.Fa fd
460826509a3SJohn Baldwinis passed, except that path walk permissions are not checked.
4618269e767SBrooks DavisSee also the description of
4628269e767SBrooks Davis.Dv AT_EMPTY_PATH
4638269e767SBrooks Davisflag for
4648269e767SBrooks Davis.Xr fstatat 2
4658269e767SBrooks Davisand related syscalls.
4668269e767SBrooks Davis.Pp
4678269e767SBrooks DavisIf successful,
4688269e767SBrooks Davis.Fn open
4698269e767SBrooks Davisreturns a non-negative integer, termed a file descriptor.
4708269e767SBrooks DavisIt returns \-1 on failure.
471826509a3SJohn BaldwinThe file descriptor value returned is the lowest numbered descriptor
472826509a3SJohn Baldwincurrently not in use by the process.
4738269e767SBrooks DavisThe file pointer used to mark the current position within the
4748269e767SBrooks Davisfile is set to the beginning of the file.
4758269e767SBrooks Davis.Pp
4768269e767SBrooks DavisIf a sleeping open of a device node from
47737bb9884SKonstantin Belousov.Xr devfs 4
4788269e767SBrooks Davisis interrupted by a signal, the call always fails with
4798269e767SBrooks Davis.Er EINTR ,
4808269e767SBrooks Daviseven if the
4818269e767SBrooks Davis.Dv SA_RESTART
4828269e767SBrooks Davisflag is set for the signal.
4838269e767SBrooks DavisA sleeping open of a fifo (see
4848269e767SBrooks Davis.Xr mkfifo 2 )
4858269e767SBrooks Davisis restarted as normal.
4868269e767SBrooks Davis.Pp
487826509a3SJohn BaldwinWhen a new file is created, it is assigned the group of the directory
4888269e767SBrooks Daviswhich contains it.
4898269e767SBrooks Davis.Pp
4908269e767SBrooks DavisUnless
4918269e767SBrooks Davis.Dv O_CLOEXEC
4928269e767SBrooks Davisflag was specified,
4938269e767SBrooks Davisthe new descriptor is set to remain open across
4948269e767SBrooks Davis.Xr execve 2
4958269e767SBrooks Davissystem calls; see
4968269e767SBrooks Davis.Xr close 2 ,
4978269e767SBrooks Davis.Xr fcntl 2
498826509a3SJohn Baldwinand the description of the
4998269e767SBrooks Davis.Dv O_CLOEXEC
500826509a3SJohn Baldwinflag.
5018269e767SBrooks Davis.Pp
5028269e767SBrooks DavisThe system imposes a limit on the number of file descriptors
5038269e767SBrooks Davisopen simultaneously by one process.
5048269e767SBrooks DavisThe
5058269e767SBrooks Davis.Xr getdtablesize 2
5068269e767SBrooks Davissystem call returns the current system limit.
5078269e767SBrooks Davis.Sh RETURN VALUES
5088269e767SBrooks DavisIf successful,
5098269e767SBrooks Davis.Fn open
5108269e767SBrooks Davisand
5118269e767SBrooks Davis.Fn openat
5128269e767SBrooks Davisreturn a non-negative integer, termed a file descriptor.
5138269e767SBrooks DavisThey return \-1 on failure, and set
5148269e767SBrooks Davis.Va errno
5158269e767SBrooks Davisto indicate the error.
5168269e767SBrooks Davis.Sh ERRORS
5178269e767SBrooks DavisThe named file is opened unless:
5188269e767SBrooks Davis.Bl -tag -width Er
5198269e767SBrooks Davis.It Bq Er ENOTDIR
5208269e767SBrooks DavisA component of the path prefix is not a directory.
5218269e767SBrooks Davis.It Bq Er ENAMETOOLONG
5228269e767SBrooks DavisA component of a pathname exceeded 255 characters,
5238269e767SBrooks Davisor an entire path name exceeded 1023 characters.
5248269e767SBrooks Davis.It Bq Er ENOENT
5258269e767SBrooks Davis.Dv O_CREAT
5268269e767SBrooks Davisis not set and the named file does not exist.
5278269e767SBrooks Davis.It Bq Er ENOENT
5288269e767SBrooks DavisA component of the path name that must exist does not exist.
5298269e767SBrooks Davis.It Bq Er EACCES
5308269e767SBrooks DavisSearch permission is denied for a component of the path prefix.
5318269e767SBrooks Davis.It Bq Er EACCES
5328269e767SBrooks DavisThe required permissions (for reading and/or writing)
5338269e767SBrooks Davisare denied for the given flags.
5348269e767SBrooks Davis.It Bq Er EACCES
5358269e767SBrooks Davis.Dv O_TRUNC
5368269e767SBrooks Davisis specified and write permission is denied.
5378269e767SBrooks Davis.It Bq Er EACCES
5388269e767SBrooks Davis.Dv O_CREAT
5398269e767SBrooks Davisis specified,
5408269e767SBrooks Davisthe file does not exist,
5418269e767SBrooks Davisand the directory in which it is to be created
5428269e767SBrooks Davisdoes not permit writing.
5438269e767SBrooks Davis.It Bq Er EPERM
5448269e767SBrooks Davis.Dv O_CREAT
5458269e767SBrooks Davisis specified, the file does not exist, and the directory in which it is to be
5468269e767SBrooks Daviscreated has its immutable flag set, see the
5478269e767SBrooks Davis.Xr chflags 2
5488269e767SBrooks Davismanual page for more information.
5498269e767SBrooks Davis.It Bq Er EPERM
5508269e767SBrooks DavisThe named file has its immutable flag set and the file is to be modified.
5518269e767SBrooks Davis.It Bq Er EPERM
5528269e767SBrooks DavisThe named file has its append-only flag set, the file is to be modified, and
5538269e767SBrooks Davis.Dv O_TRUNC
5548269e767SBrooks Davisis specified or
5558269e767SBrooks Davis.Dv O_APPEND
5568269e767SBrooks Davisis not specified.
5578269e767SBrooks Davis.It Bq Er ELOOP
5588269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname.
5598269e767SBrooks Davis.It Bq Er EISDIR
5608269e767SBrooks DavisThe named file is a directory, and the arguments specify
5618269e767SBrooks Davisit is to be modified.
5628269e767SBrooks Davis.It Bq Er EISDIR
5638269e767SBrooks DavisThe named file is a directory, and the flags specified
5648269e767SBrooks Davis.Dv O_CREAT
5658269e767SBrooks Daviswithout
5668269e767SBrooks Davis.Dv O_DIRECTORY .
5678269e767SBrooks Davis.It Bq Er EROFS
5688269e767SBrooks DavisThe named file resides on a read-only file system,
5698269e767SBrooks Davisand the file is to be modified.
5708269e767SBrooks Davis.It Bq Er EROFS
5718269e767SBrooks Davis.Dv O_CREAT
5728269e767SBrooks Davisis specified and the named file would reside on a read-only file system.
5738269e767SBrooks Davis.It Bq Er EMFILE
5748269e767SBrooks DavisThe process has already reached its limit for open file descriptors.
5758269e767SBrooks Davis.It Bq Er ENFILE
5768269e767SBrooks DavisThe system file table is full.
5778269e767SBrooks Davis.It Bq Er EMLINK
5788269e767SBrooks Davis.Dv O_NOFOLLOW
5798269e767SBrooks Daviswas specified and the target is a symbolic link.
580f5a37f8dSJose Luis DuranPOSIX
5818269e767SBrooks Davisspecifies a different error for this case; see the note in
5828269e767SBrooks Davis.Sx STANDARDS
5838269e767SBrooks Davisbelow.
5848269e767SBrooks Davis.It Bq Er ENXIO
5858269e767SBrooks DavisThe named file is a character special or block
5868269e767SBrooks Davisspecial file, and the device associated with this special file
5878269e767SBrooks Davisdoes not exist.
5888269e767SBrooks Davis.It Bq Er ENXIO
5898269e767SBrooks Davis.Dv O_NONBLOCK
5908269e767SBrooks Davisis set, the named file is a fifo,
5918269e767SBrooks Davis.Dv O_WRONLY
5928269e767SBrooks Davisis set, and no process has the file open for reading.
5938269e767SBrooks Davis.It Bq Er EINTR
5948269e767SBrooks DavisThe
5958269e767SBrooks Davis.Fn open
5968269e767SBrooks Davisoperation was interrupted by a signal.
5978269e767SBrooks Davis.It Bq Er EOPNOTSUPP
5988269e767SBrooks Davis.Dv O_SHLOCK
5998269e767SBrooks Davisor
6008269e767SBrooks Davis.Dv O_EXLOCK
6018269e767SBrooks Davisis specified but the underlying file system does not support locking.
6028269e767SBrooks Davis.It Bq Er EOPNOTSUPP
6038269e767SBrooks DavisThe named file is a special file mounted through a file system that
604826509a3SJohn Baldwindoes not support access to it (for example, NFS).
6058269e767SBrooks Davis.It Bq Er EWOULDBLOCK
6068269e767SBrooks Davis.Dv O_NONBLOCK
6078269e767SBrooks Davisand one of
6088269e767SBrooks Davis.Dv O_SHLOCK
6098269e767SBrooks Davisor
6108269e767SBrooks Davis.Dv O_EXLOCK
6118269e767SBrooks Davisis specified and the file is locked.
6128269e767SBrooks Davis.It Bq Er ENOSPC
6138269e767SBrooks Davis.Dv O_CREAT
6148269e767SBrooks Davisis specified,
6158269e767SBrooks Davisthe file does not exist,
6168269e767SBrooks Davisand the directory in which the entry for the new file is being placed
6178269e767SBrooks Daviscannot be extended because there is no space left on the file
6188269e767SBrooks Davissystem containing the directory.
6198269e767SBrooks Davis.It Bq Er ENOSPC
6208269e767SBrooks Davis.Dv O_CREAT
6218269e767SBrooks Davisis specified,
6228269e767SBrooks Davisthe file does not exist,
6238269e767SBrooks Davisand there are no free inodes on the file system on which the
6248269e767SBrooks Davisfile is being created.
6258269e767SBrooks Davis.It Bq Er EDQUOT
6268269e767SBrooks Davis.Dv O_CREAT
6278269e767SBrooks Davisis specified,
6288269e767SBrooks Davisthe file does not exist,
6298269e767SBrooks Davisand the directory in which the entry for the new file
6308269e767SBrooks Davisis being placed cannot be extended because the
6318269e767SBrooks Davisuser's quota of disk blocks on the file system
6328269e767SBrooks Daviscontaining the directory has been exhausted.
6338269e767SBrooks Davis.It Bq Er EDQUOT
6348269e767SBrooks Davis.Dv O_CREAT
6358269e767SBrooks Davisis specified,
6368269e767SBrooks Davisthe file does not exist,
6378269e767SBrooks Davisand the user's quota of inodes on the file system on
6388269e767SBrooks Daviswhich the file is being created has been exhausted.
6398269e767SBrooks Davis.It Bq Er EIO
6408269e767SBrooks DavisAn I/O error occurred while making the directory entry or
6418269e767SBrooks Davisallocating the inode for
6428269e767SBrooks Davis.Dv O_CREAT .
6438269e767SBrooks Davis.It Bq Er EINTEGRITY
6448269e767SBrooks DavisCorrupted data was detected while reading from the file system.
6458269e767SBrooks Davis.It Bq Er ETXTBSY
6468269e767SBrooks DavisThe file is a pure procedure (shared text) file that is being
6478269e767SBrooks Davisexecuted and the
6488269e767SBrooks Davis.Fn open
6498269e767SBrooks Davissystem call requests write access.
6508269e767SBrooks Davis.It Bq Er EFAULT
6518269e767SBrooks DavisThe
6528269e767SBrooks Davis.Fa path
6538269e767SBrooks Davisargument
6548269e767SBrooks Davispoints outside the process's allocated address space.
6558269e767SBrooks Davis.It Bq Er EEXIST
6568269e767SBrooks Davis.Dv O_CREAT
6578269e767SBrooks Davisand
6588269e767SBrooks Davis.Dv O_EXCL
6598269e767SBrooks Daviswere specified and the file exists.
6608269e767SBrooks Davis.It Bq Er EOPNOTSUPP
6618269e767SBrooks DavisAn attempt was made to open a socket (not currently implemented).
6628269e767SBrooks Davis.It Bq Er EINVAL
6638269e767SBrooks DavisAn attempt was made to open a descriptor with an illegal combination
6648269e767SBrooks Davisof
6658269e767SBrooks Davis.Dv O_RDONLY ,
6668269e767SBrooks Davis.Dv O_WRONLY ,
6678269e767SBrooks Davisor
6688269e767SBrooks Davis.Dv O_RDWR ,
6698269e767SBrooks Davisand
6708269e767SBrooks Davis.Dv O_EXEC
6718269e767SBrooks Davisor
6728269e767SBrooks Davis.Dv O_SEARCH .
67302c23c3bSJose Luis Duran.It Bq Er EINVAL
67402c23c3bSJose Luis Duran.Dv O_CREAT
67502c23c3bSJose Luis Duranis specified,
67602c23c3bSJose Luis Duranand the last component of the
67702c23c3bSJose Luis Duran.Fa path
67802c23c3bSJose Luis Duranargument is invalid on the file system on which the file is being created.
6798269e767SBrooks Davis.It Bq Er EBADF
6808269e767SBrooks DavisThe
6818269e767SBrooks Davis.Fa path
6828269e767SBrooks Davisargument does not specify an absolute path and the
6838269e767SBrooks Davis.Fa fd
6848269e767SBrooks Davisargument is
6858269e767SBrooks Davisneither
6868269e767SBrooks Davis.Dv AT_FDCWD
6878269e767SBrooks Davisnor a valid file descriptor open for searching.
6888269e767SBrooks Davis.It Bq Er ENOTDIR
6898269e767SBrooks DavisThe
6908269e767SBrooks Davis.Fa path
6918269e767SBrooks Davisargument is not an absolute path and
6928269e767SBrooks Davis.Fa fd
6938269e767SBrooks Davisis neither
6948269e767SBrooks Davis.Dv AT_FDCWD
6958269e767SBrooks Davisnor a file descriptor associated with a directory.
6968269e767SBrooks Davis.It Bq Er ENOTDIR
6978269e767SBrooks Davis.Dv O_DIRECTORY
6988269e767SBrooks Davisis specified and the file is not a directory.
6998269e767SBrooks Davis.It Bq Er ECAPMODE
7008269e767SBrooks Davis.Dv AT_FDCWD
7018269e767SBrooks Davisis specified and the process is in capability mode.
7028269e767SBrooks Davis.It Bq Er ECAPMODE
7038269e767SBrooks Davis.Fn open
7048269e767SBrooks Daviswas called and the process is in capability mode.
7058269e767SBrooks Davis.It Bq Er ENOTCAPABLE
7068269e767SBrooks Davis.Fa path
7078269e767SBrooks Davisis an absolute path and the process is in capability mode.
7088269e767SBrooks Davis.It Bq Er ENOTCAPABLE
7098269e767SBrooks Davis.Fa path
7108269e767SBrooks Davisis an absolute path and
7118269e767SBrooks Davis.Dv O_RESOLVE_BENEATH
7128269e767SBrooks Davisis specified.
7138269e767SBrooks Davis.It Bq Er ENOTCAPABLE
7148269e767SBrooks Davis.Fa path
7158269e767SBrooks Daviscontains a ".." component leading to a directory outside
7168269e767SBrooks Davisof the directory hierarchy specified by
7178269e767SBrooks Davis.Fa fd
7188269e767SBrooks Davisand the process is in capability mode.
7198269e767SBrooks Davis.It Bq Er ENOTCAPABLE
7208269e767SBrooks Davis.Fa path
7218269e767SBrooks Daviscontains a ".." component leading to a directory outside
7228269e767SBrooks Davisof the directory hierarchy specified by
7238269e767SBrooks Davis.Fa fd
7248269e767SBrooks Davisand
7258269e767SBrooks Davis.Dv O_RESOLVE_BENEATH
7268269e767SBrooks Davisis specified.
7278269e767SBrooks Davis.It Bq Er ENOTCAPABLE
7288269e767SBrooks Davis.Fa path
7298269e767SBrooks Daviscontains a ".." component, the
7308269e767SBrooks Davis.Dv vfs.lookup_cap_dotdot
7318269e767SBrooks Davis.Xr sysctl 3
7328269e767SBrooks Davisis set, and the process is in capability mode.
7338269e767SBrooks Davis.El
7348269e767SBrooks Davis.Sh SEE ALSO
7358269e767SBrooks Davis.Xr chmod 2 ,
7368269e767SBrooks Davis.Xr close 2 ,
7378269e767SBrooks Davis.Xr dup 2 ,
7388269e767SBrooks Davis.Xr fexecve 2 ,
7398269e767SBrooks Davis.Xr fhopen 2 ,
7408269e767SBrooks Davis.Xr getdtablesize 2 ,
7418269e767SBrooks Davis.Xr getfh 2 ,
7428269e767SBrooks Davis.Xr lgetfh 2 ,
7438269e767SBrooks Davis.Xr lseek 2 ,
7448269e767SBrooks Davis.Xr read 2 ,
7458269e767SBrooks Davis.Xr umask 2 ,
7468269e767SBrooks Davis.Xr write 2 ,
7478269e767SBrooks Davis.Xr fopen 3 ,
7488269e767SBrooks Davis.Xr capsicum 4
7498269e767SBrooks Davis.Sh STANDARDS
7508269e767SBrooks DavisThese functions are specified by
7518269e767SBrooks Davis.St -p1003.1-2008 .
7528269e767SBrooks Davis.Pp
7538269e767SBrooks Davis.Fx
7548269e767SBrooks Davissets
7558269e767SBrooks Davis.Va errno
7568269e767SBrooks Davisto
7578269e767SBrooks Davis.Er EMLINK instead of
7588269e767SBrooks Davis.Er ELOOP
7598269e767SBrooks Davisas specified by
760f5a37f8dSJose Luis DuranPOSIX
7618269e767SBrooks Daviswhen
7628269e767SBrooks Davis.Dv O_NOFOLLOW
7638269e767SBrooks Davisis set in flags and the final component of pathname is a symbolic link
7648269e767SBrooks Davisto distinguish it from the case of too many symbolic link traversals
7658269e767SBrooks Davisin one of its non-final components.
7668269e767SBrooks Davis.Pp
7678269e767SBrooks DavisThe Open Group Extended API Set 2 specification, that introduced the
7688269e767SBrooks Davis.Fn *at
7698269e767SBrooks DavisAPI, required that the test for whether
7708269e767SBrooks Davis.Fa fd
7718269e767SBrooks Davisis searchable is based on whether
7728269e767SBrooks Davis.Fa fd
7738269e767SBrooks Davisis open for searching, not whether the underlying directory currently
7748269e767SBrooks Davispermits searches.
7758269e767SBrooks DavisThe present implementation of the
7768269e767SBrooks Davis.Fa openat
7778269e767SBrooks Davissystem call is believed to be compatible with
778f5a37f8dSJose Luis Duran.\" .St -p1003.1-2017 ,
779f5a37f8dSJose Luis Duran.\" XXX: This should be replaced in the future when an appropriate argument to
780f5a37f8dSJose Luis Duran.\" the St macro is available: -p1003.1-2017
781826509a3SJohn Baldwin.No IEEE Std 1003.1-2008, 2017 Edition ("POSIX.1") ,
7828269e767SBrooks Daviswhich specifies that behavior for
7838269e767SBrooks Davis.Dv O_SEARCH ,
7848269e767SBrooks Davisin the absence of the flag the implementation checks the current
7858269e767SBrooks Davispermissions of a directory.
7868269e767SBrooks Davis.Sh HISTORY
7878269e767SBrooks DavisThe
7888269e767SBrooks Davis.Fn open
7898269e767SBrooks Davisfunction appeared in
7908269e767SBrooks Davis.At v1 .
7918269e767SBrooks DavisThe
7928269e767SBrooks Davis.Fn openat
7938269e767SBrooks Davisfunction was introduced in
7948269e767SBrooks Davis.Fx 8.0 .
7958269e767SBrooks Davis.Dv O_DSYNC
7968269e767SBrooks Davisappeared in 13.0.
7978269e767SBrooks Davis.Sh BUGS
7988269e767SBrooks DavisThe
7998269e767SBrooks Davis.Fa mode
8008269e767SBrooks Davisargument is variadic and may result in different calling conventions
8018269e767SBrooks Davisthan might otherwise be expected.
802