#include <sys/types.h> #include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fildes, mode_t mode);
S_ISUID 04000 Set user ID on execution. S_ISGID 020#0 Set group ID on execution if # is 7, 5, 3, or 1. Enable mandatory file/record locking if # is 6, 4, 2, or 0. S_ISVTX 01000 Save text image after execution. S_IRWXU 00700 Read, write, execute by owner. S_IRUSR 00400 Read by owner. S_IWUSR 00200 Write by owner. S_IXUSR 00100 Execute (search if a directory) by owner. S_IRWXG 00070 Read, write, execute by group. S_IRGRP 00040 Read by group. S_IWGRP 00020 Write by group. S_IXGRP 00010 Execute by group. S_IRWXO 00007 Read, write, execute (search) by others. S_IROTH 00004 Read by others. S_IWOTH 00002 Write by others. S_IXOTH 00001 Execute by others.
Modes are constructed by OR’ing the access permission bits.
The effective user ID of the process must match the owner of the file or the process must have the appropriate privilege to change the mode of a file.
If the process is not a privileged process and the file is not a directory, mode bit 01000 (save text image on execution) is cleared.
If neither the process is privileged, nor the file’s group is
a member of the process’s supplementary group list, and the effective group
ID
of the process does not match the group ID
of the file, mode bit 02000
(set group ID
on execution) is cleared.
If a directory is writable and has S_ISVTX (the sticky bit) set, files within that directory can be removed or renamed only if one or more of the following is true (see unlink(2) and rename(2) ):
the user is a privileged user
- the user owns the file
- the user owns the directory
- the file is writable by the user
If a directory has the set group ID bit set, a given file created within that directory will have the same group ID as the directory, if that group ID is part of the group ID set of the process that created the file. Otherwise, the newly created file’s group ID will be set to the effective group ID of the creating process.
If the mode bit 02000 (set group ID on execution) is set and the mode bit 00010 (execute or search by group) is not set, mandatory file/record locking will exist on a regular file. This may affect future calls to open(2) , creat(2) , read(2) , and write(2) on this file.
Upon successful completion, chmod() and fchmod() mark for update the st_ctime field of the file.
chmod() will fail and the file mode will be unchanged if one or more of the following are true:
fchmod() will fail and the file mode will be unchanged if: