4 ms·
Reading the code, I was surprised to see that cd was implemented by calling out to the os library. I assumed that was something the shell or at least userspace
by supermdguy 9mo ago
Reading the code, I was surprised to see that cd was implemented by calling out to the os library. I assumed that was something the shell or at least userspace handled. At what level does the concept of a “current directory” exist?
- semiquaver 9mo agoUnix defines a Working Directory that every process has, changed with chdir(2): https://man7.org/linux/man-pages/man2/chdir.2.html https://man7.org/linux/man-pages/man2/chdir.2.html
- mort96 9mo agoThis doesn't technically answer the question: POSIX doesn't concern itself with the kernel interface, only with the libc. Most POSIX systems have a kernel with a syscall interface that mirrors the libc API so that these libc functions are just syscall wrappers, but nothing technically prevents the current working directory to be a purely userspace concept maintained by the libc where all relative paths passed to filesystem functions are translated into absolute paths by the libc function before being passed to the kernel via syscall. But yes, in the BSDs, Linux and Windows, the kernel has a concept of a current working directory.
- deleted 9mo ago[deleted]
- mort96 9mo agoIs this getting downvoted only because I referred to POSIX rather than UNIX? I'm more familiar with POSIX, but I'm 99% sure the UNIX standard also doesn't say anything about the kernel interface...
- lukeh 9mo agoIn the kernel’s process structure. See NOTES - https://man7.org/linux/man-pages/man2/chdir.2.html https://man7.org/linux/man-pages/man2/chdir.2.html
- creatonez 9mo agoIt's at the kernel level. Each process has its own current working directory. On Linux, these CWD values are exposed at `/proc/[...]/cwd`. This value affects the resolution of relative paths in filesystem operations at a syscall level.
- hnlmorg 9mo agoIt’s also generally a shell builtin. Though you do find an executable called cd too for compatibility reasons.
- mattstir 9mo agoYeah, it's typically a shell built-in since you'd want cd to change the cwd for the shell process itself. Child processes (like commands being executed in the shell) can inherit the parent shell's cwd but AFAIK the opposite isn't true.
- deleted 9mo ago[deleted]
- jasomill 9mo agoInteresting. I've been using Unix systems for 30 years and never noticed this. On my Fedora system, /usr/bin/cd is just a shell script that invokes the shell builtin: #!/usr/bin/sh builtin cd "$@" I suppose it could be useful for testing whether a directory exists with search permissions for the current user safely in a multithreaded program that relies on the current directory remaining constant.
- creatonez 9mo agoWait, how did the `cd` executable used to work in old Unix? Did it instruct the kernel to reassign the CWD of the parent process?
- hnlmorg 9mo agoThe original UNIX shell (Thompson Shell) had chdir as a builtin, so I’d wager it’s always been a builtin. https://en.wikipedia.org/wiki/Thompson_shell https://en.wikipedia.org/wiki/Thompson_shell