1OPEN(2)                    Linux Programmer's Manual                   OPEN(2)
2
3
4

NAME

6       open, creat - open and possibly create a file or device
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/stat.h>
11       #include <fcntl.h>
12
13       int open(const char *pathname, int flags);
14       int open(const char *pathname, int flags, mode_t mode);
15
16       int creat(const char *pathname, mode_t mode);
17

DESCRIPTION

19       Given a pathname for a file, open() returns a file descriptor, a small,
20       nonnegative integer  for  use  in  subsequent  system  calls  (read(2),
21       write(2), lseek(2), fcntl(2), etc.).  The file descriptor returned by a
22       successful call will be the lowest-numbered file  descriptor  not  cur‐
23       rently open for the process.
24
25       By  default,  the  new  file descriptor is set to remain open across an
26       execve(2) (i.e., the  FD_CLOEXEC  file  descriptor  flag  described  in
27       fcntl(2)  is  initially  disabled;  the  Linux-specific O_CLOEXEC flag,
28       described below, can be used to change this default).  The file  offset
29       is set to the beginning of the file (see lseek(2)).
30
31       A  call  to open() creates a new open file description, an entry in the
32       system-wide table of open files.  This entry records  the  file  offset
33       and  the  file status flags (modifiable via the fcntl(2) F_SETFL opera‐
34       tion).  A file descriptor is a reference to one of these entries;  this
35       reference is unaffected if pathname is subsequently removed or modified
36       to refer to a different file.  The new open file  description  is  ini‐
37       tially  not  shared  with  any other process, but sharing may arise via
38       fork(2).
39
40       The argument flags must include one  of  the  following  access  modes:
41       O_RDONLY,  O_WRONLY,  or  O_RDWR.  These request opening the file read-
42       only, write-only, or read/write, respectively.
43
44       In addition, zero or more file creation flags and file status flags can
45       be bitwise-or'd in flags.  The file creation flags are O_CREAT, O_EXCL,
46       O_NOCTTY, and O_TRUNC.  The file status flags are all of the  remaining
47       flags  listed below.  The distinction between these two groups of flags
48       is that the file status flags can be retrieved and (in some cases) mod‐
49       ified  using  fcntl(2).   The full list of file creation flags and file
50       status flags is as follows:
51
52       O_APPEND
53              The file is opened in append mode.  Before  each  write(2),  the
54              file  offset  is  positioned  at the end of the file, as if with
55              lseek(2).  O_APPEND may lead to corrupted files on NFS file sys‐
56              tems  if  more  than one process appends data to a file at once.
57              This is because NFS does not support appending to a file, so the
58              client  kernel has to simulate it, which can't be done without a
59              race condition.
60
61       O_ASYNC
62              Enable signal-driven I/O: generate a signal (SIGIO  by  default,
63              but  this  can  be  changed  via  fcntl(2)) when input or output
64              becomes possible on this file descriptor.  This feature is  only
65              available  for  terminals, pseudo-terminals, sockets, and (since
66              Linux 2.6) pipes and FIFOs.  See fcntl(2) for further details.
67
68       O_CLOEXEC (Since Linux 2.6.23)
69              Enable the close-on-exec  flag  for  the  new  file  descriptor.
70              Specifying  this  flag  permits  a  program  to avoid additional
71              fcntl(2) F_SETFD operations to set the FD_CLOEXEC  flag.   Addi‐
72              tionally,  use  of  this flag is essential in some multithreaded
73              programs since using a separate fcntl(2)  F_SETFD  operation  to
74              set  the  FD_CLOEXEC  flag does not suffice to avoid race condi‐
75              tions where one thread opens a file descriptor at the same  time
76              as another thread does a fork(2) plus execve(2).
77
78       O_CREAT
79              If  the file does not exist it will be created.  The owner (user
80              ID) of the file is set to the effective user ID of the  process.
81              The  group  ownership  (group ID) is set either to the effective
82              group ID of the process or to the group ID of the parent  direc‐
83              tory  (depending  on file system type and mount options, and the
84              mode of the parent directory, see the  mount  options  bsdgroups
85              and sysvgroups described in mount(8)).
86
87              mode specifies the permissions to use in case a new file is cre‐
88              ated.  This argument must be supplied when O_CREAT is  specified
89              in  flags;  if  O_CREAT  is not specified, then mode is ignored.
90              The effective permissions are modified by the process's umask in
91              the   usual  way:  The  permissions  of  the  created  file  are
92              (mode & ~umask).  Note that this mode  only  applies  to  future
93              accesses of the newly created file; the open() call that creates
94              a read-only file may well return a read/write file descriptor.
95
96              The following symbolic constants are provided for mode:
97
98              S_IRWXU  00700 user (file owner) has  read,  write  and  execute
99                       permission
100
101              S_IRUSR  00400 user has read permission
102
103              S_IWUSR  00200 user has write permission
104
105              S_IXUSR  00100 user has execute permission
106
107              S_IRWXG  00070 group has read, write and execute permission
108
109              S_IRGRP  00040 group has read permission
110
111              S_IWGRP  00020 group has write permission
112
113              S_IXGRP  00010 group has execute permission
114
115              S_IRWXO  00007 others have read, write and execute permission
116
117              S_IROTH  00004 others have read permission
118
119              S_IWOTH  00002 others have write permission
120
121              S_IXOTH  00001 others have execute permission
122
123       O_DIRECT (Since Linux 2.4.10)
124              Try  to minimize cache effects of the I/O to and from this file.
125              In general this will degrade performance, but it  is  useful  in
126              special  situations,  such  as  when  applications  do their own
127              caching.  File I/O is done directly to/from user space  buffers.
128              The O_DIRECT flag on its own makes at an effort to transfer data
129              synchronously, but does not give the guarantees  of  the  O_SYNC
130              that  data and necessary metadata are transferred.  To guarantee
131              synchronous I/O the O_SYNC must be used in addition to O_DIRECT.
132              See NOTES below for further discussion.
133
134              A  semantically  similar  (but  deprecated)  interface for block
135              devices is described in raw(8).
136
137       O_DIRECTORY
138              If pathname is not a directory, cause the open  to  fail.   This
139              flag is Linux-specific, and was added in kernel version 2.1.126,
140              to avoid denial-of-service problems if opendir(3) is called on a
141              FIFO  or  tape  device,  but  should  not be used outside of the
142              implementation of opendir(3).
143
144       O_EXCL Ensure that this call creates the file: if this flag  is  speci‐
145              fied  in  conjunction with O_CREAT, and pathname already exists,
146              then open() will fail.  The behavior of O_EXCL is  undefined  if
147              O_CREAT is not specified.
148
149              When  these two flags are specified, symbolic links are not fol‐
150              lowed: if pathname is a symbolic link, then open() fails regard‐
151              less of where the symbolic link points to.
152
153              On  NFS,  O_EXCL  is only supported when using NFSv3 or later on
154              kernel 2.6 or later.  In NFS environments where  O_EXCL  support
155              is not provided, programs that rely on it for performing locking
156              tasks will contain a race  condition.   Portable  programs  that
157              want  to  perform atomic file locking using a lockfile, and need
158              to avoid reliance on NFS support for O_EXCL, can create a unique
159              file  on  the same file system (e.g., incorporating hostname and
160              PID), and use link(2) to  make  a  link  to  the  lockfile.   If
161              link(2)  returns  0,  the  lock  is  successful.  Otherwise, use
162              stat(2) on the unique file  to  check  if  its  link  count  has
163              increased to 2, in which case the lock is also successful.
164
165       O_LARGEFILE
166              (LFS)  Allow files whose sizes cannot be represented in an off_t
167              (but can be represented  in  an  off64_t)  to  be  opened.   The
168              _LARGEFILE64_SOURCE  macro  must  be  defined in order to obtain
169              this definition.  Setting  the  _FILE_OFFSET_BITS  feature  test
170              macro  to  64  (rather  than using O_LARGEFILE) is the preferred
171              method of obtaining method of accessing large  files  on  32-bit
172              systems (see feature_test_macros(7)).
173
174       O_NOATIME (Since Linux 2.6.8)
175              Do  not update the file last access time (st_atime in the inode)
176              when the file is read(2).  This flag  is  intended  for  use  by
177              indexing  or  backup  programs,  where its use can significantly
178              reduce the amount of disk activity.  This flag may not be effec‐
179              tive  on all file systems.  One example is NFS, where the server
180              maintains the access time.
181
182       O_NOCTTY
183              If pathname refers to a terminal device — see tty(4) —  it  will
184              not  become  the  process's  controlling  terminal  even  if the
185              process does not have one.
186
187       O_NOFOLLOW
188              If pathname is a symbolic link, then the open fails.  This is  a
189              FreeBSD  extension, which was added to Linux in version 2.1.126.
190              Symbolic links in earlier components of the pathname will  still
191              be followed.
192
193       O_NONBLOCK or O_NDELAY
194              When  possible, the file is opened in nonblocking mode.  Neither
195              the open() nor any subsequent operations on the file  descriptor
196              which  is  returned will cause the calling process to wait.  For
197              the handling of FIFOs (named pipes), see also  fifo(7).   For  a
198              discussion  of  the  effect  of  O_NONBLOCK  in conjunction with
199              mandatory file locks and with file leases, see fcntl(2).
200
201       O_SYNC The file is opened for synchronous I/O.  Any  write(2)s  on  the
202              resulting  file  descriptor will block the calling process until
203              the data has been physically written to the underlying hardware.
204              But see NOTES below.
205
206       O_TRUNC
207              If  the  file  already exists and is a regular file and the open
208              mode allows writing (i.e., is O_RDWR or  O_WRONLY)  it  will  be
209              truncated to length 0.  If the file is a FIFO or terminal device
210              file, the O_TRUNC flag is  ignored.   Otherwise  the  effect  of
211              O_TRUNC is unspecified.
212
213       Some  of  these  optional flags can be altered using fcntl(2) after the
214       file has been opened.
215
216       creat()   is   equivalent   to   open()    with    flags    equal    to
217       O_CREAT|O_WRONLY|O_TRUNC.
218

