Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

mknod, mknodat - make directory, special file, or text file

&pagelevel(4)&pagelevel

Syntax

#include <sys/stat.h>

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

Description

mknod() creates a new file with the pathname pointed to by path. The file type and permissions of the new file are initialized from mode. If path is a symbolic link it is not traced.

The file type for path is incorporated in the mode argument via a bit-wise OR. The file type must be one of the following symbolic constants:

S_IFIFO

FIFO special file

S_IFCHR

Character-special file (not portable)

S_IFDIR

Directory (not portable)

S_IFBLK

Block-special file (not portable)

S_IFPOSIXBS2

File in the POSIX file system (not portable)

S_IFREG

Regular file (not portable)

mknod() can only be used portably in accordance with the X/Open standard if a FIFO file is generated. If the file type is not S_IFIFO, or dev does not have the value 0, the behavior of mknod() is undefined. The access permissions of the file are also incorporated in the mode argument via a bit-wise OR. They can be defined by any combination of the following symbolic constants:

Symbolic name

Bit pattern

Meaning

S_ISUID

04000

Set user ID on execution

S_ISGID

020#0

Set group ID on execution

S_IRWXU

00700

Read, write or execute (search if a directory is involved) by owner

S_IRUSR

00400

Read by owner

S_IWUSR

00200

Write by owner

S_IXUSR

00100

Execute by owner (search if a directory is involved)

S_IRWXG

00070

Read, write or execute (search) by group

S_IRGRP

00040

Read by group

S_IWGRP

00020

Write by group

S_IXGRP

00010

Execute (search) by group

S_IRWXO

00007

Read, write or execute (search) by others

S_IROTH

00004

Read by others

S_IWOTH

00002

Write by others

S_IXOTH

00001

Execute by others

S_ISVTX

01000

For directories: unrestricted delete permission

The user ID of the file is set to the effective user ID of the process, and the group ID of the file is set to the effective group ID of the process, unless the S_ISGID bit is set in the parent directory, in which case the group ID of the file is inherited from the parent directory.

The access permission bits of mode are modified by the file mode creation mask of the process: all bits which are set in the file mode creation mask are set to 0 by mknod().

If mode indicates a block- or character-special file, dev is the configuration-dependent specification of that file; if mode does not indicate a block- or character-special file, dev is ignored (see mkdev()).

For non-FIFO file types, mknod() can only be invoked with appropriate privileges (uid = 0).

The mknodat() function is equivalent to the mknod() function except when the path parameter specifies a relative path. In this case the new directory or the new file is not created in the current directory, but in the directory connected with the file descriptor fd. If the file descriptor was opened without O_SEARCH, the function checks whether a search is permitted in the connected file descriptor with the authorizations applicable for the directory. If the file descriptor was opened with O_SEARCH, the check is not performed.

When the value AT_FDCWD is transferred to the mknodat() function for the fd parameter, the current directory is used. 

Return val.

0

if successful.

Return val.

-1

if an error occurs; errno is set to indicate the error.
In the event of an error, no new file is created.

Errors

mknod() and mknodat() will fail if: 

 

EACCES

Either there is no search permission for a component of the path, or there is no write permission for the parent directory of the new file.

 

EEXIST

The specified file already exists.

 

EINTR

A signal was caught during the mknod() system call.

 

EINVAL

An argument is invalid.

 

EIO

An I/O error occurred during access to the file system.

 

ELOOP

Too many symbolic links were encountered in resolving path.


ENAMETOOLONG




The length of the path argument exceeds {PATH_MAX}, or a component of  path is longer than {NAME_MAX}.

The resolving of a symbolic link led to an interim result whose length exceeds {PATH_MAX}.


ENOENT

A component of the path prefix does not exist or path is an empty string.


ENOLINK

path refers to a remote computer and the link to this computer is no longer active.


ENOSPC

The directory in which the file is to be created cannot be extended, or no memory is available.


ENOTDIR

A component of the path prefix is not a directory.


EPERM

The effective user ID is not that of the system administrator and the file type is not FIFO.


EROFS

The directory in which the file is to be created is located on a read-only file system.

 

In addition, mknodat() fails if the following applies: 

 

EACCES

The file descriptor fd was not opened with O_SEARCH, and the authorizations applicable for the directory do not permit the directory to be searched.

 

EBADF

The path parameter does not specify an absolute pathname, and the fd parameter does not have the value AT_FDCWD, nor does it contain a valid file descriptor opened for reading or searching.

 

ENOTDIR

The path parameter does not specify an absolute pathname, and the file descriptor fd is not connected with a directory.

Notes

mknod() and mknodat() are executed only for POSIX files.

If mknod() creates a special file in a remote directory with RFS (remote file sharing), the device class and device number will be interpreted by the server.

For reasons of portability to implementations which comply with earlier versions of the X/Open standard, the mkfifo() function is recommended for creating FIFO files.

See also

chmod(), creat(), exec(), mkdir(), mkfifo(), open(), stat(), umask(), sys/stat.h, sys/types.h.