Penguin
Annotated edit history of open(2) version 3, including all changes. View license author blame.
Rev Author # Line
1 perry 1 !!NAME
2 PerryLorier 2 open - open and possibly create a file or device
1 perry 3 !!SYNOPSIS
2 PerryLorier 4 __#include <sys/types.h>__
5 __#inclue <sys/stat.h>__
6 __#include <fcntl.h>__
1 perry 7
2 PerryLorier 8 __int open(const char *__''pathname''__, int__ ''flags''__);__
9 __int open(const char *__''pathname''__, int__ ''flags''__, mode_t__ ''mode''__);__
10 __int creat(const char *__''pathname''__, mode_t__ ''mode''__);__
1 perry 11
12 !!DESCRIPTION
2 PerryLorier 13 The open(2) system call is used to convert a pathname into a file descriptor (a small, non-negative integer for use in subsequent I/O as with __read__, __write__,
14 etc.). When the call is successful, the file descriptor returned will be the lowest file descriptor not currently open for the process. This call creates a new open file, not shared with any other process. (But shared open files may arise via the fork(2) system call.) The new file descriptor is set to remain open across exec functions (see
15 fcntl(2)). The file offset is set to the beginning of the file.
1 perry 16
2 PerryLorier 17 The parameter ''flags'' is one of __O_RDONLY__, __O_WRONLY__ or __O_RDWR__ which request opening the file read-only, write-only or read/write, respectively, bitwise-''or'''d with zero or more of the following:
1 perry 18
3 PerryLorier 19 ;__O_CREAT__: If the file does not exist it will be created. The owner (user ID) of the file is set to the effective user ID of the process. The group ownership (group ID) is set either to the effective group ID of the process or to the group ID of the parent directory (depending on filesystem type and mount options, and the mode of the parent directory, see, e.g., the mount options ''bsdgroups'' and ''sysvgroups'' of the ext2 filesystem, as described in mount(8)).
1 perry 20
2 PerryLorier 21 ;__O_EXCL__: When used with __O_CREAT__, if the file already exists it is an error and the __open__ will fail. In this context, a symbolic link exists, regardless of where its points to. __O_EXCL__ is broken on NFS file systems, programs which rely on it for performing locking tasks will contain a race condition. The solution for performing atomic file locking using a lockfile is to create a unique file on the same fs (e.g., incorporating hostname and pid), use link(2) to make a link to the lockfile. If __link()__ returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful.
1 perry 22
2 PerryLorier 23 ;__O_NOCTTY__: If ''pathname'' refers to a terminal device -- see tty(4) -- it will not become the process's controlling terminal even if the process does not have one.
1 perry 24
2 PerryLorier 25 ;__O_TRUNC__: If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the O_TRUNC flag is ignored. Otherwise the effect of O_TRUNC is unspecified. (On many Linux versions it will be ignored; on other versions it will return an error.)
1 perry 26
2 PerryLorier 27 ;__O_APPEND__: The file is opened in append mode. Before each __write__, the file pointer is positioned at the end of the file, as if with __lseek__. __O_APPEND__ may lead to corrupted files on NFS file systems if more than one process append data to a file at once. This is because NFS does not support appending to a file, so the client kernel has to simulate it, which can't be done without a race condition.
1 perry 28
2 PerryLorier 29 ;__O_NONBLOCK__ or __O_NDELAY__: When possible, the file is opened in non-blocking mode. Neither the __open__ nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait. For the handling of FIFOs (named pipes), see also fifo(4). This mode need not have any effect on files other than FIFOs.
1 perry 30
2 PerryLorier 31 ;__O_SYNC__: The file is opened for synchronous I/O. Any __write__s on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware. ''See RESTRICTIONS below, though.''
1 perry 32
2 PerryLorier 33 ;__O_NOFOLLOW__: If ''pathname'' is a symbolic link, then the open fails. This is a FreeBSD extension, which was added to Linux in version 2.1.126. Symbolic links in earlier components of the pathname will still be followed. The headers from glibc 2.0.100 and later include a definition of this flag; ''kernels before 2.1.126 will ignore it if used''.
1 perry 34
2 PerryLorier 35 ;__O_DIRECTORY__: If ''pathname'' is not a directory, cause the open to fail. This flag is Linux-specific, and was added in kernel version 2.1.126, to avoid denial-of-service problems if opendir(3) is called on a FIFO or tape device, but should not be used outside of the implementation of __opendir__.
1 perry 36
2 PerryLorier 37 ;__O_LARGEFILE__: On 32-bit systems that support the Large Files System, allow files whose sizes cannot be represented in 31 bits to be opened.
1 perry 38
2 PerryLorier 39 Some of these optional flags can be altered using __fcntl__ after the file has been opened.
1 perry 40
2 PerryLorier 41 The argument ''mode'' specifies the permissions to use in case a new file is created. It is modified by the process's __umask__ in the usual way: the permissions of the
42 created file are __(mode __. Note that this mode only applies to future accesses of the newly created file; the __open__ call that creates a read-only file may well return a read/write file descriptor.
1 perry 43
2 PerryLorier 44 The following symbolic constants are provided for ''mode'':
1 perry 45
2 PerryLorier 46 |__S_IRWXU__|00700|user (file owner) has read, write and execute permission
47 |__S_IRUSR (S_IREAD)__|00400|user has read permission
48 |__S_IWUSR (S_IWRITE)__|00200|user has write permission
49 |__S_IXUSR (S_IEXEC)__|00100|user has execute permission
50 |__S_IRWXG__|00070|group has read, write and execute permission
51 |__S_IRGRP__|00040|group has read permission
52 |__S_IWGRP__|00020|group has write permission
53 |__S_IXGRP__|00010|group has execute permission
54 |__S_IRWXO__|00007|others have read, write and execute permission
55 |__S_IROTH__|00004|others have read permission
56 |__S_IWOTH__|00002|others have write permisson
57 |__S_IXOTH__|00001|others have execute permission
1 perry 58
2 PerryLorier 59 ''mode'' should always be specified when __O_CREAT__ is in the ''flags'', and is ignored otherwise.
1 perry 60
2 PerryLorier 61 __creat__ is equivalent to __open__ with ''flags'' equal to __O_CREAT|O_WRONLY|O_TRUNC__.
1 perry 62
63 !!RETURN VALUE
2 PerryLorier 64 __open__ and __creat__ return the new file descriptor, or -1 if an error occurred (in which case, ''errno'' is set appropriately). Note that __open__ can open device
65 special files, but __creat__ cannot create them - use mknod(2) instead.
1 perry 66
2 PerryLorier 67 On NFS file systems with UID mapping enabled, __open__ may return a file descriptor but e.g. read(2) requests are denied with __EACCES__. This is because the client performs __open__ by checking the permissions, but UID mapping is performed by the server upon read and write requests.
1 perry 68
2 PerryLorier 69 If the file is newly created, its atime, ctime, mtime fields are set to the current time, and so are the ctime and mtime fields of the parent directory. Otherwise, if the file is modified because of the O_TRUNC flag, its ctime and mtime fields are set to the current time.
1 perry 70
71 !!ERRORS
2 PerryLorier 72 ;[EEXIST]: ''pathname'' already exists and __O_CREAT__ and __O_EXCL__ were used.
73 ;[EISDIR]: ''pathname'' refers to a directory and the access requested involved writing.
74 ;[EACCES]: The requested access to the file is not allowed, or one of the directories in ''pathname'' did not allow search (execute) permission, or the file did not exist yet and write access to the parent directory is not allowed.
75 ;[ENAMETOOLONG]: ''pathname'' was too long.
76 ;[ENOENT]: A directory component in ''pathname'' does not exist or is a dangling symbolic link.
77 ;[ENOTDIR]: A component used as a directory in ''pathname'' is not, in fact, a directory, or __O_DIRECTORY__ was specified and ''pathname'' was not a directory.
78 ;[ENXIO]: O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process has the file open for reading. Or, the file is a device special file and no corresponding device exists.
79 ;[ENODEV]: ''pathname'' refers to a device special file and no corresponding device exists. (This is a Linux kernel bug - in this situation ENXIO must be returned.)
80 ;[EROFS]: ''pathname'' refers to a file on a read-only filesystem and write access was requested.
81 ;[ETXTBSY]: ''pathname'' refers to an executable image which is currently being executed and write access was requested.
82 ;[EFAULT]: ''pathname'' points outside your accessible address space.
83 ;[ELOOP]: Too many symbolic links were encountered in resolving ''pathname'', or __O_NOFOLLOW__ was specified but ''pathname'' was a symbolic link.
84 ;[ENOSPC]: ''pathname'' was to be created but the device containing ''pathname'' has no room for the new file.
85 ;[ENOMEM]: Insufficient kernel memory was available.
86 ;[EMFILE]: The process already has the maximum number of files open.
87 ;[ENFILE]: The limit on the total number of files open on the system has been reached.
1 perry 88
89 !!CONFORMING TO
2 PerryLorier 90 SVr4, SVID, POSIX, X/OPEN, BSD 4.3 The __O_NOFOLLOW__ and __O_DIRECTORY__ flags are Linux-specific. One may have to define the ___GNU_SOURCE__ macro to get their
91 definitions.
1 perry 92
93 !!RESTRICTIONS
2 PerryLorier 94 There are many infelicities in the protocol underlying NFS, affecting amongst others __O_SYNC__ and __O_NDELAY__.
1 perry 95
2 PerryLorier 96 [POSIX] provides for three different variants of synchronised I/O, corresponding to the flags __O_SYNC__, __O_DSYNC__ and __O_RSYNC__. Currently (2.1.130) these are all synonymous under Linux.
1 perry 97
98 !!SEE ALSO
2 PerryLorier 99 read(2), write(2), fcntl(2), close(2), link(2), mknod(2), mount(2), stat(2), umask(2), unlink(2), socket(2), fopen(3), fifo(4)
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 7 times)