1LINK(2)                    Linux Programmer's Manual                   LINK(2)
2
3
4

NAME

6       link, linkat - make a new name for a file
7

SYNOPSIS

9       #include <unistd.h>
10
11       int link(const char *oldpath, const char *newpath);
12
13       #include <fcntl.h>           /* Definition of AT_* constants */
14       #include <unistd.h>
15
16       int linkat(int olddirfd, const char *oldpath,
17                  int newdirfd, const char *newpath, int flags);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       linkat():
22           Since glibc 2.10:
23               _POSIX_C_SOURCE >= 200809L
24           Before glibc 2.10:
25               _ATFILE_SOURCE
26

DESCRIPTION

28       link()  creates  a  new link (also known as a hard link) to an existing
29       file.
30
31       If newpath exists, it will not be overwritten.
32
33       This new name may be used exactly as the old  one  for  any  operation;
34       both names refer to the same file (and so have the same permissions and
35       ownership) and it is impossible to tell which name was the "original".
36
37   linkat()
38       The linkat() system call operates in exactly the same  way  as  link(),
39       except for the differences described here.
40
41       If  the  pathname  given in oldpath is relative, then it is interpreted
42       relative to the directory referred to by the file  descriptor  olddirfd
43       (rather  than  relative to the current working directory of the calling
44       process, as is done by link() for a relative pathname).
45
46       If oldpath is relative and olddirfd is the special value AT_FDCWD, then
47       oldpath is interpreted relative to the current working directory of the
48       calling process (like link()).
49
50       If oldpath is absolute, then olddirfd is ignored.
51
52       The interpretation of newpath is as for oldpath, except that a relative
53       pathname  is  interpreted  relative to the directory referred to by the
54       file descriptor newdirfd.
55
56       The following values can be bitwise ORed in flags:
57
58       AT_EMPTY_PATH (since Linux 2.6.39)
59              If oldpath is an empty string, create a link to the file  refer‐
60              enced  by  olddirfd  (which  may  have  been  obtained using the
61              open(2) O_PATH flag).  In this case, olddirfd can refer  to  any
62              type  of  file except a directory.  This will generally not work
63              if the file has a link count of zero (files created with  O_TMP‐
64              FILE and without O_EXCL are an exception).  The caller must have
65              the CAP_DAC_READ_SEARCH capability in order to  use  this  flag.
66              This  flag  is  Linux-specific; define _GNU_SOURCE to obtain its
67              definition.
68
69       AT_SYMLINK_FOLLOW (since Linux 2.6.18)
70              By default, linkat(), does not dereference oldpath if  it  is  a
71              symbolic  link (like link()).  The flag AT_SYMLINK_FOLLOW can be
72              specified in flags to cause oldpath to be dereferenced if it  is
73              a  symbolic  link.  If procfs is mounted, this can be used as an
74              alternative to AT_EMPTY_PATH, like this:
75
76                  linkat(AT_FDCWD, "/proc/self/fd/<fd>", newdirfd,
77                         newname, AT_SYMLINK_FOLLOW);
78
79       Before kernel 2.6.18, the flags argument was  unused,  and  had  to  be
80       specified as 0.
81
82       See openat(2) for an explanation of the need for linkat().
83

RETURN VALUE

85       On  success,  zero is returned.  On error, -1 is returned, and errno is
86       set appropriately.
87

ERRORS

