Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 4.17 Symbolic Links 121


Symbolic links wereintroduced with 4.2BSD. Initially,chowndidn’t follow symbolic links, but
this behavior was changed in 4.4BSD. System V included support for symbolic links in SVR4,
but diverged from the original BSD behavior by implementingchownto follow symbolic links.
In older versions of Linux (those beforeversion 2.1.81),chowndidn’t follow symbolic links.
From version 2.1.81 onward,chownfollows symbolic links. With FreeBSD 8.0, Mac OS X
10.6.8, and Solaris 10,chownfollows symbolic links. All of these platforms provide
implementations oflchownto change the ownership of symbolic links themselves.

Does not follow Follows
Function symbolic link symbolic link

access •
chdir •
chmod •
chown •
creat •
exec •
lchown •
link •
lstat •
open •
opendir •
pathconf •
readlink •
remove •
rename •
stat •
truncate •
unlink •

Figure 4.17Tr eatment of symbolic links by various functions

One exception to the behavior summarized in Figure4.17 occurs when theopen
function is called with bothO_CREATandO_EXCLset. In this case, if the pathname
refers to a symbolic link,openwill fail witherrnoset toEEXIST.This behavior is
intended to close a security hole so that privileged processes can’t be fooled into writing
to the wrong files.

Example


It is possible to introduce loops into the file system by using symbolic links. Most
functions that look up a pathname return anerrno ofELOOP when this occurs.
Consider the following commands:
$mkdir foo make a new directory
$touch foo/a create a 0-length file
$ln -s ../foo foo/testdir create a symbolic link
$ls -l foo
total 0
-rw-r----- 1 sar 0 Jan 22 00:16 a
lrwxrwxrwx 1 sar 6 Jan 22 00:16 testdir -> ../foo
Free download pdf