current directory of a process in linux-kernel

╄→гoц情女王★ 提交于 2019-12-22 14:00:03

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15405842/current-directory-of-a-process-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!