Reputation: 15136
On Linux, every process has its own root directory. For most processes, this is /
. However, chroot
can change that. This information is exposed via /proc
. However, how do I find out the root directory of a process programmatically? Is there a syscall, or, libc function for it?
Upvotes: 1
Views: 2448
Reputation: 67
One simple way is to just use a for loop. This is a one-liner that will print out the root directory of each of the processes you wish (proc1, proc2, proc3):
for i in $(ps -ef | grep -E 'proc1|proc2|proc3' | awk '{ print $2 }'); do ls -ld /proc/$i/root; done
Upvotes: 0
Reputation: 123600
I don't know whether there is another way, but lots of programs rely on the machine readable files in /proc
to get additional information about processes and there's nothing inherently wrong with that.
Here's an example of a process finding its own root dir programmatically via /proc
:
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
int main() {
char foo[PATH_MAX+1];
int count = readlink("/proc/self/root", foo, PATH_MAX);
if(count < 0) {
perror("Can't find root dir (is /proc mounted here?)");
} else {
foo[count]=0;
printf("My root dir is %s\n", foo);
}
}
Upvotes: 1
Reputation: 923
Well there isn't. There exists a command to do this which is pwdx, here is its code https://elixir.bootlin.com/busybox/latest/source/procps/pwdx.c. It also reads root dir from /proc. You can get the pid of your process using getpid function.
Upvotes: 0