5

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.

17
  • 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 ?
    – gaurav
    Dec 3, 2011 at 10:47
  • It is a simple function call to get the struct path. Did I misunderstand your question, and you are not actually writing kernel code?
    – Mat
    Dec 3, 2011 at 10:49
  • 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. :)
    – gaurav
    Dec 3, 2011 at 10:54
  • 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.
    – Mat
    Dec 3, 2011 at 11:13
  • 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 ?
    – gaurav
    Dec 3, 2011 at 11:32
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

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].

2
  • 2
    Sorry but I need to get current working directory in kernel code I can't use this code there.
    – gaurav
    Dec 3, 2011 at 11:07
  • @gaurav: Ok my bad, I've misunderstood your question.
    – Daimrod
    Dec 3, 2011 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.