0

考虑以下 C 程序:

#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;
}

当我在我的 GNU/Linux 系统(Devuan Beowulf,Linux 5.10.0-9,amd64 CPU)上运行它时,我得到:

bad shmfd opening /memmap_ipc_shm: Permission denied

为什么我被拒绝许可?我很确定我遵循了man shm_open页面中的所有指南,我请求的权限似乎没问题 - 那么有什么问题?

4

1 回答 1

0

当您已经创建此共享内存对象时可能会发生这种情况,但可能没有适当的 . 尝试调用shm_unlink(name)以确保是这种情况(如果再次遇到权限失败,甚至以 root 身份调用)。

请注意,此行为似乎与手册页冲突:

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.
于 2021-12-29T18:56:46.467 回答