1.\" $NetBSD: open.2,v 1.62 2019/09/16 04:59:32 wiz 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 September 16, 2019 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 the first three 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: 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.\" (This block awaits a decision about the semantics of O_SEARCH) 221.\" .It Dv O_SEARCH 222.\" If opening a directory, search permission checks will not be performed on 223.\" subsequent usage of the file descriptor for looking up relative paths by 224.\" .Xr faccessat 2 , 225.\" .Xr fchmodat 2 , 226.\" .Xr fchownat 2 , 227.\" .Xr fstatat 2 , 228.\" .Xr linkat 2 , 229.\" .Xr mkdirat 2 , 230.\" .Xr mkfifoat 2 , 231.\" .Xr mknodat 2 , 232.\" .Xr openat 2 , 233.\" .Xr readlinkat 2 , 234.\" .Xr symlinkat 2 , 235.\" .Xr unlinkat 2 , 236.\" and 237.\" .Xr utimensat 2 . 238.El 239.Pp 240Opening a file with 241.Dv O_APPEND 242set causes each write on the file 243to be appended to the end. 244If 245.Dv O_TRUNC 246is specified and the 247file exists, the file is truncated to zero length. 248.Pp 249If 250.Dv O_EXCL 251is set with 252.Dv O_CREAT 253and the file already 254exists, 255.Fn open 256returns an error. 257This may be used to implement a simple exclusive access locking mechanism. 258If 259.Dv O_EXCL 260is set and the last component of the pathname is 261a symbolic link, 262.Fn open 263will fail even if the symbolic 264link points to a non-existent name. 265.Pp 266If the 267.Dv O_NONBLOCK 268flag is specified, do not wait for the device or file to be ready or 269available. 270If the 271.Fn open 272call would result 273in the process being blocked for some reason (e.g., waiting for 274carrier on a dialup line), 275.Fn open 276returns immediately. 277This flag also has the effect of making all subsequent I/O on the open file non-blocking. 278.Pp 279When opening a file, a lock with 280.Xr flock 2 281semantics can be obtained by setting 282.Dv O_SHLOCK 283for a shared lock, or 284.Dv O_EXLOCK 285for an exclusive lock. 286If creating a file with 287.Dv O_CREAT , 288the request for the lock will never fail 289(provided that the underlying file system supports locking). 290.Pp 291If 292.Fn open 293is successful, the file pointer used to mark the current position within 294the file is set to the beginning of the file. 295.Pp 296When a new file is created it is given the group of the directory 297which contains it. 298.Pp 299The new descriptor is set to remain open across 300.Xr execve 2 301system calls; see 302.Xr close 2 303and 304.Xr fcntl 2 . 305.Pp 306The system imposes a limit on the number of file descriptors 307open simultaneously by one process. 308Calling 309.Xr getdtablesize 3 310returns the current system limit. 311.Sh RETURN VALUES 312If successful, 313.Fn open 314and 315.Fn openat 316returns a non-negative integer, termed a file descriptor. 317Otherwise, a value of \-1 is returned and 318.Va errno 319is set to indicate the error. 320.Sh ERRORS 321The named file is opened unless: 322.Bl -tag -width Er 323.It Bq Er EACCES 324Search permission is denied for a component of the path prefix, 325the required permissions (for reading and/or writing) 326are denied for the given flags, or 327.Dv O_CREAT 328is specified, 329the file does not exist, 330and the directory in which it is to be created 331does not permit writing. 332.It Bq Er EDQUOT 333.Dv O_CREAT 334is specified, 335the file does not exist, 336and the directory in which the entry for the new file 337is being placed cannot be extended because the 338user's quota of disk blocks on the file system 339containing the directory has been exhausted; or 340.Dv O_CREAT 341is specified, 342the file does not exist, 343and the user's quota of inodes on the file system on 344which the file is being created has been exhausted. 345.It Bq Er EEXIST 346.Dv O_CREAT 347and 348.Dv O_EXCL 349were specified and the file exists. 350.It Bq Er EFAULT 351.Fa path 352points outside the process's allocated address space. 353.It Bq Er EFTYPE 354.Dv O_NOFOLLOW 355was specified, but the last path component is a symlink. 356.Em Note : 357.St -p1003.1-2008 358specifies returning 359.Bq Er ELOOP 360for this case. 361.It Bq Er EINTR 362The 363.Fn open 364operation was interrupted by a signal. 365.It Bq Er EIO 366An I/O error occurred while making the directory entry or 367allocating the inode for 368.Dv O_CREAT . 369.It Bq Er EISDIR 370The named file is a directory, and the arguments specify 371it is to be opened for writing. 372.It Bq Er ELOOP 373Too many symbolic links were encountered in translating the pathname. 374.It Bq Er EMFILE 375The process has already reached its limit for open file descriptors. 376.It Bq Er ENAMETOOLONG 377A component of a pathname exceeded 378.Brq Dv NAME_MAX 379characters, or an entire path name exceeded 380.Brq Dv PATH_MAX 381characters. 382.It Bq Er ENFILE 383The system file table is full. 384.It Bq Er ENOENT 385.Dv O_CREAT 386is not set and the named file does not exist, or 387a component of the path name that must exist does not exist. 388.It Bq Er ENOSPC 389.Dv O_CREAT 390is specified, 391the file does not exist, 392and the directory in which the entry for the new file is being placed 393cannot be extended because there is no space left on the file 394system containing the directory; or 395.Dv O_CREAT 396is specified, 397the file does not exist, 398and there are no free inodes on the file system on which the 399file is being created. 400.It Bq Er ENOTDIR 401A component of the path prefix is not a directory; or 402.Dv O_DIRECTORY 403is specified and the last path component is not a directory. 404.It Bq Er ENXIO 405The named file is a character special or block 406special file, and the device associated with this special file 407does not exist, or the named file is a FIFO, 408.Dv O_NONBLOCK 409and 410.Dv O_WRONLY 411is set and no process has the file open for reading. 412.It Bq Er EOPNOTSUPP 413.Dv O_SHLOCK 414or 415.Dv O_EXLOCK 416is specified but the underlying file system does not support locking; or 417an attempt was made to open a socket (not currently implemented). 418.It Bq Er EPERM 419The file's flags (see 420.Xr chflags 2 ) 421don't allow the file to be opened. 422.It Bq Er EROFS 423The named file resides on a read-only file system, 424and the file is to be modified. 425.It Bq Er ETXTBSY 426The file is a pure procedure (shared text) file that is being 427executed and the 428.Fn open 429call requests write access. 430.El 431.Pp 432In addition, 433.Fn openat 434will fail if: 435.Bl -tag -width Er 436.It Bq Er EBADF 437.Fa path 438does not specify an absolute path and 439.Fa fd 440is neither 441.Dv AT_FDCWD 442nor a valid file descriptor open for reading or searching. 443.It Bq Er EINVAL 444An attempt was made to open a descriptor with an illegal combination 445of 446.Dv O_RDONLY , 447.Dv O_WRONLY , 448.Dv O_RDWR , 449and 450.Dv O_EXEC . 451.It Bq Er ENOTDIR 452.Fa path 453is not an absolute path and 454.Fa fd 455is a file descriptor associated with a non-directory file. 456.El 457.Sh SEE ALSO 458.Xr chmod 2 , 459.Xr close 2 , 460.Xr dup 2 , 461.Xr faccessat 2 , 462.Xr fchmodat 2 , 463.Xr fchownat 2 , 464.Xr fstatat 2 , 465.Xr linkat 2 , 466.Xr lseek 2 , 467.Xr mkdirat 2 , 468.Xr mkfifoat 2 , 469.Xr mknodat 2 , 470.Xr read 2 , 471.Xr readlinkat 2 , 472.Xr symlinkat 2 , 473.Xr umask 2 , 474.Xr unlinkat 2 , 475.Xr utimensat 2 , 476.Xr write 2 , 477.Xr getdtablesize 3 478.Sh STANDARDS 479The 480.Fn open 481function conforms to 482.St -p1003.1-90 . 483.Fn openat 484conforms to 485.St -p1003.1-2008 . 486.Pp 487The 488.Fa flags 489values 490.Dv O_DSYNC , 491.Dv O_SYNC 492and 493.Dv O_RSYNC 494are extensions defined in 495.St -p1003.1b-93 . 496.\" (This block awaits a decision about the semantics of O_SEARCH) 497.\" .Dv O_SEARCH 498.\" is defined in 499.\" .St -p1003.1-2008 . 500.Pp 501The 502.Dv O_SHLOCK 503and 504.Dv O_EXLOCK 505flags are non-standard extensions and should not be used if portability 506is of concern. 507.Sh HISTORY 508An 509.Fn open 510function call appeared in 511.At v1 . 512