Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current working directory within kernel code

I am working on a project in which I need to know the current working directory of the executable which called the system call. I think it would be possible as some system calls like open would make use of that information.

Could you please tell how I can get the current working directory path in a string?

like image 710
gaurav Avatar asked Dec 27 '22 10:12

gaurav


1 Answers

You can look at how the getcwd syscall is implemented to see how to do that.

That syscall is in fs/dcache.c and calls:

get_fs_root_and_pwd(current->fs, &root, &pwd);

root and pwd are struct path variables,

That function is defined as an inline function in include/linux/fs_struct.h, which also contains:

static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)

and that seems to be what you are after.

like image 95
Mat Avatar answered Jan 03 '23 04:01

Mat