89       EACCES Write access to the directory containing newpath is  denied,  or
90              search  permission  is  denied for one of the directories in the
91              path prefix of  oldpath  or  newpath.   (See  also  path_resolu‐
92              tion(7).)
93
94       EDQUOT The  user's  quota  of  disk  blocks  on the filesystem has been
95              exhausted.
96
97       EEXIST newpath already exists.
98
99       EFAULT oldpath or newpath points outside your accessible address space.
100
101       EIO    An I/O error occurred.
102
103       ELOOP  Too many symbolic links were encountered in resolving oldpath or
104              newpath.
105
106       EMLINK The  file  referred to by oldpath already has the maximum number
107              of links to it.  For example, on an ext4(5) filesystem that does
108              not  employ  the  dir_index  feature, the limit on the number of
109              hard links to a file is 65,000; on btrfs(5), the limit is 65,535
110              links.
111
112       ENAMETOOLONG
113              oldpath or newpath was too long.
114
115       ENOENT A directory component in oldpath or newpath does not exist or is
116              a dangling symbolic link.
117
118       ENOMEM Insufficient kernel memory was available.
119
120       ENOSPC The device containing the file has no room for the new directory
121              entry.
122
123       ENOTDIR
124              A component used as a directory in oldpath or newpath is not, in
125              fact, a directory.
126
127       EPERM  oldpath is a directory.
128
129       EPERM  The filesystem containing oldpath and newpath does  not  support
130              the creation of hard links.
131
132       EPERM (since Linux 3.6)
133              The  caller  does  not  have permission to create a hard link to
134              this   file   (see   the   description   of    /proc/sys/fs/pro‐
135              tected_hardlinks in proc(5)).
136
137       EPERM  oldpath    is    marked    immutable   or   append-only.    (See
138              ioctl_iflags(2).)
139
140       EROFS  The file is on a read-only filesystem.
141
142       EXDEV  oldpath and newpath are not  on  the  same  mounted  filesystem.
143              (Linux  permits  a  filesystem to be mounted at multiple points,
144              but link() does not work across different mount points, even  if
145              the same filesystem is mounted on both.)
146
147       The following additional errors can occur for linkat():
148
149       EBADF  olddirfd or newdirfd is not a valid file descriptor.
150
151       EINVAL An invalid flag value was specified in flags.
152
153       ENOENT AT_EMPTY_PATH  was  specified  in  flags, but the caller did not
154              have the CAP_DAC_READ_SEARCH capability.
155
156       ENOENT An attempt was made to link to the /proc/self/fd/NN file  corre‐
157              sponding to a file descriptor created with
158
159                  open(path, O_TMPFILE | O_EXCL, mode);
160
161              See open(2).
162
163       ENOENT oldpath  is  a relative pathname and olddirfd refers to a direc‐
164              tory that has been deleted, or newpath is  a  relative  pathname
165              and newdirfd refers to a directory that has been deleted.
166
167       ENOTDIR
168              oldpath  is relative and olddirfd is a file descriptor referring
169              to a file other than a directory; or  similar  for  newpath  and
170              newdirfd
171
172       EPERM  AT_EMPTY_PATH  was  specified  in  flags,  oldpath  is  an empty
173              string, and olddirfd refers to a directory.
174

VERSIONS

176       linkat() was added to Linux in kernel 2.6.16; library support was added
177       to glibc in version 2.4.
178

CONFORMING TO

180       link(): SVr4, 4.3BSD, POSIX.1-2001 (but see NOTES), POSIX.1-2008.
181
182       linkat(): POSIX.1-2008.
183

NOTES

185       Hard  links,  as  created by link(), cannot span filesystems.  Use sym‐
186       link(2) if this is required.
187
188       POSIX.1-2001 says that link() should dereference oldpath  if  it  is  a
189       symbolic  link.   However,  since  kernel 2.0, Linux does not do so: if
190       oldpath is a symbolic link, then newpath is created as a (hard) link to
191       the  same  symbolic link file (i.e., newpath becomes a symbolic link to
192       the same file that oldpath  refers  to).   Some  other  implementations
193       behave  in the same manner as Linux.  POSIX.1-2008 changes the specifi‐
194       cation of link(), making it  implementation-dependent  whether  or  not
195       oldpath  is dereferenced if it is a symbolic link.  For precise control
196       over the  treatment  of  symbolic  links  when  creating  a  link,  use
197       linkat().
198
199   Glibc notes
200       On older kernels where linkat() is unavailable, the glibc wrapper func‐
201       tion falls back to the use of link(), unless the  AT_SYMLINK_FOLLOW  is
202       specified.  When oldpath and newpath are relative pathnames, glibc con‐
203       structs pathnames based on the symbolic  links  in  /proc/self/fd  that
204       correspond to the olddirfd and newdirfd arguments.
205

BUGS

207       On NFS filesystems, the return code may be wrong in case the NFS server
208       performs the link creation and dies before it can say so.  Use  stat(2)
209       to find out if the link got created.
210

SEE ALSO

212       ln(1), open(2), rename(2), stat(2), symlink(2), unlink(2), path_resolu‐
213       tion(7), symlink(7)
214

COLOPHON

216       This page is part of release 4.15 of the Linux  man-pages  project.   A
217       description  of  the project, information about reporting bugs, and the
218       latest    version    of    this    page,    can     be     found     at
219       https://www.kernel.org/doc/man-pages/.
220
221
222
223Linux                             2017-09-15                           LINK(2)
Impressum