1.\" $NetBSD: open.2,v 1.51 2012/01/25 00:28:35 christos 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 January 23, 2012 33.Dt OPEN 2 34.Os 35.Sh NAME 36.Nm open 37.Nd open or create a file for reading or writing 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In fcntl.h 42.Ft int 43.Fn open "const char *path" "int flags" "..." 44.Sh DESCRIPTION 45The file name specified by 46.Fa path 47is opened 48for reading and/or writing as specified by the 49argument 50.Fa flags 51and the file descriptor returned to the calling process. 52The 53.Fa flags 54are specified by 55.Em or Ns 'ing 56the values listed below. 57Applications must specify exactly one of the first three values 58(file access methods): 59.Bl -tag -offset indent -width O_DIRECTORY 60.It Dv O_RDONLY 61Open for reading only. 62.It Dv O_WRONLY 63Open for writing only. 64.It Dv O_RDWR 65Open for reading and writing. 66.El 67.Pp 68Any combination of the following may be used: 69.Bl -tag -offset indent -width O_DIRECTORY 70.It Dv O_NONBLOCK 71Do not block on open or for data to become available. 72.It Dv O_APPEND 73Append to the file on each write. 74.It Dv O_CREAT 75Create the file if it does not exist. 76The third argument of type 77.Ft mode_t 78is used to compute the mode bits of the file as described in 79.Xr chmod 2 80and modified by the process' umask value (see 81.Xr umask 2 ) . 82.It Dv O_TRUNC 83Truncate size to 0. 84.It Dv O_EXCL 85Error if 86.Dv O_CREAT 87and the file already exists. 88.It Dv O_SHLOCK 89Atomically obtain a shared lock. 90.It Dv O_EXLOCK 91Atomically obtain an exclusive lock. 92.It Dv O_NOFOLLOW 93If last path element is a symlink, don't follow it. 94This option is provided for compatibility with other operating 95systems, but its security value is questionable. 96.It Dv O_CLOEXEC 97Set the 98.Xr close 2 99on 100.Xr exec 3 101flag. 102.It Dv O_NOSIGPIPE 103Return 104.Er EPIPE 105instead of raising 106.Dv SIGPIPE . 107.It Dv O_DSYNC 108If set, write operations will be performed according to synchronized 109I/O data integrity completion: 110each write will wait for the file data to be committed to stable 111storage. 112.It Dv O_SYNC 113If set, write operations will be performed according to synchronized 114I/O file integrity completion: 115each write will wait for both the file data and file status to be 116committed to stable storage. 117.It Dv O_RSYNC 118If set, read operations will complete at the same level of 119integrity which is in effect for write operations: 120if specified together with 121.Dv O_SYNC , 122each read will wait for the file status to be committed to stable 123storage. 124.Pp 125Combining 126.Dv O_RSYNC 127with 128.Dv O_DSYNC 129only, or specifying it without any other synchronized I/O integrity 130completion flag set, has no further effect. 131.It Dv O_ALT_IO 132Alternate I/O semantics will be used for read and write operations 133on the file descriptor. 134Alternate semantics are defined by the underlying layers and will not 135have any alternate effect in most cases. 136.It Dv O_NOCTTY 137If the file is a terminal device, the opened device is not 138made the controlling terminal for the session. 139This flag has no effect on 140.Nx , 141since the system defaults to the abovementioned behaviour. 142The flag is present only for standards conformance. 143.It Dv O_DIRECT 144If set on a regular file, data I/O operations will not buffer the data 145being transferred in the kernel's cache, but rather transfer the data 146directly between user memory and the underlying device driver if possible. 147This flag is advisory; the request may be performed in the normal 148buffered fashion if certain conditions are not met, e.g. if the request 149is not sufficiently aligned or if the file is mapped. 150.Pp 151To meet the alignment requirements for direct I/O, the file offset, 152the length of the I/O and the address of the buffer in memory must all 153be multiples of 154.Dv DEV_BSIZE 155(512 bytes). 156If the I/O request is made 157using an interface that supports scatter/gather via struct iovec, each 158element of the request must meet the above alignment constraints. 159.It Dv O_DIRECTORY 160Fail if the file is not a directory. 161.It Dv O_ASYNC 162Enable the 163.Dv SIGIO 164signal to be sent to the process group 165when I/O is possible, e.g., 166upon availability of data to be read. 167.El 168.Pp 169Opening a file with 170.Dv O_APPEND 171set causes each write on the file 172to be appended to the end. 173If 174.Dv O_TRUNC 175is specified and the 176file exists, the file is truncated to zero length. 177.Pp 178If 179.Dv O_EXCL 180is set with 181.Dv O_CREAT 182and the file already 183exists, 184.Fn open 185returns an error. 186This may be used to implement a simple exclusive access locking mechanism. 187If 188.Dv O_EXCL 189is set and the last component of the pathname is 190a symbolic link, 191.Fn open 192will fail even if the symbolic 193link points to a non-existent name. 194.Pp 195If the 196.Dv O_NONBLOCK 197flag is specified, do not wait for the device or file to be ready or 198available. 199If the 200.Fn open 201call would result 202in the process being blocked for some reason (e.g., waiting for 203carrier on a dialup line), 204.Fn open 205returns immediately. 206This flag also has the effect of making all subsequent I/O on the open file non-blocking. 207.Pp 208When opening a file, a lock with 209.Xr flock 2 210semantics can be obtained by setting 211.Dv O_SHLOCK 212for a shared lock, or 213.Dv O_EXLOCK 214for an exclusive lock. 215If creating a file with 216.Dv O_CREAT , 217the request for the lock will never fail 218(provided that the underlying filesystem supports locking). 219.Pp 220If 221.Fn open 222is successful, the file pointer used to mark the current position within 223the file is set to the beginning of the file. 224.Pp 225When a new file is created it is given the group of the directory 226which contains it. 227.Pp 228The new descriptor is set to remain open across 229.Xr execve 2 230system calls; see 231.Xr close 2 232and 233.Xr fcntl 2 . 234.Pp 235The system imposes a limit on the number of file descriptors 236open simultaneously by one process. 237Calling 238.Xr getdtablesize 3 239returns the current system limit. 240.Sh RETURN VALUES 241If successful, 242.Fn open 243returns a non-negative integer, termed a file descriptor. 244Otherwise, a value of \-1 is returned and 245.Va errno 246is set to indicate the error. 247.Sh ERRORS 248The named file is opened unless: 249.Bl -tag -width Er 250.It Bq Er EACCES 251Search permission is denied for a component of the path prefix, 252the required permissions (for reading and/or writing) 253are denied for the given flags, or 254.Dv O_CREAT 255is specified, 256the file does not exist, 257and the directory in which it is to be created 258does not permit writing. 259.It Bq Er EDQUOT 260.Dv O_CREAT 261is specified, 262the file does not exist, 263and the directory in which the entry for the new file 264is being placed cannot be extended because the 265user's quota of disk blocks on the file system 266containing the directory has been exhausted; or 267.Dv O_CREAT 268is specified, 269the file does not exist, 270and the user's quota of inodes on the file system on 271which the file is being created has been exhausted. 272.It Bq Er EEXIST 273.Dv O_CREAT 274and 275.Dv O_EXCL 276were specified and the file exists. 277.It Bq Er EFAULT 278.Fa path 279points outside the process's allocated address space. 280.It Bq Er EFTYPE 281.Dv O_NOFOLLOW 282was specified, but the last path component is a symlink. 283.Em Note : 284.St -p1003.1-2008 285specifies returning 286.Bq Er ELOOP 287for this case. 288.It Bq Er EINTR 289The 290.Fn open 291operation was interrupted by a signal. 292.It Bq Er EIO 293An I/O error occurred while making the directory entry or 294allocating the inode for 295.Dv O_CREAT . 296.It Bq Er EISDIR 297The named file is a directory, and the arguments specify 298it is to be opened for writing. 299.It Bq Er ELOOP 300Too many symbolic links were encountered in translating the pathname. 301.It Bq Er EMFILE 302The process has already reached its limit for open file descriptors. 303.It Bq Er ENAMETOOLONG 304A component of a pathname exceeded 305.Brq Dv NAME_MAX 306characters, or an entire path name exceeded 307.Brq Dv PATH_MAX 308characters. 309.It Bq Er ENFILE 310The system file table is full. 311.It Bq Er ENOENT 312.Dv O_CREAT 313is not set and the named file does not exist, or 314a component of the path name that must exist does not exist. 315.It Bq Er ENOSPC 316.Dv O_CREAT 317is specified, 318the file does not exist, 319and the directory in which the entry for the new file is being placed 320cannot be extended because there is no space left on the file 321system containing the directory; or 322.Dv O_CREAT 323is specified, 324the file does not exist, 325and there are no free inodes on the file system on which the 326file is being created. 327.It Bq Er ENOTDIR 328A component of the path prefix is not a directory; or 329.Dv O_DIRECTORY 330is specified and the last path component is not a directory. 331.It Bq Er ENXIO 332The named file is a character special or block 333special file, and the device associated with this special file 334does not exist, or 335the named file is a 336.Tn FIFO , 337.Dv O_NONBLOCK 338and 339.Dv O_WRONLY 340is set and no process has the file open for reading. 341.It Bq Er EOPNOTSUPP 342.Dv O_SHLOCK 343or 344.Dv O_EXLOCK 345is specified but the underlying filesystem does not support locking; or 346an attempt was made to open a socket (not currently implemented). 347.It Bq Er EPERM 348The file's flags (see 349.Xr chflags 2 ) 350don't allow the file to be opened. 351.It Bq Er EROFS 352The named file resides on a read-only file system, 353and the file is to be modified. 354.It Bq Er ETXTBSY 355The file is a pure procedure (shared text) file that is being 356executed and the 357.Fn open 358call requests write access. 359.El 360.Sh SEE ALSO 361.Xr chmod 2 , 362.Xr close 2 , 363.Xr dup 2 , 364.Xr lseek 2 , 365.Xr read 2 , 366.Xr umask 2 , 367.Xr write 2 , 368.Xr getdtablesize 3 369.Sh STANDARDS 370The 371.Fn open 372function conforms to 373.St -p1003.1-90 . 374The 375.Fa flags 376values 377.Dv O_DSYNC , 378.Dv O_SYNC 379and 380.Dv O_RSYNC 381are extensions defined in 382.St -p1003.1b-93 . 383.Pp 384The 385.Dv O_SHLOCK 386and 387.Dv O_EXLOCK 388flags are non-standard extensions and should not be used if portability 389is of concern. 390.Sh HISTORY 391An 392.Fn open 393function call appeared in 394.At v2 . 395