clinuxposixgnu

Explanation of Permission Bits


I was looking at the permission bits used in various functions like stat() and chmod(), and I wanted a description of what the macros defined actually are. For instance S_IRUSR says it's represented by 00400 (GNU/Linux). My question is, could someone describe what the 00400 actually is? Is it a number, what? I understand how to OR the macros, I just don't get what the macro actually is.


Solution

  • I am going to describe the left most three numbers in permission and that would also explain about S_IRUSR,

    So each of the numbers is an octal number. Each number could be from 0 to 7. Each octal number could be converted to 3-bit binary number. Each bit represents a permission.

    Left most bit = Read permission
    Middle bit    = Write permission
    Right most bit= Execute permission
    

    Lets write 0 to 7 into binary and see the permission bits:

    Octal    Binary 
    0        0 0 0 (No Read,  No Write,  No Execute) -- No permission
    1        0 0 1 (No Read,  No Write,  Yes you can execute)
    2        0 1 0 (No Read,  Can Write, No execute)
    3        0 1 1 (No Read,  Can Write, Can execute)
    4        1 0 0 (Can Read, No Write,  No Execute)
    5        1 0 1 (Can Read, No Write,  Can execute)
    6        1 1 0 (Can Read, Can Write, No execute)
    7        1 1 1 (Can Read, Can Write, Can execute)
    

    So each number represents permissions. Now next part is for who these pemmissions are. Let the left most three number be XYZ: Now,

    X means permission given to the owner of the file.
    Y means permission given to the group of the owner.
    Z means permission given to all other users in system , outside of user's group.
    

    Given that, Z_ISUSR = 00400, now 4 means readable by user IRUSR = Is Readable by user.

    These three are the important numbers in permissions, and these only specify the permissions given to the file.