1OPEN(3P)                   POSIX Programmer's Manual                  OPEN(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       open - open a file
13

SYNOPSIS

15       #include <sys/stat.h>
16       #include <fcntl.h>
17
18       int open(const char *path, int oflag, ...  );
19
20

DESCRIPTION

22       The open() function shall establish the connection between a file and a
23       file  descriptor.  It shall create an open file description that refers
24       to a file and a file descriptor that refers to that open file  descrip‐
25       tion.  The  file  descriptor is used by other I/O functions to refer to
26       that file. The path argument points to a pathname naming the file.
27
28       The open() function shall return a file descriptor for the  named  file
29       that is the lowest file descriptor not currently open for that process.
30       The open file description is new, and  therefore  the  file  descriptor
31       shall not share it with any other process in the system. The FD_CLOEXEC
32       file descriptor flag associated with the new file descriptor  shall  be
33       cleared.
34
35       The file offset used to mark the current position within the file shall
36       be set to the beginning of the file.
37
38       The file status flags and file access modes of the open  file  descrip‐
39       tion shall be set according to the value of oflag.
40
41       Values  for  oflag  are  constructed by a bitwise-inclusive OR of flags
42       from the following list, defined in <fcntl.h>. Applications shall spec‐
43       ify  exactly one of the first three values (file access modes) below in
44       the value of oflag:
45
46       O_RDONLY
47              Open for reading only.
48
49       O_WRONLY
50              Open for writing only.
51
52       O_RDWR Open for reading and writing. The result is  undefined  if  this
53              flag is applied to a FIFO.
54
55
56       Any combination of the following may be used:
57
58       O_APPEND
59              If  set,  the  file  offset  shall be set to the end of the file
60              prior to each write.
61
62       O_CREAT
63              If the file exists, this flag has  no  effect  except  as  noted
64              under  O_EXCL  below.  Otherwise, the file shall be created; the
65              user ID of the file shall be set to the effective user ID of the
66              process;  the  group ID of the file shall be set to the group ID
67              of the file's parent directory or to the effective group  ID  of
68              the  process;  and the access permission bits (see <sys/stat.h>)
69              of the file mode shall be set to the value of the third argument
70              taken  as type mode_t modified as follows: a bitwise AND is per‐
71              formed on the file-mode bits and the corresponding bits  in  the
72              complement  of  the  process' file mode creation mask. Thus, all
73              bits in the file mode whose corresponding bit in the  file  mode
74              creation  mask is set are cleared. When bits other than the file
75              permission bits are set, the effect is  unspecified.  The  third
76              argument  does  not affect whether the file is open for reading,
77              writing, or for both. Implementations shall  provide  a  way  to
78              initialize  the  file's  group  ID to the group ID of the parent
79              directory.  Implementations may, but need not, provide an imple‐
80              mentation-defined  way  to initialize the file's group ID to the
81              effective group ID of the calling process.
82
83       O_DSYNC
84              Write I/O operations on the file descriptor  shall  complete  as
85              defined by synchronized I/O data integrity completion.
86
87       O_EXCL If  O_CREAT  and  O_EXCL  are set, open() shall fail if the file
88              exists. The check for the existence of the file and the creation
89              of the file if it does not exist shall be atomic with respect to
90              other threads executing open() naming the same filename  in  the
91              same  directory  with  O_EXCL  and  O_CREAT  set.  If O_EXCL and
92              O_CREAT are set, and path names a symbolic  link,  open()  shall
93              fail  and  set  errno to [EEXIST], regardless of the contents of
94              the symbolic link. If O_EXCL is set and O_CREAT is not set,  the
95              result is undefined.
96
97       O_NOCTTY
98              If  set  and path identifies a terminal device, open() shall not
99              cause the terminal device to become the controlling terminal for
100              the process.
101
102       O_NONBLOCK
103              When opening a FIFO with O_RDONLY or O_WRONLY set:
104
105               * If O_NONBLOCK is set, an open() for reading-only shall return
106                 without delay. An open() for  writing-only  shall  return  an
107                 error if no process currently has the file open for reading.
108
109               * If  O_NONBLOCK  is  clear,  an  open() for reading-only shall
110                 block the calling thread until a thread opens  the  file  for
111                 writing.  An  open() for writing-only shall block the calling
112                 thread until a thread opens the file for reading.
113
114       When opening a block special or character special  file  that  supports
115       non-blocking opens:
116
117               * If  O_NONBLOCK is set, the open() function shall return with‐
118                 out blocking for the device to be ready or available.  Subse‐
119                 quent behavior of the device is device-specific.
120
121               * If  O_NONBLOCK  is clear, the open() function shall block the
122                 calling thread until the device is ready or available  before
123                 returning.
124
125       Otherwise, the behavior of O_NONBLOCK is unspecified.
126
127       O_RSYNC
128              Read I/O operations on the file descriptor shall complete at the
129              same level of integrity as specified by the O_DSYNC  and  O_SYNC
130              flags.  If  both  O_DSYNC  and O_RSYNC are set in oflag, all I/O
131              operations on the file descriptor shall complete as  defined  by
132              synchronized  I/O  data integrity completion. If both O_SYNC and
133              O_RSYNC are set  in  flags,  all  I/O  operations  on  the  file
134              descriptor  shall  complete  as defined by synchronized I/O file
135              integrity completion.
136
137       O_SYNC Write I/O operations on the file descriptor  shall  complete  as
138              defined by synchronized I/O file integrity completion.
139
140       O_TRUNC
141              If  the  file exists and is a regular file, and the file is suc‐
142              cessfully opened O_RDWR or O_WRONLY, its length shall  be  trun‐
143              cated  to 0, and the mode and owner shall be unchanged. It shall
144              have no effect on FIFO special files or terminal  device  files.
145              Its  effect  on  other file types is implementation-defined. The
146              result of using O_TRUNC with O_RDONLY is undefined.
147
148
149       If O_CREAT is set and the file did not previously exist, upon  success‐
150       ful  completion,  open()  shall mark for update the st_atime, st_ctime,
151       and st_mtime fields of the file and the st_ctime and st_mtime fields of
152       the parent directory.
153
154       If  O_TRUNC  is  set and the file did previously exist, upon successful
155       completion, open() shall mark for  update  the  st_ctime  and  st_mtime
156       fields of the file.
157
158       If  both the O_SYNC and O_DSYNC flags are set, the effect is as if only
159       the O_SYNC flag was set.
160
161       If path refers to a STREAMS file, oflag may be constructed from  O_NON‐
162       BLOCK  OR'ed with either O_RDONLY, O_WRONLY, or O_RDWR. Other flag val‐
163       ues are not applicable to STREAMS devices and shall have no  effect  on
164       them. The value O_NONBLOCK affects the operation of STREAMS drivers and
165       certain functions applied to file descriptors associated  with  STREAMS
166       files. For STREAMS drivers, the implementation of O_NONBLOCK is device-
167       specific.
168
169       If path names the master side of a pseudo-terminal device, then  it  is
170       unspecified  whether  open()  locks the slave side so that it cannot be
171       opened. Conforming applications shall call  unlockpt()  before  opening
172       the slave side.
173
174       The  largest  value  that  can be represented correctly in an object of
175       type off_t shall be established as the offset maximum in the open  file
176       description.
177

RETURN VALUE

179       Upon successful completion, the function shall open the file and return
180       a non-negative integer representing the  lowest  numbered  unused  file
181       descriptor.  Otherwise,  -1 shall be returned and errno set to indicate
182       the error. No files shall  be  created  or  modified  if  the  function
183       returns -1.
184

ERRORS

186       The open() function shall fail if:
187
188       EACCES Search  permission  is denied on a component of the path prefix,
189              or the file exists and the permissions specified  by  oflag  are
190              denied,  or  the  file  does  not  exist and write permission is
191              denied for the parent directory of the file to  be  created,  or
192              O_TRUNC is specified and write permission is denied.
193
194       EEXIST O_CREAT and O_EXCL are set, and the named file exists.
195
196       EINTR  A signal was caught during open().
197
198       EINVAL The  implementation  does  not support synchronized I/O for this
199              file.
200
201       EIO    The path argument names a STREAMS file and  a  hangup  or  error
202              occurred during the open().
203
204       EISDIR The  named  file  is  a directory and oflag includes O_WRONLY or
205              O_RDWR.
206
207       ELOOP  A loop exists in symbolic links encountered during resolution of
208              the path argument.
209
210       EMFILE {OPEN_MAX}  file  descriptors  are currently open in the calling
211              process.
212
213       ENAMETOOLONG
214              The length of the path argument exceeds {PATH_MAX} or a pathname
215              component is longer than {NAME_MAX}.
216
217       ENFILE The  maximum  allowable number of files is currently open in the
218              system.
219
220       ENOENT O_CREAT is not set and the named file does not exist; or O_CREAT
221              is  set  and  either  the path prefix does not exist or the path
222              argument points to an empty string.
223
224       ENOSR  The path argument names a STREAMS-based file and the  system  is
225              unable to allocate a STREAM.
226
227       ENOSPC The  directory  or  file  system that would contain the new file
228              cannot be expanded, the file does  not  exist,  and  O_CREAT  is
229              specified.
230
231       ENOTDIR
232              A component of the path prefix is not a directory.
233
234       ENXIO  O_NONBLOCK  is  set,  the named file is a FIFO, O_WRONLY is set,
235              and no process has the file open for reading.
236
237       ENXIO  The named file is a character special or block special file, and
238              the device associated with this special file does not exist.
239
240       EOVERFLOW
241              The named file is a regular file and the size of the file cannot
242              be represented correctly in an object of type off_t.
243
244       EROFS  The named file resides on a read-only  file  system  and  either
245              O_WRONLY,  O_RDWR,  O_CREAT  (if  the  file  does not exist), or
246              O_TRUNC is set in the oflag argument.
247
248
249       The open() function may fail if:
250
251       EAGAIN The path argument names the  slave  side  of  a  pseudo-terminal
252              device that is locked.
253
254       EINVAL The value of the oflag argument is not valid.
255
256       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
257              resolution of the path argument.
258
259       ENAMETOOLONG
260              As a result of encountering a symbolic link in resolution of the
261              path  argument,  the  length  of the substituted pathname string
262              exceeded {PATH_MAX}.
263
264       ENOMEM The path argument names a STREAMS file and the system is  unable
265              to allocate resources.
266
267       ETXTBSY
268              The  file  is  a pure procedure (shared text) file that is being
269              executed and oflag is O_WRONLY or O_RDWR.
270
271
272       The following sections are informative.
273

