7

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?

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

17 Comments

OK isn't there anything that could be simpler than this like a simple function call which could written the current working directory in a string ?
It is a simple function call to get the struct path. Did I misunderstand your question, and you are not actually writing kernel code?
No I am writing kernel code actually I am making some changes in fs/namei.c so I need to know the current working directory there ? I thought I could call some sys_getcwd type call like I can use sys_getpid to get the pid. Could you tell me where I can see what exports are done so that I could use that function ? Or I will use your answer. :)
sys_getcwd expects a pointer to user memory to store the path (uses copy_to_user). If you're in kernel space, that's not appropriate. Read that function in dcache.c to see how it works, or look for other usages of get_fs_pwd in the code.
ok I understand. Thank you very much for your help indeed. Also I need to know the complete name of the executable which called this system call can I get that ?
|
0

In newer kernels things changed a little bit. You can use this to get the current working directory:

#include <linux/init.h> // module_{init,exit}() #include <linux/module.h> #include <linux/kernel.h> #include <linux/dcache.h> // dentry_path_raw #include <linux/fs_struct.h> // get_fs_pwd static int __init get_cwd_module_init(void) { struct path abs_path; char *buf, *full_path; buf = __getname(); if (!buf) return -ENOMEM; get_fs_pwd(current->fs, &abs_path); full_path = dentry_path_raw(abs_path.dentry, buf, PATH_MAX); if (IS_ERR(full_path)) { pr_err("dentry_path_raw failed: %li", PTR_ERR(full_path)); } else { pr_info("Full path: '%s'", full_path); } __putname(buf); path_put(&abs_path); } static void __exit get_cwd_module_exit(void) { pr_info("exiting..."); } module_init(get_cwd_module_init) module_exit(get_cwd_module_exit) MODULE_AUTHOR("garlix"); MODULE_LICENSE("GPL"); 

1 Comment

What changed in the new kernel?
-1

How do you do that in a terminal ? You use pwd which looks at the environment variable named 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].

3 Comments

Sorry but I need to get current working directory in kernel code I can't use this code there.
@gaurav: Ok my bad, I've misunderstood your question.
@daimrod: I would delete this answer as it is not answering the OP's question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.