chown, fchown, lchown, fchownat - change ownership of a file
用于改变user ID and group ID,如果某一个为-1,相应的ID保持不变。
原型如下
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);
//Returns: 0 if OK, -1 on error
这些系统调用在引用的文件是symbolic link
时,才有所不同。在该情况下,lchown
and fchownat
(使用AT_SYMLINK_NOFOLLOW
flag set)会改变链接文件
本身的所有者,而不是链接文件指向的文件。
fchown
function behaves like either chown
or lchown
when the pathname argument is absolute or when the fd argument has the value AT_FDCWD
and the pathname argument is relative.In these cases, fchownat
acts like lchown
if the
AT_SYMLINK_NOFOLLOW
flag is set
in the flag argument, or it acts likechown
if the AT_SYMLINK_NOFOLLOW
flag is clear
. When the fd argument is set to the file descriptor of an open directory and the pathname argument is a relative pathname, fchownat
evaluates(评价) the pathname relative to the open directory.
Linux enforced the restriction that only the superuser can change the ownership of a file.
If _POSIX_CHOWN_RESTRICTED is in effect for the specified file, then
1. Only a superuser process can change the user ID of the file.
2. A nonsuperuser process can change the group ID of the file if the process owns the file (the effective user ID equals the user ID of the file), owner is specified as −1 or equals the user ID of the file, and group equals either the effective group ID of the process or one of the process’s supplementary group IDs.
This means that when _POSIX_CHOWN_RESTRICTED is in effect, you can’t change the user ID of your files. You can change the group ID of files that you own, but only to groups that you belong to.
If these functions are called by a process other than a superuser process, on successful return, both the set-user-ID and the set-group-ID bits are cleared.