2

Is it possible to get the process current directory via struct task_struct? I can see that struct fs_struct has pwd pointer, but I'm not able get the exact variable where this info is stored.

Also, can we change current directory value?

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
foo_l
  • 591
  • 2
  • 10
  • 28
  • In what context are you? Where do you get the `task_struct` from? – Patrick B. Mar 14 '13 at 09:53
  • Does this answers help you: http://stackoverflow.com/questions/8366559/getting-current-working-directory-within-kernel-code ? – Patrick B. Mar 14 '13 at 09:55
  • I'm working on a 2.4 kernel version.In do_coredump() (in fs/exec.c) I would like to print the current directory information.The kernel version does not have core_pattern file, so I want to change the current directory of the current process to my desired path. – foo_l Mar 14 '13 at 09:59

1 Answers1

4

Your working on quite an old kernel so I've had to do some digging. One of the easier ways to deal with this sort of thing is see if the information is in /proc and look at what it does. If we grep for cwd in fs/proc we find:

static int proc_cwd_link(struct inode *inode, struct dentry   **dentry,   struct vfsmount **mnt)
{
    struct fs_struct *fs;
    int result = -ENOENT;
    task_lock(inode->u.proc_i.task);
    fs = inode->u.proc_i.task->fs;
    if(fs)
        atomic_inc(&fs->count);
    task_unlock(inode->u.proc_i.task);
    if (fs) {
        read_lock(&fs->lock);
        *mnt = mntget(fs->pwdmnt);
        *dentry = dget(fs->pwd);
        read_unlock(&fs->lock);
        result = 0;
        put_fs_struct(fs);
    }
    return result;
}

The proc inode points to the task (inode->u.proc_i.task, also given away by the task_lock() stuff). Looking at the task_struct definition it has a reference to struct fs_struct *fs which has the dentry pointers for the pwd. Translating the dentry entry to an actual name is another exercise however.

stsquad
  • 5,712
  • 3
  • 36
  • 54
  • 1
    I think [`getcwd()`](http://fxr.watson.org/fxr/source/fs/dcache.c?v=linux-2.4.22#L1019) is probably a more straightforward example – Hasturkun Mar 14 '13 at 17:31
  • @Hasturkun you are right of course. The proc code is a little tricky to follow given all the indirection. However looking at /proc is general is a useful place when trying to track down where these things are. – stsquad Mar 15 '13 at 12:31