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