mknodat()

Updated: April 19, 2023

Make a new filesystem entry at a given location

Synopsis:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int mknodat( int fd,
             const char* path,
             mode_t mode,
             dev_t dev );

Arguments:

fd
A file descriptor that indicates the base directory for relative file paths. The pathname given in path is resolved by appending it to the directory associated with fd. 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 path specifies an absolute path, fd has no effect.

path
The pathname that you want to use for the file.
mode
A set of bits that define the file type and access permissions that you want to use. The valid file types are:
  • S_IFDIR — create a directory.
  • S_IFIFO — create a FIFO.

For more information, see the entry for struct stat.

dev
Ignored.

Library:

libc

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

Description:

The mknodat() function makes a file, named path, using the file type encoded in the mode argument. Supported file types are directories and FIFOs. The path argument can contain an absolute or relative path. In the latter case, the fd 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 fd 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, mknodat() can still be used for maintaining a per-thread current working directory, using file descriptors maintained by the application.

Note: This function is included to enhance portability with software written for Unix-compatible operating systems. For POSIX portability, use mkdirat() or mkfifoat() instead.
To make a directory with read-write-execute permissions for everyone, you could use the following:
mknodat (AT_FDCWD, name, S_IFDIR | 0777, 0);

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EACCES
Search permission is denied on a component of the path prefix of path, write permission is denied on the parent directory of path, or the access mode of the fd file descriptor is not O_SEARCH and the underlying directory doesn't permit directory searches.
EBADF
The path 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.
EEXIST
The named file already exists.
EINVAL
There's an invalid argument.
EIO
An I/O error occurred while accessing 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.
EMLINK
The link count of the parent directory would exceed LINK_MAX.
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
A component of the path prefix doesn't exist, or the path argument points to an empty string.
ENOSPC
The filesystem doesn't contain enough space to hold the contents of the new directory or to extend the parent directory, or the maximum limit of files and directories has been reached.
ENOSYS
The mknodat() function isn't implemented for the filesystem underlying the specified path.
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 path argument contains at least one non-slash character and ends with at least one slash character (/).
  • The path argument isn't an absolute path and the fd file descriptor is associated with a non-directory file.
  • The file descriptor in fd was created without O_DIRECTORY set.
EPERM
The calling process doesn't have appropriate privileges and the file type is not FIFO-special.
EROFS
The directory in which the file is to be created resides on a read-only filesystem.

Classification:

POSIX 2008

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