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