#include <unistd.h> int link(const char *path1, const char *path2); #include <fcntl.h> int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
The path1 argument points to a pathname naming an existing file. The path2 argument points to a pathname naming the new directory entry to be created. The link() function shall atomically create a new link for the existing file and the link count of the file shall be incremented by one.
If path1 names a directory, link() shall fail unless the process has appropriate privileges and the implementation supports using link() on directories.
If path1 names a symbolic link, it is implementation-defined whether link() follows the symbolic link, or creates a new link to the symbolic link itself.
Upon successful completion, link() shall mark for update the last file status change timestamp of the file. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.
If link() fails, no link shall be created and the link count of the file shall remain unchanged.
The implementation may require that the calling process has permission to access the existing file.
The linkat() function shall be equivalent to the link() function except that symbolic links shall be handled as specified by the value of flag (see below) and except in the case where either path1 or path2 or both are relative paths. In this case a relative path path1 is interpreted relative to the directory associated with the file descriptor fd1 instead of the current working directory and similarly for path2 and the file descriptor fd2. If the access mode of the open file description associated with the file descriptor is not O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the access mode is O_SEARCH, the function shall not perform the check.
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
If linkat() is passed the special value AT_FDCWD in the fd1 or fd2 parameter, the current working directory shall be used for the respective path argument. If both fd1 and fd2 have value AT_FDCWD, the behavior shall be identical to a call to link(), except that symbolic links shall be handled as specified by the value of flag.
If the AT_SYMLINK_FOLLOW flag is clear in the flag argument and the path1 argument names a symbolic link, a new link is created for the symbolic link path1 and not its target.
The linkat() function shall fail if:
These functions may fail if:
The linkat() function may fail if:
The following sections are informative.
The following example shows how to create a link to a file named /home/cnd/mod1 by creating a new directory entry named /modules/pass1.
#include <unistd.h> char *path1 = "/home/cnd/mod1"; char *path2 = "/modules/pass1"; int status; ... status = link (path1, path2);
In the following program example, the link() function links the /etc/passwd file (defined as PASSWDFILE) to a file named /etc/opasswd (defined as SAVEFILE), which is used to save the current password file. Then, after removing the current password file (defined as PASSWDFILE), the new password file is saved as the current password file using the link() function again.
#include <unistd.h> #define LOCKFILE "/etc/ptmp" #define PASSWDFILE "/etc/passwd" #define SAVEFILE "/etc/opasswd" ... /* Save current password file */ link (PASSWDFILE, SAVEFILE); /* Remove current password file. */ unlink (PASSWDFILE); /* Save new password file as current password file. */ link (LOCKFILE,PASSWDFILE);
If path1 refers to a symbolic link, application developers should use linkat() with appropriate flags to select whether or not the symbolic link should be resolved.
Some historical implementations allow linking of files on different file systems. Wording was added to explicitly allow this optional behavior.
The exception for cross-file system links is intended to apply only to links that are programmatically indistinguishable from ``hard'' links.
The purpose of the linkat() function is to link files in directories other than the current working directory without exposure to race conditions. Any part of the path of a file could be changed in parallel to a call to link(), resulting in unspecified behavior. By opening a file descriptor for the directory of both the existing file and the target location and using the linkat() function it can be guaranteed that the both filenames are in the desired directories.
The AT_SYMLINK_FOLLOW flag allows for implementing both common behaviors of the link() function. The POSIX specification requires that if path1 is a symbolic link, a new link for the target of the symbolic link is created. Many systems by default or as an alternative provide a mechanism to avoid the implicit symbolic link lookup and create a new link for the symbolic link itself.
Earlier versions of this standard specified only the link() function, and required it to behave like linkat() with the AT_SYMLINK_FOLLOW flag. However, historical practice from SVR4 and Linux kernels had link() behaving like linkat() with no flags, and many systems that attempted to provide a conforming link() function did so in a way that was rarely used, and when it was used did not conform to the standard (e.g., by not being atomic, or by dereferencing the symbolic link incorrectly). Since applications could not rely on link() following links in practice, the linkat() function was added taking a flag to specify the desired behavior for the application.
The Base Definitions volume of POSIX.1-2017, <fcntl.h>, <unistd.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .