user6456568
user6456568

Reputation: 599

How to get readable pwd in kernel

I want to get the current directory, here is my attempt

asmlinkage ssize_t fake_read(int __fd, void *__buf, size_t __nbytes){

    struct path pwd;
    get_fs_pwd(current->fs,&pwd);
    char x[1000];
    dentry_path_raw(pwd.dentry,x,999);
    fm_alert("read:%s\n",x);
    return real_read(__fd,__buf,__nbytes);
}

However, the output I get is like

[ 2170.293439] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293466] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293483] fsmonko.fake_read: read:\xffffffd8;\xffffff9b\xffffff84
[ 2170.293500] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293524] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293550] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293556] fsmonko.fake_read: read:\xffffffd8;\xffffff9b\xffffff84

It's supposed to print the readable pwd, what's wrong?

My kernel version is 4.13.0-36-generic Ubuntu 16.04.

Upvotes: 0

Views: 510

Answers (1)

user58697
user58697

Reputation: 7923

dentry_path_raw places the path at the end of the buffer. The beginning of the buffer (which you are printing) still contains garbage. An actual start of the path is the value dentry_path_raw returns. Try

    char * path = dentry_path_raw(pwd.dentry,x,999);
    fm_alert("read:%s\n", path);

Upvotes: 1

Related Questions