MAN手册翻译系列--LINUX/UNIX系统调用-open()/openat()


说明

open函数是系统调用,在LINUX/UNIX系统MAN手册第二卷。
本MAN手册取自macOS 13.0.1 (22A400)。
博主为了加强英语能力以及对系统调用的理解,大部分内容是带有一些个人理解的手工翻译。
博主水平十分有限,难免有误,恳请各位指点。


函数名

OPEN(2)                                                                                     System Calls Manual                                                                                    OPEN(2)
NAME
     open, openat – open or create a file for reading or writing
OPEN(2)                                                                                     System Calls Manual                                                                                    OPEN(2)
函数名
	open,openat - 打开或创建一个文件,对文件进行读或写。	

语法

SYNOPSIS #梗概
     #include <fcntl.h>
     
     int
     open(const char *path, int oflag, ...);

     int
     openat(int fd, const char *path, int oflag, ...);

描述

DESCRIPTION
	The file name specified by path is opened for reading and/or writing, 
	as specified by the argument oflag; 
	the file descriptor is returned to the calling process.
	通过文件路径指定的文件会被通过读/写的方式打开,并且通过 oflag 赋予指定的参数;
	
	被打开文件的文件描述符会返回给调用 open 的进程。
	The oflag argument may indicate that the file is to be created if it does not exist (by specifying the O_CREAT flag). 
	
	In this case, open() and openat() require an additional argument mode_t mode; 
	the file is created with mode mode as described in chmod(2) and modified by the process' umask value (see umask(2)).
	oflag 参数能够指定一个不存在的文件,并把文件创建出来(通过给oflag位置赋 O_CREAT宏)。
	
	这种情况下,open() 和 openat() 两个函数都需要额外的参数 -- mode_t mode;
	文件会通过chmod(2)中的描述的模式创建,并且会膜(%)上系统的 umask(参照 umask(2))#也就是指定创建文件时的权限值,例如775,open会拿775去膜系统的 umask 值,并赋予被创建的文件权限值。
	The openat() function is equivalent to the open() function except in the case where the path specifies a relative path.  
	
	In this case the file to be opened is determined relative to the directory associated with the file descriptor fd instead of the current working directory.  
	
	The oflag argument and the optional fourth argument correspond exactly to the arguments for open().  
	
	If openat() is passed the special value AT_FDCWD in the fd argument, the current working directory is used and the behavior is identical to a call to open().
	除非是赋予相对路径,否者openat()函数等价于open()函数。
	
	在赋予相对路径的情况下,要打开的文件是相对于与文件描述符fd相关联的目录而不是当前工作目录来确定的。
	
	open()中 oflag 参数和可选的第四个参数完全对应。
	
	openat() 如果在fd参数中传入特殊值 AT_FDCWD,则使用当前工作目录,其行为与调用open()相同。
The flags specified for the oflag argument must include exactly one of the following file access modes:
O_RDONLYopen for reading only
O_WRONLYopen for writing only
O_RDWRopen for reading and writing
O_SEARCHopen directory for searching
O_EXECopen for execute only
oflag参数应该被确切的指定为下面的文件访问模式。
O_RDONLY只读方式打开
O_WRONLY只写方式打开
O_RDWR读写方式打开
O_SEARCH目录搜索打开
O_EXEC只执行模式打开
In addition any combination of the following values can be or'ed in oflag:
O_NONBLOCKdo not block on open or for data to become available
O_APPENDappend on each write
O_CREATcreate file if it does not exist
O_TRUNCtruncate size to 0
O_EXCLerror if O_CREAT and the file exists
O_SHLOCKatomically obtain a shared lock
O_EXLOCKatomically obtain an exclusive lock
O_DIRECTORYrestrict open to a directory
O_NOFOLLOWdo not follow symlinks
O_SYMLINKallow open of symlinks
O_EVTONLYdescriptor requested for event notifications only
O_CLOEXECmark as close-on-exec
O_NOFOLLOW_ANYdo not follow symlinks in the entire path.
此外,以下值都可以通过或(|)的方式与 oflag 与前文oflag参数进行组合。
O_NONBLOCK非阻塞方式打开文件
O_APPEND每次以追加的方式写入
O_CREAT当文件不存在的时候创建文件
O_TRUNC截断文件为0(清空文件)
O_EXCL如果同时指定了O_CREAT,并且文件已存在,则出错返回
O_SHLOCK通过原子操作取得共享锁
O_EXLOCK通过原子操作取得互斥锁
O_DIRECTORY限制打开目录#也就是说只能打开目录,打开文件会报错
O_NOFOLLOW不使用软链接
O_SYMLINK使用软链接
O_EVTONLY只用于时间通知的文件描述符
O_CLOEXEC在进程执行exec系统调用时关闭此打开的文件描述符
O_NOFOLLOW_ANY在整个路径中不使用软链接
Opening a file with O_APPEND set causes each write on the file to be appended to the end.  

