The Open Group Base Specifications Issue 7
IEEE Std 1003.1-2008
Copyright © 2001-2008 The IEEE and The Open Group

NAME

symlink, symlinkat - make a symbolic link relative to directory file descriptor

SYNOPSIS

#include <unistd.h>

int symlink(const char *
path1, const char *path2);
int symlinkat(const char *
path1, int fd, const char *path2);

DESCRIPTION

The symlink() function shall create a symbolic link called path2 that contains the string pointed to by path1 ( path2 is the name of the symbolic link created, path1 is the string contained in the symbolic link).

The string pointed to by path1 shall be treated only as a character string and shall not be validated as a pathname.

If the symlink() function fails for any reason other than [EIO], any file named by path2 shall be unaffected.

The symbolic link's user ID shall be set to the process' effective user ID. The symbolic link's group ID shall be set to the group ID of the parent directory or to the effective group ID of the process. Implementations shall provide a way to initialize the symbolic link's group ID to the group ID of the parent directory. Implementations may, but need not, provide an implementation-defined way to initialize the symbolic link's group ID to the effective group ID of the calling process.

The values of the file mode bits for the created symbolic link are unspecified. All interfaces specified by POSIX.1-2008 shall behave as if the contents of symbolic links can always be read, except that the value of the file mode bits returned in the st_mode field of the stat structure is unspecified.

Upon successful completion, symlink() shall mark for update the last data access, last data modification, and last file status change timestamps of the symbolic link. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

The symlinkat() function shall be equivalent to the symlink() function except in the case where path2 specifies a relative path. In this case the symbolic link is created relative to the directory associated with the file descriptor fd instead of the current working directory. If the file descriptor was opened without O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the file descriptor was opened with O_SEARCH, the function shall not perform the check.

If symlinkat() is passed the special value AT_FDCWD in the fd parameter, the current working directory is used and the behavior shall be identical to a call to symlink().

RETURN VALUE

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

ERRORS

These functions shall fail if:

[EACCES]
Write permission is denied in the directory where the symbolic link is being created, or search permission is denied for a component of the path prefix of path2.
[EEXIST]
The path2 argument names an existing file or symbolic link.
[EIO]
An I/O error occurs while reading from or writing to the file system.
[ELOOP]
A loop exists in symbolic links encountered during resolution of the path2 argument.
[ENAMETOOLONG]
The length of a component of the pathname specified by the path2 argument is longer than {NAME_MAX} or the length of the path1 argument is longer than {SYMLINK_MAX}.
[ENOENT]
A component of path2 does not name an existing file or path2 is an empty string.
[ENOSPC]
The directory in which the entry for the new symbolic link is being placed cannot be extended because no space is left on the file system containing the directory, or the new symbolic link cannot be created because no space is left on the file system which shall contain the link, or the file system is out of file-allocation resources.
[ENOTDIR]
A component of the path prefix of path2 is not a directory.
[EROFS]
The new symbolic link would reside on a read-only file system.

The symlinkat() function shall fail if:

[EACCES]
fd was not opened with O_SEARCH and the permissions of the directory underlying fd do not permit directory searches.
[EBADF]
The path2 argument does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor open for reading or searching.

These functions may fail if:

[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path2 argument.
[ENAMETOOLONG]
The length of the path2 argument exceeds {PATH_MAX} or pathname resolution of a symbolic link in the path2 argument produced an intermediate result with a length that exceeds {PATH_MAX}.

The symlinkat() function may fail if:

[ENOTDIR]
The path2 argument is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.

The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

Like a hard link, a symbolic link allows a file to have multiple logical names. The presence of a hard link guarantees the existence of a file, even after the original name has been removed. A symbolic link provides no such assurance; in fact, the file named by the path1 argument need not exist when the link is created. A symbolic link can cross file system boundaries.

Normal permission checks are made on each component of the symbolic link pathname during its resolution.

RATIONALE

Since POSIX.1-2008 does not require any association of file times with symbolic links, there is no requirement that file times be updated by symlink().

The purpose of the symlinkat() function is to create symbolic links 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 symlink(), resulting in unspecified behavior. By opening a file descriptor for the target directory and using the symlinkat() function it can be guaranteed that the created symbolic link is located relative to the desired directory.

FUTURE DIRECTIONS

None.

SEE ALSO

fdopendir , fstatat , lchown , link , open , readlink , rename , unlink

XBD <unistd.h>

CHANGE HISTORY

First released in Issue 4, Version 2.

Issue 5

Moved from X/OPEN UNIX extension to BASE.

Issue 6

The following changes were made to align with the IEEE P1003.1a draft standard:

Issue 7

Austin Group Interpretation 1003.1-2001 #143 is applied.

The symlinkat() function is added from The Open Group Technical Standard, 2006, Extended API Set Part 2.

Additions have been made describing how symlink() sets the user and group IDs and file mode of the symbolic link, and its effect on timestamps.

Changes are made to allow a directory to be opened for searching.

End of informative text.

 

return to top of page

UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
Copyright © 2001-2008 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]