1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
.\" 1993 Michael Haardt, Ian Jackson.
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified Wed Jul 21 22:42:16 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified Sun Aug 21 18:18:14 1994: Michael Haardt's NFS diffs were
.\" applied by hand (faith@cs.unc.edu).
.\" Modified Sat Apr 13 16:25:28 1996 by Andries Brouwer (aeb@cwi.nl)
.\" Modified Mon May 13 00:53:52 1996: added symbolic constants
.\" as sent by Thomas Koenig
.\" Modified Fri Dec 20 16:06:45 1996 by Michael Haardt: More NFS details
.\" Modified Fri Feb 19 15:08:34 1999 by Andries Brouwer (aeb@cwi.nl)
.\" Modified 981128 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
.\" Modified Thu Jun 3 19:29:06 1999 by Michael Haardt: NFS lock optimisation
.TH OPEN 2 "June 3, 1999" "Linux" "System calls"
.SH NAME
open, creat \- open and possibly create a file or device
.SH SYNOPSIS
.nf
.B #include <sys/types.h>
.B #include <sys/stat.h>
.B #include <fcntl.h>
.sp
.BI "int open(const char *" pathname ", int " flags );
.BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
.BI "int creat(const char *" pathname ", mode_t " mode );
.fi
.SH DESCRIPTION
The
.B open()
system call is used to convert a pathname into a file descriptor
(a small, non-negative integer for use in subsequent I/O as with
.BR read ", " write ", etc.)."
When the call is successful, the file descriptor returned will be
the lowest file descriptor not currently open for the process.
This call creates a new open file, not shared with any other process.
(But shared open files may arise via the
.BR fork (2)
system call.)
The new file descriptor is set to remain open across exec functions
(see
.BR fcntl (2)).
The file offset is set to the beginning of the file.
.I flags
is one of
.BR O_RDONLY ", " O_WRONLY " or " O_RDWR
which request opening the file read-only, write-only or read/write,
respectively.
.I flags
may also be
.RI bitwise- or 'd
with one or more of the following:
.TP
.B O_CREAT
If the file does not exist it will be created.
.TP
.B O_EXCL
When used with
.BR O_CREAT ,
if the file already exists it is an error and the
.B open
will fail.
.B O_EXCL
is broken on NFS file systems, programs which rely on it for performing
locking tasks will contain a race condition. The solution for performing
atomic file locking using a lockfile is to create a unique file on the same
fs (e.g., incorporating hostname and pid), use
.BR link (2)
to make a link to the lockfile. If \fBlink()\fP returns 0, the lock is
successful. Otherwise, use
.BR stat (2)
on the unique file to check if its link count has increased to 2,
in which case the lock is also successful.
.TP
.B O_NOCTTY
If
.I pathname
refers to a terminal device \(em see
.BR tty (4)
\(em it will not become the process's controlling terminal even if the
process does not have one.
.TP
.B O_TRUNC
If the file already exists it will be truncated.
.TP
.B O_APPEND
The file is opened in append mode. Before each
.BR write ,
the file pointer is positioned at the end of the file,
as if with
.BR lseek .
.B O_APPEND
may lead to corrupted files on NFS file systems if more than one process appends data to a
file at once. This is because NFS does not support appending to a file, so the
client kernel has to simulate it, which can't be done without a race condition.
.TP
.BR O_NONBLOCK " or " O_NDELAY
The file is opened in non-blocking mode. Neither the
.B open
nor any subsequent operations on the file descriptor which is
returned will cause the calling process to wait.
For the handling of FIFOs (named pipes), see also
.BR fifo (4).
.TP
.B O_SYNC
The file is opened for synchronous I/O. Any
.BR write s
on the resulting file descriptor will block the calling process until
the data has been physically written to the underlying hardware.
.I See RESTRICTIONS below, though.
.TP
.B O_NOFOLLOW
If \fIpathname\fR is a symbolic link, then the open fails. This is a
FreeBSD extension, which was added to Linux in version 2.1.126.
Symbolic links in earlier components of the pathname will still be
followed. The headers from glibc 2.0.100 and later include a
definition of this flag; \fIkernels before 2.1.126 will ignore it if
used\fR.
.TP
.B O_DIRECTORY
If \fIpathname\fR is not a directory, cause the open to fail. This
flag is Linux-specific, and was added in kernel version 2.1.126, to
avoid denial-of-service problems if \fBopendir\fR(3) is called on a
FIFO or tape device, but should not be used outside of the
implementation of \fBopendir\fR.
.TP
.B O_LARGEFILE
On 32-bit systems that support the Large Files System, allow files
whose sizes cannot be represented in 31 bits to be opened. The Linux
kernel does not yet have the support for this (as of 2.1.130), but the
flag definition is there and the userspace LFS interfaces are present
in the glibc 2.1 test releases.
.PP
Some of these optional flags can be altered using
.B fcntl
after the file has been opened.
.I mode
specifies the permissions to use if a new file is created. It is
modified by the process's
.BR umask
in the usual way: the permissions of the created file are
.BR "(mode & ~umask)" .
.PP
The following symbolic constants are provided for
.IR mode :
.TP
.B S_IRWXU
00700 user (file owner) has read, write and execute permission
.TP
.B S_IRUSR (S_IREAD)
00400 user has read permission
.TP
.B S_IWUSR (S_IWRITE)
00200 user has write permission
.TP
.B S_IXUSR (S_IEXEC)
00100 user has execute permission
.TP
.B S_IRWXG
00070 group has read, write and execute permission
.TP
.B S_IRGRP
00040 group has read permission
.TP
.B S_IWGRP
00020 group has write permission
.TP
.B S_IXGRP
00010 group has execute permission
.TP
.B S_IRWXO
00007 others have read, write and execute permission
.TP
.B S_IROTH
00004 others have read permission
.TP
.B S_IWOTH
00002 others have write permisson
.TP
.B S_IXOTH
00001 others have execute permission
.PP
.I mode
should always be specified when
.B O_CREAT
is in the
.IR flags ,
and is ignored otherwise.
.B creat
is equivalent to
.B open
with
.I flags
equal to
.BR O_CREAT|O_WRONLY|O_TRUNC .
.SH "RETURN VALUE"
.BR open " and " creat
return the new file descriptor, or \-1 if an error occurred (in which case,
.I errno
is set appropriately).
Note that
.B open
can open device special files, but
.B creat
cannot create them - use
.BR mknod (2)
instead.
.LP
On NFS file systems with UID mapping enabled, \fBopen\fP may return a file
descriptor but e.g. \fBread\fP(2) requests are denied with \fBEACCES\fP.
This is because the client performs \fBopen\fP by checking the permissions,
but UID mapping is performed by the server upon read and write requests.
.SH ERRORS
.TP
.B EEXIST
.I pathname
already exists and
.BR O_CREAT " and " O_EXCL
were used.
.TP
.B EISDIR
.I pathname
refers to a directory and the access requested involved writing.
.TP
.B EACCES
The requested access to the file is not allowed, or one of the
directories in
.IR pathname
did not allow search (execute) permission, or the file did not exist
yet and write access to the parent directory is not allowed.
.TP
.B ENAMETOOLONG
.IR pathname " was too long."
.TP
.B ENOENT
A directory component in
.I pathname
does not exist or is a dangling symbolic link.
.TP
.B ENOTDIR
A component used as a directory in
.I pathname
is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
.I pathname
was not a directory.
.TP
.B ENXIO
O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and
no process has the file open for reading.
Or, the file is a device special file and no corresponding device exists.
.TP
.B ENODEV
.I pathname
refers to a device special file and no corresponding device exists.
(This is a Linux kernel bug - in this situation ENXIO must be returned.)
.TP
.B EROFS
.I pathname
refers to a file on a read-only filesystem and write access was
requested.
.TP
.B ETXTBSY
.I pathname
refers to an executable image which is currently being executed and
write access was requested.
.TP
.B EFAULT
.IR pathname " points outside your accessible address space."
.TP
.B ELOOP
Too many symbolic links were encountered in resolving
.IR pathname ,
or \fBO_NOFOLLOW\fR was specified but
.I pathname
was a symbolic link.
.TP
.B ENOSPC
.I pathname
was to be created but the device containing
.I pathname
has no room for the new file.
.TP
.B ENOMEM
Insufficient kernel memory was available.
.TP
.B EMFILE
The process already has the maximum number of files open.
.TP
.B ENFILE
The limit on the total number of files open on the system has been
reached.
.SH "CONFORMING TO"
SVr4, SVID, POSIX, X/OPEN, BSD 4.3
.SH RESTRICTIONS
There are many infelicities in the protocol underlying NFS, affecting
amongst others
.BR O_SYNC " and " O_NDELAY .
POSIX provides for three different variants of synchronised I/O,
corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
\fBO_RSYNC\fR. Currently (2.1.130) these are all synonymous under Linux.
.SH "SEE ALSO"
.BR read (2),
.BR write (2),
.BR fcntl (2),
.BR close (2),
.BR link (2),
.BR mknod (2),
.BR mount (2),
.BR stat (2),
.BR umask (2),
.BR unlink (2),
.BR socket (2),
.BR fopen (3),
.BR fifo (4)
|