[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 1


chmod or fchmod Subroutine

Purpose

Changes file access permissions.

Library

Standard C Library (libc.a)

Syntax

#include <sys/stat.h>


int chmod ( Path Mode)
const char *Path;
mode_t Mode;


int fchmod ( FileDescriptorMode)
int FileDescriptor;
mode_t Mode;

Description

The chmod subroutine sets the access permissions of the file specified by the Path parameter. If Network File System (NFS) is installed on your system, this path can cross into another node.

Use the fchmod subroutine to set the access permissions of an open file pointed to by the FileDescriptor parameter.

The access control information is set according to the Mode parameter.

Parameters


FileDescriptor Specifies the file descriptor of an open file.
Mode Specifies the bit pattern that determines the access permissions. The Mode parameter is constructed by logically ORing one or more of the following values, which are defined in the sys/mode.h file:

S_ISUID
Enables the setuid attribute for an executable file. A process executing this program acquires the access rights of the owner of the file.

S_ISGID
Enables the setgid attribute for an executable file. A process executing this program acquires the access rights of the group of the file. Also, enables the group-inheritance attribute for a directory. Files created in this directory have a group equal to the group of the directory.

The following attributes apply only to files that are directly executable. They have no meaning when applied to executable text files such as shell scripts and awk scripts.

S_ISVTX
Enables the link/unlink attribute for a directory. Files cannot be linked to in this directory. Files can only be unlinked if the requesting process has write permission for the directory and is either the owner of the file or the directory.

S_ISVTX
Enables the save text attribute for an executable file. The program is not unmapped after usage.

S_ENFMT
Enables enforcement-mode record locking for a regular file. File locks requested with the lockf subroutine are enforced.

S_IRUSR
Permits the file's owner to read it.

S_IWUSR
Permits the file's owner to write to it.

S_IXUSR
Permits the file's owner to execute it (or to search the directory).

S_IRGRP
Permits the file's group to read it.

S_IWGRP
Permits the file's group to write to it.

S_IXGRP
Permits the file's group to execute it (or to search the directory).

S_IROTH
Permits others to read the file.

S_IWOTH
Permits others to write to the file.

S_IXOTH
Permits others to execute the file (or to search the directory).

Other mode values exist that can be set with the mknod subroutine but not with the chmod subroutine.

Path Specifies the full path name of the file.

Return Values

Upon successful completion, the chmod subroutine and fchmod subroutines return a value of 0. If the chmod subroutine or fchmod subroutine is unsuccessful, a value of -1 is returned, and the errno global variable is set to identify the error.

Error Codes

The chmod subroutine is unsuccessful and the file permissions remain unchanged if one of the following is true:

ENOTDIR A component of the Path prefix is not a directory.
EACCES Search permission is denied on a component of the Path prefix.
EFAULT The Path parameter points to a location outside of the allocated address space of the process.
ELOOP Too many symbolic links were encountered in translating the Path parameter.
ENOENT The named file does not exist.
ENAMETOOLONG A component of the Path parameter exceeded 255 characters, or the entire Path parameter exceeded 1023 characters.

The fchmod subroutine is unsuccessful and the file permissions remain unchanged if the following is true:

EBADF The value of the FileDescriptor parameter is not valid.

The chmod or fchmod subroutine is unsuccessful and the access control information for a file remains unchanged if one of the following is true:

EPERM The effective user ID does not match the owner of the file, and the process does not have appropriate privileges.
EROFS The named file resides on a read-only file system.
EIO An I/O error occurred during the operation.

If NFS is installed on your system, the chmod and fchmod subroutines can also be unsuccessful if the following is true:

ESTALE The root or current directory of the process is located in a virtual file system that has been unmounted.
ETIMEDOUT The connection timed out.

Security

Access Control: The invoker must have search permission for all components of the Path prefix.

If you receive the EBUSY error, toggle the enforced locking attribute in the Mode parameter and retry your operation. The enforced locking attribute should never be used on a file that is part of the Trusted Computing Base.

Implementation Specifics

These subroutines are part of Base Operating System (BOS) Runtime.

Related Information

The acl_chg (acl_chg or acl_fchg Subroutine) subroutine, acl_get (acl_get or acl_fget Subroutine) subroutine, acl_put (acl_put or acl_fput Subroutine) subroutine, acl_set (acl_set or acl_fset Subroutine) subroutine, chacl (chacl or fchacl Subroutine) subroutine, statacl subroutine, stat subroutine.

The aclget command, aclput command, chmod command.

List of Security and Auditing Subroutines and Subroutines Overview in AIX 5L Version 5.1 General Programming Concepts: Writing and Debugging Programs.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]