RETURN VALUE

220       open()  and  creat()  return the new file descriptor, or -1 if an error
221       occurred (in which case, errno is set appropriately).
222

ERRORS

224       EACCES The requested access to the file is not allowed, or search  per‐
225              mission  is denied for one of the directories in the path prefix
226              of pathname, or the file did not exist yet and write  access  to
227              the  parent  directory  is  not allowed.  (See also path_resolu‐
228              tion(7).)
229
230       EEXIST pathname already exists and O_CREAT and O_EXCL were used.
231
232       EFAULT pathname points outside your accessible address space.
233
234       EFBIG  See EOVERFLOW.
235
236       EINTR  While blocked waiting to complete  an  open  of  a  slow  device
237              (e.g.,  a FIFO; see fifo(7)), the call was interrupted by a sig‐
238              nal handler; see signal(7).
239
240       EISDIR pathname refers to a directory and the access requested involved
241              writing (that is, O_WRONLY or O_RDWR is set).
242
243       ELOOP  Too  many symbolic links were encountered in resolving pathname,
244              or O_NOFOLLOW was specified but pathname was a symbolic link.
245
246       EMFILE The process already has the maximum number of files open.
247
248       ENAMETOOLONG
249              pathname was too long.
250
251       ENFILE The system limit on the total number  of  open  files  has  been
252              reached.
253
254       ENODEV pathname  refers  to  a device special file and no corresponding
255              device exists.  (This is a Linux kernel bug; in  this  situation
256              ENXIO must be returned.)
257
258       ENOENT O_CREAT  is  not  set  and the named file does not exist.  Or, a
259              directory component in pathname does not exist or is a  dangling
260              symbolic link.
261
262       ENOMEM Insufficient kernel memory was available.
263
264       ENOSPC pathname  was  to  be created but the device containing pathname
265              has no room for the new file.
266
267       ENOTDIR
268              A component used as a directory in pathname is not, in  fact,  a
269              directory,  or  O_DIRECTORY was specified and pathname was not a
270              directory.
271
272       ENXIO  O_NONBLOCK | O_WRONLY is set, the named file is a  FIFO  and  no
273              process has the file open for reading.  Or, the file is a device
274              special file and no corresponding device exists.
275
276       EOVERFLOW
277              pathname refers to a regular  file  that  is  too  large  to  be
278              opened.  The usual scenario here is that an application compiled
279              on a 32-bit platform  without  -D_FILE_OFFSET_BITS=64  tried  to
280              open a file whose size exceeds (2<<31)-1 bits; see also O_LARGE‐
281              FILE above.  This is the error  specified  by  POSIX.1-2001;  in
282              kernels before 2.6.24, Linux gave the error EFBIG for this case.
283
284       EPERM  The  O_NOATIME  flag was specified, but the effective user ID of
285              the caller did not match the owner of the file  and  the  caller
286              was not privileged (CAP_FOWNER).
287
288       EROFS  pathname  refers  to a file on a read-only file system and write
289              access was requested.
290
291       ETXTBSY
292              pathname refers to an executable image which is currently  being
293              executed and write access was requested.
294
295       EWOULDBLOCK
296              The O_NONBLOCK flag was specified, and an incompatible lease was
297              held on the file (see fcntl(2)).
298

