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