In my simple code:
#include <sys/shm.h> #include <stdio.h> int main() { key_t key = ftok(".", 'b'); int shmid = shmget(key, 4, IPC_CREAT | 0444); if (shmid == -1) { perror("shmget"); return 1; } void* addr = shmat(shmid, NULL, 0); if (addr == (void*) -1) { perror("shmat"); return 1; } printf("success"); return 0; }
I already have read access, but I got “shmat: Permission denied”.
Do I have permission to write?
Advertisement
Answer
From man shmat
:
If
SHM_RDONLY
is specified in shmflg, the segment is attached for reading and the process must have read permission for the segment. Otherwise the segment is attached for read and write and the process must have read and write permission for the segment.
So you need to use SHM_RDONLY
instead of 0
when calling shmat()
.