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.\" 28826509a3SJohn Baldwin.Dd January 3, 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_EXCL 2358269e767SBrooks Davisis set with 2368269e767SBrooks Davis.Dv O_CREAT 2378269e767SBrooks Davisand the file already 2388269e767SBrooks Davisexists, 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. 2448269e767SBrooks DavisIf 2458269e767SBrooks Davis.Dv O_EXCL 2468269e767SBrooks Davisis set and the last component of the pathname is 2478269e767SBrooks Davisa symbolic link, 2488269e767SBrooks Davis.Fn open 2498269e767SBrooks Daviswill fail even if the symbolic 2508269e767SBrooks Davislink points to a non-existent name. 251826509a3SJohn Baldwin.Pp 252826509a3SJohn BaldwinIf 2538269e767SBrooks Davis.Dv O_NONBLOCK 254826509a3SJohn Baldwinis specified and the 2558269e767SBrooks Davis.Fn open 256826509a3SJohn Baldwinsystem call would 257826509a3SJohn Baldwinblock for some reason (for example, waiting for 2588269e767SBrooks Daviscarrier on a dialup line), 2598269e767SBrooks Davis.Fn open 2608269e767SBrooks Davisreturns immediately. 2618269e767SBrooks DavisThe descriptor remains in non-blocking mode for subsequent operations. 2628269e767SBrooks Davis.Pp 2638269e767SBrooks DavisIf 2648269e767SBrooks Davis.Dv O_SYNC 2658269e767SBrooks Davisis used in the mask, all writes will 2668269e767SBrooks Davisimmediately and synchronously be written to disk. 2678269e767SBrooks Davis.Dv O_FSYNC 2688269e767SBrooks Davisis an historical synonym for 2698269e767SBrooks Davis.Dv O_SYNC . 2708269e767SBrooks Davis.Pp 2718269e767SBrooks DavisIf 2728269e767SBrooks Davis.Dv O_DSYNC 2738269e767SBrooks Davisis used in the mask, all data and metadata required to read the data will be 2748269e767SBrooks Davissynchronously written to disk, but changes to metadata such as file access and 2758269e767SBrooks Davismodification timestamps may be written later. 2768269e767SBrooks Davis.Pp 2778269e767SBrooks DavisIf 2788269e767SBrooks Davis.Dv O_NOFOLLOW 2798269e767SBrooks Davisis used in the mask and the target file passed to 2808269e767SBrooks Davis.Fn open 2818269e767SBrooks Davisis a symbolic link then the 2828269e767SBrooks Davis.Fn open 2838269e767SBrooks Daviswill fail. 2848269e767SBrooks Davis.Pp 2858269e767SBrooks DavisWhen opening a file, a lock with 2868269e767SBrooks Davis.Xr flock 2 2878269e767SBrooks Davissemantics can be obtained by setting 2888269e767SBrooks Davis.Dv O_SHLOCK 2898269e767SBrooks Davisfor a shared lock, or 2908269e767SBrooks Davis.Dv O_EXLOCK 2918269e767SBrooks Davisfor an exclusive lock. 2928269e767SBrooks DavisIf creating a file with 2938269e767SBrooks Davis.Dv O_CREAT , 2948269e767SBrooks Davisthe request for the lock will never fail 2958269e767SBrooks Davis(provided that the underlying file system supports locking). 2968269e767SBrooks Davis.Pp 2978269e767SBrooks Davis.Dv O_DIRECT 2988269e767SBrooks Davismay be used to minimize or eliminate the cache effects of reading and writing. 2998269e767SBrooks DavisThe system will attempt to avoid caching the data you read or write. 3008269e767SBrooks DavisIf it cannot avoid caching the data, 3018269e767SBrooks Davisit will minimize the impact the data has on the cache. 3028269e767SBrooks DavisUse of this flag can drastically reduce performance if not used with care. 303826509a3SJohn BaldwinThe semantics of this flag are filesystem dependent, 304826509a3SJohn Baldwinand some filesystems may ignore it entirely. 3058269e767SBrooks Davis.Pp 3068269e767SBrooks Davis.Dv O_NOCTTY 3078269e767SBrooks Davismay be used to ensure the OS does not assign this file as the 3088269e767SBrooks Daviscontrolling terminal when it opens a tty device. 3098269e767SBrooks DavisThis is the default on 3108269e767SBrooks Davis.Fx , 3118269e767SBrooks Davisbut is present for 312f5a37f8dSJose Luis DuranPOSIX 3138269e767SBrooks Daviscompatibility. 3148269e767SBrooks DavisThe 3158269e767SBrooks Davis.Fn open 3168269e767SBrooks Davissystem call will not assign controlling terminals on 3178269e767SBrooks Davis.Fx . 3188269e767SBrooks Davis.Pp 3198269e767SBrooks Davis.Dv O_TTY_INIT 3208269e767SBrooks Davismay be used to ensure the OS restores the terminal attributes when 3218269e767SBrooks Davisinitially opening a TTY. 3228269e767SBrooks DavisThis is the default on 3238269e767SBrooks Davis.Fx , 3248269e767SBrooks Davisbut is present for 325f5a37f8dSJose Luis DuranPOSIX 3268269e767SBrooks Daviscompatibility. 3278269e767SBrooks DavisThe initial call to 3288269e767SBrooks Davis.Fn open 3298269e767SBrooks Davison a TTY will always restore default terminal attributes on 3308269e767SBrooks Davis.Fx . 3318269e767SBrooks Davis.Pp 3328269e767SBrooks Davis.Dv O_DIRECTORY 3338269e767SBrooks Davismay be used to ensure the resulting file descriptor refers to a 3348269e767SBrooks Davisdirectory. 3358269e767SBrooks DavisThis flag can be used to prevent applications with elevated privileges 3368269e767SBrooks Davisfrom opening files which are even unsafe to open with 3378269e767SBrooks Davis.Dv O_RDONLY , 3388269e767SBrooks Davissuch as device nodes. 3398269e767SBrooks Davis.Pp 3408269e767SBrooks Davis.Dv O_CLOEXEC 3418269e767SBrooks Davismay be used to set 3428269e767SBrooks Davis.Dv FD_CLOEXEC 3438269e767SBrooks Davisflag for the newly returned file descriptor. 3448269e767SBrooks Davis.Pp 3458269e767SBrooks Davis.Dv O_VERIFY 3468269e767SBrooks Davismay be used to indicate to the kernel that the contents of the file should 3478269e767SBrooks Davisbe verified before allowing the open to proceed. 3488269e767SBrooks DavisThe details of what 3498269e767SBrooks Davis.Dq verified 3508269e767SBrooks Davismeans is implementation specific. 3518269e767SBrooks DavisThe run-time linker (rtld) uses this flag to ensure shared objects have 3528269e767SBrooks Davisbeen verified before operating on them. 3538269e767SBrooks Davis.Pp 3548269e767SBrooks Davis.Dv O_RESOLVE_BENEATH 3558269e767SBrooks Davisreturns 3568269e767SBrooks Davis.Er ENOTCAPABLE 3578269e767SBrooks Davisif any intermediate component of the specified relative path does not 3588269e767SBrooks Davisreside in the directory hierarchy beneath the starting directory. 3598269e767SBrooks DavisAbsolute paths or even the temporal escape from beneath of the starting 3608269e767SBrooks Davisdirectory is not allowed. 3618269e767SBrooks Davis.Pp 362826509a3SJohn BaldwinWhen a directory 3638269e767SBrooks Davisis opened with 3648269e767SBrooks Davis.Dv O_SEARCH , 3658269e767SBrooks Davisexecute permissions are checked at open time. 366826509a3SJohn BaldwinThe returned file descriptor 3678269e767SBrooks Davismay not be used for any read operations like 3688269e767SBrooks Davis.Xr getdirentries 2 . 369826509a3SJohn BaldwinThe primary use of this descriptor is as the lookup descriptor for the 3708269e767SBrooks Davis.Fn *at 3718269e767SBrooks Davisfamily of functions. 3728269e767SBrooks DavisIf 3738269e767SBrooks Davis.Dv O_SEARCH 3748269e767SBrooks Daviswas not requested at open time, then the 3758269e767SBrooks Davis.Fn *at 3768269e767SBrooks Davisfunctions use the current directory permissions for the directory referenced 377826509a3SJohn Baldwinby the descriptor at the time of the 378826509a3SJohn Baldwin.Fn *at 379826509a3SJohn Baldwincall. 3808269e767SBrooks Davis.Pp 3818269e767SBrooks Davis.Dv O_PATH 3828269e767SBrooks Davisreturns a file descriptor that can be used as a directory file descriptor for 383d3de1bd4SChristopher Davidson.Fn openat 3848269e767SBrooks Davisand other system calls taking a file descriptor argument, like 3858269e767SBrooks Davis.Xr fstatat 2 3868269e767SBrooks Davisand others. 3878269e767SBrooks DavisThe other functionality of the returned file descriptor is limited to 388826509a3SJohn Baldwinthe following descriptor-level operations: 389826509a3SJohn Baldwin.Pp 390826509a3SJohn Baldwin.Bl -tag -width __acl_aclcheck_fd -offset indent -compact 3918269e767SBrooks Davis.It Xr fcntl 2 3928269e767SBrooks Davisbut advisory locking is not allowed 3938269e767SBrooks Davis.It Xr dup 2 3948269e767SBrooks Davis.It Xr close 2 3958269e767SBrooks Davis.It Xr fstat 2 396*c8141728SCismonX.It Xr fstatfs 2 397*c8141728SCismonX.It Xr fchdir 2 398*c8141728SCismonX.It Xr fchroot 2 3998269e767SBrooks Davis.It Xr fexecve 2 400*c8141728SCismonX.It Xr funlinkat 2 401*c8141728SCismonXcan be passed as the third argument 4028269e767SBrooks Davis.It Dv SCM_RIGHTS 4038269e767SBrooks Daviscan be passed over a 4048269e767SBrooks Davis.Xr unix 4 4058269e767SBrooks Davissocket using a 4068269e767SBrooks Davis.Dv SCM_RIGHTS 4078269e767SBrooks Davismessage 4088269e767SBrooks Davis.It Xr kqueue 2 409826509a3SJohn Baldwinonly with 4108269e767SBrooks Davis.Dv EVFILT_VNODE 411826509a3SJohn Baldwin.It Xr __acl_get_fd 2 412826509a3SJohn Baldwin.It Xr __acl_aclcheck_fd 2 413*c8141728SCismonX.It Xr extattr 2 414*c8141728SCismonX.It Xr capsicum 4 415*c8141728SCismonXcan be passed to 416*c8141728SCismonX.Fn cap_*_limit 417*c8141728SCismonXand 418*c8141728SCismonX.Fn cap_*_get 419*c8141728SCismonXsystem calls (such as 420*c8141728SCismonX.Xr cap_rights_limit 2 ) . 4218269e767SBrooks Davis.El 422826509a3SJohn Baldwin.Pp 423826509a3SJohn BaldwinOther operations like 4248269e767SBrooks Davis.Xr read 2 , 4258269e767SBrooks Davis.Xr ftruncate 2 , 4268269e767SBrooks Davisand any other that operate on file and not on file descriptor (except 4278269e767SBrooks Davis.Xr fstat 2 ) , 4288269e767SBrooks Davisare not allowed. 4298269e767SBrooks Davis.Pp 4308269e767SBrooks DavisA file descriptor created with the 4318269e767SBrooks Davis.Dv O_PATH 432826509a3SJohn Baldwinflag can be opened as a normal (operable) file descriptor by 4338269e767SBrooks Davisspecifying it as the 4348269e767SBrooks Davis.Fa fd 4358269e767SBrooks Davisargument to 4368269e767SBrooks Davis.Fn openat 437826509a3SJohn Baldwinwith an empty 4388269e767SBrooks Davis.Fa path 439826509a3SJohn Baldwinand the 440826509a3SJohn Baldwin.Dv O_EMPTY_PATH 441826509a3SJohn Baldwinflag. 4428269e767SBrooks DavisSuch an open behaves as if the current path of the file referenced by 4438269e767SBrooks Davis.Fa fd 444826509a3SJohn Baldwinis passed, except that path walk permissions are not checked. 4458269e767SBrooks DavisSee also the description of 4468269e767SBrooks Davis.Dv AT_EMPTY_PATH 4478269e767SBrooks Davisflag for 4488269e767SBrooks Davis.Xr fstatat 2 4498269e767SBrooks Davisand related syscalls. 4508269e767SBrooks Davis.Pp 4518269e767SBrooks DavisIf successful, 4528269e767SBrooks Davis.Fn open 4538269e767SBrooks Davisreturns a non-negative integer, termed a file descriptor. 4548269e767SBrooks DavisIt returns \-1 on failure. 455826509a3SJohn BaldwinThe file descriptor value returned is the lowest numbered descriptor 456826509a3SJohn Baldwincurrently not in use by the process. 4578269e767SBrooks DavisThe file pointer used to mark the current position within the 4588269e767SBrooks Davisfile is set to the beginning of the file. 4598269e767SBrooks Davis.Pp 4608269e767SBrooks DavisIf a sleeping open of a device node from 46137bb9884SKonstantin Belousov.Xr devfs 4 4628269e767SBrooks Davisis interrupted by a signal, the call always fails with 4638269e767SBrooks Davis.Er EINTR , 4648269e767SBrooks Daviseven if the 4658269e767SBrooks Davis.Dv SA_RESTART 4668269e767SBrooks Davisflag is set for the signal. 4678269e767SBrooks DavisA sleeping open of a fifo (see 4688269e767SBrooks Davis.Xr mkfifo 2 ) 4698269e767SBrooks Davisis restarted as normal. 4708269e767SBrooks Davis.Pp 471826509a3SJohn BaldwinWhen a new file is created, it is assigned the group of the directory 4728269e767SBrooks Daviswhich contains it. 4738269e767SBrooks Davis.Pp 4748269e767SBrooks DavisUnless 4758269e767SBrooks Davis.Dv O_CLOEXEC 4768269e767SBrooks Davisflag was specified, 4778269e767SBrooks Davisthe new descriptor is set to remain open across 4788269e767SBrooks Davis.Xr execve 2 4798269e767SBrooks Davissystem calls; see 4808269e767SBrooks Davis.Xr close 2 , 4818269e767SBrooks Davis.Xr fcntl 2 482826509a3SJohn Baldwinand the description of the 4838269e767SBrooks Davis.Dv O_CLOEXEC 484826509a3SJohn Baldwinflag. 4858269e767SBrooks Davis.Pp 4868269e767SBrooks DavisThe system imposes a limit on the number of file descriptors 4878269e767SBrooks Davisopen simultaneously by one process. 4888269e767SBrooks DavisThe 4898269e767SBrooks Davis.Xr getdtablesize 2 4908269e767SBrooks Davissystem call returns the current system limit. 4918269e767SBrooks Davis.Sh RETURN VALUES 4928269e767SBrooks DavisIf successful, 4938269e767SBrooks Davis.Fn open 4948269e767SBrooks Davisand 4958269e767SBrooks Davis.Fn openat 4968269e767SBrooks Davisreturn a non-negative integer, termed a file descriptor. 4978269e767SBrooks DavisThey return \-1 on failure, and set 4988269e767SBrooks Davis.Va errno 4998269e767SBrooks Davisto indicate the error. 5008269e767SBrooks Davis.Sh ERRORS 5018269e767SBrooks DavisThe named file is opened unless: 5028269e767SBrooks Davis.Bl -tag -width Er 5038269e767SBrooks Davis.It Bq Er ENOTDIR 5048269e767SBrooks DavisA component of the path prefix is not a directory. 5058269e767SBrooks Davis.It Bq Er ENAMETOOLONG 5068269e767SBrooks DavisA component of a pathname exceeded 255 characters, 5078269e767SBrooks Davisor an entire path name exceeded 1023 characters. 5088269e767SBrooks Davis.It Bq Er ENOENT 5098269e767SBrooks Davis.Dv O_CREAT 5108269e767SBrooks Davisis not set and the named file does not exist. 5118269e767SBrooks Davis.It Bq Er ENOENT 5128269e767SBrooks DavisA component of the path name that must exist does not exist. 5138269e767SBrooks Davis.It Bq Er EACCES 5148269e767SBrooks DavisSearch permission is denied for a component of the path prefix. 5158269e767SBrooks Davis.It Bq Er EACCES 5168269e767SBrooks DavisThe required permissions (for reading and/or writing) 5178269e767SBrooks Davisare denied for the given flags. 5188269e767SBrooks Davis.It Bq Er EACCES 5198269e767SBrooks Davis.Dv O_TRUNC 5208269e767SBrooks Davisis specified and write permission is denied. 5218269e767SBrooks Davis.It Bq Er EACCES 5228269e767SBrooks Davis.Dv O_CREAT 5238269e767SBrooks Davisis specified, 5248269e767SBrooks Davisthe file does not exist, 5258269e767SBrooks Davisand the directory in which it is to be created 5268269e767SBrooks Davisdoes not permit writing. 5278269e767SBrooks Davis.It Bq Er EPERM 5288269e767SBrooks Davis.Dv O_CREAT 5298269e767SBrooks Davisis specified, the file does not exist, and the directory in which it is to be 5308269e767SBrooks Daviscreated has its immutable flag set, see the 5318269e767SBrooks Davis.Xr chflags 2 5328269e767SBrooks Davismanual page for more information. 5338269e767SBrooks Davis.It Bq Er EPERM 5348269e767SBrooks DavisThe named file has its immutable flag set and the file is to be modified. 5358269e767SBrooks Davis.It Bq Er EPERM 5368269e767SBrooks DavisThe named file has its append-only flag set, the file is to be modified, and 5378269e767SBrooks Davis.Dv O_TRUNC 5388269e767SBrooks Davisis specified or 5398269e767SBrooks Davis.Dv O_APPEND 5408269e767SBrooks Davisis not specified. 5418269e767SBrooks Davis.It Bq Er ELOOP 5428269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname. 5438269e767SBrooks Davis.It Bq Er EISDIR 5448269e767SBrooks DavisThe named file is a directory, and the arguments specify 5458269e767SBrooks Davisit is to be modified. 5468269e767SBrooks Davis.It Bq Er EISDIR 5478269e767SBrooks DavisThe named file is a directory, and the flags specified 5488269e767SBrooks Davis.Dv O_CREAT 5498269e767SBrooks Daviswithout 5508269e767SBrooks Davis.Dv O_DIRECTORY . 5518269e767SBrooks Davis.It Bq Er EROFS 5528269e767SBrooks DavisThe named file resides on a read-only file system, 5538269e767SBrooks Davisand the file is to be modified. 5548269e767SBrooks Davis.It Bq Er EROFS 5558269e767SBrooks Davis.Dv O_CREAT 5568269e767SBrooks Davisis specified and the named file would reside on a read-only file system. 5578269e767SBrooks Davis.It Bq Er EMFILE 5588269e767SBrooks DavisThe process has already reached its limit for open file descriptors. 5598269e767SBrooks Davis.It Bq Er ENFILE 5608269e767SBrooks DavisThe system file table is full. 5618269e767SBrooks Davis.It Bq Er EMLINK 5628269e767SBrooks Davis.Dv O_NOFOLLOW 5638269e767SBrooks Daviswas specified and the target is a symbolic link. 564f5a37f8dSJose Luis DuranPOSIX 5658269e767SBrooks Davisspecifies a different error for this case; see the note in 5668269e767SBrooks Davis.Sx STANDARDS 5678269e767SBrooks Davisbelow. 5688269e767SBrooks Davis.It Bq Er ENXIO 5698269e767SBrooks DavisThe named file is a character special or block 5708269e767SBrooks Davisspecial file, and the device associated with this special file 5718269e767SBrooks Davisdoes not exist. 5728269e767SBrooks Davis.It Bq Er ENXIO 5738269e767SBrooks Davis.Dv O_NONBLOCK 5748269e767SBrooks Davisis set, the named file is a fifo, 5758269e767SBrooks Davis.Dv O_WRONLY 5768269e767SBrooks Davisis set, and no process has the file open for reading. 5778269e767SBrooks Davis.It Bq Er EINTR 5788269e767SBrooks DavisThe 5798269e767SBrooks Davis.Fn open 5808269e767SBrooks Davisoperation was interrupted by a signal. 5818269e767SBrooks Davis.It Bq Er EOPNOTSUPP 5828269e767SBrooks Davis.Dv O_SHLOCK 5838269e767SBrooks Davisor 5848269e767SBrooks Davis.Dv O_EXLOCK 5858269e767SBrooks Davisis specified but the underlying file system does not support locking. 5868269e767SBrooks Davis.It Bq Er EOPNOTSUPP 5878269e767SBrooks DavisThe named file is a special file mounted through a file system that 588826509a3SJohn Baldwindoes not support access to it (for example, NFS). 5898269e767SBrooks Davis.It Bq Er EWOULDBLOCK 5908269e767SBrooks Davis.Dv O_NONBLOCK 5918269e767SBrooks Davisand one of 5928269e767SBrooks Davis.Dv O_SHLOCK 5938269e767SBrooks Davisor 5948269e767SBrooks Davis.Dv O_EXLOCK 5958269e767SBrooks Davisis specified and the file is locked. 5968269e767SBrooks Davis.It Bq Er ENOSPC 5978269e767SBrooks Davis.Dv O_CREAT 5988269e767SBrooks Davisis specified, 5998269e767SBrooks Davisthe file does not exist, 6008269e767SBrooks Davisand the directory in which the entry for the new file is being placed 6018269e767SBrooks Daviscannot be extended because there is no space left on the file 6028269e767SBrooks Davissystem containing the directory. 6038269e767SBrooks Davis.It Bq Er ENOSPC 6048269e767SBrooks Davis.Dv O_CREAT 6058269e767SBrooks Davisis specified, 6068269e767SBrooks Davisthe file does not exist, 6078269e767SBrooks Davisand there are no free inodes on the file system on which the 6088269e767SBrooks Davisfile is being created. 6098269e767SBrooks Davis.It Bq Er EDQUOT 6108269e767SBrooks Davis.Dv O_CREAT 6118269e767SBrooks Davisis specified, 6128269e767SBrooks Davisthe file does not exist, 6138269e767SBrooks Davisand the directory in which the entry for the new file 6148269e767SBrooks Davisis being placed cannot be extended because the 6158269e767SBrooks Davisuser's quota of disk blocks on the file system 6168269e767SBrooks Daviscontaining the directory has been exhausted. 6178269e767SBrooks Davis.It Bq Er EDQUOT 6188269e767SBrooks Davis.Dv O_CREAT 6198269e767SBrooks Davisis specified, 6208269e767SBrooks Davisthe file does not exist, 6218269e767SBrooks Davisand the user's quota of inodes on the file system on 6228269e767SBrooks Daviswhich the file is being created has been exhausted. 6238269e767SBrooks Davis.It Bq Er EIO 6248269e767SBrooks DavisAn I/O error occurred while making the directory entry or 6258269e767SBrooks Davisallocating the inode for 6268269e767SBrooks Davis.Dv O_CREAT . 6278269e767SBrooks Davis.It Bq Er EINTEGRITY 6288269e767SBrooks DavisCorrupted data was detected while reading from the file system. 6298269e767SBrooks Davis.It Bq Er ETXTBSY 6308269e767SBrooks DavisThe file is a pure procedure (shared text) file that is being 6318269e767SBrooks Davisexecuted and the 6328269e767SBrooks Davis.Fn open 6338269e767SBrooks Davissystem call requests write access. 6348269e767SBrooks Davis.It Bq Er EFAULT 6358269e767SBrooks DavisThe 6368269e767SBrooks Davis.Fa path 6378269e767SBrooks Davisargument 6388269e767SBrooks Davispoints outside the process's allocated address space. 6398269e767SBrooks Davis.It Bq Er EEXIST 6408269e767SBrooks Davis.Dv O_CREAT 6418269e767SBrooks Davisand 6428269e767SBrooks Davis.Dv O_EXCL 6438269e767SBrooks Daviswere specified and the file exists. 6448269e767SBrooks Davis.It Bq Er EOPNOTSUPP 6458269e767SBrooks DavisAn attempt was made to open a socket (not currently implemented). 6468269e767SBrooks Davis.It Bq Er EINVAL 6478269e767SBrooks DavisAn attempt was made to open a descriptor with an illegal combination 6488269e767SBrooks Davisof 6498269e767SBrooks Davis.Dv O_RDONLY , 6508269e767SBrooks Davis.Dv O_WRONLY , 6518269e767SBrooks Davisor 6528269e767SBrooks Davis.Dv O_RDWR , 6538269e767SBrooks Davisand 6548269e767SBrooks Davis.Dv O_EXEC 6558269e767SBrooks Davisor 6568269e767SBrooks Davis.Dv O_SEARCH . 65702c23c3bSJose Luis Duran.It Bq Er EINVAL 65802c23c3bSJose Luis Duran.Dv O_CREAT 65902c23c3bSJose Luis Duranis specified, 66002c23c3bSJose Luis Duranand the last component of the 66102c23c3bSJose Luis Duran.Fa path 66202c23c3bSJose Luis Duranargument is invalid on the file system on which the file is being created. 6638269e767SBrooks Davis.It Bq Er EBADF 6648269e767SBrooks DavisThe 6658269e767SBrooks Davis.Fa path 6668269e767SBrooks Davisargument does not specify an absolute path and the 6678269e767SBrooks Davis.Fa fd 6688269e767SBrooks Davisargument is 6698269e767SBrooks Davisneither 6708269e767SBrooks Davis.Dv AT_FDCWD 6718269e767SBrooks Davisnor a valid file descriptor open for searching. 6728269e767SBrooks Davis.It Bq Er ENOTDIR 6738269e767SBrooks DavisThe 6748269e767SBrooks Davis.Fa path 6758269e767SBrooks Davisargument is not an absolute path and 6768269e767SBrooks Davis.Fa fd 6778269e767SBrooks Davisis neither 6788269e767SBrooks Davis.Dv AT_FDCWD 6798269e767SBrooks Davisnor a file descriptor associated with a directory. 6808269e767SBrooks Davis.It Bq Er ENOTDIR 6818269e767SBrooks Davis.Dv O_DIRECTORY 6828269e767SBrooks Davisis specified and the file is not a directory. 6838269e767SBrooks Davis.It Bq Er ECAPMODE 6848269e767SBrooks Davis.Dv AT_FDCWD 6858269e767SBrooks Davisis specified and the process is in capability mode. 6868269e767SBrooks Davis.It Bq Er ECAPMODE 6878269e767SBrooks Davis.Fn open 6888269e767SBrooks Daviswas called and the process is in capability mode. 6898269e767SBrooks Davis.It Bq Er ENOTCAPABLE 6908269e767SBrooks Davis.Fa path 6918269e767SBrooks Davisis an absolute path and the process is in capability mode. 6928269e767SBrooks Davis.It Bq Er ENOTCAPABLE 6938269e767SBrooks Davis.Fa path 6948269e767SBrooks Davisis an absolute path and 6958269e767SBrooks Davis.Dv O_RESOLVE_BENEATH 6968269e767SBrooks Davisis specified. 6978269e767SBrooks Davis.It Bq Er ENOTCAPABLE 6988269e767SBrooks Davis.Fa path 6998269e767SBrooks Daviscontains a ".." component leading to a directory outside 7008269e767SBrooks Davisof the directory hierarchy specified by 7018269e767SBrooks Davis.Fa fd 7028269e767SBrooks Davisand the process is in capability mode. 7038269e767SBrooks Davis.It Bq Er ENOTCAPABLE 7048269e767SBrooks Davis.Fa path 7058269e767SBrooks Daviscontains a ".." component leading to a directory outside 7068269e767SBrooks Davisof the directory hierarchy specified by 7078269e767SBrooks Davis.Fa fd 7088269e767SBrooks Davisand 7098269e767SBrooks Davis.Dv O_RESOLVE_BENEATH 7108269e767SBrooks Davisis specified. 7118269e767SBrooks Davis.It Bq Er ENOTCAPABLE 7128269e767SBrooks Davis.Fa path 7138269e767SBrooks Daviscontains a ".." component, the 7148269e767SBrooks Davis.Dv vfs.lookup_cap_dotdot 7158269e767SBrooks Davis.Xr sysctl 3 7168269e767SBrooks Davisis set, and the process is in capability mode. 7178269e767SBrooks Davis.El 7188269e767SBrooks Davis.Sh SEE ALSO 7198269e767SBrooks Davis.Xr chmod 2 , 7208269e767SBrooks Davis.Xr close 2 , 7218269e767SBrooks Davis.Xr dup 2 , 7228269e767SBrooks Davis.Xr fexecve 2 , 7238269e767SBrooks Davis.Xr fhopen 2 , 7248269e767SBrooks Davis.Xr getdtablesize 2 , 7258269e767SBrooks Davis.Xr getfh 2 , 7268269e767SBrooks Davis.Xr lgetfh 2 , 7278269e767SBrooks Davis.Xr lseek 2 , 7288269e767SBrooks Davis.Xr read 2 , 7298269e767SBrooks Davis.Xr umask 2 , 7308269e767SBrooks Davis.Xr write 2 , 7318269e767SBrooks Davis.Xr fopen 3 , 7328269e767SBrooks Davis.Xr capsicum 4 7338269e767SBrooks Davis.Sh STANDARDS 7348269e767SBrooks DavisThese functions are specified by 7358269e767SBrooks Davis.St -p1003.1-2008 . 7368269e767SBrooks Davis.Pp 7378269e767SBrooks Davis.Fx 7388269e767SBrooks Davissets 7398269e767SBrooks Davis.Va errno 7408269e767SBrooks Davisto 7418269e767SBrooks Davis.Er EMLINK instead of 7428269e767SBrooks Davis.Er ELOOP 7438269e767SBrooks Davisas specified by 744f5a37f8dSJose Luis DuranPOSIX 7458269e767SBrooks Daviswhen 7468269e767SBrooks Davis.Dv O_NOFOLLOW 7478269e767SBrooks Davisis set in flags and the final component of pathname is a symbolic link 7488269e767SBrooks Davisto distinguish it from the case of too many symbolic link traversals 7498269e767SBrooks Davisin one of its non-final components. 7508269e767SBrooks Davis.Pp 7518269e767SBrooks DavisThe Open Group Extended API Set 2 specification, that introduced the 7528269e767SBrooks Davis.Fn *at 7538269e767SBrooks DavisAPI, required that the test for whether 7548269e767SBrooks Davis.Fa fd 7558269e767SBrooks Davisis searchable is based on whether 7568269e767SBrooks Davis.Fa fd 7578269e767SBrooks Davisis open for searching, not whether the underlying directory currently 7588269e767SBrooks Davispermits searches. 7598269e767SBrooks DavisThe present implementation of the 7608269e767SBrooks Davis.Fa openat 7618269e767SBrooks Davissystem call is believed to be compatible with 762f5a37f8dSJose Luis Duran.\" .St -p1003.1-2017 , 763f5a37f8dSJose Luis Duran.\" XXX: This should be replaced in the future when an appropriate argument to 764f5a37f8dSJose Luis Duran.\" the St macro is available: -p1003.1-2017 765826509a3SJohn Baldwin.No IEEE Std 1003.1-2008, 2017 Edition ("POSIX.1") , 7668269e767SBrooks Daviswhich specifies that behavior for 7678269e767SBrooks Davis.Dv O_SEARCH , 7688269e767SBrooks Davisin the absence of the flag the implementation checks the current 7698269e767SBrooks Davispermissions of a directory. 7708269e767SBrooks Davis.Sh HISTORY 7718269e767SBrooks DavisThe 7728269e767SBrooks Davis.Fn open 7738269e767SBrooks Davisfunction appeared in 7748269e767SBrooks Davis.At v1 . 7758269e767SBrooks DavisThe 7768269e767SBrooks Davis.Fn openat 7778269e767SBrooks Davisfunction was introduced in 7788269e767SBrooks Davis.Fx 8.0 . 7798269e767SBrooks Davis.Dv O_DSYNC 7808269e767SBrooks Davisappeared in 13.0. 7818269e767SBrooks Davis.Sh BUGS 7828269e767SBrooks DavisThe 7838269e767SBrooks Davis.Fa mode 7848269e767SBrooks Davisargument is variadic and may result in different calling conventions 7858269e767SBrooks Davisthan might otherwise be expected. 786