I know that when i create a shared memory block, we set the permission so that every proccess can read and write in that block with 0777 (no idea why, my teacher just said to use it like that).
I'm creating with shmget as:
shmget(IPC_PRIVATE, sizeof(server_config), IPC_CREAT|0777)
However I'd like to know:
What each number means
How to change the flag after the shared memory block is created
How to only allow 1 proccess to write, while all the other proccesses can only read
It's an octal number of
OR
ed options the same ones that you use for directory permissions.This is their meaning (source)
Where of course,
r
stands for read andw
for write thenx
means execute.There are also constants defined with these values (see man
open(2)
)As you can see
0777
has a leading0
because it's octal and is equivalent toS_IRWXU | S_IRWXG | S_IRWXO
.To answer your other two questions:
You can change the permissions on a shared memory block with
shmctl
. It goes something like this -- completely untested, may be buggy:To allow only one process to write, while all others can only read, the process with write privileges must run under its own uid. Then you have that process create the memory segment and set its permissions to
0644
. It should be clear from the explanation of mode bits why this has the desired effect.