If a process has /var as its cwd, and makes the syscall chdir("..") how does the cwd becomes / and not /var/.. ?
I tried reading linux’s source but there are to many macros for me to understand what’s happening…
Edit: I know that / and /var/.. both point to the same inode, but there are different directory entries stored in different directories, with different names. My question is: how does the linux kernel decides that ("/", 2) -> ("var", 11) -> ("..", 2) should become ("/", 2) ?
(With dirents written as (name, inode))
Context: I’m currently writing the vfs of my kernel, and I don’t know what is the “cleanest” solution. My current solution is to check, each time I’m accessing a child dirent, if it has the same pointer to in-memory inode as its parent.
Question Answer
In unix, the kernel keeps track of both the current and the root directories of all processes. They are just inode references, either in an actual filesystem or in memory.
The chdir system call just walks the path argument, starting either from the process’ root directory if it starts with / or from the process’ current directory as the current inode.
In the case of .., if the current inode is the process’ root directory, the inode associated with .. will not change (hence a chrooted process cannot escape out of its subtree), otherwise the inode associated with the entry (the parent directory) becomes the current inode.
When it gets to the final component or the path, the kernel makes the final inode the current directory of the process.
If the process’ current directory was removed from the filesystem, it is lingering as a RAM only directory and its . and .. entries have the same inode.