Skip to content

Commit

Permalink
[PATCH] Implement AT_SYMLINK_FOLLOW flag for linkat
Browse files Browse the repository at this point in the history
When the linkat() syscall was added the flag parameter was added in the
last minute but it wasn't used so far.  The following patch should change
that.  My tests show that this is all that's needed.

If OLDNAME is a symlink setting the flag causes linkat to follow the
symlink and create a hardlink with the target.  This is actually the
behavior POSIX demands for link() as well but Linux wisely does not do
this.  With this flag (which will most likely be in the next POSIX
revision) the programmer can choose the behavior, defaulting to the safe
variant.  As a side effect it is now possible to implement a
POSIX-compliant link(2) function for those who are interested.

  touch file
  ln -s file symlink

  linkat(fd, "symlink", fd, "newlink", 0)
    -> newlink is hardlink of symlink

  linkat(fd, "symlink", fd, "newlink", AT_SYMLINK_FOLLOW)
    -> newlink is hardlink of file

The value of AT_SYMLINK_FOLLOW is determined by the definition we already
use in glibc.

Signed-off-by: Ulrich Drepper <[email protected]>
Acked-by: Al Viro <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Ulrich Drepper authored and Linus Torvalds committed Jun 25, 2006
1 parent 584e123 commit 45c9b11
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -2243,14 +2243,16 @@ asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
int error;
char * to;

if (flags != 0)
if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
return -EINVAL;

to = getname(newname);
if (IS_ERR(to))
return PTR_ERR(to);

error = __user_walk_fd(olddfd, oldname, 0, &old_nd);
error = __user_walk_fd(olddfd, oldname,
flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
&old_nd);
if (error)
goto exit;
error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Expand Down
1 change: 1 addition & 0 deletions include/linux/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define AT_SYMLINK_NOFOLLOW 0x100 /* Do not follow symbolic links. */
#define AT_REMOVEDIR 0x200 /* Remove directory instead of
unlinking file. */
#define AT_SYMLINK_FOLLOW 0x400 /* Follow symbolic links. */

#ifdef __KERNEL__

Expand Down

0 comments on commit 45c9b11

Please sign in to comment.