If O_TRUNC is specified and the file exists, the file is truncated to zero length.  

If O_EXCL is set with O_CREAT and the file already exists, open() returns an error.  
This may be used to implement a simple exclusive-access locking mechanism.

If O_EXCL is set with O_CREAT and the last component of the pathname is a symbolic link, open() will fail even if the symbolic link points to a non-existent name.
当使用 O_APPEND 打开文件时,将导致对文件的每次写入都添加到文件末尾。
如果使用了 O_TRUNC 并且要打开的文件是存在的,这个文件将会被截断成0长度(文件被清空)。
如果同时使用了 O_EXCL 和 O_CREAT 并且要打开的文件已经存在了,open() 函数会返回一个错误。
这可以用来实现一个简单的独占访问锁机制。
如果同时使用了 O_EXCL 和 O_CREAT 并且路径名的最后一部分是软链接,即使软链接指向一个不存在的名称,open()函数也将会返回一个错误。
If the O_NONBLOCK flag is specified, do not wait for the device or file to be ready or available.  

If the open() call would result in the process being blocked for some reason (e.g., waiting for carrier on a dialup line), open() returns immediately.
This flag also has the effect of making all subsequent I/O on the open file non-blocking.
如果使用了 O_NONBLOCK ,将会非阻塞读取设备或网络或管道文件。
如果 open() 函数在被调用时因为某些原因阻塞了进程,(例如,等待拨号线路上的运营商),open()函数会立即返回。
该标志还具有使打开文件的所有后续I/O非阻塞的效果。
When opening a file, a lock with flock(2) semantics can be obtained by setting O_SHLOCK for a shared lock, or O_EXLOCK for an exclusive lock.  

If creating a file with O_CREAT, the request for the lock will never fail (provided that the underlying filesystem supports locking).
当打开一个文件时,可根据 flock 的语意,通过 O_SHLOCK 来设置共享锁,或通过 O_EXLOCK 来设置互斥锁。
如果用 O_CREAT 创建一个文件,对锁的请求永远不会失败(前提是底层文件系统支持锁)
If O_DIRECTORY is used in the mask and the target file passed to open() is not a directory then the open() will fail.

If O_NOFOLLOW is used in the mask and the target file passed to open() is a symbolic link then the open() will fail.

If O_SYMLINK is used in the mask and the target file passed to open() is a symbolic link then the open() will be for the symbolic link itself, not what it links to.
如果使用了 O_DIRECTORY ,但是传递给open()的文件不是一个目录,将会打开失败。
如果使用了 O_NOFOLLOW ,但是传递给open()的文件不是一个软链接,将会打开失败。
如果使用了 O_SYMLINK ,并且传递给open()的文件是一个软链接,那么open()将会打开软链接文件本身,而不是软链接指向的文件。
The O_EVTONLY flag is only intended for monitoring a file for changes (e.g. kqueue). 
Note: when this flag is used, the opened file will not prevent an unmount of the volume that contains the file.

The O_CLOEXEC flag causes the file descriptor to be marked as close-on-exec, setting the FD_CLOEXEC flag.  The state of the file descriptor flags can be inspected using the F_GETFD fcntl. See fcntl(2).

If O_NOFOLLOW_ANY is used in the mask and any component of the path passed to open() is a symbolic link then the open() will fail.
O_EVTONLY标志只用于监视文件的更改(如kqueue)。
注:当使用这个标志的时候,被打开的文件将不会阻止包含这个文件的逻辑卷的卸载。

#The O_CLOEXEC flag causes the file descriptor to be marked as close-on-exec, setting the FD_CLOEXEC flag.  The state of the file descriptor flags can be inspected using the F_GETFD fcntl. See fcntl(2).

如果使用了 O_NOFOLLOW_ANY ,并且传递给open()的路径的所有部分都是软链接,那么open()将失败。
If successful, open() returns a non-negative integer, termed a file descriptor.  It returns -1 on failure. The file pointer (used to mark the current position within the file) is set to the beginning of the file.

When a new file is created, it is given the group of the directory which contains it.

The new descriptor is set to remain open across execve system calls; see close(2) and fcntl(2).
如果打开成功了,open()将会返回一个非负整数,被称为文件描述符。
如果返回-1则代表打开失败。此时,文件指针(用于标记文件内部的当前位置)设置为文件的起始位置。

