mknod(2)

๐Ÿ’… mknod โ€” create a special or ordinary file

okay bestie, so mknod, mknodat โ€” that's the vibe, that's the name, we're creating a special or ordinary file. it's giving "system call manual" energy, and honestly? obsessed. ๐Ÿฆ‹

This one lives in the Standard C library (libc, -lc) โ€” yeah, the classic. ๐Ÿ‘‘


โœจ SYNOPSIS โœจ

#include <sys/stat.h>

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

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

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

๐ŸŽ€ Feature Test Macro Requirements for glibc

(like, go peep feature_test_macros(7) if you're lost, bestie โ€” it's all there)

  • mknod():
_XOPEN_SOURCE >= 500
    || /* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so the system call mknod() creates a filesystem node โ€” could be a file, a device special file, or a named pipe โ€” and it's named path, with all its little attributes specified by mode and dev. that's it, that's the whole job bb. ๐Ÿ’–

The mode argument is doing double duty, no cap โ€” it specifies both the file mode to use and the type of node to be created. It should be a combination (using bitwise OR) of one of the file types listed below and zero or more of the file mode bits listed in inode(7). she's versatile like that. ๐Ÿ’…

The file mode gets modified by the process's umask in the usual way: in the absence of a default ACL, the permissions of the created node are (mode & ~umask). math girlies rise. โœจ

The file type must be one of S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO, or S_IFSOCK โ€” to specify a regular file (which will be created empty, fyi), character special file, block special file, FIFO (named pipe), or UNIX domain socket, respectively. (and psst โ€” zero file type is equivalent to type S_IFREG. ๐Ÿคซ)

If the file type is S_IFCHR or S_IFBLK, then dev specifies the major and minor numbers of the newly created device special file (makedev(3) may be useful to build the value for dev, help yourself bestie); otherwise it's just ignored. not even looked at. ghosted. ๐Ÿ’€

If path already exists, or is a symbolic link, this call fails with an EEXIST error. like, it's taken, move on. ๐Ÿ™ˆ

The newly created node will be owned by the effective user ID of the process. If the directory containing the node has the set-group-ID bit set, or if the filesystem is mounted with BSD group semantics, the new node will inherit the group ownership from its parent directory; otherwise it will be owned by the effective group ID of the process. so basically it's gonna wear whatever group the situation gives it. ๐Ÿ‘‘


๐Ÿงš mknodat()

The mknodat() system call operates in exactly the same way as mknod(), except for the differences described here. same girl, different address. ๐Ÿ’œ

  • If path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by mknod() for a relative pathname).
  • If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like mknod() does โ€” twins fr fr).
  • If path is absolute, then dirfd is ignored. completely. not even a text back. ๐Ÿ˜ญ

See openat(2) for an explanation of the need for mknodat() โ€” it's a whole thing, go read it, iykyk. ๐Ÿ“


โญ RETURN VALUE โญ

mknod() and mknodat() return zero on success. and on error? -1 is returned and errno is set to indicate the error. slay for zero, sad for minus one. ๐Ÿ˜ญ


๐Ÿ’€ ERRORS ๐Ÿ’€

  • EACCES โ€” The parent directory does not allow write permission to the process, or one of the directories in the path prefix of path did not allow search permission. (See also path_resolution(7).)
  • EBADF โ€” (mknodat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
  • EDQUOT โ€” The user's quota of disk blocks or inodes on the filesystem has been exhausted.
  • EEXIST โ€” path already exists. This includes the case where path is a symbolic link, dangling or not.
  • EFAULT โ€” path points outside your accessible address space.
  • EINVAL โ€” mode requested creation of something other than a regular file, device special file, FIFO or socket.
  • ELOOP โ€” Too many symbolic links were encountered in resolving path.
  • ENAMETOOLONG โ€” path was too long.
  • ENOENT โ€” A directory component in path does not exist or is a dangling symbolic link.
  • ENOMEM โ€” Insufficient kernel memory was available.
  • ENOSPC โ€” The device containing path has no room for the new node.
  • ENOTDIR โ€” A component used as a directory in path is not, in fact, a directory.
  • ENOTDIR โ€” (mknodat()) path is relative and dirfd is a file descriptor referring to a file other than a directory.
  • EPERM โ€” mode requested creation of something other than a regular file, FIFO (named pipe), or UNIX domain socket, and the caller is not privileged (Linux: does not have the CAP_MKNOD capability); also returned if the filesystem containing path does not support the type of node requested.
  • EROFS โ€” path refers to a file on a read-only filesystem.
yes bestie, ENOTDIR shows up twice โ€” once for regular you, once for mknodat() you. both iconic, both valid. don't @ me. ๐Ÿ’…

๐Ÿ‘‘ VERSIONS ๐Ÿ‘‘

POSIX.1-2001 says: "The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified." ๐Ÿšจ okay so that's the tea, that's the fine print, do not play with it.

However, nowadays one should never use mknod() for this purpose; one should use mkfifo(3), a function especially defined for this purpose. she was literally built for this. use her. ๐ŸŽ€

Under Linux, mknod() cannot be used to create directories. One should make directories with mkdir(2). different tool, different moment, respect the division of labor. ๐ŸŒˆ


๐Ÿ’œ STANDARDS ๐Ÿ’œ

POSIX.1-2024 XSI. periodt.


๐ŸŒธ HISTORY ๐ŸŒธ

  • mknod() โ€” SVr4, 4.4BSD, SUSv1, POSIX.1-2001 XSI (but see VERSIONS โ€” yeah, scroll back up bestie, that's important ๐Ÿ‘€).
  • mknodat() โ€” glibc 2.4, Linux 2.6.16, POSIX.1-2008 XSI.

๐Ÿซถ NOTES ๐Ÿซถ

There are many infelicities in the protocol underlying NFS. Some of these affect mknod() and mknodat(). not me being dramatic about NFS, it's just true. ๐Ÿ’€


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

mknod(1), chmod(2), chown(2), fcntl(2), mkdir(2), mount(2), socket(2), stat(2), umask(2), unlink(2), makedev(3), mkfifo(3), acl(5), path_resolution(7)


and that's the whole page, bestie!! ๐ŸŒŸ this one's from Linux man-pages 6.20, dated 2026-02-11 โ€” so like, go forth and create your nodes responsibly, and remember: FIFO girls use mkfifo(3). ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹๐Ÿ’…

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms