chmod(2) chmod(2)
NAME
chmod, fchmod - change mode of file
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *path, modet mode);
int fchmod(int fildes, modet mode);
DESCRIPTION
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. Access permission bits
are described in <sys/stat.h> [see stat(5)] and are interpreted as
follows:
SISUID 04000 Set user ID on execution.
SISGID 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
SISVTX 01000 Save text image after execution.
SIRWXU 00700 Read, write, execute (search) by owner.
SIRUSR 00400 Read by owner.
SIWUSR 00200 Write by owner.
SIXUSR 00100 Execute (search if a directory) by owner.
SIRWXG 00070 Read, write, execute by group.
SIRGRP 00040 Read by group.
SIWGRP 00020 Write by group.
SIXGRP 00010 Execute by group.
SIRWXO 00007 Read, write, execute (search) by others.
SIROTH 00004 Read by others.
SIWOTH 00002 Write by others
SIXOTH 00001 Execute by others.
Other modes are constructed by bitwise OR combination of 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 nor a member of the supplementary group list is
privileged, 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.
Page 1 Reliant UNIX 5.44 Printed 11/98
chmod(2) chmod(2)
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 avail-
able (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 has the sticky bit set, files within
that directory can be removed (deleted) or renamed only if one or more
of the following are true [see unlink(2) and rename(2)]:
- the user owns the file
- the user owns the directory
- the file is writable by the user
- the user is a privileged user
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
stctime field of the file.
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
chmod() will fail and the file mode will be unchanged if one or more
of the following apply:
EACCES Search permission is denied on a component of the path
prefix of path.
EFAULT path points outside the allocated address space of the
process.
EIO An I/O error occurred while reading from or writing to
the file system.
ELOOP Too many symbolic links were encountered in translating
path.
EMULTIHOP Components of path require hopping to multiple remote
machines and file system type does not allow it.
Page 2 Reliant UNIX 5.44 Printed 11/98
chmod(2) chmod(2)
ENAMETOOLONG The length of the path argument exceeds PATHMAX or the
length of a path component exceeds NAMEMAX.
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 path-
name.
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 or the process does not have appropriate privilege.
EROFS The file referred to by path resides on a read-only file
system.
The chmod() function may fail if:
EINTR A signal was caught during execution of the function.
EINVAL The value of the mode argument is invalid.
ENAMETOOLONG Pathname resolution of a symbolic link produced an
intermediate result whose length exceeds PATHMAX.
fchmod() will fail and the file mode will be unchanged if:
EBADF fildes is not an open file descriptor.
EIO An I/O error occurred while reading from or writing to
the file system.
EINTR A signal was caught during execution of the fchmod()
system call.
EINVAL The value of the mode argument is invalid.
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 or the process does not have appropriate privilege.
EROFS The file referred to by fildes resides on a read-only
file system.
RESULT
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned and errno is set to indicate the error.
Page 3 Reliant UNIX 5.44 Printed 11/98
chmod(2) chmod(2)
NOTES
SISUID, SISGID and the file permission bits are described in
<sys/stat.h> [see stat(5)] .
If a directory is writable and the mode bit SISVTX 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 appropriate privileges.
If the SISVTX bit is set on a non-directory file, the behavior is
unspecified.
In order to ensure that the SISUID and SISGID bits are set, an
application requiring this should use stat() after a successful
chmod() in order to verify this.
Any file descriptors currently open by any process on the file may
become invalid if the mode of the file is changed to a value which
would deny access to that process. One situation where this could
occur is on a stateless file system.
SEE ALSO
chmod(1), chown(2), creat(2), fcntl(2), mknod(2), open(2), read(2),
stat(2), write(2), mkfifo(3C), stat(5), types(5).
Page 4 Reliant UNIX 5.44 Printed 11/98