xref: /openbsd-src/lib/libc/sys/link.2 (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1.\"	$OpenBSD: link.2,v 1.21 2011/07/24 06:54:44 matthew Exp $
2.\"	$NetBSD: link.2,v 1.7 1995/02/27 12:34:01 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)link.2	8.3 (Berkeley) 1/12/94
32.\"
33.Dd $Mdocdate: July 24 2011 $
34.Dt LINK 2
35.Os
36.Sh NAME
37.Nm link
38.Nd make hard link to a file
39.Sh SYNOPSIS
40.Fd #include <fcntl.h>
41.Fd #include <unistd.h>
42.Ft int
43.Fn link "const char *name1" "const char *name2"
44.Ft int
45.Fn linkat "int fd1" "const char *name1" "int fd2" "const char *name2" "int flag"
46.Sh DESCRIPTION
47The
48.Fn link
49function atomically creates the specified directory entry (hard link)
50.Fa name2
51with the attributes of the underlying object pointed at by
52.Fa name1 .
53If the link is successful: the link count of the underlying object
54is incremented;
55.Fa name1
56and
57.Fa name2
58share equal access and rights to the underlying object.
59.Pp
60If
61.Fa name1
62is removed, the file
63.Fa name2
64is not deleted and the link count of the underlying object is decremented.
65.Pp
66.Fa name1
67must exist for the hard link to succeed and both
68.Fa name1
69and
70.Fa name2
71must be in the same file system.
72As mandated by POSIX.1
73.Fa name1
74may not be a directory.
75.Pp
76The
77.Fn linkat
78function is equivalent to
79.Fn link
80except that where
81.Fa name1
82or
83.Fa name2
84specifies a relative path,
85the directory entries linked are resolved relative to
86the directories associated with file descriptors
87.Fa fd1
88or
89.Fa fd2
90(respectively) instead of the current working directory.
91.Pp
92If
93.Fn linkat
94is passed the special value
95.Dv AT_FDCWD
96(defined in
97.In fcntl.h )
98in the
99.Fa fd1
100or
101.Fa fd2
102parameter, the current working directory is used for resolving the respective
103.Fa name1
104or
105.Fa name2
106argument.
107.Pp
108Values for
109.Fa flag
110are constructed by bitwise-inclusive
111.Tn OR Ns ing
112flags from the following list defined in
113.In fcntl.h :
114.Pp
115.Bl -tag -width AT_SYMLINK_FOLLOW -offset indent -compact
116.It Dv AT_SYMLINK_FOLLOW
117If
118.Fa name1
119names a symbolic link,
120a new link for the target of the symbolic link is created.
121.El
122.Pp
123If the
124.Dv AT_SYMLINK_FOLLOW
125flag is clear and
126.Fa name1
127names a symbolic link, a new link is created for the symbolic link
128.Fa name1
129and not its target.
130.Sh RETURN VALUES
131Upon successful completion, a value of 0 is returned.
132Otherwise, a value of \-1 is returned and
133.Va errno
134is set to indicate the error.
135.Sh ERRORS
136.Fn link
137and
138.Fn linkat
139will fail and no link will be created if:
140.Bl -tag -width Er
141.It Bq Er ENOTDIR
142A component of either path prefix is not a directory.
143.It Bq Er ENAMETOOLONG
144A component of a pathname exceeded
145.Dv {NAME_MAX}
146characters, or an entire path name exceeded
147.Dv {PATH_MAX}
148characters.
149.It Bq Er ENOENT
150A component of either path prefix does not exist.
151.It Bq Er EOPNOTSUPP
152The file system containing the file named by
153.Fa name1
154does not support links.
155.It Bq Er EMLINK
156The link count of the file named by
157.Fa name1
158would exceed
159.Dv LINK_MAX .
160.It Bq Er EACCES
161A component of either path prefix denies search permission.
162.It Bq Er EACCES
163The requested link requires writing in a directory with a mode
164that denies write permission.
165.It Bq Er ELOOP
166Too many symbolic links were encountered in translating one of the pathnames.
167.It Bq Er ENOENT
168The file named by
169.Fa name1
170does not exist.
171.It Bq Er EEXIST
172The link named by
173.Fa name2
174does exist.
175.It Bq Er EPERM
176The file named by
177.Fa name1
178is a directory and the effective
179user ID is not superuser,
180or the file system containing the file does not permit the use of
181.Fn link
182on a directory.
183.It Bq Er EPERM
184The file named by
185.Fa name1
186is flagged immutable or append-only.
187.It Bq Er EXDEV
188The link named by
189.Fa name2
190and the file named by
191.Fa name1
192are on different file systems.
193.It Bq Er ENOSPC
194The directory in which the entry for the new link is being placed
195cannot be extended because there is no space left on the file
196system containing the directory.
197.It Bq Er EDQUOT
198The directory in which the entry for the new link
199is being placed cannot be extended because the
200user's quota of disk blocks on the file system
201containing the directory has been exhausted.
202.It Bq Er EIO
203An I/O error occurred while reading from or writing to
204the file system to make the directory entry.
205.It Bq Er EROFS
206The requested link requires writing in a directory on a read-only file
207system.
208.It Bq Er EFAULT
209One of the pathnames specified
210is outside the process's allocated address space.
211.El
212.Pp
213Additionally,
214.Fn linkat
215will fail if:
216.Bl -tag -width Er
217.It Bq Er EBADF
218The
219.Fa name1
220or
221.Fa name2
222argument does not specify an absolute path and
223.Fa fd1
224or
225.Fa fd2 ,
226respectively, is neither
227.Dv AT_FDCWD
228nor a valid file descriptor open for reading.
229.El
230.Sh SEE ALSO
231.Xr ln 1 ,
232.Xr readlink 2 ,
233.Xr symlink 2 ,
234.Xr unlink 2
235.Sh STANDARDS
236The
237.Fn link
238and
239.Fn linkat
240functions are expected to conform to
241.St -p1003.1-2008 .
242.Sh HISTORY
243The
244.Fn linkat
245function appeared in
246.Ox 5.0 .
247