1LINK(3P)                   POSIX Programmer's Manual                  LINK(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       link, linkat — link one file to another file
13

SYNOPSIS

15       #include <unistd.h>
16
17       int link(const char *path1, const char *path2);
18
19       #include <fcntl.h>
20
21       int linkat(int fd1, const char *path1, int fd2,
22           const char *path2, int flag);
23

DESCRIPTION

25       The link() function shall create a new link (directory entry)  for  the
26       existing file, path1.
27
28       The  path1  argument  points to a pathname naming an existing file. The
29       path2 argument points to a pathname naming the new directory  entry  to
30       be  created. The link() function shall atomically create a new link for
31       the existing file and the link count of the file shall  be  incremented
32       by one.
33
34       If  path1  names  a directory, link() shall fail unless the process has
35       appropriate privileges and the implementation supports using link()  on
36       directories.
37
38       If  path1  names  a symbolic link, it is implementation-defined whether
39       link() follows the symbolic link, or creates a new link to the symbolic
40       link itself.
41
42       Upon  successful completion, link() shall mark for update the last file
43       status change timestamp of the file. Also, the last  data  modification
44       and  last  file status change timestamps of the directory that contains
45       the new entry shall be marked for update.
46
47       If link() fails, no link shall be created and the  link  count  of  the
48       file shall remain unchanged.
49
50       The  implementation may require that the calling process has permission
51       to access the existing file.
52
53       The linkat() function shall be equivalent to the link() function except
54       that  symbolic links shall be handled as specified by the value of flag
55       (see below) and except in the case where either path1 or path2 or  both
56       are  relative  paths. In this case a relative path path1 is interpreted
57       relative to the directory  associated  with  the  file  descriptor  fd1
58       instead  of  the  current working directory and similarly for path2 and
59       the file descriptor fd2.  If the access mode of the open file  descrip‐
60       tion  associated with the file descriptor is not O_SEARCH, the function
61       shall check whether directory searches are permitted using the  current
62       permissions  of  the  directory  underlying the file descriptor. If the
63       access mode is O_SEARCH, the function shall not perform the check.
64
65       Values for flag are constructed by a bitwise-inclusive OR of flags from
66       the following list, defined in <fcntl.h>:
67
68       AT_SYMLINK_FOLLOW
69             If  path1 names a symbolic link, a new link for the target of the
70             symbolic link is created.
71
72       If linkat() is passed the special value AT_FDCWD  in  the  fd1  or  fd2
73       parameter,  the current working directory shall be used for the respec‐
74       tive path argument. If both fd1 and fd2 have value AT_FDCWD, the behav‐
75       ior  shall be identical to a call to link(), except that symbolic links
76       shall be handled as specified by the value of flag.
77
78       If the AT_SYMLINK_FOLLOW flag is clear in the  flag  argument  and  the
79       path1  argument  names  a  symbolic link, a new link is created for the
80       symbolic link path1 and not its target.
81

RETURN VALUE

83       Upon successful completion, these functions shall return 0.  Otherwise,
84       these functions shall return -1 and set errno to indicate the error.
85

ERRORS

87       These functions shall fail if:
88
89       EACCES A  component  of either path prefix denies search permission, or
90              the requested link requires writing in a directory  that  denies
91              write  permission,  or the calling process does not have permis‐
92              sion to access the existing file and this  is  required  by  the
93              implementation.
94
95       EEXIST The  path2  argument  resolves to an existing directory entry or
96              refers to a symbolic link.
97
98       ELOOP  A loop exists in symbolic links encountered during resolution of
99              the path1 or path2 argument.
100
101       EMLINK The  number  of  links  to  the file named by path1 would exceed
102              {LINK_MAX}.
103
104       ENAMETOOLONG
105              The  length  of  a  component  of  a  pathname  is  longer  than
106              {NAME_MAX}.
107
108       ENOENT A component of either path prefix does not exist; the file named
109              by path1 does not exist; or path1 or path2 points  to  an  empty
110              string.
111
112       ENOENT or ENOTDIR
113              The path1 argument names an existing non-directory file, and the
114              path2 argument contains at least one non-<slash>  character  and
115              ends  with  one  or  more  trailing <slash> characters. If path2
116              without the trailing <slash> characters would name  an  existing
117              file, an [ENOENT] error shall not occur.
118
119       ENOSPC The directory to contain the link cannot be extended.
120
121       ENOTDIR
122              A component of either path prefix names an existing file that is
123              neither a directory nor a symbolic link to a directory,  or  the
124              path1  argument  contains at least one non-<slash> character and
125              ends with one or more trailing <slash> characters and  the  last
126              pathname  component  names  an  existing  file that is neither a
127              directory nor a symbolic link to a directory, or the path1 argu‐
128              ment names an existing non-directory file and the path2 argument
129              names a nonexistent file,  contains  at  least  one  non-<slash>
130              character,  and  ends  with one or more trailing <slash> charac‐
131              ters.
132
133       EPERM  The file named by path1 is a directory and  either  the  calling
134              process  does not have appropriate privileges or the implementa‐
135              tion prohibits using link() on directories.
136
137       EROFS  The requested link requires writing in a directory  on  a  read-
138              only file system.
139
140       EXDEV  The  link named by path2 and the file named by path1 are on dif‐
141              ferent file systems and  the  implementation  does  not  support
142              links between file systems.
143
144       EXDEV  path1 refers to a named STREAM.
145
146       The linkat() function shall fail if:
147
148       EACCES The access mode of the open file description associated with fd1
149              or fd2 is not O_SEARCH and  the  permissions  of  the  directory
150              underlying  fd1  or  fd2,  respectively, do not permit directory
151              searches.
152
153       EBADF  The path1 or path2 argument does not specify  an  absolute  path
154              and  the  fd1 or fd2 argument, respectively, is neither AT_FDCWD
155              nor a valid file descriptor open for reading or searching.
156
157       ENOTDIR
158              The path1 or path2 argument is not an absolute path and  fd1  or
159              fd2,  respectively,  is a file descriptor associated with a non-
160              directory file.
161
162       These functions may fail if:
163
164       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
165              resolution of the path1 or path2 argument.
166
167       ENAMETOOLONG
168              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
169              tion of a symbolic link produced an intermediate result  with  a
170              length that exceeds {PATH_MAX}.
171
172       The linkat() function may fail if:
173
174       EINVAL The value of the flag argument is not valid.
175
176       The following sections are informative.
177