CONFORMING TO

300       SVr4, 4.3BSD, POSIX.1-2001.  The O_DIRECTORY, O_NOATIME, and O_NOFOLLOW
301       flags  are  Linux-specific,  and  one may need to define _GNU_SOURCE to
302       obtain their definitions.
303
304       The O_CLOEXEC flag is not specified in POSIX.1-2001, but  is  specified
305       in POSIX.1-2008.
306
307       O_DIRECT  is  not  specified in POSIX; one has to define _GNU_SOURCE to
308       get its definition.
309

NOTES

311       Under Linux, the O_NONBLOCK flag indicates that one wants to  open  but
312       does not necessarily have the intention to read or write.  This is typ‐
313       ically used to open devices in order to get a file descriptor  for  use
314       with ioctl(2).
315
316       Unlike the other values that can be specified in flags, the access mode
317       values O_RDONLY, O_WRONLY, and O_RDWR, do not specify individual  bits.
318       Rather,  they  define  the low order two bits of flags, and are defined
319       respectively as 0, 1, and 2.  In other words, the combination  O_RDONLY
320       |  O_WRONLY  is  a  logical error, and certainly does not have the same
321       meaning as O_RDWR.  Linux reserves the special, nonstandard access mode
322       3  (binary 11) in flags to mean: check for read and write permission on
323       the file and return a descriptor that can't  be  used  for  reading  or
324       writing.  This nonstandard access mode is used by some Linux drivers to
325       return a descriptor  that  is  only  to  be  used  for  device-specific
326       ioctl(2) operations.
327
328       The  (undefined)  effect of O_RDONLY | O_TRUNC varies among implementa‐
329       tions.  On many systems the file is actually truncated.
330
331       There are many infelicities in the protocol underlying  NFS,  affecting
332       amongst others O_SYNC and O_NDELAY.
333
334       POSIX provides for three different variants of synchronized I/O, corre‐
335       sponding  to  the  flags  O_SYNC,  O_DSYNC,  and  O_RSYNC.    Currently
336       (2.6.31),  Linux  only  implements  O_SYNC,  but glibc maps O_DSYNC and
337       O_RSYNC to the same numerical value as O_SYNC Most Linux  file  systems
338       don't  actually implement the POSIX O_SYNC semantics, which require all
339       metadata updates of a write to be on disk on  returning  to  userspace,
340       but only the O_DSYNC semantics, which require only actual file data and
341       metadata necessary to retrieve it to be on disk by the time the  system
342       call returns.
343
344       Note that open() can open device special files, but creat() cannot cre‐
345       ate them; use mknod(2) instead.
346
347       On NFS file systems with UID mapping enabled, open() may return a  file
348       descriptor  but,  for example, read(2) requests are denied with EACCES.
349       This is because the client performs open() by checking the permissions,
350       but  UID  mapping  is  performed  by  the  server  upon  read and write
351       requests.
352
353       If the file is newly created, its st_atime, st_ctime,  st_mtime  fields
354       (respectively,  time  of  last  access, time of last status change, and
355       time of last modification; see stat(2)) are set to  the  current  time,
356       and  so  are  the st_ctime and st_mtime fields of the parent directory.
357       Otherwise, if the file is modified because of  the  O_TRUNC  flag,  its
358       st_ctime and st_mtime fields are set to the current time.
359
360   O_DIRECT
361       The  O_DIRECT  flag may impose alignment restrictions on the length and
362       address of userspace buffers and the file offset  of  I/Os.   In  Linux
363       alignment restrictions vary by file system and kernel version and might
364       be absent entirely.  However there is currently no file system-indepen‐
365       dent  interface for an application to discover these restrictions for a
366       given file or file system.  Some file systems provide their own  inter‐
367       faces  for  doing  so,  for  example  the  XFS_IOC_DIOINFO operation in
368       xfsctl(3).
369
370       Under Linux 2.4, transfer sizes, and the alignment of the  user  buffer
371       and  the file offset must all be multiples of the logical block size of
372       the file system.  Under Linux 2.6,  alignment  to  512-byte  boundaries
373       suffices.
374
375       The  O_DIRECT  flag  was introduced in SGI IRIX, where it has alignment
376       restrictions similar to those of Linux 2.4.  IRIX has also  a  fcntl(2)
377       call  to  query  appropriate alignments, and sizes.  FreeBSD 4.x intro‐
378       duced a flag of the same name, but without alignment restrictions.
379
380       O_DIRECT support was added under Linux in kernel version 2.4.10.  Older
381       Linux  kernels  simply  ignore  this  flag.   Some file systems may not
382       implement the flag and open() will fail with EINVAL if it is used.
383
384       Applications should avoid mixing O_DIRECT and normal I/O  to  the  same
385       file,  and  especially  to  overlapping  byte regions in the same file.
386       Even when the file system correctly handles  the  coherency  issues  in
387       this  situation,  overall  I/O  throughput  is likely to be slower than
388       using either mode alone.  Likewise, applications  should  avoid  mixing
389       mmap(2) of files with direct I/O to the same files.
390
391       The behaviour of O_DIRECT with NFS will differ from local file systems.
392       Older kernels, or kernels configured in certain ways, may  not  support
393       this  combination.   The NFS protocol does not support passing the flag
394       to the server, so O_DIRECT I/O will only bypass the page cache  on  the
395       client; the server may still cache the I/O.  The client asks the server
396       to make the I/O synchronous to preserve the  synchronous  semantics  of
397       O_DIRECT.   Some servers will perform poorly under these circumstances,
398       especially if the I/O size is small.  Some servers may also be  config‐
399       ured  to  lie  to  clients about the I/O having reached stable storage;
400       this will avoid the performance penalty at some risk to data  integrity
401       in  the  event of server power failure.  The Linux NFS client places no
402       alignment restrictions on O_DIRECT I/O.
403
404       In summary, O_DIRECT is a potentially powerful tool that should be used
405       with  caution.   It  is  recommended  that  applications  treat  use of
406       O_DIRECT as a performance option which is disabled by default.
407
408              "The thing that has always disturbed me about O_DIRECT  is  that
409              the whole interface is just stupid, and was probably designed by
410              a deranged monkey on some serious mind-controlling  substances."
411              — Linus
412

BUGS

414       Currently, it is not possible to enable signal-driven I/O by specifying
415       O_ASYNC when calling open(); use fcntl(2) to enable this flag.
416

SEE ALSO

418       chmod(2), chown(2),  close(2),  dup(2),  fcntl(2),  link(2),  lseek(2),
419       mknod(2),  mmap(2),  mount(2),  openat(2), read(2), socket(2), stat(2),
420       umask(2),  unlink(2),   write(2),   fopen(3),   feature_test_macros(7),
421       fifo(7), path_resolution(7), symlink(7)
422

COLOPHON

424       This  page  is  part of release 3.25 of the Linux man-pages project.  A
425       description of the project, and information about reporting  bugs,  can
426       be found at http://www.kernel.org/doc/man-pages/.
427
428
429
430Linux                             2010-06-14                           OPEN(2)
Impressum