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