EXAMPLES

275   Opening a File for Writing by the Owner
276       The following example opens the file /tmp/file, either by  creating  it
277       (if it does not already exist), or by truncating its length to 0 (if it
278       does exist). In the former case, if the call creates a  new  file,  the
279       access  permission  bits in the file mode of the file are set to permit
280       reading and writing by the owner, and to permit reading only  by  group
281       members and others.
282
283       If the call to open() is successful, the file is opened for writing.
284
285
286              #include <fcntl.h>
287              ...
288              int fd;
289              mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
290              char *filename = "/tmp/file";
291              ...
292              fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
293              ...
294
295   Opening a File Using an Existence Check
296       The  following  example  uses  the open() function to try to create the
297       LOCKFILE file and open it for writing. Since the open() function speci‐
298       fies  the  O_EXCL  flag,  the call fails if the file already exists. In
299       that case, the program assumes that someone else is updating the  pass‐
300       word file and exits.
301
302
303              #include <fcntl.h>
304              #include <stdio.h>
305              #include <stdlib.h>
306
307
308              #define LOCKFILE "/etc/ptmp"
309              ...
310              int pfd; /* Integer for file descriptor returned by open() call. */
311              ...
312              if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
313                  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
314              {
315                  fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
316                  exit(1);
317              }
318              ...
319
320   Opening a File for Writing
321       The following example opens a file for writing, creating the file if it
322       does not already exist. If the file does exist,  the  system  truncates
323       the file to zero bytes.
324
325
326              #include <fcntl.h>
327              #include <stdio.h>
328              #include <stdlib.h>
329
330
331              #define LOCKFILE "/etc/ptmp"
332              ...
333              int pfd;
334              char filename[PATH_MAX+1];
335              ...
336              if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
337                  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
338              {
339                  perror("Cannot open output file\n"); exit(1);
340              }
341              ...
342

APPLICATION USAGE

344       None.
345

RATIONALE

