Search code examples
cmmap

mmap returning 'No such file or directory'


I'm trying to mmap() a file descriptor to memory but I'm getting errors.

LOG_BUFFER_SIZE is defined as 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);
}

The file descriptor is valid, as I've checked:

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

This is the 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\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) = 20
writev(7, [{"\0", 1}, {"\320\33@\0\0\0\0\0\354\370@\0\0\0\0\0H7\0\0d\0\0\0seconds\0"..., 40}, {"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 28304}], 3) = 28345
writev(7, [{"\1", 1}, {"\320*@\0\0\0\0\0$\35@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\300,@\0\0\0\0\0A\255@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\320,@\0\0\0\0\0\222\265@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\340,@\0\0\0\0\0n\265@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\360,@\0\0\0\0\0\341\316@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"0-@\0\0\0\0\0\367\342@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\240-@\0\0\0\0\0>\321@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"0/@\0\0\0\0\0\22\300@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"01@\0\0\0\0\0\37\275@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\2601@\0\0\0\0\0\232\310@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\320\257@\0\0\0\0\0\246\262@\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"0\260@\0\0\0\0\0-\256@\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\220\261@\0\0\0\0\0\22\300@\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\300\262@\0\0\0\0\0-\256@\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\200\265@\0\0\0\0\0\"\264@\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\240\265@\0\0\0\0\0\"\264@\0\0\0\0\0\1\0\0\0", 20}, ...], 62) = 651
close(7)                                = 0
exit_group(1)                           = ?
+++ exited with 1 +++

This is all pretty new stuff to me, so I'm probably making some basic mistake somewhere. Something with the flags I guess?


Solution

  • You are setting ss->log_mmap but checking ss->data_mmap.

    From your strace, mmap() has returned a valid pointer. It does not look like anything incorrect has happened or that there has been any failure. I suspect your mmap() has worked fine. Also, ENOENT is not a valid error code for mmap() to return, so it's likely you are checking errno when there is no error from the mmap().

    On failure, mmap() returns MAP_FAILED, not NULL. They do not have the same value.

    A private write only mapping of a file does not seem very useful. You will not write to the file, just to a private memory region you cannot read and no other process can see. You probably want MAP_SHARED so that writes can go to the file.