• Uncategorized

About linux : Why-am-I-denied-permission-trying-to-shmopen

Question Detail

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?

Question Answer

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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.