1

I recently began studying Linux's source code in order to create my own POSIX operating system from scratch. From what I understand, everything on a UNIX-like system is a file. Anything that you can read bytes from / write bytes to can be abstracted as a "stream". But here I've stumbled onto a problem no one on the Internet seems to care about (at least I didn't find anything explaining it) : What exactly does Linux do under the hood to manage everything as a file?

My question might be unclear, so here's an example of what I'd like to understand : When you type "ls -l" in a shell, it not only lists physical files stored on the disk, but also block and char devices, links, and other things that are not files stored on disk. How does it work?

Thanks in advance, Dave.

2
  • 1
    Read man readdir getdents. Read man -k inode. The d_type field in the inode holds the info. Commented Oct 21, 2020 at 0:22
  • ls "lists block and char devices [...] and other things that are not stored on disk." Yes, but the directory entries are stored in the same place on the disk as the entries for disk files. (With the exception of virtual file systems like /proc.) You can think of a directory as a file with records of (name, inode number) pairs that each map a name to an inode. Commented Oct 22, 2020 at 3:17

1 Answer 1

1

Note that writing a POSIX operating system from scratch is way beyond the scope of this site. Note also that the title and the actual question don't match.

If you want to know how the getdents system call works in Linux, look at the code, it seems you already did that for other parts. But getdents is not that important, and early UNIX systems didn't have it. The getdents function was introduced to support different file systems.

Regarding the actual question, your misconception seems to be that the ls command lists file, but it lists directory entries. Directory entries in UNIX can refer to files, but also to directories, named pipes and devices. The character and block device entries are the interface to the different drivers in the kernel. They contain a number to identify the driver, and another number to identify different devices handled by the same driver.

Newer systems also support file system sockets, but the whole socket concept is not part of the original UNIX design. Sockets are not files, and "everything is a file" refers to the original UNIX design. But once sockets are set up, they support some operations that also work on files.

You can continue to study the source code, but I recommend that you get some overview of a POSIX system before you start to write your own.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.