简体   繁体   中英

Why am I denied permission trying to shm_open?

Consider the following C program:

#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>

int main() {
    const char* name = "/memmap_ipc_shm";
    int shmFd = shm_open(name, O_RDWR | O_CREAT, 0777);
    if (shmFd < 0) {
        fprintf(stderr,"bad shmfd opening %s: %s\n", name, strerror(errno));
        return -1;
    }
    return 0;
}

When I run it on my GNU/Linux system (Devuan Beowulf, Linux 5.10.0-9, amd64 CPU), I get:

bad shmfd opening /memmap_ipc_shm: Permission denied

Why am I denied permission? I'm pretty sure I followed all the guidelines in the man shm_open page, my requested permissions seem ok - so what's wrong?

This may be happening when you have already created this shared memory object, but perhaps without the appropriate. Try calling shm_unlink(name) to ensure that's the case (or even do so as root if you encounter a permission failure again).

Note that this behavior seems to clash with the man page:

O_EXCL If O_CREAT was also specified, and a shared memory object with the given
       name already exists, return an error.  The check for the existence of the
       object, and its creation if it does not exist, are performed atomically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM