link(2) System Calls Manual link(2) link, linkat - LIBRARY Standard C library (libc, -lc) #include int link(const char *oldpath, const char *newpath); #include /* AT_* */ #include int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags); glibc (. feature_test_macros(7)): linkat(): glibc 2.10: _POSIX_C_SOURCE >= 200809L glibc 2.10: _ATFILE_SOURCE link() ( ) . newpath , . ; ( ) "". linkat() linkat() link(), , . oldpath , , olddirfd ( , link()). oldpath olddirfd AT_FDCWD, oldpath ( link()). oldpath , olddirfd . newpath oldpath, , , newdirfd. (OR) flags: AT_EMPTY_PATH ( Linux 2.6.39) oldpath , , olddirfd ( open(2) O_PATH). olddirfd . , , , ( -- , O_TMPFILE O_EXCL). CAP_DAC_READ_SEARCH, . Linux; _GNU_SOURCE. AT_SYMLINK_FOLLOW ( Linux 2.6.18) , linkat() oldpath, ( link()). flags AT_SYMLINK_FOLLOW, oldpath , . procfs, AT_EMPTY_PATH, : linkat(AT_FDCWD, "/proc/self/fd/", newdirfd, newname, AT_SYMLINK_FOLLOW); Before Linux 2.6.18, the flags argument was unused, and had to be specified as 0. openat(2) linkat(). On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error. EACCES , newpath, oldpath newpath (. path_resolution(7)). EDQUOT . EEXIST newpath . EFAULT oldpath newpath . EIO -. ELOOP oldpath newpath . EMLINK , oldpath, . , ext4(5), dir_index, 65000; btrfs(5) -- 65535 . ENAMETOOLONG oldpath newpath. ENOENT oldpath newpath . ENOMEM . ENOSPC , , . ENOTDIR , oldpath newpath, . EPERM oldpath . EPERM , oldpath newpath, . EPERM ( Linux 3.6) ( /proc/sys/fs/protected_hardlinks proc(5)). EPERM oldpath (immutable) ( ioctl_iflags(2)). EROFS , . EXDEV oldpath newpath (Linux , link() , ). linkat() : EBADF oldpath (newpath) is relative but olddirfd (newdirfd) is neither AT_FDCWD nor a valid file descriptor. EINVAL flags . ENOENT AT_EMPTY_PATH flags, CAP_DAC_READ_SEARCH. ENOENT /proc/self/fd/NN, , open(path, O_TMPFILE | O_EXCL, mode); open(2). ENOENT An attempt was made to link to a /proc/self/fd/NN file corresponding to a file that has been deleted. ENOENT oldpath olddirfd , , newpath newdirfd , . ENOTDIR oldpath olddirfd , , ; newpath newdirfd. EPERM AT_EMPTY_PATH flags, oldpath olddirfd . POSIX.1-2001 says that link() should dereference oldpath if it is a symbolic link. However, since Linux 2.0, Linux does not do so: if oldpath is a symbolic link, then newpath is created as a (hard) link to the same symbolic link file (i.e., newpath becomes a symbolic link to the same file that oldpath refers to). Some other implementations behave in the same manner as Linux. POSIX.1-2008 changes the specification of link(), making it implementation-dependent whether or not oldpath is dereferenced if it is a symbolic link. For precise control over the treatment of symbolic links when creating a link, use linkat(). glibc , linkat() ( AT_SYMLINK_NOFOLLOW), glibc link(). oldpath newpath , glibc /proc/self/fd, olddirfd newdirfd. link() POSIX.1-2008. link() SVr4, 4.3BSD, POSIX.1-2001 (but see VERSIONS). linkat() POSIX.1-2008. Linux 2.6.16, glibc 2.4. , link(), . , symlink(2). NFS , NFS . stat(2), . . ln(1), open(2), rename(2), stat(2), symlink(2), unlink(2), path_resolution(7), symlink(7) Artyom Kunyov , Azamat Hackimov , Dmitry Bolkhovskikh , Katrin Kutepova , Konstantin Shvaykovskiy , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . link(2)