open, openat - open a file
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *path, int oflag, /* mode_t mode */);
int openat(int fildes, const char *path, int oflag, /* mode_t mode */);
The open() function establishes the connection between a file and a file descriptor. It creates an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file.
The openat() function is identical to the open() function except that the path argument is interpreted relative to the starting point implied by the fildes argument. If the fildes argument has the special value AT_FDCWD, a relative path argument will be resolved relative to the current working directory. If the path argument is absolute, the fildes argument is ignored.
The open() function returns a file descriptor for the named file that is the lowest file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not share it with any other process in the system. The FD_CLOEXEC file descriptor flag associated with the new file descriptor is cleared.
The file offset used to mark the current position within the file is set to the beginning of the file.
The file status flags and file access modes of the open file description are set according to the value of oflag. The mode argument is used only when O_CREAT is specified (see below.)
Values for oflag are constructed by a bitwise-inclusive-OR of flags from the following list, defined in <fcntl.h>. Applications must specify exactly one of the first three values (file access modes) below in the value of oflag:
O_RDONLY
O_WRONLY
O_RDWR
Any combination of the following may be used:
O_APPEND
O_CREAT
If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file is created with the user ID of the file set to the effective user ID of the process. The group ID of the file is set to the effective group IDs of the process, or if the S_ISGID bit is set in the directory in which the file is being created, the file's group ID is set to the group ID of its parent directory. If the group ID of the new file does not match the effective group ID or one of the supplementary groups IDs, the S_ISGID bit is cleared. The access permission bits (see <sys/stat.h>) of the file mode are set to the value of mode, modified as follows (see creat(2)): a bitwise-AND is performed on the file-mode bits and the corresponding bits in the complement of the process's file mode creation mask. Thus, all bits set in the process's file mode creation mask (see umask(2)) are correspondingly cleared in the file's permission mask. The "save text image after execution bit" of the mode is cleared (see chmod(2)). O_SYNC Write I/O operations on the file descriptor complete as defined by synchronized I/O file integrity completion (see fcntl.h(3HEAD) definition of O_SYNC.) When bits other than the file permission bits are set, the effect is unspecified. The mode argument does not affect whether the file is open for reading, writing or for both.
O_DSYNC
O_EXCL
O_LARGEFILE
O_NOCTTY
O_NOFOLLOW
O_NOLINKS
O_NONBLOCK or O_NDELAY
When opening a FIFO with O_RDONLY or O_WRONLY set:
When opening a block special or character special file that supports non-blocking opens:
O_RSYNC
O_SYNC
O_TRUNC
O_XATTR
If O_CREAT is set and the file did not previously exist, upon successful completion, open() marks for update the st_atime, st_ctime, and st_mtime fields of the file and the st_ctime and st_mtime fields of the parent directory.
If O_TRUNC is set and the file did previously exist, upon successful completion, open() marks for update the st_ctime and st_mtime fields of the file.
If both the O_SYNC and O_DSYNC flags are set, the effect is as if only the O_SYNC flag was set.
If path refers to a STREAMS file, oflag may be constructed from O_NONBLOCK or O_NODELAY OR-ed with either O_RDONLY, O_WRONLY, or O_RDWR. Other flag values are not applicable to STREAMS devices and have no effect on them. The values O_NONBLOCK and O_NODELAY affect the operation of STREAMS drivers and certain functions (see read(2), getmsg(2), putmsg(2), and write(2)) applied to file descriptors associated with STREAMS files. For STREAMS drivers, the implementation of O_NONBLOCK and O_NODELAY is device-specific.
When open() is invoked to open a named stream, and the connld module (see connld(7M)) has been pushed on the pipe, open() blocks until the server process has issued an I_RECVFD ioctl() (see streamio(7I)) to receive the file descriptor.
If path names the master side of a pseudo-terminal device, then it is unspecified whether open() locks the slave side so that it cannot be opened. Portable applications must call unlockpt(3C) before opening the slave side.
If the file is a regular file and the local file system is mounted with the nbmand mount option, then a mandatory share reservation is automatically obtained on the file. The share reservation is obtained as if fcntl(2) were called with cmd F_SHARE_NBMAND and the fshare_t values set as follows:
f_access
f_deny
f_id
If path is a symbolic link and O_CREAT and O_EXCL are set, the link is not followed.
Certain flag values can be set following open() as described in fcntl(2).
The largest value that can be represented correctly in an object of type off_t is established as the offset maximum in the open file description.
Upon successful completion, the open() function opens the file and return a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 is returned, errno is set to indicate the error, and no files are created or modified.
The open() and openat() functions will fail if:
EACCES
The file exists and the permissions specified by oflag are denied.
The file does not exist and write permission is denied for the parent directory of the file to be created.
O_TRUNC is specified and write permission is denied.
The {PRIV_FILE_DAC_SEARCH} privilege allows processes to search directories regardless of permission bits. The {PRIV_FILE_DAC_WRITE} privilege allows processes to open files for writing regardless of permission bits. See privileges(5) for special considerations when opening files owned by UID 0 for writing. The {PRIV_FILE_DAC_READ} privilege allows processes to open files for reading regardless of permission bits.
EAGAIN
EBADF
EDQUOT
EEXIST
EILSEQ
EINTR
EFAULT
EINVAL
EIO
EISDIR
ELOOP
A loop exists in symbolic links encountered during resolution of the path argument.
The O_NOFOLLOW flag is set and the final component of path is a symbolic link.
EMFILE
EMLINK
EMULTIHOP
ENAMETOOLONG
ENFILE
ENOENT
ENOLINK
ENOSR
ENOSPC
ENOSYS
ENOTDIR
ENXIO
EOPNOTSUPP
EOVERFLOW
EROFS
The openat() function will fail if:
EBADF
The open() function may fail if:
EAGAIN
EINVAL
ENAMETOOLONG
ENOMEM
ETXTBSY
Example 1 Open a file for writing by the owner.
The following example opens the file /tmp/file, either by creating it if it does not already exist, or by truncating its length to 0 if it does exist. If the call creates a new file, the access permission bits in the file mode of the file are set to permit reading and writing by the owner, and to permit reading only by group members and others.
If the call to open() is successful, the file is opened for writing.
#include <fcntl.h> ... int fd; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char *filename = "/tmp/file"; ... fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); ...
Example 2 Open a file using an existence check.
The following example uses the open() function to try to create the LOCKFILE file and open it for writing. Since the open() function specifies the O_EXCL flag, the call fails if the file already exists. In that case, the application assumes that someone else is updating the password file and exits.
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #define LOCKFILE "/etc/ptmp" ... int pfd; /* Integer for file descriptor returned by open() call. */ ... if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n"); exit(1); } ...
Example 3 Open a file for writing.
The following example opens a file for writing, creating the file if it does not already exist. If the file does exist, the system truncates the file to zero bytes.
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #define LOCKFILE "/etc/ptmp" ... int pfd; char filename[PATH_MAX+1]; ... if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { perror("Cannot open output file\n"); exit(1); } ...
The open() function has a transitional interface for 64-bit file offsets. See lf64(5). Note that using open64() is equivalent to using open() with O_LARGEFILE set in oflag.
See attributes(5) for descriptions of the following attributes:
|
Intro(2), chmod(2), close(2), creat(2), dup(2), exec(2), fcntl(2), getmsg(2), getrlimit(2), lseek(2), putmsg(2), read(2), stat(2), umask(2), write(2), attropen(3C), fcntl.h(3HEAD), stat.h(3HEAD), unlockpt(3C), attributes(5), lf64(5), privileges(5), standards(5), connld(7M), streamio(7I)
Hierarchical Storage Management (HSM) file systems can sometimes cause long delays when opening a file, since HSM files must be recalled from secondary storage.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |