1.\" $NetBSD: link.2,v 1.7 1995/02/27 12:34:01 cgd Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)link.2 8.3 (Berkeley) 1/12/94 35.\" 36.Dd June 3, 2021 37.Dt LINK 2 38.Os BSD 4 39.Sh NAME 40.Nm link , 41.Nm linkat 42.Nd make a hard file link 43.Sh SYNOPSIS 44.Fd #include <unistd.h> 45.Ft int 46.Fo link 47.Fa "const char *path1" 48.Fa "const char *path2" 49.Fc 50.Ft int 51.Fo linkat 52.Fa "int fd1" "const char *name1" "int fd2" "const char *name2" "int flag" 53.Fc 54.Sh DESCRIPTION 55The 56.Fn link 57function call 58atomically creates the specified directory entry (hard link) 59.Fa path2 60with the attributes of the underlying object pointed at by 61.Fa path1 . 62If the link is successful, 63the link count of the underlying object is incremented; 64.Fa path1 65and 66.Fa path2 67share equal access and rights 68to the 69underlying object. 70.Pp 71If 72.Fa path1 73is removed, the file 74.Fa path2 75is not deleted and the link count of the 76underlying object is 77decremented. 78.Pp 79In order for the system call to succeed, 80.Fa path1 81must exist and both 82.Fa path1 83and 84.Fa path2 85must be in the same file system. 86As mandated by POSIX.1, 87.Fa path1 88may not be a directory. 89.Pp 90.Fn link 91will resolve and follow symbolic links contained within both 92.Fa path1 93and 94.Fa path2 . 95If the last component of 96.Fa path1 97is a symbolic link, 98.Fn link 99will point the hard link, 100.Fa path2 , 101to the underlying object pointed to by 102.Fa path1 , 103not to the symbolic link itself. 104.Pp 105The 106.Fn linkat 107system call is equivalent to 108.Fa link 109except in the case where either 110.Fa name1 111or 112.Fa name2 113or both are relative paths. 114In this case a relative path 115.Fa name1 116is interpreted relative to 117the directory associated with the file descriptor 118.Fa fd1 119instead of the current working directory and similarly for 120.Fa name2 121and the file descriptor 122.Fa fd2 . 123.Pp 124Values for 125.Fa flag 126are constructed by a bitwise-inclusive OR of flags from the following 127list, defined in 128.In fcntl.h : 129.Bl -tag -width indent 130.It Dv AT_SYMLINK_FOLLOW 131If 132.Fa name1 133names a symbolic link, a new link for the target of the symbolic link is 134created. 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. On OS X, not assigning AT_SYMLINK_FOLLOW to 165.Fa flag 166may result in some file systems returning an error. 167.Sh RETURN VALUES 168Upon successful completion, a value of 0 is returned. Otherwise, 169a value of -1 is returned and 170.Va errno 171is set to indicate the error. 172.Sh ERRORS 173.Fn link 174will fail and no link will be created if: 175.Bl -tag -width Er 176.\" ========== 177.It Bq Er EACCES 178A component of either path prefix denies search permission. 179.\" ========== 180.It Bq Er EACCES 181The requested link requires writing in a directory with a mode 182that denies write permission. 183.\" ========== 184.It Bq Er EACCES 185The current process cannot access the existing file. 186.\" ========== 187.It Bq Er EDQUOT 188The directory in which the entry for the new link 189is being placed cannot be extended because the 190user's quota of disk blocks on the file system 191containing the directory has been exhausted. 192.\" ========== 193.It Bq Er EEXIST 194The link named by 195.Fa path2 196already exists. 197.\" ========== 198.It Bq Er EFAULT 199One of the pathnames specified 200is outside the process's allocated address space. 201.\" ========== 202.It Bq Er EIO 203An I/O error occurs while reading from or writing to 204the file system to make the directory entry. 205.\" ========== 206.It Bq Er ELOOP 207Too many symbolic links are encountered in translating one of the pathnames. 208This is taken to be indicative of a looping symbolic link. 209.\" ========== 210.It Bq Er EMLINK 211The file already has {LINK_MAX} links. 212.\" ========== 213.It Bq Er ENAMETOOLONG 214A component of a pathname exceeds 215.Dv {NAME_MAX} 216characters, or an entire path name exceeded 217.Dv {PATH_MAX} 218characters. 219.\" ========== 220.It Bq Er ENOENT 221A component of either path prefix does not exist, or is a dangling symbolic link. 222.\" ========== 223.It Bq Er ENOENT 224The file named by 225.Fa path1 226does not exist, or is a dangling symbolic link. 227.\" ========== 228.It Bq Er ENOSPC 229The directory in which the entry for the new link is being placed 230cannot be extended because there is no space left on the file 231system containing the directory. 232.\" ========== 233.It Bq Er ENOTDIR 234A component of either path prefix is not a directory. 235.\" ========== 236.It Bq Er EPERM 237The file named by 238.Fa path1 239is a directory. 240.\" ========== 241.It Bq Er EROFS 242The requested link requires writing in a directory 243on a read-only file system. 244.\" ========== 245.It Bq Er EXDEV 246The link named by 247.Fa path2 248and the file named by 249.Fa path1 250are on different file systems. 251.\" ========== 252.It Bq Er EDEADLK 253The file named by 254.Fa path1 255is a 256.Dq dataless 257file that must be materialized before being linked and the I/O policy of 258the current thread or process disallows file materialization 259.Po see 260.Xr getiopolicy_np 3 261.Pc . 262.El 263.Pp 264In addition to the errors returned by the 265.Fn link , 266the 267.Fn linkat 268system call may fail if: 269.Bl -tag -width Er 270.It Bq Er EBADF 271The 272.Fa name1 273or 274.Fa name2 275argument does not specify an absolute path and the 276.Fa fd1 277or 278.Fa fd2 279argument, respectively, is neither 280.Dv AT_FDCWD 281nor a valid file descriptor open for searching. 282.It Bq Er EINVAL 283The value of the 284.Fa flag 285argument is not valid. 286.It Bq Er ENOTSUP 287.Fa flag 288was not set to 289.Dv AT_SYMLINK_FOLLOW (some file systems only). 290.It Bq Er ENOTSUP 291The underlying file system does not support this call. 292.It Bq Er ENOTDIR 293The 294.Fa name1 295or 296.Fa name2 297argument is not an absolute path and 298.Fa fd1 299or 300.Fa fd2 , 301respectively, is neither 302.Dv AT_FDCWD 303nor a file descriptor associated with a directory. 304.El 305.Sh SEE ALSO 306.Xr symlink 2 , 307.Xr unlink 2 308.Sh STANDARDS 309The 310.Fn link 311function is expected to conform to 312.St -p1003.1-88 . 313The 314.Fn linkat 315system call is expected to conform to POSIX.1-2008 . 316