xref: /freebsd-14.2/lib/libc/sys/link.2 (revision b2c76c41)
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.\"
30.Dd March 30, 2021
31.Dt LINK 2
32.Os
33.Sh NAME
34.Nm link ,
35.Nm linkat
36.Nd make a hard file link
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In unistd.h
41.Ft int
42.Fn link "const char *name1" "const char *name2"
43.Ft int
44.Fo linkat
45.Fa "int fd1" "const char *name1" "int fd2" "const char *name2" "int flag"
46.Fc
47.Sh DESCRIPTION
48The
49.Fn link
50system call
51atomically creates the specified directory entry (hard link)
52.Fa name2
53with the attributes of the underlying object pointed at by
54.Fa name1 .
55If the link is successful: the link count of the underlying object
56is incremented;
57.Fa name1
58and
59.Fa name2
60share equal access and rights
61to the
62underlying object.
63.Pp
64If
65.Fa name1
66is removed, the file
67.Fa name2
68is not deleted and the link count of the
69underlying object is
70decremented.
71.Pp
72The object pointed at by the
73.Fa name1
74argument
75must exist for the hard link to
76succeed and
77both
78.Fa name1
79and
80.Fa name2
81must be in the same file system.
82The
83.Fa name1
84argument
85may not be a directory.
86.Pp
87The
88.Fn linkat
89system call is equivalent to
90.Fa link
91except in the case where either
92.Fa name1
93or
94.Fa name2
95or both are relative paths.
96In this case a relative path
97.Fa name1
98is interpreted relative to
99the directory associated with the file descriptor
100.Fa fd1
101instead of the current working directory and similarly for
102.Fa name2
103and the file descriptor
104.Fa fd2 .
105.Pp
106Values for
107.Fa flag
108are constructed by a bitwise-inclusive OR of flags from the following
109list, defined in
110.In fcntl.h :
111.Bl -tag -width indent
112.It Dv AT_SYMLINK_FOLLOW
113If
114.Fa name1
115names a symbolic link, a new link for the target of the symbolic link is
116created.
117.It Dv AT_RESOLVE_BENEATH
118Only walk paths below the directory specified by the
119.Ar fd1
120descriptor.
121See the description of the
122.Dv O_RESOLVE_BENEATH
123flag in the
124.Xr open 2
125manual page.
126.It Dv AT_EMPTY_PATH
127If the
128.Fa name1
129argument is an empty string, link the file referenced by the descriptor
130.Fa fd1 .
131The operation requires that the calling process has the
132.Dv PRIV_VFS_FHOPEN
133privilege, effectively being executed with effective user
134.Dv root .
135.El
136.Pp
137If
138.Fn linkat
139is passed the special value
140.Dv AT_FDCWD
141in the
142.Fa fd1
143or
144.Fa fd2
145parameter, the current working directory is used for the respective
146.Fa name
147argument.
148If both
149.Fa fd1
150and
151.Fa fd2
152have value
153.Dv AT_FDCWD ,
154the behavior is identical to a call to
155.Fn link .
156Unless
157.Fa flag
158contains the
159.Dv AT_SYMLINK_FOLLOW
160flag, if
161.Fa name1
162names a symbolic link, a new link is created for the symbolic link
163.Fa name1
164and not its target.
165.Sh RETURN VALUES
166.Rv -std link
167.Sh ERRORS
168The
169.Fn link
170system call
171will fail and no link will be created if:
172.Bl -tag -width Er
173.It Bq Er ENOTDIR
174A component of either path prefix is not a directory.
175.It Bq Er ENAMETOOLONG
176A component of either pathname exceeded 255 characters,
177or entire length of either path name exceeded 1023 characters.
178.It Bq Er ENOENT
179A component of either path prefix does not exist.
180.It Bq Er EOPNOTSUPP
181The file system containing the file named by
182.Fa name1
183does not support links.
184.It Bq Er EMLINK
185The link count of the file named by
186.Fa name1
187would exceed 32767.
188.It Bq Er EACCES
189A component of either path prefix denies search permission.
190.It Bq Er EACCES
191The requested link requires writing in a directory with a mode
192that denies write permission.
193.It Bq Er ELOOP
194Too many symbolic links were encountered in translating one of the pathnames.
195.It Bq Er ENOENT
196The file named by
197.Fa name1
198does not exist.
199.It Bq Er EEXIST
200The link named by
201.Fa name2
202does exist.
203.It Bq Er EPERM
204The file named by
205.Fa name1
206is a directory.
207.It Bq Er EPERM
208The file named by
209.Fa name1
210has its immutable or append-only flag set, see the
211.Xr chflags 2
212manual page for more information.
213.It Bq Er EPERM
214The parent directory of the file named by
215.Fa name2
216has its immutable flag set.
217.It Bq Er EXDEV
218The link named by
219.Fa name2
220and the file named by
221.Fa name1
222are on different file systems.
223.It Bq Er ENOSPC
224The directory in which the entry for the new link is being placed
225cannot be extended because there is no space left on the file
226system containing the directory.
227.It Bq Er EDQUOT
228The directory in which the entry for the new link
229is being placed cannot be extended because the
230user's quota of disk blocks on the file system
231containing the directory has been exhausted.
232.It Bq Er EIO
233An I/O error occurred while reading from or writing to
234the file system to make the directory entry.
235.It Bq Er EINTEGRITY
236Corrupted data was detected while reading from the file system.
237.It Bq Er EROFS
238The requested link requires writing in a directory on a read-only file
239system.
240.It Bq Er EFAULT
241One of the pathnames specified
242is outside the process's allocated address space.
243.El
244.Pp
245In addition to the errors returned by the
246.Fn link ,
247the
248.Fn linkat
249system call may fail if:
250.Bl -tag -width Er
251.It Bq Er EBADF
252The
253.Fa name1
254or
255.Fa name2
256argument does not specify an absolute path and the
257.Fa fd1
258or
259.Fa fd2
260argument, respectively, is neither
261.Dv AT_FDCWD
262nor a valid file descriptor open for searching.
263.It Bq Er EINVAL
264The value of the
265.Fa flag
266argument is not valid.
267.It Bq Er ENOTDIR
268The
269.Fa name1
270or
271.Fa name2
272argument is not an absolute path and
273.Fa fd1
274or
275.Fa fd2 ,
276respectively, is neither
277.Dv AT_FDCWD
278nor a file descriptor associated with a directory.
279.It Bq Er ENOTCAPABLE
280.Fa name1
281is not strictly relative to the starting directory.
282For example,
283.Fa name1
284is absolute or includes a ".." component that escapes
285the directory hierarchy specified by
286.Fa fd ,
287and the process is in capability mode or the
288.Dv AT_RESOLVE_BENEATH
289flag was specified.
290.El
291.Sh SEE ALSO
292.Xr chflags 2 ,
293.Xr readlink 2 ,
294.Xr symlink 2 ,
295.Xr unlink 2
296.Sh STANDARDS
297The
298.Fn link
299system call is expected to conform to
300.St -p1003.1-90 .
301The
302.Fn linkat
303system call follows The Open Group Extended API Set 2 specification.
304.Sh HISTORY
305The
306.Fn link
307function appeared in
308.At v1 .
309The
310.Fn linkat
311system call appeared in
312.Fx 8.0 .
313.Pp
314The
315.Fn link
316system call traditionally allows the super-user to link directories which
317corrupts the file system coherency.
318This implementation no longer permits it.
319