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