open 中文man页面

系统
open() 通常 用于 将 路径名 转换为 一个 文件描述符 (一个 非负的 小 整数, 在 read , write 等 I/O操作中 将会被使用). 当 open() 调用 成功, 它会 返回 一个 新的 文件描述符 (永远取 未用 描述符的 最小值). 这个调用 创建 一个 新的 打开文件, 即 分配 一个 新的 独一无二的 文件描述符, 不会与 运行中的 任何 其他程序 共享 (但 可以 通过 fork (2) 系统调用 实现 共享). 这个 新的 文件描述符 在其后 对 打开文件操作 的函数 中 使用.(参考 fcntl(2)). 文件的 读写 指针 被 置于 文件头

NAME

open, creat - 用来 打开和创建 一个 文件或设备  

SYNOPSIS 总览

#includ e <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

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_WRONLYO_RDWR (指明 文件 是以 只读 , 只写 或 读写 方式 打开的) 与 下面的 零个 或 多个 可选模式 按位 -or 操作 得到的:

O_CREAT
若文件 不存在 将 创建 一个 新 文件. 新 文件 的 属主 (用户ID) 被 设置 为 此 程序 的 有效 用户 的 ID. 同样 文件 所属 分组 也 被 设置 为 此 程序 的 有效 分组 的 ID 或者 上层 目录 的 分组 ID (这 依赖 文件系统 类型 ,装载选项 和 上层目录 的 模式, 参考,在 mount(8) 中 描述 的 ext2 文件系统 的 装载选项 bsdgroupssysvgroups )
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_NONBLOCKO_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 返回值

opencreat 都 返回 一个 新的 文件描述符 (若是 有 错误 发生 返回 -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)

#p#

NAME

open, creat - open and possibly create a file or device  

SYNOPSIS

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

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

The open() 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, 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 fcntl(2)). The file offset is set to the beginning of the file.

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:

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)).
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.
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.
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.
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 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 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.
O_SYNC
The file is opened for synchronous I/O. Any writes 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.
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.
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.
O_DIRECT
Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of the read(2) or write(2) system call, data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment to 512-byte boundaries suffices.
A semantically similar interface for block devices is described in raw(8).
O_ASYNC
Generate a signal (SIGIO by default, but this can be changed via fcntl(2)) when input or output becomes possible on this file descriptor. This feature is only available for terminals, pseudo-terminals, and sockets. See fcntl(2) for further details.
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.

Some of these optional flags can be altered using fcntl after the file has been opened.

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 created file are (mode & ~umask). 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.

The following symbolic constants are provided for mode:

S_IRWXU
00700 user (file owner) has read, write and execute permission
S_IRUSR (S_IREAD)
00400 user has read permission
S_IWUSR (S_IWRITE)
00200 user has write permission
S_IXUSR (S_IEXEC)
00100 user has execute permission
S_IRWXG
00070 group has read, write and execute permission
S_IRGRP
00040 group has read permission
S_IWGRP
00020 group has write permission
S_IXGRP
00010 group has execute permission
S_IRWXO
00007 others have read, write and execute permission
S_IROTH
00004 others have read permission
S_IWOTH
00002 others have write permisson
S_IXOTH
00001 others have execute permission

mode must be specified when O_CREAT is in the flags, and is ignored otherwise.

creat is equivalent to open with flags equal to O_CREAT|O_WRONLY|O_TRUNC.  

RETURN VALUE

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 special files, but creat cannot create them - use mknod(2) instead.

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.

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.

ERRORS

EEXIST
pathname already exists and O_CREAT and O_EXCL were used.
EISDIR
pathname refers to a directory and the access requested involved writing (that is, O_WRONLY or O_RDWR is set).
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.
ENAMETOOLONG
pathname was too long.
ENOENT
O_CREAT is not set and the named file does not exist. Or, a directory component in pathname does not exist or is a dangling symbolic link.
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.
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.
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.)
EROFS
pathname refers to a file on a read-only filesystem and write access was requested.
ETXTBSY
pathname refers to an executable image which is currently being executed and write access was requested.
EFAULT
pathname points outside your accessible address space.
ELOOP
Too many symbolic links were encountered in resolving pathname, or O_NOFOLLOW was specified but pathname was a symbolic link.
ENOSPC
pathname was to be created but the device containing pathname has no room for the new file.
ENOMEM
Insufficient kernel memory was available.
EMFILE
The process already has the maximum number of files open.
ENFILE
The limit on the total number of files open on the system has been reached.

NOTE

Under Linux, the O_NONBLOCK flag indicates that one wants to open but does not necessarily have the intention to read or write. This is typically used to open devices in order to get a file descriptor for use with ioctl(2).  

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.

The (undefined) effect of O_RDONLY | O_TRUNC various among implementations. On many systems the file is actually truncated.

The O_DIRECT flag was introduced in SGI IRIX, where it has alignment restrictions similar to those of Linux 2.4. IRIX has also a fcntl(2) call to query appropriate alignments, and sizes. FreeBSD 4.x introduced a flag of same name, but without alignment restrictions. Support was added under Linux in kernel version 2.4.10. Older Linux kernels simply ignore this flag.  

BUGS

"The thing that has always disturbed me about O_DIRECT is that the whole interface is just stupid, and was probably designed by a deranged monkey on some serious mind-controlling substances." -- Linus  

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)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-25 17:00:46

iconv_open中文man

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 15:54:08

ferror中文man

2011-08-25 14:40:27

basename中文man

2011-08-25 17:24:54

puts中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 16:24:57

smbmnt中文man

2011-08-23 10:29:02

chpasswd中文man
点赞
收藏

51CTO技术栈公众号