]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3p/open.3p
Import of man-pages 1.70
[thirdparty/man-pages.git] / man3p / open.3p
1 .\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
2 .TH "OPEN" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
3 .\" open
4 .SH NAME
5 open \- open a file
6 .SH SYNOPSIS
7 .LP
8 \fB#include <sys/stat.h> \fP
9 \fB
10 .br
11 #include <fcntl.h>
12 .br
13 .sp
14 int open(const char *\fP\fIpath\fP\fB, int\fP \fIoflag\fP\fB, ...
15 );
16 .br
17 \fP
18 .SH DESCRIPTION
19 .LP
20 The \fIopen\fP() function shall establish the connection between a
21 file and a file descriptor. It shall create an open file
22 description that refers to a file and a file descriptor that refers
23 to that open file description. The file descriptor is used by
24 other I/O functions to refer to that file. The \fIpath\fP argument
25 points to a pathname naming the file.
26 .LP
27 The \fIopen\fP() function shall return a file descriptor for the named
28 file that is the lowest file descriptor not currently
29 open for that process. The open file description is new, and therefore
30 the file descriptor shall not share it with any other
31 process in the system. The FD_CLOEXEC file descriptor flag associated
32 with the new file descriptor shall be cleared.
33 .LP
34 The file offset used to mark the current position within the file
35 shall be set to the beginning of the file.
36 .LP
37 The file status flags and file access modes of the open file description
38 shall be set according to the value of
39 \fIoflag\fP.
40 .LP
41 Values for \fIoflag\fP are constructed by a bitwise-inclusive OR of
42 flags from the following list, defined in \fI<fcntl.h>\fP. Applications
43 shall specify exactly one of the first three values (file
44 access modes) below in the value of \fIoflag\fP:
45 .TP 7
46 O_RDONLY
47 Open for reading only.
48 .TP 7
49 O_WRONLY
50 Open for writing only.
51 .TP 7
52 O_RDWR
53 Open for reading and writing. The result is undefined if this flag
54 is applied to a FIFO.
55 .sp
56 .LP
57 Any combination of the following may be used:
58 .TP 7
59 O_APPEND
60 If set, the file offset shall be set to the end of the file prior
61 to each write.
62 .TP 7
63 O_CREAT
64 If the file exists, this flag has no effect except as noted under
65 O_EXCL below. Otherwise, the file shall be created; the user
66 ID of the file shall be set to the effective user ID of the process;
67 the group ID of the file shall be set to the group ID of the
68 file's parent directory or to the effective group ID of the process;
69 and the access permission bits (see \fI<sys/stat.h>\fP) of the file
70 mode shall be set to the value of the third argument taken
71 as type \fBmode_t\fP modified as follows: a bitwise AND is performed
72 on the file-mode bits and the corresponding bits in the
73 complement of the process' file mode creation mask. Thus, all bits
74 in the file mode whose corresponding bit in the file mode
75 creation mask is set are cleared. When bits other than the file permission
76 bits are set, the effect is unspecified. The third
77 argument does not affect whether the file is open for reading, writing,
78 or for both. Implementations shall provide a way to
79 initialize the file's group ID to the group ID of the parent directory.
80 Implementations may, but need not, provide an
81 implementation-defined way to initialize the file's group ID to the
82 effective group ID of the calling process.
83 .TP 7
84 O_DSYNC
85 Write I/O operations on the file descriptor shall complete as defined
86 by synchronized I/O data integrity completion.
87 .TP 7
88 O_EXCL
89 If O_CREAT and O_EXCL are set, \fIopen\fP() shall fail if the file
90 exists. The check for the existence of the file and the
91 creation of the file if it does not exist shall be atomic with respect
92 to other threads executing \fIopen\fP() naming the same
93 filename in the same directory with O_EXCL and O_CREAT set. If O_EXCL
94 and O_CREAT are set, and \fIpath\fP names a symbolic link,
95 \fIopen\fP() shall fail and set \fIerrno\fP to [EEXIST], regardless
96 of the contents of the symbolic link. If O_EXCL is set and
97 O_CREAT is not set, the result is undefined.
98 .TP 7
99 O_NOCTTY
100 If set and \fIpath\fP identifies a terminal device, \fIopen\fP() shall
101 not cause the terminal device to become the
102 controlling terminal for the process.
103 .TP 7
104 O_NONBLOCK
105 When opening a FIFO with O_RDONLY or O_WRONLY set:
106 .RS
107 .IP " *" 3
108 If O_NONBLOCK is set, an \fIopen\fP() for reading-only shall return
109 without delay. An \fIopen\fP() for writing-only shall
110 return an error if no process currently has the file open for reading.
111 .LP
112 .IP " *" 3
113 If O_NONBLOCK is clear, an \fIopen\fP() for reading-only shall block
114 the calling thread until a thread opens the file for
115 writing. An \fIopen\fP() for writing-only shall block the calling
116 thread until a thread opens the file for reading.
117 .LP
118 .RE
119 .LP
120 When opening a block special or character special file that supports
121 non-blocking opens:
122 .RS
123 .IP " *" 3
124 If O_NONBLOCK is set, the \fIopen\fP() function shall return without
125 blocking for the device to be ready or available.
126 Subsequent behavior of the device is device-specific.
127 .LP
128 .IP " *" 3
129 If O_NONBLOCK is clear, the \fIopen\fP() function shall block the
130 calling thread until the device is ready or available before
131 returning.
132 .LP
133 .RE
134 .LP
135 Otherwise, the behavior of O_NONBLOCK is unspecified.
136 .TP 7
137 O_RSYNC
138 Read I/O operations on the file descriptor shall complete at the same
139 level of integrity as specified by the O_DSYNC and O_SYNC
140 flags. If both O_DSYNC and O_RSYNC are set in \fIoflag\fP, all I/O
141 operations on the file descriptor shall complete as defined by
142 synchronized I/O data integrity completion. If both O_SYNC and O_RSYNC
143 are set in flags, all I/O operations on the file descriptor
144 shall complete as defined by synchronized I/O file integrity completion.
145 .TP 7
146 O_SYNC
147 Write I/O operations on the file descriptor shall complete as defined
148 by synchronized I/O file integrity completion.
149 .TP 7
150 O_TRUNC
151 If the file exists and is a regular file, and the file is successfully
152 opened O_RDWR or O_WRONLY, its length shall be truncated
153 to 0, and the mode and owner shall be unchanged. It shall have no
154 effect on FIFO special files or terminal device files. Its effect
155 on other file types is implementation-defined. The result of using
156 O_TRUNC with O_RDONLY is undefined.
157 .sp
158 .LP
159 If O_CREAT is set and the file did not previously exist, upon successful
160 completion, \fIopen\fP() shall mark for update the
161 \fIst_atime,\fP \fIst_ctime\fP, and \fIst_mtime\fP fields of the file
162 and the \fIst_ctime\fP and \fIst_mtime\fP fields of the
163 parent directory.
164 .LP
165 If O_TRUNC is set and the file did previously exist, upon successful
166 completion, \fIopen\fP() shall mark for update the
167 \fIst_ctime\fP and \fIst_mtime\fP fields of the file.
168 .LP
169 If both the O_SYNC and O_DSYNC flags are set, the effect is as if
170 only the O_SYNC flag was set.
171 .LP
172 If \fIpath\fP refers to a STREAMS file, \fIoflag\fP may be constructed
173 from O_NONBLOCK OR'ed with either O_RDONLY, O_WRONLY, or
174 O_RDWR. Other flag values are not applicable to STREAMS devices and
175 shall have no effect on them. The value O_NONBLOCK affects the
176 operation of STREAMS drivers and certain functions applied to file
177 descriptors associated with STREAMS files. For STREAMS drivers,
178 the implementation of O_NONBLOCK is device-specific.
179 .LP
180 If \fIpath\fP names the master side of a pseudo-terminal device, then
181 it is unspecified whether \fIopen\fP() locks the slave side
182 so that it cannot be opened. Conforming applications shall call \fIunlockpt\fP()
183 before
184 opening the slave side.
185 .LP
186 The largest value that can be represented correctly in an object of
187 type \fBoff_t\fP shall be established as the offset maximum
188 in the open file description.
189 .SH RETURN VALUE
190 .LP
191 Upon successful completion, the function shall open the file and return
192 a non-negative integer representing the lowest numbered
193 unused file descriptor. Otherwise, -1 shall be returned and \fIerrno\fP
194 set to indicate the error. No files shall be created or
195 modified if the function returns -1.
196 .SH ERRORS
197 .LP
198 The \fIopen\fP() function shall fail if:
199 .TP 7
200 .B EACCES
201 Search permission is denied on a component of the path prefix, or
202 the file exists and the permissions specified by \fIoflag\fP
203 are denied, or the file does not exist and write permission is denied
204 for the parent directory of the file to be created, or
205 O_TRUNC is specified and write permission is denied.
206 .TP 7
207 .B EEXIST
208 O_CREAT and O_EXCL are set, and the named file exists.
209 .TP 7
210 .B EINTR
211 A signal was caught during \fIopen\fP().
212 .TP 7
213 .B EINVAL
214 The implementation does not support synchronized I/O for this file.
215 .TP 7
216 .B EIO
217 The \fIpath\fP argument names a STREAMS file and a hangup or error
218 occurred during the \fIopen\fP().
219 .TP 7
220 .B EISDIR
221 The named file is a directory and \fIoflag\fP includes O_WRONLY or
222 O_RDWR.
223 .TP 7
224 .B ELOOP
225 A loop exists in symbolic links encountered during resolution of the
226 \fIpath\fP argument.
227 .TP 7
228 .B EMFILE
229 {OPEN_MAX} file descriptors are currently open in the calling process.
230 .TP 7
231 .B ENAMETOOLONG
232 The length of the \fIpath\fP argument exceeds {PATH_MAX} or a pathname
233 component is longer than {NAME_MAX}.
234 .TP 7
235 .B ENFILE
236 The maximum allowable number of files is currently open in the system.
237 .TP 7
238 .B ENOENT
239 O_CREAT is not set and the named file does not exist; or O_CREAT is
240 set and either the path prefix does not exist or the
241 \fIpath\fP argument points to an empty string.
242 .TP 7
243 .B ENOSR
244 The \fIpath\fP argument names a STREAMS-based file and the system
245 is unable to allocate a STREAM.
246 .TP 7
247 .B ENOSPC
248 The directory or file system that would contain the new file cannot
249 be expanded, the file does not exist, and O_CREAT is
250 specified.
251 .TP 7
252 .B ENOTDIR
253 A component of the path prefix is not a directory.
254 .TP 7
255 .B ENXIO
256 O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set, and
257 no process has the file open for reading.
258 .TP 7
259 .B ENXIO
260 The named file is a character special or block special file, and the
261 device associated with this special file does not
262 exist.
263 .TP 7
264 .B EOVERFLOW
265 The named file is a regular file and the size of the file cannot be
266 represented correctly in an object of type
267 \fBoff_t\fP.
268 .TP 7
269 .B EROFS
270 The named file resides on a read-only file system and either O_WRONLY,
271 O_RDWR, O_CREAT (if the file does not exist), or O_TRUNC
272 is set in the \fIoflag\fP argument.
273 .sp
274 .LP
275 The \fIopen\fP() function may fail if:
276 .TP 7
277 .B EAGAIN
278 The \fIpath\fP argument names the slave side of a pseudo-terminal
279 device that is locked.
280 .TP 7
281 .B EINVAL
282 The value of the \fIoflag\fP argument is not valid.
283 .TP 7
284 .B ELOOP
285 More than {SYMLOOP_MAX} symbolic links were encountered during resolution
286 of the \fIpath\fP argument.
287 .TP 7
288 .B ENAMETOOLONG
289 As a result of encountering a symbolic link in resolution of the \fIpath\fP
290 argument, the length of the substituted pathname
291 string exceeded {PATH_MAX}.
292 .TP 7
293 .B ENOMEM
294 The \fIpath\fP argument names a STREAMS file and the system is unable
295 to allocate resources.
296 .TP 7
297 .B ETXTBSY
298 The file is a pure procedure (shared text) file that is being executed
299 and \fIoflag\fP is O_WRONLY or O_RDWR.
300 .sp
301 .LP
302 \fIThe following sections are informative.\fP
303 .SH EXAMPLES
304 .SS Opening a File for Writing by the Owner
305 .LP
306 The following example opens the file \fB/tmp/file\fP, either by creating
307 it (if it does not already exist), or by truncating
308 its length to 0 (if it does exist). In the former case, if the call
309 creates a new file, the access permission bits in the file mode
310 of the file are set to permit reading and writing by the owner, and
311 to permit reading only by group members and others.
312 .LP
313 If the call to \fIopen\fP() is successful, the file is opened for
314 writing.
315 .sp
316 .RS
317 .nf
318
319 \fB#include <fcntl.h>
320 \&...
321 int fd;
322 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
323 char *filename = "/tmp/file";
324 \&...
325 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
326 \&...
327 \fP
328 .fi
329 .RE
330 .SS Opening a File Using an Existence Check
331 .LP
332 The following example uses the \fIopen\fP() function to try to create
333 the \fBLOCKFILE\fP file and open it for writing. Since
334 the \fIopen\fP() function specifies the O_EXCL flag, the call fails
335 if the file already exists. In that case, the program assumes
336 that someone else is updating the password file and exits.
337 .sp
338 .RS
339 .nf
340
341 \fB#include <fcntl.h>
342 #include <stdio.h>
343 #include <stdlib.h>
344 .sp
345
346 #define LOCKFILE "/etc/ptmp"
347 \&...
348 int pfd; /* Integer for file descriptor returned by open() call. */
349 \&...
350 if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
351 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
352 {
353 fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\\n");
354 exit(1);
355 }
356 \&...
357 \fP
358 .fi
359 .RE
360 .SS Opening a File for Writing
361 .LP
362 The following example opens a file for writing, creating the file
363 if it does not already exist. If the file does exist, the
364 system truncates the file to zero bytes.
365 .sp
366 .RS
367 .nf
368
369 \fB#include <fcntl.h>
370 #include <stdio.h>
371 #include <stdlib.h>
372 .sp
373
374 #define LOCKFILE "/etc/ptmp"
375 \&...
376 int pfd;
377 char filename[PATH_MAX+1];
378 \&...
379 if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
380 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
381 {
382 perror("Cannot open output file\\n"); exit(1);
383 }
384 \&...
385 \fP
386 .fi
387 .RE
388 .SH APPLICATION USAGE
389 .LP
390 None.
391 .SH RATIONALE
392 .LP
393 Except as specified in this volume of IEEE\ Std\ 1003.1-2001, the
394 flags allowed in \fIoflag\fP are not
395 mutually-exclusive and any number of them may be used simultaneously.
396 .LP
397 Some implementations permit opening FIFOs with O_RDWR. Since FIFOs
398 could be implemented in other ways, and since two file
399 descriptors can be used to the same effect, this possibility is left
400 as undefined.
401 .LP
402 See \fIgetgroups\fP() about the group of a newly created file.
403 .LP
404 The use of \fIopen\fP() to create a regular file is preferable to
405 the use of \fIcreat\fP(), because the latter is redundant and included
406 only for historical reasons.
407 .LP
408 The use of the O_TRUNC flag on FIFOs and directories (pipes cannot
409 be \fIopen\fP()-ed) must be permissible without unexpected
410 side effects (for example, \fIcreat\fP() on a FIFO must not remove
411 data). Since terminal
412 special files might have type-ahead data stored in the buffer, O_TRUNC
413 should not affect their content, particularly if a program
414 that normally opens a regular file should open the current controlling
415 terminal instead. Other file types, particularly
416 implementation-defined ones, are left implementation-defined.
417 .LP
418 IEEE\ Std\ 1003.1-2001 permits [EACCES] to be returned for conditions
419 other than those explicitly listed.
420 .LP
421 The O_NOCTTY flag was added to allow applications to avoid unintentionally
422 acquiring a controlling terminal as a side effect of
423 opening a terminal file. This volume of IEEE\ Std\ 1003.1-2001 does
424 not specify how a controlling terminal is acquired, but
425 it allows an implementation to provide this on \fIopen\fP() if the
426 O_NOCTTY flag is not set and other conditions specified in the
427 Base Definitions volume of IEEE\ Std\ 1003.1-2001, Chapter 11, General
428 Terminal Interface are met. The O_NOCTTY flag is an effective no-op
429 if the file being opened is not a terminal device.
430 .LP
431 In historical implementations the value of O_RDONLY is zero. Because
432 of that, it is not possible to detect the presence of
433 O_RDONLY and another option. Future implementations should encode
434 O_RDONLY and O_WRONLY as bit flags so that:
435 .sp
436 .RS
437 .nf
438
439 \fBO_RDONLY | O_WRONLY == O_RDWR
440 \fP
441 .fi
442 .RE
443 .LP
444 In general, the \fIopen\fP() function follows the symbolic link if
445 \fIpath\fP names a symbolic link. However, the
446 \fIopen\fP() function, when called with O_CREAT and O_EXCL, is required
447 to fail with [EEXIST] if \fIpath\fP names an existing
448 symbolic link, even if the symbolic link refers to a nonexistent file.
449 This behavior is required so that privileged applications
450 can create a new file in a known location without the possibility
451 that a symbolic link might cause the file to be created in a
452 different location.
453 .LP
454 For example, a privileged application that must create a file with
455 a predictable name in a user-writable directory, such as the
456 user's home directory, could be compromised if the user creates a
457 symbolic link with that name that refers to a nonexistent file in
458 a system directory. If the user can influence the contents of a file,
459 the user could compromise the system by creating a new system
460 configuration or spool file that would then be interpreted by the
461 system. The test for a symbolic link which refers to a
462 nonexisting file must be atomic with the creation of a new file.
463 .LP
464 The POSIX.1-1990 standard required that the group ID of a newly created
465 file be set to the group ID of its parent directory or
466 to the effective group ID of the creating process. FIPS 151-2 required
467 that implementations provide a way to have the group ID be
468 set to the group ID of the containing directory, but did not prohibit
469 implementations also supporting a way to set the group ID to
470 the effective group ID of the creating process. Conforming applications
471 should not assume which group ID will be used. If it
472 matters, an application can use \fIchown\fP() to set the group ID
473 after the file is created,
474 or determine under what conditions the implementation will set the
475 desired group ID.
476 .SH FUTURE DIRECTIONS
477 .LP
478 None.
479 .SH SEE ALSO
480 .LP
481 \fIchmod\fP() , \fIclose\fP() , \fIcreat\fP() , \fIdup\fP() , \fIfcntl\fP()
482 , \fIlseek\fP() , \fIread\fP() , \fIumask\fP() , \fIunlockpt\fP()
483 , \fIwrite\fP() , the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
484 \fI<fcntl.h>\fP, \fI<sys/stat.h>\fP, \fI<sys/types.h>\fP
485 .SH COPYRIGHT
486 Portions of this text are reprinted and reproduced in electronic form
487 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
488 -- Portable Operating System Interface (POSIX), The Open Group Base
489 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
490 Electrical and Electronics Engineers, Inc and The Open Group. In the
491 event of any discrepancy between this version and the original IEEE and
492 The Open Group Standard, the original IEEE and The Open Group Standard
493 is the referee document. The original Standard can be obtained online at
494 http://www.opengroup.org/unix/online.html .