Blame man-pages-posix-2013-a/man3p/link.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH LINK "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
link, linkat
Packit 7cfc04
\(em link one file to another file relative to two directory
Packit 7cfc04
file descriptors
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
.P
Packit 7cfc04
int link(const char *\fIpath1\fP, const char *\fIpath2\fP);
Packit 7cfc04
int linkat(int \fIfd1\fP, const char *\fIpath1\fP, int \fIfd2\fP,
Packit 7cfc04
    const char *\fIpath2\fP, int \fIflag\fP);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function shall create a new link (directory entry) for the existing file,
Packit 7cfc04
.IR path1 .
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
.IR path1
Packit 7cfc04
argument points to a pathname naming an existing file. The
Packit 7cfc04
.IR path2
Packit 7cfc04
argument points to a pathname naming the new directory entry to be
Packit 7cfc04
created. The
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function shall atomically create a new link for the existing file and
Packit 7cfc04
the link count of the file shall be incremented by one.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR path1
Packit 7cfc04
names a directory,
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
shall fail unless the process has appropriate privileges and the
Packit 7cfc04
implementation supports using
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
on directories.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR path1
Packit 7cfc04
names a symbolic link, it is implementation-defined whether
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
follows the symbolic link, or creates a new link to the symbolic
Packit 7cfc04
link itself.
Packit 7cfc04
.P
Packit 7cfc04
Upon successful completion,
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
shall mark for update the last file status change timestamp of the
Packit 7cfc04
file. Also, the last data modification and last file status change
Packit 7cfc04
timestamps of the directory that contains the new entry shall be marked
Packit 7cfc04
for update.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
fails, no link shall be created and the link count of the file shall
Packit 7cfc04
remain unchanged.
Packit 7cfc04
.P
Packit 7cfc04
The implementation may require that the calling process has permission
Packit 7cfc04
to access the existing file.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function shall be equivalent to the
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function except that symbolic links shall be handled as specified by
Packit 7cfc04
the value of
Packit 7cfc04
.IR flag
Packit 7cfc04
(see below) and except in the case where either
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
or both are relative paths. In this case a relative path
Packit 7cfc04
.IR path1
Packit 7cfc04
is interpreted relative to the directory associated with the file
Packit 7cfc04
descriptor
Packit 7cfc04
.IR fd1
Packit 7cfc04
instead of the current working directory and similarly for
Packit 7cfc04
.IR path2
Packit 7cfc04
and the file descriptor
Packit 7cfc04
.IR fd2 .
Packit 7cfc04
If the file descriptor was opened without O_SEARCH, the function
Packit 7cfc04
shall check whether directory searches are permitted using the current
Packit 7cfc04
permissions of the directory underlying the file descriptor. If the
Packit 7cfc04
file descriptor was opened with O_SEARCH, the function shall not perform
Packit 7cfc04
the check.
Packit 7cfc04
.P
Packit 7cfc04
Values for
Packit 7cfc04
.IR flag
Packit 7cfc04
are constructed by a bitwise-inclusive OR of flags from the following
Packit 7cfc04
list, defined in
Packit 7cfc04
.IR <fcntl.h> :
Packit 7cfc04
.IP AT_SYMLINK_FOLLOW 6
Packit 7cfc04
.br
Packit 7cfc04
If
Packit 7cfc04
.IR path1
Packit 7cfc04
names a symbolic link, a new link for the target of the symbolic link
Packit 7cfc04
is created.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
is passed the special value AT_FDCWD in the
Packit 7cfc04
.IR fd1
Packit 7cfc04
or
Packit 7cfc04
.IR fd2
Packit 7cfc04
parameter, the current working directory shall be used for the respective
Packit 7cfc04
.IR path
Packit 7cfc04
argument. If both
Packit 7cfc04
.IR fd1
Packit 7cfc04
and
Packit 7cfc04
.IR fd2
Packit 7cfc04
have value AT_FDCWD, the behavior shall be identical to a call to
Packit 7cfc04
\fIlink\fR(),
Packit 7cfc04
except that symbolic links shall be handled as specified by the value of
Packit 7cfc04
.IR flag .
Packit 7cfc04
.P
Packit 7cfc04
If the AT_SYMLINK_FOLLOW flag is clear in the
Packit 7cfc04
.IR flag
Packit 7cfc04
argument and the
Packit 7cfc04
.IR path1
Packit 7cfc04
argument names a symbolic link, a new link is created for the symbolic
Packit 7cfc04
link
Packit 7cfc04
.IR path1
Packit 7cfc04
and not its target.
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
Upon successful completion, these functions shall return 0. Otherwise,
Packit 7cfc04
these functions shall return \(mi1 and set
Packit 7cfc04
.IR errno
Packit 7cfc04
to indicate the error.
Packit 7cfc04
.br
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
These functions shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EACCES
Packit 7cfc04
A component of either path prefix denies search permission, or the
Packit 7cfc04
requested link requires writing in a directory that denies write
Packit 7cfc04
permission, or the calling process does not have permission to access
Packit 7cfc04
the existing file and this is required by the implementation.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EEXIST
Packit 7cfc04
The
Packit 7cfc04
.IR path2
Packit 7cfc04
argument resolves to an existing directory entry or refers to a symbolic
Packit 7cfc04
link.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ELOOP
Packit 7cfc04
A loop exists in symbolic links encountered during resolution of the
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EMLINK
Packit 7cfc04
The number of links to the file named by
Packit 7cfc04
.IR path1
Packit 7cfc04
would exceed
Packit 7cfc04
{LINK_MAX}.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENAMETOOLONG
Packit 7cfc04
.br
Packit 7cfc04
The length of a component of a pathname is longer than
Packit 7cfc04
{NAME_MAX}.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOENT
Packit 7cfc04
A component of either path prefix does not exist; the file named by
Packit 7cfc04
.IR path1
Packit 7cfc04
does not exist; or
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
points to an empty string.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOSPC
Packit 7cfc04
The directory to contain the link cannot be extended.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOTDIR
Packit 7cfc04
A component of either path prefix names an existing file that is neither
Packit 7cfc04
a directory nor a symbolic link to a directory, or the
Packit 7cfc04
.IR path1
Packit 7cfc04
argument contains at least one non-\c
Packit 7cfc04
<slash>
Packit 7cfc04
character and ends with one or more trailing
Packit 7cfc04
<slash>
Packit 7cfc04
characters and the last pathname component names an existing file
Packit 7cfc04
that is neither a directory nor a symbolic link to a directory, or the
Packit 7cfc04
.IR path1
Packit 7cfc04
argument names an existing non-directory file and the
Packit 7cfc04
.IR path2
Packit 7cfc04
argument names a nonexistent file, contains at least one non-\c
Packit 7cfc04
<slash>
Packit 7cfc04
character, and ends with one or more trailing
Packit 7cfc04
<slash>
Packit 7cfc04
characters.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EPERM
Packit 7cfc04
The file named by
Packit 7cfc04
.IR path1
Packit 7cfc04
is a directory and either the calling process does not have appropriate
Packit 7cfc04
privileges or the implementation prohibits using
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
on directories.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EROFS
Packit 7cfc04
The requested link requires writing in a directory on a read-only file
Packit 7cfc04
system.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EXDEV
Packit 7cfc04
The link named by
Packit 7cfc04
.IR path2
Packit 7cfc04
and the file named by
Packit 7cfc04
.IR path1
Packit 7cfc04
are on different file systems and the implementation does not support
Packit 7cfc04
links between file systems.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EXDEV
Packit 7cfc04
.IR path1
Packit 7cfc04
refers to a named STREAM.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EBADF
Packit 7cfc04
The
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
argument does not specify an absolute path and the
Packit 7cfc04
.IR fd1
Packit 7cfc04
or
Packit 7cfc04
.IR fd2
Packit 7cfc04
argument, respectively, is neither AT_FDCWD nor a valid file descriptor
Packit 7cfc04
open for reading or searching.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOTDIR
Packit 7cfc04
The
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
argument is not an absolute path and
Packit 7cfc04
.IR fd1
Packit 7cfc04
or
Packit 7cfc04
.IR fd2 ,
Packit 7cfc04
respectively, is a file descriptor associated with a non-directory file.
Packit 7cfc04
.P
Packit 7cfc04
These functions may fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR ELOOP
Packit 7cfc04
More than
Packit 7cfc04
{SYMLOOP_MAX}
Packit 7cfc04
symbolic links were encountered during resolution of the
Packit 7cfc04
.IR path1
Packit 7cfc04
or
Packit 7cfc04
.IR path2
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENAMETOOLONG
Packit 7cfc04
.br
Packit 7cfc04
The length of a pathname exceeds
Packit 7cfc04
{PATH_MAX},
Packit 7cfc04
or pathname resolution of a symbolic link produced an intermediate
Packit 7cfc04
result with a length that exceeds
Packit 7cfc04
{PATH_MAX}.
Packit 7cfc04
.br
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function may fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINVAL
Packit 7cfc04
The value of the
Packit 7cfc04
.IR flag
Packit 7cfc04
argument is not valid.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
.SS "Creating a Link to a File"
Packit 7cfc04
.P
Packit 7cfc04
The following example shows how to create a link to a file named
Packit 7cfc04
.BR /home/cnd/mod1
Packit 7cfc04
by creating a new directory entry named
Packit 7cfc04
.BR /modules/pass1 .
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
.P
Packit 7cfc04
char *path1 = "/home/cnd/mod1";
Packit 7cfc04
char *path2 = "/modules/pass1";
Packit 7cfc04
int   status;
Packit 7cfc04
\&...
Packit 7cfc04
status = link (path1, path2);
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SS "Creating a Link to a File Within a Program"
Packit 7cfc04
.P
Packit 7cfc04
In the following program example, the
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function links the
Packit 7cfc04
.BR /etc/passwd
Packit 7cfc04
file (defined as
Packit 7cfc04
.BR PASSWDFILE )
Packit 7cfc04
to a file named
Packit 7cfc04
.BR /etc/opasswd
Packit 7cfc04
(defined as
Packit 7cfc04
.BR SAVEFILE ),
Packit 7cfc04
which is used to save the current password file. Then, after removing
Packit 7cfc04
the current password file (defined as
Packit 7cfc04
.BR PASSWDFILE ),
Packit 7cfc04
the new password file is saved as the current password file using the
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function again.
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
.P
Packit 7cfc04
#define LOCKFILE "/etc/ptmp"
Packit 7cfc04
#define PASSWDFILE "/etc/passwd"
Packit 7cfc04
#define SAVEFILE "/etc/opasswd"
Packit 7cfc04
\&...
Packit 7cfc04
/* Save current password file */
Packit 7cfc04
link (PASSWDFILE, SAVEFILE);
Packit 7cfc04
.P
Packit 7cfc04
/* Remove current password file. */
Packit 7cfc04
unlink (PASSWDFILE);
Packit 7cfc04
.P
Packit 7cfc04
/* Save new password file as current password file. */
Packit 7cfc04
link (LOCKFILE,PASSWDFILE);
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
Some implementations do allow links between file systems.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR path1
Packit 7cfc04
refers to a symbolic link, application developers should use
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
with appropriate flags to select whether or not the symbolic link should
Packit 7cfc04
be resolved.
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
Linking to a directory is restricted to the superuser
Packit 7cfc04
in most historical implementations because this capability may produce
Packit 7cfc04
loops in the file hierarchy or otherwise corrupt the file system. This volume of POSIX.1\(hy2008
Packit 7cfc04
continues that philosophy by prohibiting
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
and
Packit 7cfc04
\fIunlink\fR()
Packit 7cfc04
from doing this. Other functions could do it if the implementor designed
Packit 7cfc04
such an extension.
Packit 7cfc04
.P
Packit 7cfc04
Some historical implementations allow linking of files on different file
Packit 7cfc04
systems. Wording was added to explicitly allow this optional behavior.
Packit 7cfc04
.P
Packit 7cfc04
The exception for cross-file system links is intended to apply only to
Packit 7cfc04
links that are programmatically indistinguishable from ``hard'' links.
Packit 7cfc04
.P
Packit 7cfc04
The purpose of the
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function is to link files in directories other than the current working
Packit 7cfc04
directory without exposure to race conditions. Any part of the path of
Packit 7cfc04
a file could be changed in parallel to a call to
Packit 7cfc04
\fIlink\fR(),
Packit 7cfc04
resulting in unspecified behavior. By opening a file descriptor for the
Packit 7cfc04
directory of both the existing file and the target location and using the
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function it can be guaranteed that the both filenames are in the desired
Packit 7cfc04
directories.
Packit 7cfc04
.P
Packit 7cfc04
The AT_SYMLINK_FOLLOW flag allows for implementing both common behaviors
Packit 7cfc04
of the
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function. The POSIX specification requires that if
Packit 7cfc04
.IR path1
Packit 7cfc04
is a symbolic link, a new link for the target of the symbolic link is
Packit 7cfc04
created. Many systems by default or as an alternative provide a mechanism
Packit 7cfc04
to avoid the implicit symbolic link lookup and create a new link for
Packit 7cfc04
the symbolic link itself.
Packit 7cfc04
.P
Packit 7cfc04
Earlier versions of this standard specified only the
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function, and required it to behave like
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
with the AT_SYMLINK_FOLLOW flag. However, historical practice from SVR4
Packit 7cfc04
and Linux kernels had
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
behaving like
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
with no flags, and many systems that attempted to provide a conforming
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
function did so in a way that was rarely used, and when it was used
Packit 7cfc04
did not conform to the standard (e.g., by not being atomic, or by
Packit 7cfc04
dereferencing the symbolic link incorrectly). Since applications could
Packit 7cfc04
not rely on
Packit 7cfc04
\fIlink\fR()
Packit 7cfc04
following links in practice, the
Packit 7cfc04
\fIlinkat\fR()
Packit 7cfc04
function was added taking a flag to specify the desired behavior
Packit 7cfc04
for the application.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIrename\fR\^(\|)",
Packit 7cfc04
.IR "\fIsymlink\fR\^(\|)",
Packit 7cfc04
.IR "\fIunlink\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fB<fcntl.h>\fP",
Packit 7cfc04
.IR "\fB<unistd.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .