Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fchmod - change mode of file

&pagelevel(4)&pagelevel

Syntax

#include <sys/types.h>
#include <sys/stat.h>

int fchmod(int fildes, mode_t mode); 

Description

Like chmod(), fchmod() changes S_ISUID, S_ISGID and the file mode bits of the addressed file into the corresponding bits of mode, except that the file whose access permissions are to be changed is identified not by the pathname but by the file descriptor fildes. The file mode bits are interpreted as follows (see also sys/stat.h):

Symbolic name

Bit pattern

Meaning

S_ISUID

04000

Set user ID on execution

S_ISGID

020#0

Set group ID on execution if the value of # is 7, 5, 3 or 1. Remove mandatory lock on files and file records if # is 6, 4, 2 or 0

S_ISVTX

01000

Save text segment after execution

S_IRWXU

00700

Read, write or execute (search) 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 or execute (search) 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 or execute (search) by others

S_IROTH

00004

Read by others

S_IWOTH

00002

Write by others

S_IXOTH

00001

Execute by others

Other modes are constructed by a bit-wise OR combination of the file mode 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 neither the process nor a member of the supplementary group list is privileged, and if the effective group ID of the process does not match the group ID of the file, the mode bit 02000 (set group ID on execution) is cleared.

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(), creat(), read() and write() on this file.

If the process is not a privileged process and the file is not a directory, the mode bit 01000 (save text segment after execution) is deleted.

If a directory can be written to and the sticky bit is set, files in this directory can only be deleted or renamed if at least one of the following is true (see unlink() and rename()):

the file belongs to the user
the directory belongs to the user
the user has right permission for the file
the user is a privileged user

On successful completion, fchmod() marks the st_ctime field of the file for update.

Return val.

0

-1

if successful.

if an error occurs. The file mode is not changed.
errno is set to indicate the error.

Errors

fchmod() will fail if:

 

EBADF

fildes is not an open file descriptor.

 

EINVAL

An attempt was made to access a BS2000 file, or the value of mode is invalid.

 

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.

 

EPERM

user ID does not match that of the file owner, and the process does not have the appropriate privileges.

 

EROFS

The file referred to by fildes resides on a read-only file system.

Notes

fchmod() is executed only for POSIX files. 

See also

chmod(), chown(), creat(), fcntl(), fstatvfs(), mknod(), open(), read(), rename(), stat(), unlink(), write(), sys/stat.h, sys/types.h.