Getting the current working directory in kernel code

I am working on a project in which I need to know the current working directory of an executable file called a system call. I think this is possible, as some system calls, such as open , will use this information.

Could you tell me how I can get the current working directory in a row?

+4
source share
2 answers

You can watch syscall getcwd getcwd to see how to do it.

This syscall is located in fs/dcache.c and calls:

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

root and pwd are struct path variables,

This function is defined as a built-in function in include/linux/fs_struct.h , which also contains:

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

and it looks like what you need.

+10
source

How do you do this in the terminal? You are using pwd , which looks at an environment variable called pwd .

 #include <stdlib.h> int main(int ac, char **av) { printf("%s\n", getenv("PWD"); return 0; } 

If you want to know in which directory the executable is located, you can combine the information from getenv and from argv[0] .

0
source

Source: https://habr.com/ru/post/1384453/


All Articles