EXAMPLES

179   Creating a Link to a File
180       The  following  example  shows  how  to  create  a link to a file named
181       /home/cnd/mod1 by creating a new directory entry named /modules/pass1.
182
183
184           #include <unistd.h>
185
186           char *path1 = "/home/cnd/mod1";
187           char *path2 = "/modules/pass1";
188           int   status;
189           ...
190           status = link (path1, path2);
191
192   Creating a Link to a File Within a Program
193       In the  following  program  example,  the  link()  function  links  the
194       /etc/passwd  file  (defined as PASSWDFILE) to a file named /etc/opasswd
195       (defined as SAVEFILE), which is used to save the current password file.
196       Then, after removing the current password file (defined as PASSWDFILE),
197       the new password file is saved as the current password file  using  the
198       link() function again.
199
200
201           #include <unistd.h>
202
203           #define LOCKFILE "/etc/ptmp"
204           #define PASSWDFILE "/etc/passwd"
205           #define SAVEFILE "/etc/opasswd"
206           ...
207           /* Save current password file */
208           link (PASSWDFILE, SAVEFILE);
209
210           /* Remove current password file. */
211           unlink (PASSWDFILE);
212
213           /* Save new password file as current password file. */
214           link (LOCKFILE,PASSWDFILE);
215

APPLICATION USAGE

217       Some implementations do allow links between file systems.
218
219       If  path1  refers to a symbolic link, application developers should use
220       linkat() with appropriate flags to select whether or not  the  symbolic
221       link should be resolved.
222

RATIONALE

224       Linking  to a directory is restricted to the superuser in most histori‐
225       cal implementations because this capability may produce  loops  in  the
226       file  hierarchy  or  otherwise  corrupt the file system. This volume of
227       POSIX.1‐2017  continues  that  philosophy  by  prohibiting  link()  and
228       unlink()  from doing this. Other functions could do it if the implemen‐
229       tor designed such an extension.
230
231       Some historical implementations allow linking  of  files  on  different
232       file  systems.  Wording  was  added  to  explicitly allow this optional
233       behavior.
234
235       The exception for cross-file system links is intended to apply only  to
236       links that are programmatically indistinguishable from ``hard'' links.
237
238       The  purpose  of  the linkat() function is to link files in directories
239       other than the current working directory without exposure to race  con‐
240       ditions. Any part of the path of a file could be changed in parallel to
241       a call to link(), resulting in unspecified behavior. By opening a  file
242       descriptor  for  the directory of both the existing file and the target
243       location and using the linkat() function it can be guaranteed that  the
244       both filenames are in the desired directories.
245
246       The  AT_SYMLINK_FOLLOW  flag allows for implementing both common behav‐
247       iors of the link() function. The POSIX specification requires  that  if
248       path1  is  a  symbolic  link, a new link for the target of the symbolic
249       link is created. Many systems by default or as an alternative provide a
250       mechanism  to  avoid the implicit symbolic link lookup and create a new
251       link for the symbolic link itself.
252
253       Earlier versions of this standard specified only the  link()  function,
254       and  required  it  to  behave  like linkat() with the AT_SYMLINK_FOLLOW
255       flag. However, historical practice from  SVR4  and  Linux  kernels  had
256       link()  behaving  like  linkat()  with  no flags, and many systems that
257       attempted to provide a conforming link() function did so in a way  that
258       was  rarely  used, and when it was used did not conform to the standard
259       (e.g., by not being atomic,  or  by  dereferencing  the  symbolic  link
260       incorrectly).  Since  applications  could  not rely on link() following
261       links in practice, the linkat() function was added  taking  a  flag  to
262       specify the desired behavior for the application.
263

FUTURE DIRECTIONS

265       None.
266

SEE ALSO

268       rename(), symlink(), unlink()
269
270       The Base Definitions volume of POSIX.1‐2017, <fcntl.h>, <unistd.h>
271
273       Portions  of  this text are reprinted and reproduced in electronic form
274       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
275       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
276       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
277       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
278       event of any discrepancy between this version and the original IEEE and
279       The  Open Group Standard, the original IEEE and The Open Group Standard
280       is the referee document. The original Standard can be  obtained  online
281       at http://www.opengroup.org/unix/online.html .
282
283       Any  typographical  or  formatting  errors that appear in this page are
284       most likely to have been introduced during the conversion of the source
285       files  to  man page format. To report such errors, see https://www.ker
286       nel.org/doc/man-pages/reporting_bugs.html .
287
288
289
290IEEE/The Open Group                  2017                             LINK(3P)
Impressum