Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

chmod, fchmodat - change mode of file

&pagelevel(4)&pagelevel

Syntax

#include <sys/stat.h>

Optional
#include <sys/types.h> (End)

int chmod(const char *path, mode_t mode);
int fchmodat(int fd, const char *path, mode_t mode, int flag); 

Description

chmod() changes S_ISUID, S_ISGID and the file permission bits of the file pointed to by the path argument to the corresponding bits in the mode argument. The effective user ID of the process must match the owner of the file or have appropriate privileges for this purpose.

S_ISUID, S_ISGID and the file permission bits are described in sys/stat.h.

If the calling process does not have appropriate privileges, and if the group ID of the file does not match the effective group ID or one of the supplementary group IDs, and if the file is a regular file, the S_ISGID (set-group-ID on execution) bit in the file ́s mode will be cleared upon successful return from chmod().

In the C runtime system, chmod() is also executed for open files. Other X/Open-compatible systems can define other specifications for this case.

Upon successful completion, chmod() will mark for update the st_ctime field of the file.

The fchmodat() function is equivalent to the chmod() function except when the path parameter specifies a relative path. In this case the file to be updated is not searched in the current directory, but in the directory connected with the file descriptor fd. If the file descriptor was opened without O_SEARCH, the function checks whether a search is permitted in the connected file descriptor with the authorizations applicable for the directory.
If the file descriptor was opened with O_SEARCH, the check is not performed.

In the flag parameter, the value AT_SYMLINK_NOFOLLOW, which is defined in the fnctl.h header, can be transferred. If path specifies a symbolic link, the symbolic link is updated.

When the value AT_FDCWD is transferred to the fchmodat() function for the fd parameter, the current directory is used.


Return val.

0

if successful. The access permissions of the specified file are set as defined.


-1

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

Errors

chmod and fchmodat() will fail if:

 

EACCES

Search permission is denied on a component of the path prefix.

 

Extension

 

EFAULT

path points outside the allocated address space of the process.

 

EINTR

A signal was caught during execution of the system call. (End)

 


EINVAL

The value of mode is invalid.

 

Extension

 

EIO

An I/O error occurred while reading from or writing to the file system.

 

ELOOP

Too many symbolic links were encountered in resolving path. (End)

 

ENAMETOOLONG



The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.

 

ENOENT

path points to the name of a file that does not exist or to an empty string.

 

ENOTDIR

A component of path is not a directory.

 

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.


In addition, fchmodat() fails if the following applies:

 

EACCES

The fd parameter was not opened with O_SEARCH, and the authorizations applicable for the directory do not permit the directory to be searched.

 

EBADF

The path parameter does not specify an absolute pathname, and the fd parameter does not have the value AT_FDCWD, nor does it contain a valid file descriptor opened for reading or searching.

 

Extension

 

EBADF

An attempt was made to access a BS2000 file. (End)

 

ENOTDIR

The path parameter does not specify an absolute pathname, and the file descriptor fd is not connected with a directory.

 

EINVAL

The value of the flag parameter is invalid.

Notes

fchmodat() is only allowed for POSIX files. 

See also

chown(), fchmod(), mkdir(), mkfifo(), open(), stat(), fcntl.h, sys/types.h, sys/stat.h.