xref: /freebsd-14.2/lib/libc/sys/link.2 (revision 2c19e8ed)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)link.2	8.3 (Berkeley) 1/12/94
29.\" $FreeBSD$
30.\"
31.Dd September 23, 2020
32.Dt LINK 2
33.Os
34.Sh NAME
35.Nm link ,
36.Nm linkat
37.Nd make a hard file link
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn link "const char *name1" "const char *name2"
44.Ft int
45.Fo linkat
46.Fa "int fd1" "const char *name1" "int fd2" "const char *name2" "int flag"
47.Fc
48.Sh DESCRIPTION
49The
50.Fn link
51system call
52atomically creates the specified directory entry (hard link)
53.Fa name2
54with the attributes of the underlying object pointed at by
55.Fa name1 .
56If the link is successful: the link count of the underlying object
57is incremented;
58.Fa name1
59and
60.Fa name2
61share equal access and rights
62to the
63underlying object.
64.Pp
65If
66.Fa name1
67is removed, the file
68.Fa name2
69is not deleted and the link count of the
70underlying object is
71decremented.
72.Pp
73The object pointed at by the
74.Fa name1
75argument
76must exist for the hard link to
77succeed and
78both
79.Fa name1
80and
81.Fa name2
82must be in the same file system.
83The
84.Fa name1
85argument
86may not be a directory.
87.Pp
88The
89.Fn linkat
90system call is equivalent to
91.Fa link
92except in the case where either
93.Fa name1
94or
95.Fa name2
96or both are relative paths.
97In this case a relative path
98.Fa name1
99is interpreted relative to
100the directory associated with the file descriptor
101.Fa fd1
102instead of the current working directory and similarly for
103.Fa name2
104and the file descriptor
105.Fa fd2 .
106.Pp
107Values for
108.Fa flag
109are constructed by a bitwise-inclusive OR of flags from the following
110list, defined in
111.In fcntl.h :
112.Bl -tag -width indent
113.It Dv AT_SYMLINK_FOLLOW
114If
115.Fa name1
116names a symbolic link, a new link for the target of the symbolic link is
117created.
118.It Dv AT_BENEATH
119Only allow to link to a file which is beneath of the topping directory.
120See the description of the
121.Dv O_BENEATH
122flag in the
123.Xr open 2
124manual page.
125.It Dv AT_RESOLVE_BENEATH
126Only walks paths below the topping directory.
127See the description of the
128.Dv O_RESOLVE_BENEATH
129flag in the
130.Xr open 2
131manual page.
132.El
133.Pp
134If
135.Fn linkat
136is passed the special value
137.Dv AT_FDCWD
138in the
139.Fa fd1
140or
141.Fa fd2
142parameter, the current working directory is used for the respective
143.Fa name
144argument.
145If both
146.Fa fd1
147and
148.Fa fd2
149have value
150.Dv AT_FDCWD ,
151the behavior is identical to a call to
152.Fn link .
153Unless
154.Fa flag
155contains the
156.Dv AT_SYMLINK_FOLLOW
157flag, if
158.Fa name1
159names a symbolic link, a new link is created for the symbolic link
160.Fa name1
161and not its target.
162.Sh RETURN VALUES
163.Rv -std link
164.Sh ERRORS
165The
166.Fn link
167system call
168will fail and no link will be created if:
169.Bl -tag -width Er
170.It Bq Er ENOTDIR
171A component of either path prefix is not a directory.
172.It Bq Er ENAMETOOLONG
173A component of either pathname exceeded 255 characters,
174or entire length of either path name exceeded 1023 characters.
175.It Bq Er ENOENT
176A component of either path prefix does not exist.
177.It Bq Er EOPNOTSUPP
178The file system containing the file named by
179.Fa name1
180does not support links.
181.It Bq Er EMLINK
182The link count of the file named by
183.Fa name1
184would exceed 32767.
185.It Bq Er EACCES
186A component of either path prefix denies search permission.
187.It Bq Er EACCES
188The requested link requires writing in a directory with a mode
189that denies write permission.
190.It Bq Er ELOOP
191Too many symbolic links were encountered in translating one of the pathnames.
192.It Bq Er ENOENT
193The file named by
194.Fa name1
195does not exist.
196.It Bq Er EEXIST
197The link named by
198.Fa name2
199does exist.
200.It Bq Er EPERM
201The file named by
202.Fa name1
203is a directory.
204.It Bq Er EPERM
205The file named by
206.Fa name1
207has its immutable or append-only flag set, see the
208.Xr chflags 2
209manual page for more information.
210.It Bq Er EPERM
211The parent directory of the file named by
212.Fa name2
213has its immutable flag set.
214.It Bq Er EXDEV
215The link named by
216.Fa name2
217and the file named by
218.Fa name1
219are on different file systems.
220.It Bq Er ENOSPC
221The directory in which the entry for the new link is being placed
222cannot be extended because there is no space left on the file
223system containing the directory.
224.It Bq Er EDQUOT
225The directory in which the entry for the new link
226is being placed cannot be extended because the
227user's quota of disk blocks on the file system
228containing the directory has been exhausted.
229.It Bq Er EIO
230An I/O error occurred while reading from or writing to
231the file system to make the directory entry.
232.It Bq Er EINTEGRITY
233Corrupted data was detected while reading from the file system.
234.It Bq Er EROFS
235The requested link requires writing in a directory on a read-only file
236system.
237.It Bq Er EFAULT
238One of the pathnames specified
239is outside the process's allocated address space.
240.El
241.Pp
242In addition to the errors returned by the
243.Fn link ,
244the
245.Fn linkat
246system call may fail if:
247.Bl -tag -width Er
248.It Bq Er EBADF
249The
250.Fa name1
251or
252.Fa name2
253argument does not specify an absolute path and the
254.Fa fd1
255or
256.Fa fd2
257argument, respectively, is neither
258.Dv AT_FDCWD
259nor a valid file descriptor open for searching.
260.It Bq Er EINVAL
261The value of the
262.Fa flag
263argument is not valid.
264.It Bq Er ENOTDIR
265The
266.Fa name1
267or
268.Fa name2
269argument is not an absolute path and
270.Fa fd1
271or
272.Fa fd2 ,
273respectively, is neither
274.Dv AT_FDCWD
275nor a file descriptor associated with a directory.
276.It Bq Er ENOTCAPABLE
277.Fa name1
278is not strictly relative to the starting directory.
279For example,
280.Fa name1
281is absolute or includes a ".." component that escapes
282the directory hierarchy specified by
283.Fa fd ,
284and the process is in capability mode.
285.It Bq Er ENOTCAPABLE
286The
287.Dv AT_BENEATH
288flag was provided to
289.Fa linkat
290and the absolute path
291.Fa name1
292does not have its tail fully contained under the topping directory,
293or the relative path
294.Fa name1
295escapes it.
296.El
297.Sh SEE ALSO
298.Xr chflags 2 ,
299.Xr readlink 2 ,
300.Xr symlink 2 ,
301.Xr unlink 2
302.Sh STANDARDS
303The
304.Fn link
305system call is expected to conform to
306.St -p1003.1-90 .
307The
308.Fn linkat
309system call follows The Open Group Extended API Set 2 specification.
310.Sh HISTORY
311The
312.Fn link
313function appeared in
314.At v1 .
315The
316.Fn linkat
317system call appeared in
318.Fx 8.0 .
319.Pp
320The
321.Fn link
322system call traditionally allows the super-user to link directories which
323corrupts the file system coherency.
324This implementation no longer permits it.
325