1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)open.2 8.2 (Berkeley) 11/16/93 29.\" $FreeBSD$ 30.\" 31.Dd September 23, 2020 32.Dt OPEN 2 33.Os 34.Sh NAME 35.Nm open , openat 36.Nd open or create a file for reading, writing or executing 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In fcntl.h 41.Ft int 42.Fn open "const char *path" "int flags" "..." 43.Ft int 44.Fn openat "int fd" "const char *path" "int flags" "..." 45.Sh DESCRIPTION 46The file name specified by 47.Fa path 48is opened 49for either execution or reading and/or writing as specified by the 50argument 51.Fa flags 52and the file descriptor returned to the calling process. 53The 54.Fa flags 55argument may indicate the file is to be 56created if it does not exist (by specifying the 57.Dv O_CREAT 58flag). 59In this case 60.Fn open 61and 62.Fn openat 63require an additional argument 64.Fa "mode_t mode" , 65and the file is created with mode 66.Fa mode 67as described in 68.Xr chmod 2 69and modified by the process' umask value (see 70.Xr umask 2 ) . 71.Pp 72The 73.Fn openat 74function is equivalent to the 75.Fn open 76function except in the case where the 77.Fa path 78specifies a relative path, or the 79.Dv O_BENEATH 80flag is provided. 81For 82.Fn openat 83and relative 84.Fa path , 85the file to be opened is determined relative to the directory 86associated with the file descriptor 87.Fa fd 88instead of the current working directory. 89The 90.Fa flag 91parameter and the optional fourth parameter correspond exactly to 92the parameters of 93.Fn open . 94If 95.Fn openat 96is passed the special value 97.Dv AT_FDCWD 98in the 99.Fa fd 100parameter, the current working directory is used 101and the behavior is identical to a call to 102.Fn open . 103.Pp 104When 105.Fn openat 106is called with an absolute 107.Fa path 108without the 109.Dv O_BENEATH 110flag, it ignores the 111.Fa fd 112argument. 113When 114.Dv O_BENEATH 115is specified with an absolute 116.Fa path , 117a directory passed by the 118.Fa fd 119argument is used as the topping point for the resolution. 120When 121.Dv O_BENEATH 122is specified with a relative path, the 123.Fa fd 124argument is used both as the starting point, and as the topping point 125for the resolution. 126See the definition of the 127.Dv O_BENEATH 128flag below. 129.Pp 130In 131.Xr capsicum 4 132capability mode, 133.Fn open 134is not permitted. 135The 136.Fa path 137argument to 138.Fn openat 139must be strictly relative to a file descriptor 140.Fa fd , 141as defined in 142.Pa sys/kern/vfs_lookup.c . 143.Fa path 144must not be an absolute path and must not contain ".." components 145which cause the path resolution to escape the directory hierarchy 146starting at 147.Fa fd . 148Additionally, no symbolic link in 149.Fa path 150may target absolute path or contain escaping ".." components. 151.Fa fd 152must not be 153.Dv AT_FDCWD . 154.Pp 155If the 156.Dv vfs.lookup_cap_dotdot 157.Xr sysctl 3 158MIB is set to zero, ".." components in the paths, 159used in capability mode, or with the 160.Dv O_BENEATH 161flag, are completely disabled. 162If the 163.Dv vfs.lookup_cap_dotdot_nonlocal 164MIB is set to zero, ".." is not allowed if found on non-local filesystem. 165.Pp 166The flags specified are formed by 167.Em or Ns 'ing 168the following values 169.Pp 170.Bd -literal -offset indent -compact 171O_RDONLY open for reading only 172O_WRONLY open for writing only 173O_RDWR open for reading and writing 174O_EXEC open for execute only 175O_SEARCH open for search only, an alias for O_EXEC 176O_NONBLOCK do not block on open 177O_APPEND append on each write 178O_CREAT create file if it does not exist 179O_TRUNC truncate size to 0 180O_EXCL error if create and file exists 181O_SHLOCK atomically obtain a shared lock 182O_EXLOCK atomically obtain an exclusive lock 183O_DIRECT eliminate or reduce cache effects 184O_FSYNC synchronous writes (historical synonym for O_SYNC) 185O_SYNC synchronous writes 186O_DSYNC synchronous data writes 187O_NOFOLLOW do not follow symlinks 188O_NOCTTY ignored 189O_TTY_INIT ignored 190O_DIRECTORY error if file is not a directory 191O_CLOEXEC set FD_CLOEXEC upon open 192O_VERIFY verify the contents of the file 193O_BENEATH require resolved path to be strictly relative to topping directory 194O_RESOLVE_BENEATH require walked path to be strictly relative to topping directory 195.Ed 196.Pp 197Opening a file with 198.Dv O_APPEND 199set causes each write on the file 200to be appended to the end. 201If 202.Dv O_TRUNC 203is specified and the 204file exists, the file is truncated to zero length. 205If 206.Dv O_EXCL 207is set with 208.Dv O_CREAT 209and the file already 210exists, 211.Fn open 212returns an error. 213This may be used to 214implement a simple exclusive access locking mechanism. 215If 216.Dv O_EXCL 217is set and the last component of the pathname is 218a symbolic link, 219.Fn open 220will fail even if the symbolic 221link points to a non-existent name. 222If the 223.Dv O_NONBLOCK 224flag is specified and the 225.Fn open 226system call would result 227in the process being blocked for some reason (e.g., waiting for 228carrier on a dialup line), 229.Fn open 230returns immediately. 231The descriptor remains in non-blocking mode for subsequent operations. 232.Pp 233If 234.Dv O_SYNC 235is used in the mask, all writes will 236immediately and synchronously be written to disk. 237.Dv O_FSYNC 238is an historical synonym for 239.Dv O_SYNC . 240.Pp 241If 242.Dv O_DSYNC 243is used in the mask, all data and metadata required to read the data will be 244synchronously written to disk, but changes to metadata such as file access and 245modification timestamps may be written later. 246.Pp 247If 248.Dv O_NOFOLLOW 249is used in the mask and the target file passed to 250.Fn open 251is a symbolic link then the 252.Fn open 253will fail. 254.Pp 255When opening a file, a lock with 256.Xr flock 2 257semantics can be obtained by setting 258.Dv O_SHLOCK 259for a shared lock, or 260.Dv O_EXLOCK 261for an exclusive lock. 262If creating a file with 263.Dv O_CREAT , 264the request for the lock will never fail 265(provided that the underlying file system supports locking). 266.Pp 267.Dv O_DIRECT 268may be used to minimize or eliminate the cache effects of reading and writing. 269The system will attempt to avoid caching the data you read or write. 270If it cannot avoid caching the data, 271it will minimize the impact the data has on the cache. 272Use of this flag can drastically reduce performance if not used with care. 273.Pp 274.Dv O_NOCTTY 275may be used to ensure the OS does not assign this file as the 276controlling terminal when it opens a tty device. 277This is the default on 278.Fx , 279but is present for 280.Tn POSIX 281compatibility. 282The 283.Fn open 284system call will not assign controlling terminals on 285.Fx . 286.Pp 287.Dv O_TTY_INIT 288may be used to ensure the OS restores the terminal attributes when 289initially opening a TTY. 290This is the default on 291.Fx , 292but is present for 293.Tn POSIX 294compatibility. 295The initial call to 296.Fn open 297on a TTY will always restore default terminal attributes on 298.Fx . 299.Pp 300.Dv O_DIRECTORY 301may be used to ensure the resulting file descriptor refers to a 302directory. 303This flag can be used to prevent applications with elevated privileges 304from opening files which are even unsafe to open with 305.Dv O_RDONLY , 306such as device nodes. 307.Pp 308.Dv O_CLOEXEC 309may be used to set 310.Dv FD_CLOEXEC 311flag for the newly returned file descriptor. 312.Pp 313.Dv O_VERIFY 314may be used to indicate to the kernel that the contents of the file should 315be verified before allowing the open to proceed. 316The details of what 317.Dq verified 318means is implementation specific. 319The run-time linker (rtld) uses this flag to ensure shared objects have 320been verified before operating on them. 321.Pp 322.Dv O_BENEATH 323returns 324.Er ENOTCAPABLE 325if the specified path, after resolving all symlinks and ".." 326references, does not end up with tail residing in the directory hierarchy of 327children beneath the topping directory. 328Topping directory is the process current directory if relative 329.Fa path 330is used for 331.Fn open , 332and the directory referenced by the 333.Fa fd 334argument when using 335.Fn openat . 336.Dv O_BENEATH 337allows arbitrary prefix that ends up at the topping directory, 338after which all further resolved components must be under it. 339.Pp 340.Dv O_RESOLVE_BENEATH 341returns 342.Er ENOTCAPABLE 343if any intermediate component of the specified relative path does not 344reside in the directory hierarchy beneath the topping directory. 345Comparing to 346.Dv O_BENEATH , 347absolute paths or even the temporal escape from beneath of the topping 348directory is not allowed. 349.Pp 350When 351.Fa fd 352is opened with 353.Dv O_SEARCH , 354execute permissions are checked at open time. 355The 356.Fa fd 357may not be used for any read operations like 358.Xr getdirentries 2 . 359The primary use for this descriptor will be as the lookup descriptor for the 360.Fn *at 361family of functions. 362.Pp 363If successful, 364.Fn open 365returns a non-negative integer, termed a file descriptor. 366It returns \-1 on failure. 367The file pointer used to mark the current position within the 368file is set to the beginning of the file. 369.Pp 370If a sleeping open of a device node from 371.Xr devfs 5 372is interrupted by a signal, the call always fails with 373.Er EINTR , 374even if the 375.Dv SA_RESTART 376flag is set for the signal. 377A sleeping open of a fifo (see 378.Xr mkfifo 2 ) 379is restarted as normal. 380.Pp 381When a new file is created it is given the group of the directory 382which contains it. 383.Pp 384Unless 385.Dv O_CLOEXEC 386flag was specified, 387the new descriptor is set to remain open across 388.Xr execve 2 389system calls; see 390.Xr close 2 , 391.Xr fcntl 2 392and 393.Dv O_CLOEXEC 394description. 395.Pp 396The system imposes a limit on the number of file descriptors 397open simultaneously by one process. 398The 399.Xr getdtablesize 2 400system call returns the current system limit. 401.Sh RETURN VALUES 402If successful, 403.Fn open 404and 405.Fn openat 406return a non-negative integer, termed a file descriptor. 407They return \-1 on failure, and set 408.Va errno 409to indicate the error. 410.Sh ERRORS 411The named file is opened unless: 412.Bl -tag -width Er 413.It Bq Er ENOTDIR 414A component of the path prefix is not a directory. 415.It Bq Er ENAMETOOLONG 416A component of a pathname exceeded 255 characters, 417or an entire path name exceeded 1023 characters. 418.It Bq Er ENOENT 419.Dv O_CREAT 420is not set and the named file does not exist. 421.It Bq Er ENOENT 422A component of the path name that must exist does not exist. 423.It Bq Er EACCES 424Search permission is denied for a component of the path prefix. 425.It Bq Er EACCES 426The required permissions (for reading and/or writing) 427are denied for the given flags. 428.It Bq Er EACCES 429.Dv O_TRUNC 430is specified and write permission is denied. 431.It Bq Er EACCES 432.Dv O_CREAT 433is specified, 434the file does not exist, 435and the directory in which it is to be created 436does not permit writing. 437.It Bq Er EPERM 438.Dv O_CREAT 439is specified, the file does not exist, and the directory in which it is to be 440created has its immutable flag set, see the 441.Xr chflags 2 442manual page for more information. 443.It Bq Er EPERM 444The named file has its immutable flag set and the file is to be modified. 445.It Bq Er EPERM 446The named file has its append-only flag set, the file is to be modified, and 447.Dv O_TRUNC 448is specified or 449.Dv O_APPEND 450is not specified. 451.It Bq Er ELOOP 452Too many symbolic links were encountered in translating the pathname. 453.It Bq Er EISDIR 454The named file is a directory, and the arguments specify 455it is to be modified. 456.It Bq Er EISDIR 457The named file is a directory, and the flags specified 458.Dv O_CREAT 459without 460.Dv O_DIRECTORY . 461.It Bq Er EROFS 462The named file resides on a read-only file system, 463and the file is to be modified. 464.It Bq Er EROFS 465.Dv O_CREAT 466is specified and the named file would reside on a read-only file system. 467.It Bq Er EMFILE 468The process has already reached its limit for open file descriptors. 469.It Bq Er ENFILE 470The system file table is full. 471.It Bq Er EMLINK 472.Dv O_NOFOLLOW 473was specified and the target is a symbolic link. 474.It Bq Er ENXIO 475The named file is a character special or block 476special file, and the device associated with this special file 477does not exist. 478.It Bq Er ENXIO 479.Dv O_NONBLOCK 480is set, the named file is a fifo, 481.Dv O_WRONLY 482is set, and no process has the file open for reading. 483.It Bq Er EINTR 484The 485.Fn open 486operation was interrupted by a signal. 487.It Bq Er EOPNOTSUPP 488.Dv O_SHLOCK 489or 490.Dv O_EXLOCK 491is specified but the underlying file system does not support locking. 492.It Bq Er EOPNOTSUPP 493The named file is a special file mounted through a file system that 494does not support access to it (e.g.\& NFS). 495.It Bq Er EWOULDBLOCK 496.Dv O_NONBLOCK 497and one of 498.Dv O_SHLOCK 499or 500.Dv O_EXLOCK 501is specified and the file is locked. 502.It Bq Er ENOSPC 503.Dv O_CREAT 504is specified, 505the file does not exist, 506and the directory in which the entry for the new file is being placed 507cannot be extended because there is no space left on the file 508system containing the directory. 509.It Bq Er ENOSPC 510.Dv O_CREAT 511is specified, 512the file does not exist, 513and there are no free inodes on the file system on which the 514file is being created. 515.It Bq Er EDQUOT 516.Dv O_CREAT 517is specified, 518the file does not exist, 519and the directory in which the entry for the new file 520is being placed cannot be extended because the 521user's quota of disk blocks on the file system 522containing the directory has been exhausted. 523.It Bq Er EDQUOT 524.Dv O_CREAT 525is specified, 526the file does not exist, 527and the user's quota of inodes on the file system on 528which the file is being created has been exhausted. 529.It Bq Er EIO 530An I/O error occurred while making the directory entry or 531allocating the inode for 532.Dv O_CREAT . 533.It Bq Er EINTEGRITY 534Corrupted data was detected while reading from the file system. 535.It Bq Er ETXTBSY 536The file is a pure procedure (shared text) file that is being 537executed and the 538.Fn open 539system call requests write access. 540.It Bq Er EFAULT 541The 542.Fa path 543argument 544points outside the process's allocated address space. 545.It Bq Er EEXIST 546.Dv O_CREAT 547and 548.Dv O_EXCL 549were specified and the file exists. 550.It Bq Er EOPNOTSUPP 551An attempt was made to open a socket (not currently implemented). 552.It Bq Er EINVAL 553An attempt was made to open a descriptor with an illegal combination 554of 555.Dv O_RDONLY , 556.Dv O_WRONLY , 557or 558.Dv O_RDWR , 559and 560.Dv O_EXEC 561or 562.Dv O_SEARCH . 563.It Bq Er EINVAL 564The 565.Dv O_RESOLVE_BENEATH 566flag is specified and 567.Dv path 568is absolute. 569.It Bq Er EBADF 570The 571.Fa path 572argument does not specify an absolute path and the 573.Fa fd 574argument is 575neither 576.Dv AT_FDCWD 577nor a valid file descriptor open for searching. 578.It Bq Er ENOTDIR 579The 580.Fa path 581argument is not an absolute path and 582.Fa fd 583is neither 584.Dv AT_FDCWD 585nor a file descriptor associated with a directory. 586.It Bq Er ENOTDIR 587.Dv O_DIRECTORY 588is specified and the file is not a directory. 589.It Bq Er ECAPMODE 590.Dv AT_FDCWD 591is specified and the process is in capability mode. 592.It Bq Er ECAPMODE 593.Fn open 594was called and the process is in capability mode. 595.It Bq Er ENOTCAPABLE 596.Fa path 597is an absolute path, 598or contained a ".." component leading to a 599directory outside of the directory hierarchy specified by 600.Fa fd , 601and the process is in capability mode. 602.It Bq Er ENOTCAPABLE 603The 604.Dv O_BENEATH 605flag was provided, and the absolute 606.Fa path 607does not have its tail fully contained under the topping directory, 608or the relative 609.Fa path 610escapes it. 611.It Bq Er ENOTCAPABLE 612The 613.Dv O_RESOLVE_BENEATH 614flag was provided, and the relative 615.Fa path 616escapes topping directory. 617.El 618.Sh SEE ALSO 619.Xr chmod 2 , 620.Xr close 2 , 621.Xr dup 2 , 622.Xr fexecve 2 , 623.Xr fhopen 2 , 624.Xr getdtablesize 2 , 625.Xr getfh 2 , 626.Xr lgetfh 2 , 627.Xr lseek 2 , 628.Xr read 2 , 629.Xr umask 2 , 630.Xr write 2 , 631.Xr fopen 3 , 632.Xr capsicum 4 633.Sh STANDARDS 634These functions are specified by 635.St -p1003.1-2008 . 636.Fx 637sets 638.Va errno 639to 640.Er EMLINK instead of 641.Er ELOOP 642as specified by 643.Tn POSIX 644when 645.Dv O_NOFOLLOW 646is set in flags and the final component of pathname is a symbolic link 647to distinguish it from the case of too many symbolic link traversals 648in one of its non-final components. 649.Sh HISTORY 650The 651.Fn open 652function appeared in 653.At v1 . 654The 655.Fn openat 656function was introduced in 657.Fx 8.0 . 658.Dv O_DSYNC 659appeared in 13.0. 660.Sh BUGS 661The Open Group Extended API Set 2 specification requires that the test 662for whether 663.Fa fd 664is searchable is based on whether 665.Fa fd 666is open for searching, not whether the underlying directory currently 667permits searches. 668The present implementation of the 669.Fa openat 670checks the current permissions of directory instead. 671.Pp 672The 673.Fa mode 674argument is variadic and may result in different calling conventions 675than might otherwise be expected. 676