How to get current/working directory of user in kernel?

Hello!

Is it possible to get the current/working directory (in string format) of user in kernel code? For example if the user executes some command that finally does something in kernel, is it possible to get the path where user was when he/she/it executed the command?

I already found constant named "AT_FDCWD". I think that references to current working directory using file descriptor, but I need it in string format (like /home/user).

Then I found function __getcwd(), but I think it didn't work because the charbuf it filled contained garbage. Also nothing in kernel code used it, so maybe it's meant to be called only from user code.
 
I got it working somehow, but it's not good yet, since it causes kernel panics sometimes.

First of all, __getcwd() causes error because it tries to copy the result to userspace (or something like that).

I tried to use function kern___getcwd() (that is called by __getcwd). You can pass an argument that tells something about user and kernelspaces so I passed UIO_SYSSPACE to it. I have no idea what it does, but I do know that it causes kern___getcwd() not to copy data between user- and kernelspaces.

After this I managed to get the current working directory, but unfortunately the system became unusable. I used rmdir as a "test event" to try these things and now the kernel panics in some cases when removing directories. Here is error message I get:
Code:
shared lock of (lockmgr) ufs @ /usr/src/sys/kern/vfs_cache.c:1091
while exclusive locked from /usr/src/sys/kern/vfs_lookup.c:495
panic: share->excl
cpuid = 0
KDB: enter: panic
[thread pid 812 tid 100112 ]
Stopped at      kdb_enter+0x3d: movq    $0,0x683b30(%rip)
It actually looks pretty clear. If I understood correctly, my function call tries to lock something that is already locked.

If you have any ideas, please comment :)
 
Back
Top