linux内核中进程的当前目录 [英] current directory of a process in linux-kernel

查看:199
本文介绍了linux内核中进程的当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过struct task_struct获取进程的当前目录?我可以看到struct fs_struct具有pwd指针,但是我无法获得存储此信息的确切变量.

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?

推荐答案

您正在使用相当旧的内核,因此我不得不进行一些挖掘.处理此类问题的一种较简单的方法是查看信息是否位于/proc中,并查看其作用.如果我们在fs/proc中用grep替换cwd,我们会发现:

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;
}

proc的inode指向任务(inode-> u.proc_i.task,也由task_lock()东西提供).查看task_struct定义,它有对struct fs_struct * fs的引用,该文件具有pwd的dentry指针.但是,将dentry条目转换为实际名称是另一项工作.

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.

这篇关于linux内核中进程的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