LINK(3)                    POSIX Programmer's Manual                   LINK(3)



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

NNAAMMEE
       link, linkat -- link one file to another file

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int link(const char *_p_a_t_h_1, const char *_p_a_t_h_2);

       #include <fcntl.h>

       int linkat(int _f_d_1, const char *_p_a_t_h_1, int _f_d_2,
           const char *_p_a_t_h_2, int _f_l_a_g);

DDEESSCCRRIIPPTTIIOONN
       The _l_i_n_k() function shall create a new link (directory entry) for the
       existing file, _p_a_t_h_1.

       The _p_a_t_h_1 argument points to a pathname naming an existing file. The
       _p_a_t_h_2 argument points to a pathname naming the new directory entry to
       be created. The _l_i_n_k() function shall atomically create a new link for
       the existing file and the link count of the file shall be incremented
       by one.

       If _p_a_t_h_1 names a directory, _l_i_n_k() shall fail unless the process has
       appropriate privileges and the implementation supports using _l_i_n_k() on
       directories.

       If _p_a_t_h_1 names a symbolic link, it is implementation-defined whether
       _l_i_n_k() follows the symbolic link, or creates a new link to the symbolic
       link itself.

       Upon successful completion, _l_i_n_k() 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 _l_i_n_k() 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 _l_i_n_k_a_t() function shall be equivalent to the _l_i_n_k() function except
       that symbolic links shall be handled as specified by the value of _f_l_a_g
       (see below) and except in the case where either _p_a_t_h_1 or _p_a_t_h_2 or both
       are relative paths. In this case a relative path _p_a_t_h_1 is interpreted
       relative to the directory associated with the file descriptor _f_d_1
       instead of the current working directory and similarly for _p_a_t_h_2 and
       the file descriptor _f_d_2.  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 _f_l_a_g are constructed by a bitwise-inclusive OR of flags from
       the following list, defined in _<_f_c_n_t_l_._h_>:

       AT_SYMLINK_FOLLOW
             If _p_a_t_h_1 names a symbolic link, a new link for the target of the
             symbolic link is created.

       If _l_i_n_k_a_t() is passed the special value AT_FDCWD in the _f_d_1 or _f_d_2
       parameter, the current working directory shall be used for the
       respective _p_a_t_h argument. If both _f_d_1 and _f_d_2 have value AT_FDCWD, the
       behavior shall be identical to a call to _l_i_n_k(), except that symbolic
       links shall be handled as specified by the value of _f_l_a_g.

       If the AT_SYMLINK_FOLLOW flag is clear in the _f_l_a_g argument and the
       _p_a_t_h_1 argument names a symbolic link, a new link is created for the
       symbolic link _p_a_t_h_1 and not its target.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, these functions shall return 0. Otherwise,
       these functions shall return -1 and set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       These functions shall fail if:

       EEAACCCCEESS A component of either path prefix denies search permission, or
              the requested link requires writing in a directory that denies
              write permission, or the calling process does not have
              permission to access the existing file and this is required by
              the implementation.

       EEEEXXIISSTT The _p_a_t_h_2 argument resolves to an existing directory entry or
              refers to a symbolic link.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _p_a_t_h_1 or _p_a_t_h_2 argument.

       EEMMLLIINNKK The number of links to the file named by _p_a_t_h_1 would exceed
              {LINK_MAX}.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of either path prefix does not exist; the file named
              by _p_a_t_h_1 does not exist; or _p_a_t_h_1 or _p_a_t_h_2 points to an empty
              string.

       EENNOOEENNTT or EENNOOTTDDIIRR
              The _p_a_t_h_1 argument names an existing non-directory file, and the
              _p_a_t_h_2 argument contains at least one non-<slash> character and
              ends with one or more trailing <slash> characters. If _p_a_t_h_2
              without the trailing <slash> characters would name an existing
              file, an [[EENNOOEENNTT]] error shall not occur.

       EENNOOSSPPCC The directory to contain the link cannot be extended.

       EENNOOTTDDIIRR
              A component of either path prefix names an existing file that is
              neither a directory nor a symbolic link to a directory, or the
              _p_a_t_h_1 argument contains at least one non-<slash> character and
              ends with one or more trailing <slash> characters and the last
              pathname component names an existing file that is neither a
              directory nor a symbolic link to a directory, or the _p_a_t_h_1
              argument names an existing non-directory file and the _p_a_t_h_2
              argument names a nonexistent file, contains at least one
              non-<slash> character, and ends with one or more trailing
              <slash> characters.

       EEPPEERRMM  The file named by _p_a_t_h_1 is a directory and either the calling
              process does not have appropriate privileges or the
              implementation prohibits using _l_i_n_k() on directories.

       EERROOFFSS  The requested link requires writing in a directory on a read-
              only file system.

       EEXXDDEEVV  The link named by _p_a_t_h_2 and the file named by _p_a_t_h_1 are on
              different file systems and the implementation does not support
              links between file systems.

       EEXXDDEEVV  _p_a_t_h_1 refers to a named STREAM.

       The _l_i_n_k_a_t() function shall fail if:

       EEAACCCCEESS The access mode of the open file description associated with _f_d_1
              or _f_d_2 is not O_SEARCH and the permissions of the directory
              underlying _f_d_1 or _f_d_2, respectively, do not permit directory
              searches.

       EEBBAADDFF  The _p_a_t_h_1 or _p_a_t_h_2 argument does not specify an absolute path
              and the _f_d_1 or _f_d_2 argument, respectively, is neither AT_FDCWD
              nor a valid file descriptor open for reading or searching.

       EENNOOTTDDIIRR
              The _p_a_t_h_1 or _p_a_t_h_2 argument is not an absolute path and _f_d_1 or
              _f_d_2, respectively, is a file descriptor associated with a non-
              directory file.

       These functions may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _p_a_t_h_1 or _p_a_t_h_2 argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       The _l_i_n_k_a_t() function may fail if:

       EEIINNVVAALL The value of the _f_l_a_g argument is not valid.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   CCrreeaattiinngg aa LLiinnkk ttoo aa FFiillee
       The following example shows how to create a link to a file named
       //hhoommee//ccnndd//mmoodd11 by creating a new directory entry named //mmoodduulleess//ppaassss11.


           #include <unistd.h>

           char *path1 = "/home/cnd/mod1";
           char *path2 = "/modules/pass1";
           int   status;
           ...
           status = link (path1, path2);

   CCrreeaattiinngg aa LLiinnkk ttoo aa FFiillee WWiitthhiinn aa PPrrooggrraamm
       In the following program example, the _l_i_n_k() function links the
       //eettcc//ppaasssswwdd file (defined as PPAASSSSWWDDFFIILLEE) to a file named //eettcc//ooppaasssswwdd
       (defined as SSAAVVEEFFIILLEE), which is used to save the current password file.
       Then, after removing the current password file (defined as PPAASSSSWWDDFFIILLEE),
       the new password file is saved as the current password file using the
       _l_i_n_k() 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);

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Some implementations do allow links between file systems.

       If _p_a_t_h_1 refers to a symbolic link, application developers should use
       _l_i_n_k_a_t() with appropriate flags to select whether or not the symbolic
       link should be resolved.

