cunixshared-memorysysv-ipc

System V shared memory permission bits: meaning, and how to change


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:


Solution

  • It's an octal number of ORed options the same ones that you use for directory permissions.

    This is their meaning (source)

    rwx rwx rwx = 111 111 111
    rw- rw- rw- = 110 110 110
    rwx --- --- = 111 000 000
    
    and so on...
    
    rwx = 111 in binary = 7
    rw- = 110 in binary = 6
    r-x = 101 in binary = 5
    r-- = 100 in binary = 4
    

    Where of course, r stands for read and w for write then x means execute.

    There are also constants defined with these values (see man open(2))

    S_IRWXU  00700 user (file owner) has read, write and execute permission
    S_IRUSR  00400 user has read permission
    S_IWUSR  00200 user has write permission
    S_IXUSR  00100 user has execute permission
    S_IRWXG  00070 group has read, write and execute permission
    S_IRGRP  00040 group has read permission
    S_IWGRP  00020 group has write permission
    S_IXGRP  00010 group has execute permission
    S_IRWXO  00007 others have read, write and execute permission
    S_IROTH  00004 others have read permission
    S_IWOTH  00002 others have write permission
    S_IXOTH  00001 others have execute permission
    

    As you can see 0777 has a leading 0 because it's octal and is equivalent to S_IRWXU | S_IRWXG | S_IRWXO.

    To answer your other two questions: