MKNOD(2)

πŸ’– MKNOD(2) πŸ’–

okay bestie, sit down, hydrate, because today we're talking about the System Calls Manual page for mknod and mknodat β€” the little syscalls that let you make a special file node. this is real one-on-one unix lore and i am obsessed πŸŽ€


✨ NAME ✨

mknod, mknodat β€” make a special file node

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

🧚 SYNOPSIS 🧚

#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 🌸

so here's the tea, bestie πŸ’… β€” the file system node at path gets created with the file type and the access permissions you specify in mode. and then, plot twist, those access permissions get modified by the process's umask value. yes, the umask is always in the group chat, always chiming in πŸ™ˆ

if mode says this is a block or character special file, then dev is a configuration dependent specification that denotes a particular device on the system. if it's not, then dev is just... ignored. ghosted. literally no thoughts, head empty πŸ’€

⚠️ the mknod() system call requires super-user privileges. like, actual root energy. you can't just walk in there with your little user account and start making device nodes, that's not the vibe.

now for mknodat() β€” she's mknod()'s cooler cousin, and she's equivalent to mknod() except in the case where path specifies a relative path. in that case, the newly created device node is created relative to the directory associated with the file descriptor fd instead of the current working directory. iconic behavior honestly πŸ‘‘

and if you pass mknodat() the special value AT_FDCWD in the fd parameter, then the current working directory is used, and the behavior is identical to a call to mknod(). so basically it's the same girl with a different outfit. slay 🌈

🫢 RETURN VALUES 🫢

the mknod() function returns the value 0 if successful. otherwise, the value -1 is returned and the global variable errno is set to indicate the error. that's it, that's the whole vibe check ✨

😭 ERRORS 😭

the mknod() system call will fail and the file will be not created if any of these happen:

  • [ENOTDIR] β€” a component of the path prefix is not a directory.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. she long, she too long πŸ’€
  • [ENOENT] β€” a component of the path prefix does not exist. where? not here. doesn't exist.
  • [EACCES] β€” search permission is denied for a component of the path prefix. no searching for you 🚫
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname. it's giving link loop labyrinth πŸŒ€
  • [EPERM] β€” the process's effective user ID is not super-user. told you, bestie, root only πŸ‘‘
  • [EIO] β€” an I/O error occurred while making the directory entry or allocating the inode.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system. yikes 😭
  • [ENOSPC] β€” the directory in which the entry for the new node is being placed cannot be extended because there is no space left on the file system containing the directory.
  • [ENOSPC] β€” there are no free inodes on the file system on which the node is being created.
  • [EDQUOT] β€” the directory in which the entry for the new node is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted.
  • [EDQUOT] β€” the user's quota of inodes on the file system on which the node is being created has been exhausted.
  • [EROFS] β€” the named file resides on a read-only file system. read only means read only, babes πŸ“–
  • [EEXIST] β€” the named file exists. it's already here, bestie, we been knew πŸ’…
  • [EFAULT] β€” the path argument points outside the process's allocated address space.
  • [EINVAL] β€” creating anything else than a block or character special file (or a whiteout) is not supported. sorry, no custom file types, that's not on the menu πŸ“

and in addition to the errors returned by mknod(), mknodat() may fail if:

  • [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 searching.
  • [ENOTDIR] β€” the path argument is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.

πŸ¦‹ SEE ALSO πŸ¦‹

chmod(2), mkfifo(2), stat(2), umask(2)

⭐ STANDARDS ⭐

the mknodat() system call follows The Open Group Extended API Set 2 specification. she's certified, she's spec-compliant, she's got receipts πŸ“œβœ¨

πŸ’œ HISTORY πŸ’œ

the mknod() function appeared in Version 4 AT&T UNIX. the mknodat() system call appeared in FreeBSD 8.0. two icons from two different eras, and we love both πŸŽ€


and that's the whole page, bestie!! this manual page came from Debian and it's dated March 30, 2020 β€” so it's a lil vintage but it's still serving exactly what it needs to serve 🫢 go forth and make your special file nodes (if you're root, which you're probably not, no offense) πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from