Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 4.11 chown,fchown,fchownat,andlchownFunctions 109


portion of a program is the machine instructions.) The program would then load into
memory morequickly the next time it was executed, because the swap area was
handled as a contiguous file, as compared to the possibly random location of data
blocks in a normal UNIX file system. The sticky bit was often set for common
application programs, such as the text editor and the passes of the C compiler.
Naturally,therewas a limit to the number of sticky files that could be contained in the
swap area beforerunning out of swap space, but it was a useful technique. The name
stickycame about because the text portion of the file stuck around in the swap area until
the system was rebooted. Later versions of the UNIX System referred to this as the
saved-textbit; hence the constantS_ISVTX.With today’s newer UNIX systems, most of
which have a virtual memory system and a faster file system, the need for this
technique has disappeared.
On contemporary systems, the use of the sticky bit has been extended. The Single
UNIX Specification allows the sticky bit to be set for a directory.Ifthe bit is set for a
directory,afile in the directory can be removed or renamed only if the user has write
permission for the directory and meets one of the following criteria:
•Owns the file
•Owns the directory
•Isthe superuser
The directories/tmpand/var/tmparetypical candidates for the sticky bit—they are
directories in which any user can typically create files. The permissions for these two
directories areoften read, write, and execute for everyone (user,group, and other). But
users should not be able to delete or rename files owned by others.

The saved-text bit is not part of POSIX.1. It is part of the XSI option defined in the Single
UNIX Specification, and is supported by FreeBSD 8.0, Linux 3.2.0, Mac OS X 10.6.8, and
Solaris 10.
Solaris 10 places special meaning on the sticky bit if it is set on a regular file. In this case, if
none of the execute bits is set, the operating system will not cache the contents of the file.

4.11 chown, fchown, fchownat,and lchown Functions


Thechownfunctions allow us to change a file’s user ID and group ID, but if either of
the argumentsownerorgroupis−1, the corresponding ID is left unchanged.
#include <unistd.h>
int chown(const char *pathname,uid_towner,gid_tgroup);
int fchown(intfd,uid_towner,gid_tgroup);
int fchownat(intfd,const char *pathname,uid_towner,gid_tgroup,
intflag);
int lchown(const char *pathname,uid_towner,gid_tgroup);
All four return: 0 if OK,−1 on error
Free download pdf