]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Hey Emacs! This file is -*- nroff -*- source. |
2 | .\" | |
3 | .\" This manpage is Copyright (C) 1992 Drew Eckhardt; | |
4 | .\" 1993 Michael Haardt, Ian Jackson. | |
5 | .\" | |
6 | .\" Permission is granted to make and distribute verbatim copies of this | |
7 | .\" manual provided the copyright notice and this permission notice are | |
8 | .\" preserved on all copies. | |
9 | .\" | |
10 | .\" Permission is granted to copy and distribute modified versions of this | |
11 | .\" manual under the conditions for verbatim copying, provided that the | |
12 | .\" entire resulting derived work is distributed under the terms of a | |
13 | .\" permission notice identical to this one. | |
c13182ef | 14 | .\" |
fea681da MK |
15 | .\" Since the Linux kernel and libraries are constantly changing, this |
16 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
17 | .\" responsibility for errors or omissions, or for damages resulting from | |
18 | .\" the use of the information contained herein. The author(s) may not | |
19 | .\" have taken the same level of care in the production of this manual, | |
20 | .\" which is licensed free of charge, as they might when working | |
21 | .\" professionally. | |
c13182ef | 22 | .\" |
fea681da MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
25 | .\" | |
26 | .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu> | |
27 | .\" Modified 1994-08-21 by Michael Haardt | |
28 | .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl> | |
29 | .\" Modified 1996-05-13 by Thomas Koenig | |
30 | .\" Modified 1996-12-20 by Michael Haardt | |
31 | .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl> | |
32 | .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk> | |
33 | .\" Modified 1999-06-03 by Michael Haardt | |
c11b1abf MK |
34 | .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com> |
35 | .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com> | |
1c1e15ed MK |
36 | .\" 2004-12-08, mtk, reordered flags list alphabetically |
37 | .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME | |
fe75ec04 | 38 | .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits |
447bb15e | 39 | .\" 2008-01-03, mtk, with input from Trond Myklebust |
f4b9d6a5 MK |
40 | .\" <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi> |
41 | .\" Rewrite description of O_EXCL. | |
fea681da | 42 | .\" |
f4b9d6a5 | 43 | .TH OPEN 2 2008-01-03 "Linux" "Linux Programmer's Manual" |
fea681da MK |
44 | .SH NAME |
45 | open, creat \- open and possibly create a file or device | |
46 | .SH SYNOPSIS | |
47 | .nf | |
48 | .B #include <sys/types.h> | |
49 | .B #include <sys/stat.h> | |
50 | .B #include <fcntl.h> | |
51 | .sp | |
52 | .BI "int open(const char *" pathname ", int " flags ); | |
53 | .BI "int open(const char *" pathname ", int " flags ", mode_t " mode ); | |
5895e7eb | 54 | |
fea681da MK |
55 | .BI "int creat(const char *" pathname ", mode_t " mode ); |
56 | .fi | |
57 | .SH DESCRIPTION | |
e366dbc4 | 58 | Given a |
0daa9e92 | 59 | .I pathname |
e366dbc4 | 60 | for a file, |
1f6ceb40 | 61 | .BR open () |
c13182ef | 62 | returns a file descriptor, a small, non-negative integer |
e366dbc4 MK |
63 | for use in subsequent system calls |
64 | .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)." | |
65 | The file descriptor returned by a successful call will be | |
2c4bff36 | 66 | the lowest-numbered file descriptor not currently open for the process. |
e366dbc4 | 67 | .PP |
fe75ec04 | 68 | By default, the new file descriptor is set to remain open across an |
e366dbc4 | 69 | .BR execve (2) |
1f6ceb40 MK |
70 | (i.e., the |
71 | .B FD_CLOEXEC | |
72 | file descriptor flag described in | |
73 | .BR fcntl (2) | |
fe75ec04 MK |
74 | is initially disabled; the Linux-specific |
75 | .B O_CLOEXEC | |
76 | flag, described below, can be used to change this default). | |
1f6ceb40 | 77 | The file offset is set to the beginning of the file (see |
c13182ef | 78 | .BR lseek (2)). |
e366dbc4 MK |
79 | .PP |
80 | A call to | |
81 | .BR open () | |
82 | creates a new | |
83 | .IR "open file description" , | |
84 | an entry in the system-wide table of open files. | |
e366dbc4 MK |
85 | This entry records the file offset and the file status flags |
86 | (modifiable via the | |
0bfa087b | 87 | .BR fcntl (2) |
e366dbc4 MK |
88 | .B F_SETFL |
89 | operation). | |
2c4bff36 MK |
90 | A file descriptor is a reference to one of these entries; |
91 | this reference is unaffected if | |
92 | .I pathname | |
93 | is subsequently removed or modified to refer to a different file. | |
e366dbc4 | 94 | The new open file description is initially not shared |
2c4bff36 MK |
95 | with any other process, |
96 | but sharing may arise via | |
97 | .BR fork (2). | |
e366dbc4 | 98 | .PP |
fea681da MK |
99 | The parameter |
100 | .I flags | |
e366dbc4 MK |
101 | must include one of the following |
102 | .IR "access modes" : | |
c7992edc | 103 | .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR . |
e366dbc4 MK |
104 | These request opening the file read-only, write-only, or read/write, |
105 | respectively. | |
bfe9ba67 MK |
106 | |
107 | In addition, zero or more file creation flags and file status flags | |
c13182ef | 108 | can be |
fea681da | 109 | .RI bitwise- or 'd |
e366dbc4 | 110 | in |
bfe9ba67 | 111 | .IR flags . |
c13182ef MK |
112 | The |
113 | .I file creation flags | |
114 | are | |
bfe9ba67 | 115 | .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC . |
c13182ef MK |
116 | The |
117 | .I file status flags | |
bfe9ba67 MK |
118 | are all of the remaining flags listed below. |
119 | The distinction between these two groups of flags is that | |
120 | the file status flags can be retrieved and (in some cases) | |
121 | modified using | |
122 | .BR fcntl (2). | |
123 | The full list of file creation flags and file status flags is as follows: | |
fea681da | 124 | .TP |
1c1e15ed | 125 | .B O_APPEND |
c13182ef MK |
126 | The file is opened in append mode. |
127 | Before each | |
0bfa087b | 128 | .BR write (2), |
1e568304 | 129 | the file offset is positioned at the end of the file, |
1c1e15ed | 130 | as if with |
0bfa087b | 131 | .BR lseek (2). |
1c1e15ed MK |
132 | .B O_APPEND |
133 | may lead to corrupted files on NFS file systems if more than one process | |
c13182ef MK |
134 | appends data to a file at once. |
135 | This is because NFS does not support | |
1c1e15ed MK |
136 | appending to a file, so the client kernel has to simulate it, which |
137 | can't be done without a race condition. | |
138 | .TP | |
139 | .B O_ASYNC | |
b50582eb | 140 | Enable signal-driven I/O: |
8bd58774 MK |
141 | generate a signal |
142 | .RB ( SIGIO | |
143 | by default, but this can be changed via | |
1c1e15ed MK |
144 | .BR fcntl (2)) |
145 | when input or output becomes possible on this file descriptor. | |
c13182ef | 146 | This feature is only available for terminals, pseudo-terminals, |
1f6ceb40 MK |
147 | sockets, and (since Linux 2.6) pipes and FIFOs. |
148 | See | |
1c1e15ed MK |
149 | .BR fcntl (2) |
150 | for further details. | |
fe75ec04 MK |
151 | .TP |
152 | .BR O_CLOEXEC " (Since Linux 2.6.23)" | |
153 | Enable the close-on-exec flag for the new file descriptor. | |
fb7339df | 154 | Specifying this flag permits a program to avoid an additional |
fe75ec04 MK |
155 | .BR fcntl (2) |
156 | .B F_SETFD | |
157 | operation to set the | |
0daa9e92 | 158 | .B FD_CLOEXEC |
fe75ec04 MK |
159 | flag. |
160 | Additionally, | |
161 | use of this flag is essential in some multithreaded programs | |
162 | since using a separate | |
163 | .BR fcntl (2) | |
164 | .B F_SETFD | |
165 | operation to set the | |
0daa9e92 | 166 | .B FD_CLOEXEC |
fe75ec04 MK |
167 | flag does not suffice to avoid race conditions |
168 | where one thread opens a file descriptor at the same | |
169 | time as another thread does a | |
170 | .BR fork (2) | |
171 | plus | |
172 | .BR execve (2). | |
173 | .\" This flag fixes only one form of the race condition; | |
174 | .\" The race can also occur with, for example, descriptors | |
175 | .\" returned by accept(), pipe(), etc. | |
1c1e15ed | 176 | .TP |
fea681da MK |
177 | .B O_CREAT |
178 | If the file does not exist it will be created. | |
179 | The owner (user ID) of the file is set to the effective user ID | |
c13182ef MK |
180 | of the process. |
181 | The group ownership (group ID) is set either to | |
fea681da MK |
182 | the effective group ID of the process or to the group ID of the |
183 | parent directory (depending on filesystem type and mount options, | |
75b94dc3 | 184 | and the mode of the parent directory, see, for example, the mount options |
fea681da MK |
185 | .I bsdgroups |
186 | and | |
187 | .I sysvgroups | |
188 | of the ext2 filesystem, as described in | |
189 | .BR mount (8)). | |
190 | .TP | |
fe75ec04 | 191 | .BR O_DIRECT " (Since Linux 2.6.10)" |
1c1e15ed MK |
192 | Try to minimize cache effects of the I/O to and from this file. |
193 | In general this will degrade performance, but it is useful in | |
194 | special situations, such as when applications do their own caching. | |
195 | File I/O is done directly to/from user space buffers. | |
75b94dc3 | 196 | The I/O is synchronous, that is, at the completion of a |
1c1e15ed MK |
197 | .BR read (2) |
198 | or | |
e366dbc4 MK |
199 | .BR write (2), |
200 | data is guaranteed to have been transferred. | |
1c1e15ed MK |
201 | Under Linux 2.4 transfer sizes, and the alignment of user buffer |
202 | and file offset must all be multiples of the logical block size | |
c13182ef MK |
203 | of the file system. |
204 | Under Linux 2.6 alignment to 512-byte boundaries | |
1c1e15ed MK |
205 | suffices. |
206 | .\" Alignment should satisfy requirements for the underlying device | |
207 | .\" There may be coherency problems. | |
9b54d4fa | 208 | .sp |
c13182ef | 209 | A semantically similar (but deprecated) interface for block devices |
9b54d4fa | 210 | is described in |
1c1e15ed MK |
211 | .BR raw (8). |
212 | .TP | |
213 | .B O_DIRECTORY | |
a8d55537 | 214 | If \fIpathname\fP is not a directory, cause the open to fail. |
9f8d688a MK |
215 | .\" But see the following and its replies: |
216 | .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2 | |
217 | .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail | |
218 | .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored. | |
8382f16d | 219 | This flag is Linux-specific, and was added in kernel version 2.1.126, to |
60a90ecd MK |
220 | avoid denial-of-service problems if |
221 | .BR opendir (3) | |
222 | is called on a | |
1c1e15ed | 223 | FIFO or tape device, but should not be used outside of the |
9e370fba MK |
224 | implementation of |
225 | .BR opendir (3). | |
1c1e15ed | 226 | .TP |
fea681da | 227 | .B O_EXCL |
f4b9d6a5 MK |
228 | Ensure that this call creates the file: |
229 | if this flag is specified in conjunction with | |
fea681da | 230 | .BR O_CREAT , |
f4b9d6a5 MK |
231 | and |
232 | .I pathname | |
233 | already exists, then | |
1c1e15ed | 234 | .BR open () |
c13182ef | 235 | will fail. |
f4b9d6a5 MK |
236 | The behavior of |
237 | .B O_EXCL | |
238 | is undefined if | |
239 | .B O_CREAT | |
240 | is not specified. | |
241 | ||
242 | When these two flags are specified, symbolic links are not followed: | |
243 | .\" POSIX.1-2001 explicitly requires this behavior. | |
244 | if | |
245 | .I pathname | |
246 | is a symbolic link, then | |
247 | .BR open () | |
248 | fails regardless of where the symbolic link points to. | |
249 | ||
250 | .B O_EXCL | |
7db44341 | 251 | is not supported on NFSv2 or on Linux before kernel 2.6; |
f4b9d6a5 MK |
252 | it is supported on Linux 2.6 and later, with NFSv3 or later. |
253 | In environments where NFS | |
fea681da | 254 | .B O_EXCL |
f4b9d6a5 MK |
255 | support is not provided, programs that rely on it |
256 | for performing locking tasks will contain a race condition. | |
257 | Portable programs that want to perform atomic file locking using a lockfile, | |
258 | and need to avoid reliance on NFS support for | |
259 | .BR O_EXCL , | |
260 | can create a unique file on | |
261 | the same file system (e.g., incorporating hostname and PID), and use | |
fea681da | 262 | .BR link (2) |
c13182ef | 263 | to make a link to the lockfile. |
60a90ecd MK |
264 | If |
265 | .BR link (2) | |
f4b9d6a5 | 266 | returns 0, the lock is successful. |
c13182ef | 267 | Otherwise, use |
fea681da MK |
268 | .BR stat (2) |
269 | on the unique file to check if its link count has increased to 2, | |
270 | in which case the lock is also successful. | |
271 | .TP | |
1c1e15ed MK |
272 | .B O_LARGEFILE |
273 | (LFS) | |
274 | Allow files whose sizes cannot be represented in an | |
8478ee02 | 275 | .I off_t |
1c1e15ed | 276 | (but can be represented in an |
8478ee02 | 277 | .IR off64_t ) |
1c1e15ed | 278 | to be opened. |
c13182ef | 279 | The |
bcdd964e MK |
280 | .B _LARGEFILE64_SOURCE |
281 | macro must be defined in order to obtain this definition. | |
c13182ef | 282 | Setting the |
bcdd964e | 283 | .B _FILE_OFFSET_BITS |
9f3d8b28 MK |
284 | feature test macro to 64 (rather than using |
285 | .BR O_LARGEFILE ) | |
286 | is the preferred method of obtaining | |
287 | method of accessing large files on 32-bit systems (see | |
2dcbf4f7 | 288 | .BR feature_test_macros (7)). |
1c1e15ed | 289 | .TP |
fe75ec04 | 290 | .BR O_NOATIME " (Since Linux 2.6.8)" |
310b7919 MK |
291 | Do not update the file last access time (st_atime in the inode) |
292 | when the file is | |
1c1e15ed MK |
293 | .BR read (2). |
294 | This flag is intended for use by indexing or backup programs, | |
295 | where its use can significantly reduce the amount of disk activity. | |
296 | This flag may not be effective on all filesystems. | |
297 | One example is NFS, where the server maintains the access time. | |
0e1ad98c | 298 | .\" The O_NOATIME flag also affects the treatment of st_atime |
92057f4d | 299 | .\" by mmap() and readdir(2), MTK, Dec 04. |
1c1e15ed | 300 | .TP |
fea681da MK |
301 | .B O_NOCTTY |
302 | If | |
303 | .I pathname | |
304 | refers to a terminal device \(em see | |
305 | .BR tty (4) | |
306 | \(em it will not become the process's controlling terminal even if the | |
307 | process does not have one. | |
308 | .TP | |
1c1e15ed | 309 | .B O_NOFOLLOW |
a8d55537 | 310 | If \fIpathname\fP is a symbolic link, then the open fails. |
c13182ef | 311 | This is a FreeBSD extension, which was added to Linux in version 2.1.126. |
1c1e15ed | 312 | Symbolic links in earlier components of the pathname will still be |
e366dbc4 MK |
313 | followed. |
314 | .\" The headers from glibc 2.0.100 and later include a | |
315 | .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if | |
a8d55537 | 316 | .\" used\fP. |
fea681da MK |
317 | .TP |
318 | .BR O_NONBLOCK " or " O_NDELAY | |
c13182ef MK |
319 | When possible, the file is opened in non-blocking mode. |
320 | Neither the | |
1c1e15ed | 321 | .BR open () |
fea681da MK |
322 | nor any subsequent operations on the file descriptor which is |
323 | returned will cause the calling process to wait. | |
324 | For the handling of FIFOs (named pipes), see also | |
af5b2ef2 | 325 | .BR fifo (7). |
db28bfac | 326 | For a discussion of the effect of |
0daa9e92 | 327 | .B O_NONBLOCK |
db28bfac MK |
328 | in conjunction with mandatory file locks and with file leases, see |
329 | .BR fcntl (2). | |
fea681da MK |
330 | .TP |
331 | .B O_SYNC | |
c13182ef MK |
332 | The file is opened for synchronous I/O. |
333 | Any | |
0bfa087b | 334 | .BR write (2)s |
fea681da MK |
335 | on the resulting file descriptor will block the calling process until |
336 | the data has been physically written to the underlying hardware. | |
b07cd0a9 | 337 | .IR "But see NOTES below" . |
fea681da | 338 | .TP |
1c1e15ed MK |
339 | .B O_TRUNC |
340 | If the file already exists and is a regular file and the open mode allows | |
682edefb MK |
341 | writing (i.e., is |
342 | .B O_RDWR | |
343 | or | |
344 | .BR O_WRONLY ) | |
345 | it will be truncated to length 0. | |
346 | If the file is a FIFO or terminal device file, the | |
347 | .B O_TRUNC | |
c13182ef | 348 | flag is ignored. |
682edefb MK |
349 | Otherwise the effect of |
350 | .B O_TRUNC | |
351 | is unspecified. | |
fea681da MK |
352 | .PP |
353 | Some of these optional flags can be altered using | |
0bfa087b | 354 | .BR fcntl (2) |
fea681da MK |
355 | after the file has been opened. |
356 | ||
357 | The argument | |
358 | .I mode | |
c13182ef MK |
359 | specifies the permissions to use in case a new file is created. |
360 | It is | |
fea681da | 361 | modified by the process's |
0daa9e92 | 362 | .I umask |
fea681da | 363 | in the usual way: the permissions of the created file are |
66ee0c7e | 364 | .IR "(mode & ~umask)" . |
fea681da MK |
365 | Note that this mode only applies to future accesses of the |
366 | newly created file; the | |
1c1e15ed | 367 | .BR open () |
fea681da MK |
368 | call that creates a read-only file may well return a read/write |
369 | file descriptor. | |
370 | .PP | |
371 | The following symbolic constants are provided for | |
372 | .IR mode : | |
373 | .TP | |
374 | .B S_IRWXU | |
375 | 00700 user (file owner) has read, write and execute permission | |
376 | .TP | |
cc513f7f | 377 | .B S_IRUSR |
fea681da MK |
378 | 00400 user has read permission |
379 | .TP | |
cc513f7f | 380 | .B S_IWUSR |
fea681da MK |
381 | 00200 user has write permission |
382 | .TP | |
cc513f7f | 383 | .B S_IXUSR |
fea681da MK |
384 | 00100 user has execute permission |
385 | .TP | |
386 | .B S_IRWXG | |
387 | 00070 group has read, write and execute permission | |
388 | .TP | |
389 | .B S_IRGRP | |
390 | 00040 group has read permission | |
391 | .TP | |
392 | .B S_IWGRP | |
393 | 00020 group has write permission | |
394 | .TP | |
395 | .B S_IXGRP | |
396 | 00010 group has execute permission | |
397 | .TP | |
398 | .B S_IRWXO | |
399 | 00007 others have read, write and execute permission | |
400 | .TP | |
401 | .B S_IROTH | |
402 | 00004 others have read permission | |
403 | .TP | |
404 | .B S_IWOTH | |
d301ee6c | 405 | 00002 others have write permission |
fea681da MK |
406 | .TP |
407 | .B S_IXOTH | |
408 | 00001 others have execute permission | |
409 | .PP | |
410 | .I mode | |
411 | must be specified when | |
412 | .B O_CREAT | |
413 | is in the | |
414 | .IR flags , | |
415 | and is ignored otherwise. | |
416 | ||
1c1e15ed | 417 | .BR creat () |
fea681da | 418 | is equivalent to |
1c1e15ed | 419 | .BR open () |
fea681da MK |
420 | with |
421 | .I flags | |
422 | equal to | |
423 | .BR O_CREAT|O_WRONLY|O_TRUNC . | |
424 | .SH "RETURN VALUE" | |
c13182ef MK |
425 | .BR open () |
426 | and | |
e1d6264d | 427 | .BR creat () |
1c1e15ed MK |
428 | return the new file descriptor, or \-1 if an error occurred |
429 | (in which case, | |
fea681da MK |
430 | .I errno |
431 | is set appropriately). | |
fea681da MK |
432 | .SH ERRORS |
433 | .TP | |
434 | .B EACCES | |
435 | The requested access to the file is not allowed, or search permission | |
436 | is denied for one of the directories in the path prefix of | |
437 | .IR pathname , | |
438 | or the file did not exist yet and write access to the parent directory | |
439 | is not allowed. | |
440 | (See also | |
ad7cc990 | 441 | .BR path_resolution (7).) |
fea681da MK |
442 | .TP |
443 | .B EEXIST | |
444 | .I pathname | |
445 | already exists and | |
446 | .BR O_CREAT " and " O_EXCL | |
447 | were used. | |
448 | .TP | |
449 | .B EFAULT | |
0daa9e92 | 450 | .I pathname |
e1d6264d | 451 | points outside your accessible address space. |
fea681da | 452 | .TP |
9f5773f7 MK |
453 | .B EFBIG |
454 | .I pathname | |
682edefb MK |
455 | refers to a regular file, too large to be opened; see |
456 | .B O_LARGEFILE | |
457 | above. | |
c13182ef | 458 | (POSIX.1-2001 specifies the error |
9f5773f7 MK |
459 | .B EOVERFLOW |
460 | for this case.) | |
0e1ad98c | 461 | .\" FIXME . Maybe this deviation from the standard will get repaired. |
9f5773f7 MK |
462 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253 |
463 | .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW" | |
464 | .\" Reported 2006-10-03 | |
465 | .TP | |
fea681da MK |
466 | .B EISDIR |
467 | .I pathname | |
468 | refers to a directory and the access requested involved writing | |
469 | (that is, | |
470 | .B O_WRONLY | |
471 | or | |
472 | .B O_RDWR | |
473 | is set). | |
474 | .TP | |
475 | .B ELOOP | |
476 | Too many symbolic links were encountered in resolving | |
477 | .IR pathname , | |
a8d55537 | 478 | or \fBO_NOFOLLOW\fP was specified but |
fea681da MK |
479 | .I pathname |
480 | was a symbolic link. | |
481 | .TP | |
482 | .B EMFILE | |
483 | The process already has the maximum number of files open. | |
484 | .TP | |
485 | .B ENAMETOOLONG | |
0daa9e92 | 486 | .I pathname |
e1d6264d | 487 | was too long. |
fea681da MK |
488 | .TP |
489 | .B ENFILE | |
490 | The system limit on the total number of open files has been reached. | |
491 | .TP | |
492 | .B ENODEV | |
493 | .I pathname | |
494 | refers to a device special file and no corresponding device exists. | |
682edefb MK |
495 | (This is a Linux kernel bug; in this situation |
496 | .B ENXIO | |
497 | must be returned.) | |
fea681da MK |
498 | .TP |
499 | .B ENOENT | |
682edefb MK |
500 | .B O_CREAT |
501 | is not set and the named file does not exist. | |
fea681da MK |
502 | Or, a directory component in |
503 | .I pathname | |
504 | does not exist or is a dangling symbolic link. | |
505 | .TP | |
506 | .B ENOMEM | |
507 | Insufficient kernel memory was available. | |
508 | .TP | |
509 | .B ENOSPC | |
510 | .I pathname | |
511 | was to be created but the device containing | |
512 | .I pathname | |
513 | has no room for the new file. | |
514 | .TP | |
515 | .B ENOTDIR | |
516 | A component used as a directory in | |
517 | .I pathname | |
a8d55537 | 518 | is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and |
fea681da MK |
519 | .I pathname |
520 | was not a directory. | |
521 | .TP | |
522 | .B ENXIO | |
682edefb MK |
523 | .BR O_NONBLOCK " | " O_WRONLY |
524 | is set, the named file is a FIFO and | |
fea681da MK |
525 | no process has the file open for reading. |
526 | Or, the file is a device special file and no corresponding device exists. | |
527 | .TP | |
1c1e15ed MK |
528 | .B EPERM |
529 | The | |
530 | .B O_NOATIME | |
531 | flag was specified, but the effective user ID of the caller | |
532 | .\" Strictly speaking, it's the file system UID... (MTK) | |
533 | did not match the owner of the file and the caller was not privileged | |
534 | .RB ( CAP_FOWNER ). | |
535 | .TP | |
fea681da MK |
536 | .B EROFS |
537 | .I pathname | |
538 | refers to a file on a read-only filesystem and write access was | |
539 | requested. | |
540 | .TP | |
541 | .B ETXTBSY | |
542 | .I pathname | |
543 | refers to an executable image which is currently being executed and | |
544 | write access was requested. | |
d3952311 MK |
545 | .TP |
546 | .B EWOULDBLOCK | |
547 | The | |
548 | .B O_NONBLOCK | |
549 | flag was specified, and an incompatible lease was held on the file | |
550 | (see | |
551 | .BR fcntl (2)). | |
fea681da | 552 | .SH "CONFORMING TO" |
97c1eac8 | 553 | SVr4, 4.3BSD, POSIX.1-2001. |
fea681da | 554 | The |
fe75ec04 MK |
555 | .BR O_CLOEXEC , |
556 | .BR O_DIRECTORY , | |
1c1e15ed | 557 | .BR O_NOATIME , |
fea681da | 558 | and |
0daa9e92 | 559 | .B O_NOFOLLOW |
8382f16d | 560 | flags are Linux-specific. |
fea681da MK |
561 | One may have to define the |
562 | .B _GNU_SOURCE | |
563 | macro to get their definitions. | |
0daa9e92 | 564 | .B O_DIRECT |
fb7339df | 565 | is not specified in POSIX; one has to define |
fe75ec04 MK |
566 | .B _GNU_SOURCE |
567 | to get its definition. | |
a1d5f77c | 568 | .SH NOTES |
988db661 | 569 | Under Linux, the |
a1d5f77c MK |
570 | .B O_NONBLOCK |
571 | flag indicates that one wants to open | |
572 | but does not necessarily have the intention to read or write. | |
573 | This is typically used to open devices in order to get a file descriptor | |
574 | for use with | |
575 | .BR ioctl (2). | |
fea681da MK |
576 | .LP |
577 | The (undefined) effect of | |
578 | .B O_RDONLY | O_TRUNC | |
c13182ef | 579 | varies among implementations. |
bcdd964e | 580 | On many systems the file is actually truncated. |
fea681da MK |
581 | .\" Linux 2.0, 2.5: truncate |
582 | .\" Solaris 5.7, 5.8: truncate | |
583 | .\" Irix 6.5: truncate | |
584 | .\" Tru64 5.1B: truncate | |
585 | .\" HP-UX 11.22: truncate | |
586 | .\" FreeBSD 4.7: truncate | |
587 | .LP | |
588 | The | |
589 | .B O_DIRECT | |
590 | flag was introduced in SGI IRIX, where it has alignment restrictions | |
c13182ef MK |
591 | similar to those of Linux 2.4. |
592 | IRIX has also a fcntl(2) call to | |
593 | query appropriate alignments, and sizes. | |
594 | FreeBSD 4.x introduced | |
fea681da MK |
595 | a flag of same name, but without alignment restrictions. |
596 | Support was added under Linux in kernel version 2.4.10. | |
597 | Older Linux kernels simply ignore this flag. | |
1f6ceb40 MK |
598 | One may have to define the |
599 | .B _GNU_SOURCE | |
600 | macro to get its definition. | |
a1d5f77c MK |
601 | .PP |
602 | There are many infelicities in the protocol underlying NFS, affecting | |
603 | amongst others | |
604 | .BR O_SYNC " and " O_NDELAY . | |
605 | ||
d9bfdb9c | 606 | POSIX provides for three different variants of synchronized I/O, |
a8d55537 MK |
607 | corresponding to the flags \fBO_SYNC\fP, \fBO_DSYNC\fP and |
608 | \fBO_RSYNC\fP. | |
a1d5f77c MK |
609 | Currently (2.1.130) these are all synonymous under Linux. |
610 | ||
611 | Note that | |
612 | .BR open () | |
613 | can open device special files, but | |
614 | .BR creat () | |
615 | cannot create them; use | |
616 | .BR mknod (2) | |
617 | instead. | |
618 | .LP | |
619 | On NFS file systems with UID mapping enabled, | |
620 | .BR open () | |
621 | may | |
75b94dc3 | 622 | return a file descriptor but, for example, |
a1d5f77c MK |
623 | .BR read (2) |
624 | requests are denied | |
625 | with \fBEACCES\fP. | |
626 | This is because the client performs | |
627 | .BR open () | |
628 | by checking the | |
629 | permissions, but UID mapping is performed by the server upon | |
630 | read and write requests. | |
631 | ||
632 | If the file is newly created, its | |
988db661 | 633 | .IR st_atime , |
a1d5f77c MK |
634 | .IR st_ctime , |
635 | .I st_mtime | |
636 | fields | |
637 | (respectively, time of last access, time of last status change, and | |
638 | time of last modification; see | |
639 | .BR stat (2)) | |
640 | are set | |
641 | to the current time, and so are the | |
642 | .I st_ctime | |
988db661 | 643 | and |
a1d5f77c MK |
644 | .I st_mtime |
645 | fields of the | |
646 | parent directory. | |
988db661 | 647 | Otherwise, if the file is modified because of the |
a1d5f77c MK |
648 | .B O_TRUNC |
649 | flag, its st_ctime and st_mtime fields are set to the current time. | |
fea681da MK |
650 | .SH BUGS |
651 | "The thing that has always disturbed me about O_DIRECT is that the whole | |
652 | interface is just stupid, and was probably designed by a deranged monkey | |
e9496f74 | 653 | on some serious mind-controlling substances." \(em Linus |
b50582eb MK |
654 | |
655 | Currently, it is not possible to enable signal-driven | |
656 | I/O by specifying | |
657 | .B O_ASYNC | |
c13182ef | 658 | when calling |
b50582eb MK |
659 | .BR open (); |
660 | use | |
661 | .BR fcntl (2) | |
662 | to enable this flag. | |
0e1ad98c | 663 | .\" FIXME . Check bugzilla report on open(O_ASYNC) |
92057f4d | 664 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993 |
fea681da MK |
665 | .SH "SEE ALSO" |
666 | .BR close (2), | |
e366dbc4 | 667 | .BR dup (2), |
fea681da MK |
668 | .BR fcntl (2), |
669 | .BR link (2), | |
1f6ceb40 | 670 | .BR lseek (2), |
fea681da MK |
671 | .BR mknod (2), |
672 | .BR mount (2), | |
e366dbc4 | 673 | .BR mmap (2), |
28c54d45 | 674 | .BR openat (2), |
fea681da MK |
675 | .BR read (2), |
676 | .BR socket (2), | |
677 | .BR stat (2), | |
678 | .BR umask (2), | |
679 | .BR unlink (2), | |
680 | .BR write (2), | |
681 | .BR fopen (3), | |
50e5322c | 682 | .BR fifo (7), |
ad7cc990 MK |
683 | .BR feature_test_macros (7), |
684 | .BR path_resolution (7) |