FD(4)

πŸ’– FD(4) πŸ’–

okay bestie, we're doing the file descriptor files page from the Kernel Interfaces Manual, and honestly? it's giving ✨the filesystem is a lie but a useful one✨


✨ NAME ✨

fd, stdin, stdout, stderr β€” file descriptor files πŸŽ€

like. literally the files that ARE your file descriptors. the way that's so iconic of them.


🌸 DESCRIPTION 🌸

okay so. the files /dev/fd/0 through /dev/fd/# refer to file descriptors that you can access through the file system. yes queen, your fds are just... files now. we love a crossover episode πŸ’…

so here's the tea πŸ“ β€” if the file descriptor is open, and the mode the file is being opened with is a subset of the mode of the existing descriptor, then this call:

fd = open("/dev/fd/0", mode);

and this call:

fd = fcntl(0, F_DUPFD, 0);

are equivalent. same vibe, same result, no cap. πŸ”₯

and opening /dev/stdin, /dev/stdout and /dev/stderr? that's equivalent to these three calls right here:

fd = fcntl(STDIN_FILENO,  F_DUPFD, 0);
fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
fd = fcntl(STDERR_FILENO, F_DUPFD, 0);

so cute. so consistent. πŸ¦‹

⚠️ okay important bestie: flags to the open(2) call other than O_RDONLY, O_WRONLY and O_RDWR are ignored. like, straight up not read. don't be out here expecting them to do something, because they will not, and that's just how it is. πŸ™ˆ

🧚 IMPLEMENTATION NOTES 🧚

so by default, /dev/fd is provided by devfs(5), which provides nodes for the first three file descriptors. that's your main character trio right there, we stan. πŸ‘‘

some sites may require nodes for additional file descriptors though β€” and those can be made available by mounting fdescfs(5) on /dev/fd. so if you need more than three, that's the move. slay. πŸ’œ


🌈 FILES 🌈

  • /dev/fd/#
  • /dev/stdin
  • /dev/stdout
  • /dev/stderr

the whole squad, assembled. 🫢


⭐ SEE ALSO ⭐

go read those next bestie, they're part of the lore πŸ“š


okay so that's the whole page!! shoutout to the Debian girlies for shipping this one way back on June 9, 1993 β€” literally older than all of us and still lowkey essential. periodt πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from