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