当创建了一个新文件,将被赋予包含文件本身的目录的组权限。

新的描述符被设置为在每次 execve 系统调用时都保持打开状态。见close(2)和fcntl(2)
The system imposes a limit on the number of file descriptors that can be held open simultaneously by one process.

A file's metadata can be updated even if the file was opened in read-only mode.  Getdtablesize(2) returns the current system limit.
系统对一个进程可以同时打开的文件描述符的数目施加了限制。

即使文件以只读模式打开,也可以更新文件的元数据。Getdtablesize(2)返回当前系统限制。

返回值

RETURN VALUES
     If successful, open() returns a non-negative integer, termed a file descriptor.  It returns -1 on failure, and sets errno to indicate the error.
如果打开成功了,open()将会返回一个非负整数,被称为文件描述符。
如果返回-1则代表打开失败,并且会设置错误码指明这个错误。#可用perror输出

错误说明

The named file is opened unless:
[EACCES]Search permission is denied for a component of the path prefix.
[EACCES]The required permissions (for reading and/or writing or search or executing) are denied for the given flags.
[EACCES]O_CREAT is specified, the file does not exist, and the directory in which it is to be created does not permit writing.
[EACCES]O_TRUNC is specified and write permission is denied.
[EAGAIN]path specifies the slave side of a locked pseudo-terminal device.
[EDQUOT]O_CREAT is specified, the file does not exist, and the directory in which the entry for the new file is being placed cannot be extended because the user’s quota of disk blocks on the file system containing the directory has been exhausted.
[EDQUOT]O_CREAT is specified, the file does not exist, and the user’s quota of inodes on the file system on which the file is being created has been exhausted.
[EEXIST]O_CREAT and O_EXCL are specified and the file exists.
[EFAULT]Path points outside the process’s allocated address space.
[EINTR]The open() operation is interrupted by a signal.
[EINVAL]The value of oflag is not valid.
[EIO]An I/O error occurs while making the directory entry or allocating the inode for O_CREAT.
[EISDIR]The named file is a directory, and the arguments specify that it is to be opened for writing.
[EISDIR]The named file is a directory, and the arguments specify that it is to be opened for executing.
[ELOOP]Too many symbolic links are encountered in translating the pathname. This is taken to be indicative of a looping symbolic link.
[EMFILE]The process has already reached its limit for open file descriptors.
[ENAMETOOLONG]A component of a pathname exceeds {NAME_MAX} characters, or an entire path name exceeded {PATH_MAX} characters.
[ENFILE]The system file table is full.
[ENOTDIR]O_DIRECTORY was specified and the target is not a directory.
[ENOTDIR]O_SEARCH was specified and the target is not a directory.
[ELOOP]O_NOFOLLOW was specified and the target is a symbolic link.
[ELOOP]O_NOFOLLOW_ANY was specified and and a component of the path is a symbolic link.
[EMFILE]The process has already reached its limit for open file descriptors.
[ENAMETOOLONG]A component of a pathname exceeds {NAME_MAX} characters, or an entire path name exceeded {PATH_MAX} characters.
[ENFILE]The system file table is full.
[ENOTDIR]O_DIRECTORY was specified and the target is not a directory.
[ENOTDIR]O_SEARCH was specified and the target is not a directory.
[ELOOP]O_NOFOLLOW was specified and the target is a symbolic link.
[ELOOP]O_NOFOLLOW_ANY was specified and and a component of the path is a symbolic link.
[ENOENT]O_CREAT is not set and the named file does not exist.
[ENOENT]A component of the path name that must exist does not exist.
[ENOSPC]O_CREAT is specified, the file does not exist, and the directory in which the entry for the new file is being placed cannot be extended because there is no space left on the file system containing the directory.
[ENOSPC]O_CREAT is specified, the file does not exist, and there are no free inodes on the file system on which the file is being created.
[ENOTDIR]A component of the path prefix is not a directory.
[EDEADLK]A component of the pathname refers to a “dataless” directory that requires materialization and the I/O policy of the current thread or process disallows dataless directory materialization (see getiopolicy_np(3)).
[ENXIO]The named file is a character-special or block-special file and the device associated with this special file does not exist.
[ENXIO]O_NONBLOCK and O_WRONLY are set, the file is a FIFO, and no process has it open for reading.
[EOPNOTSUPP]O_SHLOCK or O_EXLOCK is specified, but the underlying filesystem does not support locking.
[EOPNOTSUPP]An attempt is made to open a socket (not currently implemented).
[EOVERFLOW]The named file is a regular file and its size does not fit in an object of type off_t.
[EROFS]The named file resides on a read-only file system, and the file is to be modified.
[ETXTBSY]The file is a pure procedure (shared text) file that is being executed and the open() call requests write access.
[EBADF]The path argument does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor open for searching.
[ENOTDIR]The path argument is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.
[EILSEQ]The filename does not match the encoding rules.
[EWOULDBLOCK]O_SHLOCK or O_EXLOCK is specified, but the file is locked and the O_NONBLOCK option was specified.
指定文件会被打开,除非:
[EACCES]路径前缀的某个组件的搜索权限被拒绝
[EACCES]对于赋予的 oflag 读/写/查/执行权限被拒绝。
[EACCES]O_CREAT 被使用,但文件不存在,并且指定目录下无写文件权限。
[EACCES]O_TRUNC被使用,但无写权限。
[EAGAIN]路径指定被锁定的伪终端设备的从端。
[EDQUOT]O_CREAT 被使用,但文件不存在,并且存放新文件条目的目录无法被拓展,因为用户在包含该目录的文件系统上的磁盘块配额已满。
[EEXIST]O_CREAT 和 O_EXCL 被使用并且文件已经存在。
[EFAULT]进程分配的地址空间之外的路径点。
[EINTR]open()操作被信号中断。
[EINVAL]oflag 的值无效
[EIO]当使用 O_CREAT 生成目录相,并且分配inode时,发生了一个 I/O 错误。
[EISDIR]打开的是一个目录,但是参数指定是以写方式打开。
[EISDIR]打开的是一个目录,但是参数指定是以执行方式打开。
[ELOOP]路径遇到了太多的软链接。意味着循环软链接。
[EMFILE]进程已经达到了打开文件描述符的极限。
[ENAMETOOLONG]路径或文件名的长度超过了最大值{NAME_MAX}。
[ENFILE]系统文件表已满。
[ENOTDIR]O_DIRECTORY 被指定但是要打开的目标不是一个目录。
[ENOTDIR]O_SEARCH 被指定但是要打开的目标不是一个目录。
[ELOOP]O_NOFOLLOW 被指定但是要打开的目标是一个软链接。
[ELOOP]O_NOFOLLOW_ANY 被指定但是路径的一部分存在软链接。
[ENOENT]O_CREAT 没被指定,但是要打开的文件也不存在。
[ENOENT]必须存在的路径名组件不存在。
[ENOSPC]O_CREAT 被指定,文件不存在,在创建该文件的文件系统上没有空闲的inode。
[ENOTDIR]组件路径前缀不是一个目录。
[EDEADLK]路径名的一个组成部分是指一个需要物化的“无数据”目录,而当前线程或进程的I/O策略不允许物化无数据目录(见getiopolicy_np(3))。
[ENXIO]所命名的文件是字符特殊或块特殊的文件,与该特殊文件关联的设备不存在。
[ENXIO]O_NONBLOCK 和 O_WRONLY 被指定,文件是FIFO(先进先出),且没有进程打开该文件进行读取。
[EOPNOTSUPP]试图打开一个套接字(当前没有实现)。
[EOVERFLOW]命名的文件是一个普通文件,它的大小无法放入off_t类型的对象中。
[EROFS]命名的文件在一个只读系统里,并且文件被修改了。
[ETXTBSY]该文件是一个正在执行的纯过程(共享文本)文件,open()调用请求写访问。
[EBADF]path参数没有指定绝对路径,fd参数既不是AT_FDCWD,也不是一个有效的文件描述符。
[ENOTDIR]path参数不是绝对路径,fd既不是AT_FDCWD,也不是与目录相关的文件描述符。
[EILSEQ]文件名与编码规则不匹配。
[EWOULDBLOCK]指定了O_SHLOCK或O_EXLOCK,但是文件被锁定并且指定了O_NONBLOCK选项。

兼容性

COMPATIBILITY
	open() on a terminal device (i.e., /dev/console) will now make that device a controlling terminal for the process.  
	Use the O_NOCTTY flag to open a terminal device without changing your controlling terminal.
在终端设备(即/dev/console)上执行Open()方法,会让该设备成为进程的控制终端。
使用 O_NOCTTY 标志可以在不改变控制终端的情况下打开终端设备。

see also

SEE ALSO
     chmod(2), close(2), dup(2), getdtablesize(2), lseek(2), read(2), umask(2), write(2)

历史

HISTORY
     An open() function call appeared in Version 6 AT&T UNIX.  The openat() function was introduced in OS X 10.10

BSD 4                                                                                          June 3, 2021                                                                                          BSD 4
(END)
	open()调用出现在Version 6 AT&T UNIX中。
	openat() 在OS X 10.10中引入。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值