.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" 1993 Michael Haardt, Ian Jackson. .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified Wed Jul 21 22:42:16 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Sun Aug 21 18:18:14 1994: Michael Haardt's NFS diffs were .\" applied by hand (faith@cs.unc.edu). .\" Modified Sat Apr 13 16:25:28 1996 by Andries Brouwer (aeb@cwi.nl) .\" Modified Mon May 13 00:53:52 1996: added symbolic constants .\" as sent by Thomas Koenig .\" Modified Fri Dec 20 16:06:45 1996 by Michael Haardt: More NFS details .TH OPEN 2 "December 20, 1996" "Linux 2.0.32" "Linux Programmer's Manual" .SH NAME open, creat \- open and possibly create a file or device .SH SYNOPSIS .nf .B #include .B #include .B #include .sp .BI "int open(const char *" pathname ", int " flags ); .BI "int open(const char *" pathname ", int " flags ", mode_t " mode ); .BI "int creat(const char *" pathname ", mode_t " mode ); .fi .SH DESCRIPTION .B open attempts to open a file and return a file descriptor (a small, non-negative integer for use in .BR read ", " write ", etc.)" .I flags is one of .BR O_RDONLY ", " O_WRONLY " or " O_RDWR which request opening the file read-only, write-only or read/write, respectively. .I flags may also be .RI bitwise- or 'd with one or more of the following: .TP .B O_CREAT If the file does not exist it will be created. .TP .B O_EXCL When used with .BR O_CREAT , if the file already exists it is an error and the .B open will fail. .B 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 .BR link (2) to make a link to the lockfile and use .BR stat (2) on the unique file to check if its link count has increased to 2. Do not use the return value of the \fBlink()\fP call. .TP .B O_NOCTTY If .I pathname refers to a terminal device \(em see .BR tty (4) \(em it will not become the process's controlling terminal even if the process does not have one. .TP .B O_TRUNC If the file already exists it will be truncated. .TP .B O_APPEND The file is opened in append mode. Initially, and before each .BR write , the file pointer is positioned at the end of the file, as if with .BR lseek . .B 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. .TP .BR O_NONBLOCK " or " O_NDELAY The file is opened in non-blocking mode. Neither the .B open nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait. .TP .B O_SYNC The file is opened for synchronous I/O. Any .BR write s on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware. .I See RESTRICTIONS below, though. .PP Some of these optional flags can be altered using .B fcntl after the file has been opened. .I mode specifies the permissions to use if a new file is created. It is modified by the process's .BR umask in the usual way: the permissions of the created file are .BR "(mode & ~umask)" . .PP The following symbolic constants are provided for .IR mode : .TP .B S_IRWXU 00700 user (file owner) has read, write and execute permission .TP .B S_IRUSR (S_IREAD) 00400 user has read permission .TP .B S_IWUSR (S_IWRITE) 00200 user has write permission .TP .B S_IXUSR (S_IEXEC) 00100 user has execute permission .TP .B S_IRWXG 00070 group has read, write and execute permission .TP .B S_IRGRP 00040 group has read permission .TP .B S_IWGRP 00020 group has write permission .TP .B S_IXGRP 00010 group has execute permission .TP .B S_IRWXO 00007 others have read, write and execute permission .TP .B S_IROTH 00004 others have read permission .TP .B S_IWOTH 00002 others have write permisson .TP .B S_IXOTH 00001 others have execute permission .PP .I mode should always be specified when .B O_CREAT is in the .IR flags , and is ignored otherwise. .B creat is equivalent to .B open with .I flags equal to .BR O_CREAT|O_WRONLY|O_TRUNC . .SH "RETURN VALUE" .BR open " and " creat return the new file descriptor, or \-1 if an error occurred (in which case, .I errno is set appropriately). Note that .B open can open device special files, but .B creat cannot create them - use .BR mknod (2) instead. .LP On NFS file systems with UID mapping enabled, \fBopen\fP may return a file descriptor but e.g. \fBread\fP(2) requests are denied with \fBEACCES\fP. This is because the client performs \fBopen\fP by checking the permissions, but UID mapping is performed by the server upon read and write requests. .SH ERRORS .TP .B EEXIST .I pathname already exists and .BR O_CREAT " and " O_EXCL were used. .TP .B EISDIR .I pathname refers to a directory and the access requested involved writing. .TP .B ETXTBSY .I pathname refers to an executable image which is currently being executed and write access was requested. .TP .B EFAULT .IR pathname " points outside your accessible address space." .TP .B EACCES The requested access to the file is not allowed, or one of the directories in .IR pathname did not allow search (execute) permission. .TP .B ENAMETOOLONG .IR pathname " was too long." .TP .B ENOENT A directory component in .I pathname does not exist or is a dangling symbolic link. .TP .B ENOTDIR A component used as a directory in .I pathname is not, in fact, a directory. .TP .B EMFILE The process already has the maximum number of files open. .TP .B ENFILE The limit on the total number of files open on the system has been reached. .TP .B ENOMEM Insufficient kernel memory was available. .TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was requested. .TP .B ELOOP Too many symbolic links were encountered in resolving .IR pathname . .TP .B ENOSPC .I pathname was to be created but the device containing .I pathname has no room for the new file. .SH "CONFORMING TO" SVr4, SVID, POSIX, X/OPEN, BSD 4.3 .SH RESTRICTIONS There are many infelicities in the protocol underlying NFS, affecting amongst others .BR O_SYNC " and " O_NDELAY . .SH "SEE ALSO" .BR read "(2), " write "(2), " fcntl "(2), " close (2), .BR unlink "(2), " mknod "(2), " stat "(2), " umask (2), .BR mount "(2), " socket "(2), " socket "(2), " fopen (3), .BR link (2).