android mmap failed: invalid argument (errno 22)

2019-07-06 23:57发布

问题:

I was porting/compiling androidvncserver for my android device, but when I run it (as root) I keep getting mmap errors.

The offending code looks like this:

/* Android does not use /dev/fb0. */
#define FB_DEVICE "/dev/graphics/fb0"

if ((fbfd = open(FB_DEVICE, O_RDONLY)) == -1)
{
    printf("cannot open fb device %s\n", FB_DEVICE);
    exit(EXIT_FAILURE);
}

<SNIP>

fbmmap = mmap(NULL, pixels * bytespp, PROT_READ, MAP_SHARED, fbfd, 0);
if (fbmmap == MAP_FAILED)
{
    printf("mmap failed errno = %d\n", errno);
    exit(EXIT_FAILURE);
}

I am looking for thoughts on how to debug this further.

For the record, pixels=614400 and bytespp=4. Also, /dev/graphics/fb0 is owned by root (group=graphics), and has permissions of 660.

回答1:

prmatta,

Error code 22 is EINVAL.

From the mmap() documentation that tells you...

EINVAL We don't like start or length or offset. (E.g., they are too large, or not aligned on a page boundary.)

Perhaps you didn't page align your memory?