How do I get the current working directory of a process in FreeBSD in C?
I am currently trying to connect a terminal emulator written in C from Linux to FreeBSD. But the terminal is trying to get the current working directory (CWD) from the parent process.
He does this by referring to /proc/$PID/cwd
.
I am now looking for a way to replace this functionality with something that works on FreeBSD.
So how do I get CWD from a process in FreeBSD?
Is there even a POSIX solution?
I know that I can get the CWD from my process using getcwd
, but I need the CWD of the parent process where I only know the PID.
source to share
Well, you actually have two options. One of them - use the shell utilities, such as lsof -p
, fstat -p
(as I mentioned above), or another utility named procstat
as described here . The procstat
solution looks like this:
procstat -f <pid> | awk '$3 == "cwd" { print $10 }'
another possible solution is to use the libprocstat library call, especially procstat_getfiles()
to get complete information in your C program. Take a look at the procstat sources for an example of using the API.
source to share