xref: /freebsd-13.1/lib/libc/sys/link.2 (revision 8c384596)
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 March 30, 2021
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_RESOLVE_BENEATH
119Only walk paths below the directory specified by the
120.Ar fd1
121descriptor.
122See the description of the
123.Dv O_RESOLVE_BENEATH
124flag in the
125.Xr open 2
126manual page.
127.It Dv AT_EMPTY_PATH
128If the
129.Fa name1
130argument is an empty string, link the file referenced by the descriptor
131.Fa fd1 .
132The operation requires that the calling process has the
133.Dv PRIV_VFS_FHOPEN
134privilege, effectively being executed with effective user
135.Dv root .
136.El
137.Pp
138If
139.Fn linkat
140is passed the special value
141.Dv AT_FDCWD
142in the
143.Fa fd1
144or
145.Fa fd2
146parameter, the current working directory is used for the respective
147.Fa name
148argument.
149If both
150.Fa fd1
151and
152.Fa fd2
153have value
154.Dv AT_FDCWD ,
155the behavior is identical to a call to
156.Fn link .
157Unless
158.Fa flag
159contains the
160.Dv AT_SYMLINK_FOLLOW
161flag, if
162.Fa name1
163names a symbolic link, a new link is created for the symbolic link
164.Fa name1
165and not its target.
166.Sh RETURN VALUES
167.Rv -std link
168.Sh ERRORS
169The
170.Fn link
171system call
172will fail and no link will be created if:
173.Bl -tag -width Er
174.It Bq Er ENOTDIR
175A component of either path prefix is not a directory.
176.It Bq Er ENAMETOOLONG
177A component of either pathname exceeded 255 characters,
178or entire length of either path name exceeded 1023 characters.
179.It Bq Er ENOENT
180A component of either path prefix does not exist.
181.It Bq Er EOPNOTSUPP
182The file system containing the file named by
183.Fa name1
184does not support links.
185.It Bq Er EMLINK
186The link count of the file named by
187.Fa name1
188would exceed 32767.
189.It Bq Er EACCES
190A component of either path prefix denies search permission.
191.It Bq Er EACCES
192The requested link requires writing in a directory with a mode
193that denies write permission.
194.It Bq Er ELOOP
195Too many symbolic links were encountered in translating one of the pathnames.
196.It Bq Er ENOENT
197The file named by
198.Fa name1
199does not exist.
200.It Bq Er EEXIST
201The link named by
202.Fa name2
203does exist.
204.It Bq Er EPERM
205The file named by
206.Fa name1
207is a directory.
208.It Bq Er EPERM
209The file named by
210.Fa name1
211has its immutable or append-only flag set, see the
212.Xr chflags 2
213manual page for more information.
214.It Bq Er EPERM
215The parent directory of the file named by
216.Fa name2
217has its immutable flag set.
218.It Bq Er EXDEV
219The link named by
220.Fa name2
221and the file named by
222.Fa name1
223are on different file systems.
224.It Bq Er ENOSPC
225The directory in which the entry for the new link is being placed
226cannot be extended because there is no space left on the file
227system containing the directory.
228.It Bq Er EDQUOT
229The directory in which the entry for the new link
230is being placed cannot be extended because the
231user's quota of disk blocks on the file system
232containing the directory has been exhausted.
233.It Bq Er EIO
234An I/O error occurred while reading from or writing to
235the file system to make the directory entry.
236.It Bq Er EINTEGRITY
237Corrupted data was detected while reading from the file system.
238.It Bq Er EROFS
239The requested link requires writing in a directory on a read-only file
240system.
241.It Bq Er EFAULT
242One of the pathnames specified
243is outside the process's allocated address space.
244.El
245.Pp
246In addition to the errors returned by the
247.Fn link ,
248the
249.Fn linkat
250system call may fail if:
251.Bl -tag -width Er
252.It Bq Er EBADF
253The
254.Fa name1
255or
256.Fa name2
257argument does not specify an absolute path and the
258.Fa fd1
259or
260.Fa fd2
261argument, respectively, is neither
262.Dv AT_FDCWD
263nor a valid file descriptor open for searching.
264.It Bq Er EINVAL
265The value of the
266.Fa flag
267argument is not valid.
268.It Bq Er ENOTDIR
269The
270.Fa name1
271or
272.Fa name2
273argument is not an absolute path and
274.Fa fd1
275or
276.Fa fd2 ,
277respectively, is neither
278.Dv AT_FDCWD
279nor a file descriptor associated with a directory.
280.It Bq Er ENOTCAPABLE
281.Fa name1
282is not strictly relative to the starting directory.
283For example,
284.Fa name1
285is absolute or includes a ".." component that escapes
286the directory hierarchy specified by
287.Fa fd ,
288and the process is in capability mode or the
289.Dv AT_RESOLVE_BENEATH
290flag was specified.
291.El
292.Sh SEE ALSO
293.Xr chflags 2 ,
294.Xr readlink 2 ,
295.Xr symlink 2 ,
296.Xr unlink 2
297.Sh STANDARDS
298The
299.Fn link
300system call is expected to conform to
301.St -p1003.1-90 .
302The
303.Fn linkat
304system call follows The Open Group Extended API Set 2 specification.
305.Sh HISTORY
306The
307.Fn link
308function appeared in
309.At v1 .
310The
311.Fn linkat
312system call appeared in
313.Fx 8.0 .
314.Pp
315The
316.Fn link
317system call traditionally allows the super-user to link directories which
318corrupts the file system coherency.
319This implementation no longer permits it.
320