OPEN(2) System calls OPEN(2) NAME open, creat - / SYNOPSIS #include #include #include int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode) int creat(const char *pathname, mode_t mode); (DESCRIPTION) open() read , write I/O open() fork (2) fcntl(2) flags O_RDONLY, O_WRONLY O_RDWR ( , ) -or : O_CREAT . (ID) ID. ID ID ( , , , mount(8) ext2 bsdgroups sysvgroups ) O_EXCL O_CREAT, , , open , . , . 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. O_NOCTTY pathname -- tty(4) -- , . O_TRUNC , , ( O_RDWR O_WRONLY ) , , . FIFO , O_TRUNC . O_TRUNC ( Linux , , ) O_APPEND . , . as if with lseek. O_APPEND may lead to corrupted files on NFS file systems if more than one process appends 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. O_NONBLOCK O_NDELAY (open) (non-blocking) . , , . FIFO () , fifo(4). FIFO . O_SYNC I/O . write , . , RESTRICTIONS. O_NOFOLLOW pathname , . FreeBSD , 2.1.126 Linux . glibc2.0.100 , ; kernel 2.1.126 . O_DIRECTORY pathname , . Linux , kernel 2.1.126 , FIFO denial- of-service , opendir . O_LARGEFILE 32 , 31 . , fcntl . , mode . umask . (mode & ~umask). ; open , . mode : S_IRWXU 00700 , S_IRUSR (S_IREAD) 00400 S_IWUSR (S_IWRITE) 00200 S_IXUSR (S_IEXEC) 00100 S_IRWXG 00070 , S_IRGRP 00040 S_IWGRP 00020 S_IXGRP 00010 S_IRWXO 00007 , S_IROTH 00004 S_IWOTH 00002 S_IXOTH 00001 mode flags O_CREAT , . creat open flags O_CREAT|O_WRONLY|O_TRUNC. RETURN VALUE open creat ( -1 , errno ). open , creat , mknod(2) . 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. , atime(), ctime(), mtime() , atime , ctime . , O_TRUNC , ctime , mtime . ERRORS EEXIST O_CREAT and O_EXCL ,( pathname ). EISDIR ( pathname ) , . EACCES () , ( pathname ) ( ) , . ENAMETOOLONG ( pathname ) ENOENT ( pathname ) . ENOTDIR pathname ENXIO O_NONBLOCK | O_WRONLY, FIFO , , , ENODEV ( pathname ) , . ( linux kernel bug - ENXIO .) EROFS ( pathname ) ETXTBSY ( pathname ) EFAULT pathname . ELOOP pathname , O_NOFOLLOW pathname ENOSPC pathname , pathname ENOMEM (kernel memory) EMFILE ENFILE CONFORMING TO 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 definitions. RESTRICTIONS There are many infelicities in the protocol underlying NFS, affecting amongst others O_SYNC and O_NDELAY. 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. SEE ALSO 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) [] Daniel [] 2002/01/10 linuxman: http://cmpp.linuxforum.net man man https://github.com/man-pages-zh/manpages- zh Linux 1999-06-03 OPEN(2)