mmap 返回 'No such file or directory'

mmap returning 'No such file or directory'

我正在尝试 mmap() 将文件描述符写入内存,但出现错误。

LOG_BUFFER_SIZE 定义为 500。

printf("fcntl fd check: %d\n", fcntl(ss->fd_log, F_GETFD));
ss->log_mmap = mmap(NULL, LOG_BUFFER_SIZE, PROT_WRITE | PROT_WRITE, MAP_PRIVATE, ss->fd_log, 0);

if (ss->data_mmap == NULL){
    printf("mmap() error: %s (errno=%d)\n", strerror(errno), errno);
    exit(EXIT_FAILURE);
}

文件描述符有效,正如我检查过的:

fcntl fd check: 1
mmap() error for fd #6: No such file or directory (errno=2)

这是 strace:

access("../ss_data/SunLoadRight", F_OK) = -1 ENOENT (No such file or directory)
open("../ss_data/log/SunLoadRight.log", O_RDWR|O_CREAT|O_APPEND|O_CLOEXEC, 0662) = 6
fcntl(6, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
write(1, "fcntl fd check: 1\n", 18fcntl fd check: 1
)     = 18
mmap(NULL, 500, PROT_WRITE, MAP_PRIVATE, 6, 0) = 0x7f624c426000
write(1, "mmap() error for fd #6: No such "..., 60mmap() error for fd #6: No such file or directory (errno=2)
) = 60
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
rt_sigaction(SIGPROF, {SIG_DFL, [], SA_RESTORER, 0x7f624be704b0}, NULL, 8) = 0
open("gmon.out", O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0666) = 7
write(7, "gmon[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20) = 20
writev(7, [{"[=13=]", 1}, {"0@[=13=][=13=][=13=][=13=][=13=]40@[=13=][=13=][=13=][=13=][=13=]H7[=13=][=13=]d[=13=][=13=][=13=]seconds[=13=]"..., 40}, {"[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]"..., 28304}], 3) = 28345
writev(7, [{"", 1}, {"0*@[=13=][=13=][=13=][=13=][=13=]$@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0,@[=13=][=13=][=13=][=13=][=13=]A5@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0,@[=13=][=13=][=13=][=13=][=13=]25@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0,@[=13=][=13=][=13=][=13=][=13=]n5@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0,@[=13=][=13=][=13=][=13=][=13=]16@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0-@[=13=][=13=][=13=][=13=][=13=]72@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0-@[=13=][=13=][=13=][=13=][=13=]>1@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"0/@[=13=][=13=][=13=][=13=][=13=]0@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"01@[=13=][=13=][=13=][=13=][=13=]5@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"01@[=13=][=13=][=13=][=13=][=13=]20@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"07@[=13=][=13=][=13=][=13=][=13=]62@[=13=][=13=][=13=][=13=][=13=]P[=13=][=13=][=13=]", 20}, {"", 1}, {"00@[=13=][=13=][=13=][=13=][=13=]-6@[=13=][=13=][=13=][=13=][=13=]P[=13=][=13=][=13=]", 20}, {"", 1}, {"01@[=13=][=13=][=13=][=13=][=13=]0@[=13=][=13=][=13=][=13=][=13=]P[=13=][=13=][=13=]", 20}, {"", 1}, {"02@[=13=][=13=][=13=][=13=][=13=]-6@[=13=][=13=][=13=][=13=][=13=]P[=13=][=13=][=13=]", 20}, {"", 1}, {"05@[=13=][=13=][=13=][=13=][=13=]\"4@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, {"", 1}, {"05@[=13=][=13=][=13=][=13=][=13=]\"4@[=13=][=13=][=13=][=13=][=13=][=13=][=13=][=13=]", 20}, ...], 62) = 651
close(7)                                = 0
exit_group(1)                           = ?
+++ exited with 1 +++

这对我来说都是全新的东西,所以我可能在某个地方犯了一些基本错误。我猜是有旗帜的东西?

您正在设置 ss->log_mmap 但正在检查 ss->data_mmap

从您的 strace 中,mmap() 已经 returned 了一个有效的指针。看起来没有发生任何不正确的事情,也没有出现任何故障。我怀疑您的 mmap() 工作正常。此外,ENOENT 不是 mmap() 到 return 的有效错误代码,因此当 mmap() 没有错误时,您可能正在检查 errno

失败时,mmap() returns MAP_FAILED,而不是 NULL。它们没有相同的值。

A private 文件的只写映射似乎不是很有用。您不会写入该文件,只会写入您无法读取且其他进程无法看到的私有内存区域。您可能需要 MAP_SHARED 以便写入文件。