chmod and fchmod set the access permission
portion of the mode of the file whose name is given by
path or referenced by the descriptor fildes to
the bit pattern contained in mode. If path or
fildes are symbolic links, the access permissions of the
target of the symbolic links are set. Access permission bits are
interpreted as follows:
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 an OR of the access permission
bits.
The effective user ID of the process must match the owner
of the file or be the superuser, to change the mode of a file.
If the effective user ID of the process is not the
superuser, and the file is not a directory, mode bit 01000 (save
text image on execution) is cleared.
If the effective user ID of the process is not superuser
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 0410 executable file has the sticky bit (mode bit 01000) set,
the operating system will not delete the program text from the swap
area when the last user process terminates. If a 0413 or
ELF executable file has the sticky bit set, the operating
system will not delete the program text from memory when the last
user process terminates. In either case, if the sticky bit is set
the text will already be available (either in a swap area or in
memory) when the next user of the file executes it, thus making
execution faster.
If a directory is writable and the sticky bit, S_ISVT,
is set on the directory, a process may remove or rename files
within that directory only if one or more of the following
is true:
the effective user ID of the process is the same as that
of the owner ID of the file
the effective user ID of the process is the same as that
of the owner ID of the directory
the process has write permission for the file
the process has appropriate privileges
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(S),
creat(S),
read(S),
and
write(S)
on this file.
If the mode bit 02000 is not set, the group ID of all
created sub-directories is set to the group ID of the
user. If it is set, the group ID of all created
sub-directories is set to that of the parent directory and the set
group ID bit is set.
Return values
On success, chmod and fchmod return 0 and mark
for update the st_ctime field of the file. On failure,
chmod and fchmod return -1, set errno
to identify the error, and the file mode is unchanged.
Diagnostics
In the following conditions, chmod fails and sets
errno to:
[EACCES]
Search permission is denied on a component of the path prefix of
path.
[EACCES]
Write permission on the named file is denied.
[EFAULT]
path
points outside the allocated address space of the process.
[EINTR]
A signal was caught during execution of the system call.
[EIO]
An I/O error occurred while reading from or writing to the
filesystem.
[ELOOP]
Too many symbolic links were encountered in translating
path.
[EMULTIHOP]
Components of path require hopping to multiple remote
machines and filesystem type does not allow it.
[ENAMETOOLONG
The length of the path argument exceeds
{PATH_MAX}, or the length of a path component
exceeds {NAME_MAX} while {_POSIX_NO_TRUNC} is in
effect.
[ENOTDIR]
A component of the prefix of path is not a directory.
[ENOENT]
Either a component of the path prefix, or the file referred to by
path does not exist or is a null pathname.
[ENOLINK]
fildes
points to a remote machine and the link to that machine is no longer
active.
[EPERM]
The effective user ID does not match the owner of the file
and the effective user ID is not superuser.
[EROFS]
The file referred to by path resides on a read-only
filesystem.
In the following conditions, fchmod fails and sets
errno to:
[EBADF]
fildes is not an open file descriptor
[EIO]
An I/O error occurred while reading from or writing to the
filesystem.
[EINTR]
A signal was caught during execution of the fchmod system
call.
[ENOLINK]
path
points to a remote machine and the link to that machine is no longer
active.
[EPERM]
The effective user ID does not match the owner of the file
and the effective user ID is not superuser.
[EROFS]
The file referred to by fildes resides on a read-only
filesystem.