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