347       Except  as  specified in this volume of IEEE Std 1003.1-2001, the flags
348       allowed in oflag are not mutually-exclusive and any number of them  may
349       be used simultaneously.
350
351       Some  implementations  permit  opening  FIFOs  with O_RDWR. Since FIFOs
352       could be implemented in other ways, and since two file descriptors  can
353       be used to the same effect, this possibility is left as undefined.
354
355       See getgroups() about the group of a newly created file.
356
357       The  use of open() to create a regular file is preferable to the use of
358       creat(), because the latter is redundant and included only for histori‐
359       cal reasons.
360
361       The  use  of the O_TRUNC flag on FIFOs and directories (pipes cannot be
362       open()-ed) must be permissible without  unexpected  side  effects  (for
363       example,  creat()  on a FIFO must not remove data). Since terminal spe‐
364       cial files might have type-ahead data stored  in  the  buffer,  O_TRUNC
365       should  not  affect  their content, particularly if a program that nor‐
366       mally opens a regular file should open the current controlling terminal
367       instead.  Other  file  types, particularly implementation-defined ones,
368       are left implementation-defined.
369
370       IEEE Std 1003.1-2001 permits [EACCES] to  be  returned  for  conditions
371       other than those explicitly listed.
372
373       The O_NOCTTY flag was added to allow applications to avoid unintention‐
374       ally acquiring a controlling terminal as a side  effect  of  opening  a
375       terminal file. This volume of IEEE Std 1003.1-2001 does not specify how
376       a controlling terminal is acquired, but it allows an implementation  to
377       provide this on open() if the O_NOCTTY flag is not set and other condi‐
378       tions specified in the Base Definitions volume of IEEE Std 1003.1-2001,
379       Chapter 11, General Terminal Interface are met. The O_NOCTTY flag is an
380       effective no-op if the file being opened is not a terminal device.
381
382       In historical implementations the value of O_RDONLY is zero. Because of
383       that, it is not possible to detect the presence of O_RDONLY and another
384       option. Future implementations should encode O_RDONLY and  O_WRONLY  as
385       bit flags so that:
386
387
388              O_RDONLY | O_WRONLY == O_RDWR
389
390       In general, the open() function follows the symbolic link if path names
391       a symbolic link. However, the open() function, when called with O_CREAT
392       and O_EXCL, is required to fail with [EEXIST] if path names an existing
393       symbolic link, even if the symbolic link refers to a nonexistent  file.
394       This  behavior is required so that privileged applications can create a
395       new file in a known location without the possibility  that  a  symbolic
396       link might cause the file to be created in a different location.
397
398       For  example,  a  privileged application that must create a file with a
399       predictable name in a user-writable directory, such as the user's  home
400       directory,  could  be  compromised  if the user creates a symbolic link
401       with that name that refers to a nonexistent file in a system directory.
402       If  the  user can influence the contents of a file, the user could com‐
403       promise the system by creating a new system configuration or spool file
404       that  would  then be interpreted by the system. The test for a symbolic
405       link which refers to a nonexisting file must be atomic  with  the  cre‐
406       ation of a new file.
407
408       The POSIX.1-1990 standard required that the group ID of a newly created
409       file be set to the group ID of its parent directory or to the effective
410       group  ID of the creating process. FIPS 151-2 required that implementa‐
411       tions provide a way to have the group ID be set to the group ID of  the
412       containing  directory,  but  did not prohibit implementations also sup‐
413       porting a way to set the group ID to the effective group ID of the cre‐
414       ating process. Conforming applications should not assume which group ID
415       will be used. If it matters, an application can use chown() to set  the
416       group  ID after the file is created, or determine under what conditions
417       the implementation will set the desired group ID.
418

FUTURE DIRECTIONS

420       None.
421

SEE ALSO

423       chmod(), close(), creat(), dup(), fcntl(),  lseek(),  read(),  umask(),
424       unlockpt(),     write(),     the    Base    Definitions    volume    of
425       IEEE Std 1003.1-2001, <fcntl.h>, <sys/stat.h>, <sys/types.h>
426
428       Portions of this text are reprinted and reproduced in  electronic  form
429       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
430       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
431       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
432       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
433       event of any discrepancy between this version and the original IEEE and
434       The Open Group Standard, the original IEEE and The Open Group  Standard
435       is  the  referee document. The original Standard can be obtained online
436       at http://www.opengroup.org/unix/online.html .
437
438
439
440IEEE/The Open Group                  2003                             OPEN(3P)
Impressum