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