The uid and gid of a file are stored in its inode. When you change a
file's uid or gid, the change is reflected in all the hard links that
are mapped to that file.
The chown() Syscall
The chown() syscall is declared in <unistd.h>. It has the following
prototype:
int chown(const char *path, uid_t owner, gid_t group);
Notice that this function is used for changing both the owner and the
group of a file. The first argument is the file's name or path. The
second and third arguments specify the new owner and group for the file.
Passing -1 as an owner or group indicates that the original value
remains unchanged. Only a root user may change a file's owner. For
security reasons, the setuid bit of the file is cleared whenever its
owner is changed.
To change a file's group, the user must be the file's owner or a root
user. In addition, the owner must belong to the group to which he or she
is changing the file. If the file's group-execute bit is set, the setgid
bit of that file is cleared for security reasons.
The fchown() Syscall
Linux defines another syscall for changing a file's owner and group
called fchown(). This function is declared in <unistd.h> as follows:
int fchown(int fd, uid_t owner, gid_t group);
In terms of functionality, fchown() is identical to chown() except that
it takes a file descriptor as the first argument rather than a filename.
A Note on a Previous Newsletter
The ANSI function tmpnam() discussed in the newsletter from August 16th
is deprecated. The use of this function is considered unsafe since
there's no guarantee the generated filenames are accessed by the process
that called tmpnam(). That said, this limitation is a
quality-of-implementation issue rather than an inherent flaw in the ANSI
specification. Still, the recommendation is to use the POSIX mkstemp()
function instead of tmpnam().