Every file has an owner which is one of the registered user names defined on the system. Each file also has a group, which is one of the defined groups. The file owner can often be useful for showing you who edited the file (especially when you edit with GNU Emacs), but its main purpose is for access control.
The file owner and group play a role in determining access because the file has one set of access permission bits for the user that is the owner, another set that apply to users who belong to the file's group, and a third set of bits that apply to everyone else. See Access Permission, for the details of how access is decided based on this data.
When a file is created, its owner is set from the effective user ID of the process that creates it (see Process Persona). The file's group ID may be set from either effective group ID of the process, or the group ID of the directory that contains the file, depending on the system where the file is stored. When you access a remote file system, it behaves according to its own rule, not according to the system your program is running on. Thus, your program must be prepared to encounter either kind of behavior, no matter what kind of system you run it on.
You can change the owner and/or group owner of an existing file using the chown
function. This is the primitive for the chown
and chgrp
shell commands.
The prototype for this function is declared in `unistd.h'.
chown
function changes the owner of the file filename to owner, and its group owner to group. Changing the owner of the file on certain systems clears the set-user-ID and set-group-ID bits of the file's permissions. (This is because those bits may not be appropriate for the new owner.) The other file permission bits are not changed.
The return value is 0
on success and -1
on failure. In addition to the usual file name syntax errors (see File Name Errors), the following errno
error conditions are defined for this function:
EPERM
Only privileged users or the file's owner can change the file's group. On most file systems, only privileged users can change the file owner; some file systems allow you to change the owner if you are currently the owner. When you access a remote file system, the behavior you encounter is determined by the system that actually holds the file, not by the system your program is running on.
See Options for Files, for information about the _POSIX_CHOWN_RESTRICTED
macro.
EROFS
chown
, except that it changes the owner of the file with open file descriptor filedes.
The return value from fchown
is 0
on success and -1
on failure. The following errno
error codes are defined for this function:
EBADF
EINVAL
EPERM
chmod
, above.
EROFS