Invalid argument for read-write mmap?
Asked Answered
P

3

21

I'm getting -EINVAL for some reason, and it's not clear to me why. Here's where I open and attempt to mmap the file:

if ((fd = open(argv[1], O_RDWR)) < 0)
{
    fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
    return 1;
}

struct stat statbuf;
if (fstat(fd, &statbuf))
{
    fprintf(stderr, "stat filed: %s\n", strerror(errno));
    return 1;
}

char* fbase = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fbase == MAP_FAILED)
{
    fprintf(stderr, "mmap failed: %s\n", strerror(errno));
    return 1;
}

EDIT: I should add, the error is occurring in the mmap.

Paola answered 24/8, 2013 at 16:5 Comment(4)
What do you mean? This is a snippet from something I'm writing.Paola
The question is "where" in which line of your code snippet the EINVAL occurrs.Atal
Sorry, I forgot to specify. The error is in the mmap.Paola
What is the value of st_size?Unfriendly
P
60

Turns out changing the MAP_SHARED to MAP_PRIVATE allows this to succeed.

This reason this was failing is subtle: My code is running inside a VirtualBox VM, and the file I was attempting to mmap was in a shared directory on my host machine. The VirtualBox virtual filesystem apparently doesn't implement mmap with the MAP_SHARED option across the boundary of the hypervisor.

If you'll read jxh's helpful comments on both my question and on his answer, it turns out that this code was working for him because he was likely attempting to mmap a host filesystem file into the host memory.

My observation that switching from MAP_SHARED to MAP_PRIVATE is also consistent with this: since privately mapped memory is invisible to other processes, the virtual filesystem driver will probably have no objection to mapping the memory.

The solution was to move the file I wanted to map into the guest's hard drive and perform manipulation from there.

Paola answered 24/8, 2013 at 17:10 Comment(2)
This is why I love stackoverflow.Spavin
Please don't think switching from MAP_SHARED to MAP_PRIVATE is an actual solution. This isn't even a workaround. Programs that use MAP_SHARED to modify the output file in-place will silently fail when using MAP_PRIVATE. It may look like it's working, but it is probably not.Jacklynjackman
U
16

Your statbuf.st_size is 0. mmap() will fail if the length parameter is 0.

There are 3 listed reasons for EINVAL error mmap():

void *mmap(void *addr, size_t length, int prot, int flags,
           int fd, off_t offset);

...

  • We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).
  • (since Linux 2.6.12) length was 0.
  • flags contained neither MAP_PRIVATE or MAP_SHARED, or contained both of these values.
Unfriendly answered 24/8, 2013 at 16:13 Comment(6)
I feel like a moron, but: it was zero, I've replaced the empty file, and it turns out the issue persists.Paola
While the length was zero previously, I ensured that the file was nonempty, and the issue is still present. This question is a mess, I'll open a new one with more detail.Paola
I must be losing my mind. I tried it inside a clean file and it worked, then I tried pasting the exact same code into the file it came from that it failed.Paola
It turns out that setting MAP_PRIVATE instead of MAP_SHARED was enough to make it work.Paola
That doesn't explain anything, because you said the code worked as it was in a clean file.Unfriendly
Aha! I finally figured it out! See my updated answer for details, but the long and the short of it that I was attempting to mmap a file that lived on my host machine from a VM.Paola
S
0

edit grub to add iomem=relaxed and reboot, make sure cat /proc/cmdline shows entry for iomem=relaxed after boot, re-run your program and check

[root@fedora ~]# cat /proc/cmdline 
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-5.18.19-200.fc36.x86_64 root=/dev/mapper/fedora_fedora-root ro rd.lvm.lv=fedora_fedora/root iomem=relaxed rhgb quiet
Supramolecular answered 20/9, 2022 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.