mmap()
是否应该能够创建一个O_WRONLY
打开文件的只写映射?
我问是因为以下在 Linux 4.0.4 x86-64 系统上失败(strace
日志):
mkdir("test", 0700) = 0
open("test/foo", O_WRONLY|O_CREAT, 0666) = 3
ftruncate(3, 11) = 0
mmap(NULL, 11, PROT_WRITE, MAP_SHARED, 3, 0) = -1 EACCES (Permission denied)
errno
等于EACCESS
。
将打开标志 O_WRONLY
替换为 O_RDWR
会产生成功的映射。
Linux mmap
手册页将 errno 记录为:
EACCES A file descriptor refers to a non-regular file. Or a file map‐ ping was requested, but fd is not open for reading. Or MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode. Or PROT_WRITE is set, but the file is append-only.
因此,第二句话记录了该行为。
但这背后的原因是什么?
POSIX 允许吗?
是内核还是库限制? (快速浏览一下,我在 Linux/mm/mmap.c
中找不到任何明显的内容)
最佳答案
IEEE Std 1003.1, 2004 Edition (POSIX.1 2004)似乎禁止它。
An implementation may permit accesses other than those specified by
prot
; however, if the Memory Protection option is supported, the implementation shall not permit a write to succeed wherePROT_WRITE
has not been set or shall not permit any access wherePROT_NONE
alone has been set. The implementation shall support at least the following values ofprot
:PROT_NONE
,PROT_READ
,PROT_WRITE
, and the bitwise-inclusive OR ofPROT_READ
andPROT_WRITE
. If the Memory Protection option is not supported, the result of any access that conflicts with the specified protection is undefined. The file descriptorfildes
shall have been opened with read permission, regardless of the protection options specified. IfPROT_WRITE
is specified, the application shall ensure that it has opened the file descriptorfildes
with write permission unlessMAP_PRIVATE
is specified in theflags
parameter as described below.
(强调)
此外,在 x86 上,不可能有只写内存,这是页表条目的限制。页面可以被标记为只读或读写,并且独立地可以是可执行的或不可执行的,但不能是只写的。此外,mprotect()
的手册页说:
Whether
PROT_EXEC
has any effect different fromPROT_READ
is architecture- and kernel version-dependent. On some hardware architectures (e.g., i386),PROT_WRITE
impliesPROT_READ
.
在这种情况下,您打开了一个没有读取权限的文件描述符,但是 mmap()
会通过给您 PROT_READ< 来绕过
权利。相反,它会使用 O_WRONLY
/EACCESS
直接拒绝。
关于linux - 只写映射一个 O_WRONLY 打开的文件应该工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014515/