Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

chown, fchownat - change owner and group of file

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

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

int chown(const char *path, uid_t owner, gid_t group);
int fchownat(int fd, const char *path, uid_t owner, gid_t group, int flag);

Description

path points to a pathname naming a file. The user ID and group ID of the named file are set to the numeric values contained in owner and group, respectively.

Only processes with an effective user ID equal to the user ID of the file or processes with appropriate privileges may change the user ID and group ID of a file. The following applies if {_POSIX_CHOWN_RESTRICTED} has been activated for path:

Changing the user ID is restricted to processes with appropriate privileges.

Changing the group ID is permitted to a process with an effective user ID equal to the user ID of the file, but without appropriate privileges, if and only if owner is equal to the user ID of the file and group is equal to either the effective group ID of the process or to one of its supplementary group IDs.

If path refers to a regular file, the set-user-ID (S_ISUID) and set-group-ID (S_ISGID) bits of the file mode are cleared upon successful return from chown(), unless the call is made by a process with appropriate privileges, in which case these bits are not altered under POSIX. If chown() is successfully invoked on a file that is not a regular file, these bits may be cleared. These bits are defined in sys/stat.h.

If owner or group is specified as (uid_t)-1 or (gid_t)-1, respectively, the corresponding ID of the file is unchanged.

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

The fchownat() function is equivalent to the chown() or lchown() function except when the path parameter specifies a relative path. In this case the file whose user and group numbers are to be updated is not searched for 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 user and group numbers of the symbolic link are updated.

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

Return val.

0

-1

if successful.

if an error occurs; errno is set to indicate the error.

Errors

chown() and fchownat() will fail if:

 

EACCES

Search permission is denied on a component of path.

 

Extension

 

EFAULT

EINTR

EINVAL

An invalid address was passed as an argument.

A signal was caught during the chown call. (End)

The value of the specified user ID or group ID is not supported (e.g. if the value is less than 0) or an attempt was made to access a BS2000 file.

 

Extension 

 

EIO

ELOOP

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

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

EPERM

A component of path is not a directory.

The effective user ID does not match the owner of the file or the calling process does not have the appropriate privileges, although {_POSIX _CHOWN_RESTRICTED} indicates that such privileges are required.

 

EROFS

The named file resides on a read-only file system.

 

In addition, fchownat() 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.

 

ENOTDIR

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

 

EINVAL

The value of the flag parameter is invalid.

Notes

chown() and fchownat() are executed only for POSIX files.

See also

chmod(), fcntl.h, sys/stat.h, sys/types.h, unistd.h.