xref: /netbsd-src/lib/libc/sys/link.2 (revision f6266847792ee268885ec5fb404ded8cc5493c5b)
1.\"	$NetBSD: link.2,v 1.34 2024/07/20 13:12:19 uwe Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993, 2011
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)link.2	8.3 (Berkeley) 1/12/94
31.\"
32.Dd July 20, 2024
33.Dt LINK 2
34.Os
35.Sh NAME
36.Nm link
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.In fcntl.h
45.Ft int
46.Fn linkat "int fd1" "const char *name1" "int fd2" "const char *name2" "int flags"
47.Sh DESCRIPTION
48The
49.Fn link
50function 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
72.Fa name1
73must exist for the hard link to
74succeed and
75both
76.Fa name1
77and
78.Fa name2
79must be in the same file system.
80.Fa name1
81may not be a directory.
82.Pp
83.Fn linkat
84works the same way as
85.Fn link
86except if
87.Fa name1
88(resp.
89.Fa name2 )
90is relative.
91In that case, it is looked up from a directory whose file
92descriptor was passed as
93.Fa fd1
94(resp.
95.Fa fd2 ) .
96Search permission is required on the directories named by
97.Fa fd1
98and
99.Fa fd2 .
100.\"    (These alternatives await a decision about the semantics of O_SEARCH)
101.\" Search permission is required on the directories named by
102.\" .Fa fd1
103.\" and
104.\" .Fa fd2
105.\" unless they were opened with the
106.\" .Dv O_SEARCH
107.\" flag.
108.\"    - or -
109.\" The directories named by
110.\" .Fa fd1
111.\" and
112.\" .Fa fd2
113.\" must have been opened with the
114.\" .Dv O_SEARCH
115.\" flag.
116.Fa fd1
117or
118.Fa fd2
119can be set to
120.Dv AT_FDCWD
121in order to specify the current directory.
122.Pp
123When operating on a symlink,
124.Fn link
125resolves the symlink and creates a hard link on the target.
126.Fn linkat
127will do the same if
128.Dv AT_SYMLINK_FOLLOW
129is set in
130.Fa flags ,
131but it will link on the symlink itself if the flag is clear.
132.Sh RETURN VALUES
133.Rv -std link linkat
134.Sh ERRORS
135.Fn link
136and
137.Fn linkat
138will fail and no link will be created if:
139.Bl -tag -width Er
140.It Bq Er EACCES
141A component of either path prefix denies search permission, or
142the requested link requires writing in a directory with a mode
143that denies write permission.
144.It Bq Er EDQUOT
145The directory in which the entry for the new link
146is being placed cannot be extended because the
147user's quota of disk blocks on the file system
148containing the directory has been exhausted.
149.It Bq Er EEXIST
150The link named by
151.Fa name2
152does exist.
153.It Bq Er EFAULT
154One of the pathnames specified
155is outside the process's allocated address space.
156.It Bq Er EIO
157An I/O error occurred while reading from or writing to
158the file system to make the directory entry.
159.It Bq Er ELOOP
160Too many symbolic links were encountered in translating one of the pathnames.
161.It Bq Er EMLINK
162The link count of the file named by
163.Fa name1
164would exceed
165.Brq Dv LINK_MAX .
166.It Bq Er ENAMETOOLONG
167A component of a pathname exceeded
168.Brq Dv NAME_MAX
169characters, or an entire path name exceeded
170.Brq Dv PATH_MAX
171characters.
172.It Bq Er ENOENT
173A component of either path prefix does not exist, or the file named
174by
175.Fa name1
176does not exist.
177.It Bq Er ENOSPC
178The directory in which the entry for the new link is being placed
179cannot be extended because there is no space left on the file
180system containing the directory.
181.It Bq Er ENOTDIR
182A component of either path prefix is not a directory.
183.It Bq Er EOPNOTSUPP
184The file system containing the file named by
185.Fa name1
186does not support links.
187.It Bq Er EPERM
188The file named by
189.Fa name1
190is a directory.
191.It Bq Er EROFS
192The requested link requires writing in a directory on a read-only file
193system.
194.It Bq Er EXDEV
195The link named by
196.Fa name2
197and the file named by
198.Fa name1
199are on different file systems.
200.El
201.Pp
202In addition,
203.Fn linkat
204will fail if:
205.Bl -tag -width Er
206.It Bq Er EBADF
207.Fa name1
208or
209.Fa name2
210does not specify an absolute path and
211.Fa fd1
212or
213.Fa fd2 ,
214respectively, is neither
215.Dv AT_FDCWD
216nor a valid file descriptor open for reading or searching.
217.It Bq Er ENOTDIR
218.Fa name1
219or
220.Fa name2
221is not an absolute path and
222.Fa fd1
223or
224.Fa fd2 ,
225respectively, is a file descriptor associated with a non-directory file.
226.El
227.Sh SEE ALSO
228.Xr symlink 2 ,
229.Xr unlink 2
230.Sh STANDARDS
231The
232.Fn link
233function conforms to
234.St -p1003.1-90 .
235.Fn linkat
236conforms to
237.St -p1003.1-2008 .
238.Sh HISTORY
239The
240.Fn link
241function appeared in
242.At v1 .
243