RRAATTIIOONNAALLEE
       Linking to a directory is restricted to the superuser in most
       historical implementations because this capability may produce loops in
       the file hierarchy or otherwise corrupt the file system. This volume of
       POSIX.1-2017 continues that philosophy by prohibiting _l_i_n_k() and
       _u_n_l_i_n_k() from doing this. Other functions could do it if the
       implementor designed such an extension.

       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 _l_i_n_k_a_t() 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 _l_i_n_k(), resulting in unspecified behavior. By opening a
       file descriptor for the directory of both the existing file and the
       target location and using the _l_i_n_k_a_t() 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 _l_i_n_k() function. The POSIX specification requires that
       if _p_a_t_h_1 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 _l_i_n_k() function,
       and required it to behave like _l_i_n_k_a_t() with the AT_SYMLINK_FOLLOW
       flag. However, historical practice from SVR4 and Linux kernels had
       _l_i_n_k() behaving like _l_i_n_k_a_t() with no flags, and many systems that
       attempted to provide a conforming _l_i_n_k() 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 _l_i_n_k() following
       links in practice, the _l_i_n_k_a_t() function was added taking a flag to
       specify the desired behavior for the application.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _r_e_n_a_m_e(), _s_y_m_l_i_n_k(), _u_n_l_i_n_k()

       The Base Definitions volume of POSIX.1-2017, <<ffccnnttll..hh>>, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       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 .



IEEE/The Open Group                  2017                              LINK(3)
