4

I was wandering on the Linux filesystem, which looks like a good way of understanding how it works. In UNIX "everything is a file", so I was looking at /dev/stdin and noticed it is a soft link to /proc/self/fd/0. So I decided to go take a look at the /proc folder, finding that /proc/self is actually a soft link to /proc/{PID}. To be more precise, the PID is the one of the ls -l /proc/self command I issue to check its value.

My theory is that the soft link is changed everytime a program tries to read it, to point to the right PID process folder. Is this done by the kernel on every request it gets to read the /proc folder? I don't think you need to actually read the file to get the output of ls or we wouldn't be able to see files with 700 permission, so does the soft link change happens when the /proc folder is accessed?

Going even further: Is this kind of control restricted for the kernel itself or can some applications make use of that? (Maybe checking when a folder is being accessed to dynamically change its contents/links according to "who" is doing it)

3

/proc is a mount point for procfs (kind of tmpfs, lives in memory), which is an interface to the kernel's internal data structure. Whatever you see in /proc is directly populated by Linux and also maintained by the kernel itself.

/proc/self is a symlink to the /proc/<PID> directory, where <PID> is the Process ID of the process accessing /proc at that very instant. So while in bash if you do cd /proc/self, you will be cd-id into the /proc/<PID>/, where <PID> is the PID for that instance of bash, while if some other process, let's say for cat:

$ cat /proc/self/comm
cat

You should absolutely not change anything in /proc except if kernel provides some interface to change the values. For example, kernel provides the interface /proc/sys/ to change kernel parameters at runtime, although mostly you should use sysctl to change values under /proc/sys/ rather than directly editing any file. Mishandling of Kernel's internal data structure would result in an unusable system or even system crash.

  • Is the reason the cd /proc/selfredirects to the bash /proc/<BASH_PID> and ls -l /proc/self shows the /proc/<LS_PID> the fact cd is a shell built-in and ls is a binary application? – IanC Aug 9 '16 at 18:04
  • @IanC spot on.. – heemayl Aug 9 '16 at 18:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.