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: src/lib/libc/sys/open.2,v 1.11.2.9 2001/12/14 18:34:01 ru Exp $ 30.\" 31.Dd February 17, 2021 32.Dt OPEN 2 33.Os 34.Sh NAME 35.Nm open , openat 36.Nd open or create a file for reading or writing 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 reading and/or writing as specified by the 50argument 51.Fa flags 52and the lowest unused file descriptor in the process' file descriptor table 53is returned. 54The 55.Fa flags 56argument may indicate the file is to be 57created if it does not exist (by specifying the 58.Dv O_CREAT 59flag). 60In this case 61.Fn open 62and 63.Fn openat 64require a third argument 65.Fa "mode_t mode" , 66and the file is created with mode 67.Fa mode 68as described in 69.Xr chmod 2 70and modified by the process' umask value (see 71.Xr umask 2 ) . 72.Pp 73The 74.Fn openat 75function is equivalent to the 76.Fn open 77function except in the case where the 78.Fa path 79specifies a relative path. 80In this case the file to be opened is determined relative to the directory 81associated with the file descriptor 82.Fa fd 83instead of the current working directory. 84The 85.Fa flag 86parameter and the optional fourth parameter correspond exactly to 87the parameters of 88.Fn open . 89If 90.Fn openat 91is passed the special value 92.Dv AT_FDCWD 93in the 94.Fa fd 95parameter, the current working directory is used 96and the behavior is identical to a call to 97.Fn open . 98.Pp 99The flags specified are formed by 100.Em or Ns 'ing 101the following values 102.Pp 103.Bd -literal -offset indent -compact 104O_RDONLY open for reading only 105O_WRONLY open for writing only 106O_RDWR open for reading and writing 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_NOFOLLOW do not follow symlinks 117O_DIRECTORY error if file is not a directory 118O_CLOEXEC set FD_CLOEXEC upon open 119.Ed 120.Pp 121Opening a file with 122.Dv O_APPEND 123set causes each write on the file 124to be appended to the end. 125If 126.Dv O_TRUNC 127is specified and the 128file exists, the file is truncated to zero length. 129If 130.Dv O_EXCL 131is set with 132.Dv O_CREAT 133and the file already 134exists, 135.Fn open 136returns an error. 137This may be used to 138implement a simple exclusive access locking mechanism. 139If 140.Dv O_EXCL 141is set and the last component of the pathname is 142a symbolic link, 143.Fn open 144will fail even if the symbolic 145link points to a non-existent name. 146If the 147.Dv O_NONBLOCK 148flag is specified and the 149.Fn open 150call would result 151in the process being blocked for some reason (e.g., waiting for 152carrier on a dialup line), 153.Fn open 154returns immediately. 155The first time the process attempts to perform I/O on the open 156file it will block (not currently implemented). 157.Pp 158If 159.Dv O_FSYNC 160is used in the mask, all writes will 161immediately be written to disk, 162the kernel will not cache written data 163and all writes on the descriptor will not return until 164the data to be written completes. 165.Pp 166If 167.Dv O_NOFOLLOW 168is used in the mask and the target file passed to 169.Fn open 170is a symbolic link then the 171.Fn open 172will fail. 173.Pp 174When opening a file, a lock with 175.Xr flock 2 176semantics can be obtained by setting 177.Dv O_SHLOCK 178for a shared lock, or 179.Dv O_EXLOCK 180for an exclusive lock. 181If creating a file with 182.Dv O_CREAT , 183the request for the lock will never fail 184(provided that the underlying filesystem supports locking). 185.Pp 186.Dv O_DIRECT 187may be used to minimize or eliminate the cache effects of reading and writing. 188The system will attempt to avoid caching the data you read or write. 189If it cannot avoid caching the data, 190it will minimize the impact the data has on the cache. 191Use of this flag can drastically reduce performance if not used with care. 192.Pp 193.Dv O_DIRECTORY 194may be used to ensure the resulting file descriptor refers to a directory. 195This flag can be used to prevent applications with elevated privileges 196from opening files which are even unsafe to open with 197.Dv O_RDONLY , 198such as device nodes. 199.Pp 200.Dv O_CLOEXEC 201may be used to atomically set the 202.Dv FD_CLOEXEC 203flag for the newly returned file descriptor. 204.Pp 205If successful, 206.Fn open 207and 208.Fn openat 209return a non-negative integer, termed a file descriptor. 210It returns -1 on failure. 211The file pointer used to mark the current position within the 212file is set to the beginning of the file. 213.Pp 214When a new file is created it is given the group of the directory 215which contains it. 216.Pp 217Unless 218.Dv O_CLOEXEC 219was specified, the new descriptor is set to remain open across 220.Xr execve 2 221system calls; see 222.Xr close 2 , 223.Xr fcntl 2 224and 225.Dv O_CLOEXEC 226description. 227.Pp 228The system imposes a limit on the number of file descriptors 229open simultaneously by one process. 230.Xr Getdtablesize 2 231returns the current system limit. 232.Sh RETURN VALUES 233If successful, 234.Fn open 235and 236.Fn openat 237return a non-negative integer, termed a file descriptor. 238They return -1 on failure, and set 239.Va errno 240to indicate the error. 241.Sh ERRORS 242The named file is opened unless: 243.Bl -tag -width Er 244.It Bq Er ENOTDIR 245A component of the path prefix is not a directory or the 246.Fa path 247argument is not an absolute path and the 248.Fa fd 249argument is neither 250.Dv AT_FDCWD 251nor a file descriptor associated with a directory or 252.Dv O_DIRECTORY 253is specified and the file is not a directory. 254.It Bq Er ENAMETOOLONG 255A component of a pathname exceeded 255 characters, 256or an entire path name exceeded 1023 characters. 257.It Bq Er ENOENT 258.Dv O_CREAT 259is not set and the named file does not exist. 260.It Bq Er ENOENT 261A component of the path name that must exist does not exist. 262.It Bq Er EACCES 263Search permission is denied for a component of the path prefix. 264.It Bq Er EACCES 265The required permissions (for reading and/or writing) 266are denied for the given flags. 267.It Bq Er EACCES 268.Dv O_CREAT 269is specified, 270the file does not exist, 271and the directory in which it is to be created 272does not permit writing. 273.It Bq Er ELOOP 274Too many symbolic links were encountered in translating the pathname. 275.It Bq Er EISDIR 276The named file is a directory, and the arguments specify 277it is to be opened for writing. 278.It Bq Er EROFS 279The named file resides on a read-only file system, 280and the file is to be modified. 281.It Bq Er EMFILE 282The process has already reached its limit for open file descriptors. 283.It Bq Er ENFILE 284The system file table is full. 285.It Bq Er EMLINK 286.Dv O_NOFOLLOW 287was specified and the target is a symbolic link. 288.It Bq Er ENXIO 289The named file is a character special or block 290special file, and the device associated with this special file 291does not exist. 292.It Bq Er ENXIO 293The named file is a fifo, no process has 294it open for reading, and the arguments specify it is 295to be opened for writing. 296.It Bq Er EINTR 297The 298.Fn open 299operation was interrupted by a signal. 300.It Bq Er EOPNOTSUPP 301.Dv O_SHLOCK 302or 303.Dv O_EXLOCK 304is specified but the underlying filesystem does not support locking. 305.It Bq Er EWOULDBLOCK 306.Dv O_NONBLOCK 307and one of 308.Dv O_SHLOCK 309or 310.Dv O_EXLOCK 311is specified and the file is locked. 312.It Bq Er ENOSPC 313.Dv O_CREAT 314is specified, 315the file does not exist, 316and the directory in which the entry for the new file is being placed 317cannot be extended because there is no space left on the file 318system containing the directory. 319.It Bq Er ENOSPC 320.Dv O_CREAT 321is specified, 322the file does not exist, 323and there are no free inodes on the file system on which the 324file is being created. 325.It Bq Er EDQUOT 326.Dv O_CREAT 327is specified, 328the file does not exist, 329and the directory in which the entry for the new file 330is being placed cannot be extended because the 331user's quota of disk blocks on the file system 332containing the directory has been exhausted. 333.It Bq Er EDQUOT 334.Dv O_CREAT 335is specified, 336the file does not exist, 337and the user's quota of inodes on the file system on 338which the file is being created has been exhausted. 339.It Bq Er EIO 340An I/O error occurred while making the directory entry or 341allocating the inode for 342.Dv O_CREAT . 343.It Bq Er ETXTBSY 344The file is a pure procedure (shared text) file that is being 345executed and the 346.Fn open 347call requests write access. 348.It Bq Er EFAULT 349.Fa Path 350points outside the process's allocated address space. 351.It Bq Er EEXIST 352.Dv O_CREAT 353and 354.Dv O_EXCL 355were specified and the file exists. 356.It Bq Er EOPNOTSUPP 357An attempt was made to open a socket (not currently implemented). 358.It Bq Er EINVAL 359An attempt was made to open a descriptor with an illegal combination 360of 361.Dv O_RDONLY , 362.Dv O_WRONLY , 363and 364.Dv O_RDWR . 365.El 366.Sh SEE ALSO 367.Xr chmod 2 , 368.Xr close 2 , 369.Xr dup 2 , 370.Xr fexecve 2 , 371.Xr getdtablesize 2 , 372.Xr lseek 2 , 373.Xr read 2 , 374.Xr umask 2 , 375.Xr write 2 376.Sh HISTORY 377An 378.Fn open 379function call appeared in 380.At v6 . 381An 382.Fn openat 383function call appeared first in Solaris and was ported to 384.Dx 2.3 . 385.Sh BUGS 386The Open Group Extended API Set 2 specification requires that the test 387for 388.Fa fd Ap s 389searchability is based on whether it is open for searching, 390and not whether the underlying directory currently permits searches. 391The present implementation of 392.Fn openat 393checks the current permissions of directory instead. 394