readlinkat()

Updated: April 19, 2023

Place the contents of a symbolic link found at a given location into a buffer

Synopsis:

#include <fcntl.h>

int readlinkat( int dirfd,
                const char* pathname, 
                char* buf, 
                size_t bufsize );

Arguments:

dirfd
A file descriptor that indicates the base directory for relative file paths. The pathname given in pathname is resolved by appending it to the directory associated with dirfd. You can set this argument to AT_FDCWD to use the current working directory as the base directory.
Note: You must use a file descriptor obtained from an open() call with the O_DIRECTORY flag set. Otherwise, the function fails and sets errno to ENOTDIR.

If pathname specifies an absolute path, dirfd has no effect.

pathname
The name of the symbolic link.
buf
A pointer to a buffer where the function can store the contents of the symbolic link (i.e., the path linked to).
bufsize
The size of the buffer.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The readlinkat() function places the contents of the symbolic link named by pathname into the buffer pointed to by buf, which has a size of bufsize. The pathname argument can contain an absolute or relative path. In the latter case, the dirfd argument is used to resolve the pathname, as explained in this argument's description (above).

If the access mode of the open file description associated with the dirfd file descriptor is not O_SEARCH, the function checks if directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the access mode is O_SEARCH, the function doesn't perform the check.

QNX Neutrino has no concept of a path operation (open(), readlink(), etc.) that references a path relative to an already open file descriptor (for a directory), as far as the filesystem resource manager is concerned. Therefore, it's not possible to eliminate the race inherent in looking up a pathname with multiple intervening directories or symbolic links. However, readlinkat() can still be used for maintaining a per-thread current working directory, using file descriptors maintained by the application.

The contents of the returned symbolic link don't include a NULL terminator. Their length must be determined from the stat structure returned by lstat(), or by the return value of the readlinkat() call.

If readlinkat() is successful, up to bufsize bytes from the contents of the symbolic link are placed in buf.

Returns:

The number of bytes placed in the buffer, or -1 if an error occurs (errno is set).

Errors:

EACCES
Search permission is denied on a component of the path prefix of path, or the access mode of the dirfd file descriptor is not O_SEARCH and the underlying directory doesn't permit directory searches.
EBADF
The pathname argument does not specify an absolute path and the dirfd argument is neither AT_FDCWD nor a valid file descriptor open for reading or searching.
EINVAL
The file named in path isn't a symbolic link.
EIO
An I/O error occurred while reading from the filesystem.
ELOOP
During resolution of the path argument, a loop was found in the symbolic links or more than SYMLOOP_MAX symbolic links were encountered.
ENAMETOOLONG
The length of the path string exceeds PATH_MAX, the resolution of a symbolic link within path produced a result longer than PATH_MAX, or a pathname component is longer than NAME_MAX.
ENOENT
The named file doesn't exist, or path is an empty string.
ENOTDIR
One of the following is true:
  • A component of the path prefix names an existing file that is neither a directory nor a symbolic link to one.
  • The pathname argument contains at least one non-slash character, ends with at least one slash character (/), and the last component names an existing file that is neither a directory nor a symbolic link to one.
  • The pathname argument is not absolute and fd is associated with a non-directory file.
  • The file descriptor in dirfd was created without O_DIRECTORY set.

Classification:

POSIX 2008

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes