1.\" $NetBSD: open.2,v 1.66 2023/03/05 16:24:31 thorpej Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)open.2 8.2 (Berkeley) 11/16/93 31.\" 32.Dd March 5, 2023 33.Dt OPEN 2 34.Os 35.Sh NAME 36.Nm open , 37.Nm openat 38.Nd open or create a file for reading, writing or executing 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In fcntl.h 43.Ft int 44.Fn open "const char *path" "int flags" "..." 45.Ft int 46.Fn openat "int fd" "const char *path" "int flags" "..." 47.Sh DESCRIPTION 48The file name specified by 49.Fa path 50is opened 51for either execution or reading and/or writing as specified by the 52argument 53.Fa flags 54and the file descriptor returned to the calling process. 55The 56.Fa flags 57argument may indicate the file is to be 58created if it does not exist (by specifying the 59.Dv O_CREAT 60flag). 61In this case 62.Fn open 63and 64.Fn openat 65require an additional argument 66.Fa "mode_t mode" , 67and the file is created with mode 68.Fa mode 69as described in 70.Xr chmod 2 71and modified by the process' umask value (see 72.Xr umask 2 ) . 73.Pp 74The 75.Fn openat 76function is equivalent to the 77.Fn open 78function except in the case where the 79.Fa path 80is relative. 81In that case, it is looked up from a directory whose file 82descriptor was passed as 83.Fa fd . 84Search permission is required on this directory. 85.\" (These alternatives await a decision about the semantics of O_SEARCH) 86.\" Search permission is required on this directory 87.\" except if 88.\" .Fa fd 89.\" was opened with the 90.\" .Dv O_SEARCH 91.\" flag. 92.\" - or - 93.\" This file descriptor must have been opened with the 94.\" .Dv O_SEARCH 95.\" flag. 96.Fa fd 97can be set to 98.Dv AT_FDCWD 99in order to specify the current directory. 100.Pp 101The 102.Fa flags 103are specified by 104.Em or Ns 'ing 105the values listed below. 106Applications must specify exactly one of these four values 107(file access methods): 108.Bl -tag -offset indent -width O_DIRECTORY 109.It Dv O_RDONLY 110Open for reading only. 111.It Dv O_WRONLY 112Open for writing only. 113.It Dv O_EXEC 114Open for execute only. 115.It Dv O_RDWR 116Open for reading and writing. 117.El 118.Pp 119Any combination of the following may be used as well: 120.Bl -tag -offset indent -width O_DIRECTORY 121.It Dv O_NONBLOCK 122Do not block on open or for data to become available. 123.It Dv O_APPEND 124Append to the file on each write. 125.It Dv O_CREAT 126Create the file if it does not exist. 127The third argument of type 128.Ft mode_t 129is used to compute the mode bits of the file as described in 130.Xr chmod 2 131and modified by the process' umask value (see 132.Xr umask 2 ) . 133.It Dv O_TRUNC 134Truncate size to 0. 135.It Dv O_EXCL 136Error if 137.Dv O_CREAT 138and the file already exists. 139.It Dv O_SHLOCK 140Atomically obtain a shared lock. 141.It Dv O_EXLOCK 142Atomically obtain an exclusive lock. 143.It Dv O_NOFOLLOW 144If last path element is a symlink, don't follow it. 145This option is provided for compatibility with other operating 146systems, but its security value is questionable. 147.It Dv O_CLOEXEC 148Set the 149.Xr close 2 150on 151.Xr exec 3 152flag. 153.It Dv O_NOSIGPIPE 154Return 155.Er EPIPE 156instead of raising 157.Dv SIGPIPE . 158.It Dv O_DSYNC 159If set, write operations will be performed according to synchronized 160I/O data integrity completion: 161each write will wait for the file data to be committed to stable 162storage. 163.It Dv O_SYNC 164If set, write operations will be performed according to synchronized 165I/O file integrity completion: 166each write will wait for both the file data and file status to be 167committed to stable storage. 168.It Dv O_RSYNC 169If set, read operations will complete at the same level of 170integrity which is in effect for write operations: 171if specified together with 172.Dv O_SYNC , 173each read will wait for the file status to be committed to stable 174storage. 175.Pp 176Combining 177.Dv O_RSYNC 178with 179.Dv O_DSYNC 180only, or specifying it without any other synchronized I/O integrity 181completion flag set, has no further effect. 182.It Dv O_ALT_IO 183Alternate I/O semantics will be used for read and write operations 184on the file descriptor. 185Alternate semantics are defined by the underlying layers and will not 186have any alternate effect in most cases. 187.It Dv O_NOCTTY 188If the file is a terminal device, the opened device is not 189made the controlling terminal for the session. 190This flag has no effect on 191.Nx , 192since the system defaults to the abovementioned behaviour. 193The flag is present only for standards conformance. 194.It Dv O_DIRECT 195If set on a regular file, data I/O operations will not buffer the data 196being transferred in the kernel's cache, but rather transfer the data 197directly between user memory and the underlying device driver if possible. 198This flag is advisory; the request may be performed in the normal 199buffered fashion if certain conditions are not met, e.g. if the request 200is not sufficiently aligned or if the file is mapped. 201.Pp 202To meet the alignment requirements for direct I/O, the file offset, 203the length of the I/O and the address of the buffer in memory must all 204be multiples of 205.Dv DEV_BSIZE 206(512 bytes). 207If the I/O request is made 208using an interface that supports scatter/gather via struct iovec, each 209element of the request must meet the above alignment constraints. 210.It Dv O_DIRECTORY 211Fail if the file is not a directory. 212.It Dv O_REGULAR 213Fail if the path does not refer to a regular file. 214.It Dv O_ASYNC 215Enable the 216.Dv SIGIO 217signal to be sent to the process group 218when I/O is possible, e.g., 219upon availability of data to be read. 220.Pp 221Note: This is broken in 222.Fn open ; 223it must be set explicitly with the 224.Dv F_SETFL 225command to 226.Xr fcntl 2 . 227.\" (This block awaits a decision about the semantics of O_SEARCH) 228.\" .It Dv O_SEARCH 229.\" If opening a directory, search permission checks will not be performed on 230.\" subsequent usage of the file descriptor for looking up relative paths by 231.\" .Xr faccessat 2 , 232.\" .Xr fchmodat 2 , 233.\" .Xr fchownat 2 , 234.\" .Xr fstatat 2 , 235.\" .Xr linkat 2 , 236.\" .Xr mkdirat 2 , 237.\" .Xr mkfifoat 2 , 238.\" .Xr mknodat 2 , 239.\" .Xr openat 2 , 240.\" .Xr readlinkat 2 , 241.\" .Xr symlinkat 2 , 242.\" .Xr unlinkat 2 , 243.\" and 244.\" .Xr utimensat 2 . 245.El 246.Pp 247Opening a file with 248.Dv O_APPEND 249set causes each write on the file 250to be appended to the end. 251If 252.Dv O_TRUNC 253is specified and the 254file exists, the file is truncated to zero length. 255.Pp 256If 257.Dv O_EXCL 258is set with 259.Dv O_CREAT 260and the file already 261exists, 262.Fn open 263returns an error. 264This may be used to implement a simple exclusive access locking mechanism. 265If 266.Dv O_EXCL 267is set and the last component of the pathname is 268a symbolic link, 269.Fn open 270will fail even if the symbolic 271link points to a non-existent name. 272.Pp 273If the 274.Dv O_NONBLOCK 275flag is specified, do not wait for the device or file to be ready or 276available. 277If the 278.Fn open 279call would result 280in the process being blocked for some reason (e.g., waiting for 281carrier on a dialup line), 282.Fn open 283returns immediately. 284This flag also has the effect of making all subsequent I/O on the open file non-blocking. 285.Pp 286When opening a file, a lock with 287.Xr flock 2 288semantics can be obtained by setting 289.Dv O_SHLOCK 290for a shared lock, or 291.Dv O_EXLOCK 292for an exclusive lock. 293If creating a file with 294.Dv O_CREAT , 295the request for the lock will never fail 296(provided that the underlying file system supports locking). 297.Pp 298If 299.Fn open 300is successful, the file pointer used to mark the current position within 301the file is set to the beginning of the file. 302.Pp 303When a new file is created it is given the group of the directory 304which contains it. 305.Pp 306The new descriptor is set to remain open across 307.Xr execve 2 308system calls; see 309.Xr close 2 310and 311.Xr fcntl 2 . 312.Pp 313The system imposes a limit on the number of file descriptors 314open simultaneously by one process. 315Calling 316.Xr getdtablesize 3 317returns the current system limit. 318.Sh RETURN VALUES 319If successful, 320.Fn open 321and 322.Fn openat 323returns a non-negative integer, termed a file descriptor. 324Otherwise, a value of \-1 is returned and 325.Va errno 326is set to indicate the error. 327.Sh ERRORS 328The named file is opened unless: 329.Bl -tag -width Er 330.It Bq Er EACCES 331Search permission is denied for a component of the path prefix, 332the required permissions (for reading and/or writing) 333are denied for the given flags, or 334.Dv O_CREAT 335is specified, 336the file does not exist, 337and the directory in which it is to be created 338does not permit writing. 339.It Bq Er EDQUOT 340.Dv O_CREAT 341is specified, 342the file does not exist, 343and the directory in which the entry for the new file 344is being placed cannot be extended because the 345user's quota of disk blocks on the file system 346containing the directory has been exhausted; or 347.Dv O_CREAT 348is specified, 349the file does not exist, 350and the user's quota of inodes on the file system on 351which the file is being created has been exhausted. 352.It Bq Er EEXIST 353.Dv O_CREAT 354and 355.Dv O_EXCL 356were specified and the file exists. 357.It Bq Er EFAULT 358.Fa path 359points outside the process's allocated address space. 360.It Bq Er EFTYPE 361.Dv O_NOFOLLOW 362was specified, but the last path component is a symlink. 363.Em Note : 364.St -p1003.1-2008 365specifies returning 366.Bq Er ELOOP 367for this case. 368.It Bq Er EFTYPE 369.Dv O_REGULAR 370is specified and the last path component is not a regular file. 371.It Bq Er EINTR 372The 373.Fn open 374operation was interrupted by a signal. 375.It Bq Er EIO 376An I/O error occurred while making the directory entry or 377allocating the inode for 378.Dv O_CREAT . 379.It Bq Er EISDIR 380The named file is a directory, and the arguments specify 381it is to be opened for writing. 382.It Bq Er ELOOP 383Too many symbolic links were encountered in translating the pathname. 384.It Bq Er EMFILE 385The process has already reached its limit for open file descriptors. 386.It Bq Er ENAMETOOLONG 387A component of a pathname exceeded 388.Brq Dv NAME_MAX 389characters, or an entire path name exceeded 390.Brq Dv PATH_MAX 391characters. 392.It Bq Er ENFILE 393The system file table is full. 394.It Bq Er ENOENT 395.Dv O_CREAT 396is not set and the named file does not exist, or 397a component of the path name that must exist does not exist. 398.It Bq Er ENOSPC 399.Dv O_CREAT 400is specified, 401the file does not exist, 402and the directory in which the entry for the new file is being placed 403cannot be extended because there is no space left on the file 404system containing the directory; or 405.Dv O_CREAT 406is specified, 407the file does not exist, 408and there are no free inodes on the file system on which the 409file is being created. 410.It Bq Er ENOTDIR 411A component of the path prefix is not a directory; or 412.Dv O_DIRECTORY 413is specified and the last path component is not a directory. 414.It Bq Er ENXIO 415The named file is a character special or block 416special file, and the device associated with this special file 417does not exist, or the named file is a FIFO, 418.Dv O_NONBLOCK 419and 420.Dv O_WRONLY 421is set and no process has the file open for reading. 422.It Bq Er EOPNOTSUPP 423.Dv O_SHLOCK 424or 425.Dv O_EXLOCK 426is specified but the underlying file system does not support locking; or 427an attempt was made to open a socket (not currently implemented). 428.It Bq Er EPERM 429The file's flags (see 430.Xr chflags 2 ) 431don't allow the file to be opened. 432.It Bq Er EROFS 433The named file resides on a read-only file system, 434and the file is to be modified. 435.It Bq Er ETXTBSY 436The file is a pure procedure (shared text) file that is being 437executed and the 438.Fn open 439call requests write access. 440.El 441.Pp 442In addition, 443.Fn openat 444will fail if: 445.Bl -tag -width Er 446.It Bq Er EBADF 447.Fa path 448does not specify an absolute path and 449.Fa fd 450is neither 451.Dv AT_FDCWD 452nor a valid file descriptor open for reading or searching. 453.It Bq Er EINVAL 454An attempt was made to open a descriptor with an illegal combination 455of 456.Dv O_RDONLY , 457.Dv O_WRONLY , 458.Dv O_RDWR , 459and 460.Dv O_EXEC . 461.It Bq Er ENOTDIR 462.Fa path 463is not an absolute path and 464.Fa fd 465is a file descriptor associated with a non-directory file. 466.El 467.Sh SEE ALSO 468.Xr chmod 2 , 469.Xr close 2 , 470.Xr dup 2 , 471.Xr faccessat 2 , 472.Xr fchmodat 2 , 473.Xr fchownat 2 , 474.Xr fstatat 2 , 475.Xr linkat 2 , 476.Xr lseek 2 , 477.Xr mkdirat 2 , 478.Xr mkfifoat 2 , 479.Xr mknodat 2 , 480.Xr read 2 , 481.Xr readlinkat 2 , 482.Xr symlinkat 2 , 483.Xr umask 2 , 484.Xr unlinkat 2 , 485.Xr utimensat 2 , 486.Xr write 2 , 487.Xr getdtablesize 3 488.Sh STANDARDS 489The 490.Fn open 491function conforms to 492.St -p1003.1-90 . 493.Fn openat 494conforms to 495.St -p1003.1-2008 . 496.Pp 497The 498.Fa flags 499values 500.Dv O_DSYNC , 501.Dv O_SYNC 502and 503.Dv O_RSYNC 504are extensions defined in 505.St -p1003.1b-93 . 506.\" (This block awaits a decision about the semantics of O_SEARCH) 507.\" .Dv O_SEARCH 508.\" is defined in 509.\" .St -p1003.1-2008 . 510.Pp 511The 512.Dv O_SHLOCK 513and 514.Dv O_EXLOCK 515flags are non-standard extensions and should not be used if portability 516is of concern. 517.Sh HISTORY 518An 519.Fn open 520function call appeared in 521.At v1 . 522.Sh BUGS 523.Dv O_ASYNC 524doesn't actually work as advertised with 525.Nm ; 526you must set 527.Dv O_ASYNC 528explicitly with the 529.Dv F_SETFL 530command to 531.Xr fcntl 2 . 532