1.\" $NetBSD: open.2,v 1.32 2003/08/07 16:44:03 agc 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 May 8, 2003 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" "mode_t mode" 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 following values. 57Applications must specify exactly one of the first three values 58(file access modes): 59.Bl -tag -offset indent -width O_NONBLOCK 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_NONBLOCK 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, in which case the file is 76created with mode 77.Ar mode 78as 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. This option is 94provided for compatibility with other operating systems, but its 95security value is questionable. 96.It Dv O_DSYNC 97If set, write operations will be performed according to synchronized 98I/O data integrity completion: 99each write will wait for the file data to be committed to stable 100storage. 101.It Dv O_SYNC 102If set, write operations will be performed according to synchronized 103I/O file integrity completion: 104each write will wait for both the file data and file status to be 105committed to stable storage. 106.It Dv O_RSYNC 107If set, read operations will complete at the same level of 108integrity which is in effect for write operations: 109if specified together with 110.Dv O_SYNC , 111each read will wait for the file status to be committed to stable 112storage. 113.Pp 114Combining 115.Dv O_RSYNC 116with 117.Dv O_DSYNC 118only, or specifying it without any other synchronized I/O integrity 119completion flag set, has no further effect. 120.El 121.Pp 122Opening a file with 123.Dv O_APPEND 124set causes each write on the file 125to be appended to the end. 126If 127.Dv O_TRUNC 128is specified and the 129file exists, the file is truncated to zero length. 130.Pp 131If 132.Dv O_EXCL 133is set with 134.Dv O_CREAT 135and the file already 136exists, 137.Fn open 138returns an error. 139This may be used to implement a simple exclusive access locking mechanism. 140If 141.Dv O_EXCL 142is set and the last component of the pathname is 143a symbolic link, 144.Fn open 145will fail even if the symbolic 146link points to a non-existent name. 147.Pp 148If the 149.Dv O_NONBLOCK 150flag is specified, do not wait for the device or file to be ready or 151available. 152If the 153.Fn open 154call would result 155in the process being blocked for some reason (e.g., waiting for 156carrier on a dialup line), 157.Fn open 158returns immediately. 159This flag also has the effect of making all subsequent I/O on the open file non-blocking. 160.Pp 161When opening a file, a lock with 162.Xr flock 2 163semantics can be obtained by setting 164.Dv O_SHLOCK 165for a shared lock, or 166.Dv O_EXLOCK 167for an exclusive lock. 168If creating a file with 169.Dv O_CREAT , 170the request for the lock will never fail 171(provided that the underlying filesystem supports locking). 172.Pp 173If 174.Fn open 175is successful, the file pointer used to mark the current position within 176the file is set to the beginning of the file. 177.Pp 178When a new file is created it is given the group of the directory 179which contains it. 180.Pp 181The new descriptor is set to remain open across 182.Xr execve 2 183system calls; see 184.Xr close 2 185and 186.Xr fcntl 2 . 187.Pp 188The system imposes a limit on the number of file descriptors 189open simultaneously by one process. 190Calling 191.Xr getdtablesize 3 192returns the current system limit. 193.Sh RETURN VALUES 194If successful, 195.Fn open 196returns a non-negative integer, termed a file descriptor. 197Otherwise, a value of \-1 is returned and 198.Va errno 199is set to indicate the error. 200.Sh ERRORS 201The named file is opened unless: 202.Bl -tag -width Er 203.It Bq Er ENOTDIR 204A component of the path prefix is not a directory. 205.It Bq Er ENAMETOOLONG 206A component of a pathname exceeded 207.Dv {NAME_MAX} 208characters, or an entire path name exceeded 209.Dv {PATH_MAX} 210characters. 211.It Bq Er ENOENT 212.Dv O_CREAT 213is not set and the named file does not exist, or 214a component of the path name that must exist does not exist. 215.It Bq Er EACCES 216Search permission is denied for a component of the path prefix, 217the required permissions (for reading and/or writing) 218are denied for the given flags, or 219.Dv O_CREAT 220is specified, 221the file does not exist, 222and the directory in which it is to be created 223does not permit writing. 224.It Bq Er ELOOP 225Too many symbolic links were encountered in translating the pathname. 226.It Bq Er EISDIR 227The named file is a directory, and the arguments specify 228it is to be opened for writing. 229.It Bq Er EROFS 230The named file resides on a read-only file system, 231and the file is to be modified. 232.It Bq Er EMFILE 233The process has already reached its limit for open file descriptors. 234.It Bq Er ENFILE 235The system file table is full. 236.It Bq Er ENXIO 237The named file is a character special or block 238special file, and the device associated with this special file 239does not exist, or 240the named file is a 241.Tn FIFO , 242.Dv O_NONBLOCK 243and 244.Dv O_WRONLY 245is set and no process has the file open for reading. 246.It Bq Er EINTR 247The 248.Fn open 249operation was interrupted by a signal. 250.It Bq Er EOPNOTSUPP 251.Dv O_SHLOCK 252or 253.Dv O_EXLOCK 254is specified but the underlying filesystem does not support locking. 255.It Bq Er ENOSPC 256.Dv O_CREAT 257is specified, 258the file does not exist, 259and the directory in which the entry for the new file is being placed 260cannot be extended because there is no space left on the file 261system containing the directory. 262.It Bq Er ENOSPC 263.Dv O_CREAT 264is specified, 265the file does not exist, 266and there are no free inodes on the file system on which the 267file is being created. 268.It Bq Er EDQUOT 269.Dv O_CREAT 270is specified, 271the file does not exist, 272and the directory in which the entry for the new file 273is being placed cannot be extended because the 274user's quota of disk blocks on the file system 275containing the directory has been exhausted. 276.It Bq Er EDQUOT 277.Dv O_CREAT 278is specified, 279the file does not exist, 280and the user's quota of inodes on the file system on 281which the file is being created has been exhausted. 282.It Bq Er EIO 283An I/O error occurred while making the directory entry or 284allocating the inode for 285.Dv O_CREAT . 286.It Bq Er ETXTBSY 287The file is a pure procedure (shared text) file that is being 288executed and the 289.Fn open 290call requests write access. 291.It Bq Er EFAULT 292.Fa path 293points outside the process's allocated address space. 294.It Bq Er EEXIST 295.Dv O_CREAT 296and 297.Dv O_EXCL 298were specified and the file exists. 299.It Bq Er EOPNOTSUPP 300An attempt was made to open a socket (not currently implemented). 301.El 302.Sh SEE ALSO 303.Xr chmod 2 , 304.Xr close 2 , 305.Xr dup 2 , 306.Xr lseek 2 , 307.Xr read 2 , 308.Xr umask 2 , 309.Xr write 2 , 310.Xr getdtablesize 3 311.Sh STANDARDS 312The 313.Fn open 314function conforms to 315.St -p1003.1-90 . 316The 317.Fa flags 318values 319.Dv O_DSYNC , 320.Dv O_SYNC 321and 322.Dv O_RSYNC 323are extensions defined in 324.St -p1003.1b-93 . 325.Pp 326The 327.Dv O_SHLOCK , 328.Dv O_EXLOCK , 329and 330.Dv O_NOFOLLOW 331flags are non-standard extensions and should not be used if portability 332is of concern. 333.Sh HISTORY 334An 335.Fn open 336function call appeared in 337.At v6 . 338