xref: /illumos-gate/usr/src/lib/libc/port/sys/link.c (revision f9e0b1dc6f25356fd35837dc9e1124b2d91ed6f3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include "lint.h"
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/syscall.h>
30 
31 extern	int __xpg4; /* defined in port/gen/xpg4.c; 0 if not xpg4/xpg4v2 */
32 
33 int
34 linkat(int fd1, const char *path1, int fd2, const char *path2, int flag)
35 {
36 	return (syscall(SYS_linkat, fd1, path1, fd2, path2, flag));
37 }
38 
39 #pragma	weak _link = link
40 int
41 link(const char *path1, const char *path2)
42 {
43 	/*
44 	 * XPG4v2 link() requires that the link count of a symbolic
45 	 * link target be updated rather than the link itself.  This
46 	 * matches SunOS 4.x and other BSD based implementations.
47 	 * However, the SVR4 merge apparently introduced the change
48 	 * that allowed link(src, dest) when "src" was a symbolic link,
49 	 * to create "dest" as a hard link to "src".  Hence, the link
50 	 * count of the symbolic link is updated rather than the target
51 	 * of the symbolic link. This latter behavior remains for
52 	 * non-XPG4 based environments. For a more detailed discussion,
53 	 * see bug 1256170.
54 	 */
55 	if (__xpg4 != 0)
56 		return (linkat(AT_FDCWD, path1, AT_FDCWD, path2,
57 		    AT_SYMLINK_FOLLOW));
58 
59 #if defined(_RETAIN_OLD_SYSCALLS)
60 	return (syscall(SYS_link, path1, path2));
61 #else
62 	return (linkat(AT_FDCWD, path1, AT_FDCWD, path2, 0));
63 #endif
64 }
65