1.\" $OpenBSD: open.2,v 1.23 2001/08/05 21:08:06 hugh Exp $ 2.\" $NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)open.2 8.2 (Berkeley) 11/16/93 36.\" 37.Dd November 16, 1993 38.Dt OPEN 2 39.Os 40.Sh NAME 41.Nm open 42.Nd open or create a file for reading or writing 43.Sh SYNOPSIS 44.Fd #include <fcntl.h> 45.Ft int 46.Fn open "const char *path" "int flags" "mode_t mode" 47.Sh DESCRIPTION 48The file name specified by 49.Fa path 50is opened 51for 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), in which case the file is created with mode 61.Fa mode 62as described in 63.Xr chmod 2 64and modified by the process' umask value (see 65.Xr umask 2 ) . 66.Pp 67The flags specified are formed by 68.Tn OR Ns 'ing 69the following values 70.Pp 71.Bl -tag -width O_NONBLOCK -offset indent -compact 72.It Dv O_RDONLY 73Open for reading only. 74.It Dv O_WRONLY 75Open for writing only. 76.It Dv O_RDWR 77Open for reading and writing. 78.It Dv O_NONBLOCK 79Do not block on open or for data to become available. 80.It Dv O_APPEND 81Append on each write. 82.It Dv O_CREAT 83Create file if it does not exist. 84.It Dv O_TRUNC 85Truncate size to 0. 86.It Dv O_EXCL 87Error if create and file exists. 88.It Dv O_SYNC 89Perform synchronous I/O operations. 90.It Dv O_SHLOCK 91Atomically obtain a shared lock. 92.It Dv O_EXLOCK 93Atomically obtain an exclusive lock. 94.It Dv O_NOFOLLOW 95If last path element is a symlink, don't follow it. 96.El 97.Pp 98Opening a file with 99.Dv O_APPEND 100set causes each write on the file 101to be appended to the end. 102If 103.Dv O_TRUNC 104and a writing mode are specified and the 105file exists, the file is truncated to zero length. 106If 107.Dv O_EXCL 108is set with 109.Dv O_CREAT 110and the file already 111exists, 112.Fn open 113returns an error. 114This may be used to implement a simple exclusive access locking mechanism. 115If either of 116.Dv O_EXCL 117or 118.Dv O_NOFOLLOW 119are set and the last component of the pathname is 120a symbolic link, 121.Fn open 122will fail even if the symbolic 123link points to a non-existent name. 124If the 125.Dv O_NONBLOCK 126flag is specified, do not wait for the device or file to be ready or 127available. 128If the 129.Fn open 130call would result 131in the process being blocked for some reason (e.g., waiting for 132carrier on a dialup line), 133.Fn open 134returns immediately. 135This flag also has the effect of making all subsequent I/O on the open file non-blocking. 136If the 137.Dv O_SYNC 138flag is set, all I/O operations on the file will be done synchronously. 139.Pp 140A fifo should either be opened with 141.Dv O_RDONLY 142or with 143.Dv O_WRONLY . 144The behavior for opening a fifo with 145.Dv O_RDWR 146is undefined. 147.Pp 148When opening a file, a lock with 149.Xr flock 2 150semantics can be obtained by setting 151.Dv O_SHLOCK 152for a shared lock, or 153.Dv O_EXLOCK 154for an exclusive lock. 155If creating a file with 156.Dv O_CREAT , 157the request for the lock will never fail 158(provided that the underlying filesystem supports locking). 159.Pp 160If successful, 161.Fn open 162returns a non-negative integer, termed a file descriptor. 163It returns \-1 on failure. 164The file pointer used to mark the current position within the 165file is set to the beginning of the file. 166.Pp 167When a new file is created it is given the group of the directory 168which contains it. 169.Pp 170The new descriptor is set to remain open across 171.Xr execve 2 172system calls; see 173.Xr close 2 174and 175.Xr fcntl 2 . 176.Pp 177The system imposes a limit on the number of file descriptors 178open simultaneously by one process. 179.Xr getdtablesize 3 180returns the current system limit. 181.Sh ERRORS 182The named file is opened unless: 183.Bl -tag -width Er 184.It Bq Er ENOTDIR 185A component of the path prefix is not a directory. 186.It Bq Er ENAMETOOLONG 187A component of a pathname exceeded 188.Dv {NAME_MAX} 189characters, or an entire path name exceeded 190.Dv {PATH_MAX} 191characters. 192.It Bq Er ENOENT 193.Dv O_CREAT 194is not set and the named file does not exist. 195.It Bq Er ENOENT 196A component of the path name that must exist does not exist. 197.It Bq Er EACCES 198Search permission is denied for a component of the path prefix. 199.It Bq Er EACCES 200The required permissions (for reading and/or writing) 201are denied for the given flags. 202.It Bq Er EACCES 203.Dv O_CREAT 204is specified, 205the file does not exist, 206and the directory in which it is to be created 207does not permit writing. 208.It Bq Er ELOOP 209Too many symbolic links were encountered in translating the pathname, 210or the 211.Dv O_NOFOLLOW 212flag was specified and the target is a symbolic link. 213.It Bq Er EISDIR 214The named file is a directory, and the arguments specify 215it is to be opened for writing. 216.It Bq Er EINVAL 217The flags specified for opening the file are not valid. 218.It Bq Er EROFS 219The named file resides on a read-only file system, 220and the file is to be modified. 221.It Bq Er EMFILE 222The process has already reached its limit for open file descriptors. 223.It Bq Er ENFILE 224The system file table is full. 225.It Bq Er ENXIO 226The named file is a character special or block 227special file, and the device associated with this special file 228does not exist. 229.It Bq Er EINTR 230The 231.Fn open 232operation was interrupted by a signal. 233.It Bq Er EOPNOTSUPP 234.Dv O_SHLOCK 235or 236.Dv O_EXLOCK 237is specified but the underlying filesystem does not support locking. 238.It Bq Er EWOULDBLOCK 239.Dv O_NONBLOCK 240and one of 241.Dv O_SHLOCK 242or 243.Dv O_EXLOCK 244is specified and the file is locked. 245.It Bq Er ENOSPC 246.Dv O_CREAT 247is specified, 248the file does not exist, 249and the directory in which the entry for the new file is being placed 250cannot be extended because there is no space left on the file 251system containing the directory. 252.It Bq Er ENOSPC 253.Dv O_CREAT 254is specified, 255the file does not exist, 256and there are no free inodes on the file system on which the 257file is being created. 258.It Bq Er EDQUOT 259.Dv O_CREAT 260is specified, 261the file does not exist, 262and the directory in which the entry for the new file 263is being placed cannot be extended because the 264user's quota of disk blocks on the file system 265containing the directory has been exhausted. 266.It Bq Er EDQUOT 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 EIO 273An I/O error occurred while making the directory entry or 274allocating the inode for 275.Dv O_CREAT . 276.It Bq Er ETXTBSY 277The file is a pure procedure (shared text) file that is being 278executed and the 279.Fn open 280call requests write access. 281.It Bq Er EFAULT 282.Fa path 283points outside the process's allocated address space. 284.It Bq Er EEXIST 285.Dv O_CREAT 286and 287.Dv O_EXCL 288were specified and the file exists. 289.It Bq Er EOPNOTSUPP 290An attempt was made to open a socket (not currently implemented). 291.It Bq Er EAGAIN 292.Dv O_NONBLOCK 293and either 294.Dv O_EXLOCK 295or 296.Dv O_SHLOCK 297are set and the file is already locked. 298.El 299.Sh SEE ALSO 300.Xr chmod 2 , 301.Xr close 2 , 302.Xr dup 2 , 303.Xr flock 2 , 304.Xr lseek 2 , 305.Xr read 2 , 306.Xr umask 2 , 307.Xr write 2 , 308.Xr getdtablesize 3 309.Sh STANDARDS 310The 311.Fn open 312function conforms to 313.St -ansiC , 314.St -p1003.1-90 315and 316.St -xpg4.2 . 317.Pp 318.Dv POSIX 319specifies three different flavors for synchronous I/O: 320.Dv O_SYNC , 321.Dv O_DSYNC , 322and 323.Dv O_RSYNC . 324In 325.Ox , 326these are all equivalent. 327.Pp 328The 329.Dv O_SHLOCK , 330.Dv O_EXLOCK , 331and 332.Dv O_NOFOLLOW 333flags are non-standard extensions and should not be used if portability 334is of concern. 335.Sh HISTORY 336An 337.Fn open 338function call appeared in 339.At v6 . 340.Sh CAVEATS 341The 342.Dv O_TRUNC 343flag requires that one of 344.Dv O_RDWR 345or 346.Dv O_WRONLY 347also be specified, else 348.Dv EINVAL 349is returned. 350