【问题标题】:How to access all pipes in the system from Linux kernel space如何从 Linux 内核空间访问系统中的所有管道
【发布时间】:2011-08-24 21:06:01
【问题描述】:

我想向 Linux 内核添加一个新的系统调用,它将显示有关系统中创建的所有管道的信息。

如何获取 pipefs 中每个管道的 inode(或任何其他允许我访问 pipe_inode_info 的相关结构)?

我一直在看struct vfsmountstruct dentrystruct super_block,但我没有找到合适的方法。 有什么方法可以获取 pipefs 中每个文件的文件结构?

【问题讨论】:

    标签: file linux-kernel pipe inode system-calls


    【解决方案1】:

    首先我去 /proc 目录和问题:

    ls -al */fd |grep pipe
    

    (试着去掉上面的“管道”,你会学到更多。)结果是(只是一个快照):

    l-wx------ 1 root     root     64 2011-05-14 23:12 17 -> pipe:[39208]
    l-wx------ 1 root     root     64 2011-05-14 23:12 2 -> pipe:[16245]
    lr-x------ 1 root     root     64 2011-05-14 23:12 4 -> pipe:[23406]
    l-wx------ 1 root     root     64 2011-05-14 23:12 8 -> pipe:[23406]
    l-wx------ 1 root     root     64 2011-05-14 23:12 17 -> pipe:[39532]
    l-wx------ 1 root     root     64 2011-05-14 23:12 2 -> pipe:[16245]
    lr-x------ 1 root     root     64 2011-05-14 23:12 4 -> pipe:[23406]
    l-wx------ 1 root     root     64 2011-05-14 23:12 8 -> pipe:[23406]
    l-wx------ 1 root     root     64 2011-05-14 23:12 1 -> pipe:[16245]
    lr-x------ 1 root     root     64 2011-05-14 23:12 16 -> pipe:[40032]
    l-wx------ 1 root     root     64 2011-05-14 23:12 17 -> pipe:[40032]
    l-wx------ 1 root     root     64 2011-05-14 23:12 2 -> pipe:[16245]
    lr-x------ 1 root     root     64 2011-05-14 23:12 4 -> pipe:[23406]
    l-wx------ 1 root     root     64 2011-05-14 23:12 8 -> pipe:[23406]
    l-wx------ 1 tteikhua tteikhua 64 2011-05-14 23:13 1 -> pipe:[16245]
    l-wx------ 1 tteikhua tteikhua 64 2011-05-14 23:13 12 -> pipe:[66674]
    lr-x------ 1 tteikhua tteikhua 64 2011-05-14 23:13 13 -> pipe:[66674]
    l-wx------ 1 root root 64 2011-05-14 23:30 1 -> pipe:[101794]
    

    如果你想查看创建管道的进程,只需删除“grep”,例如:

    这里显示 pid=1 在 6759 处有一个管道 fd:

    1/fd:
    total 0
    dr-x------ 2 root root  0 2011-05-14 23:29 .
    dr-xr-xr-x 7 root root  0 2011-05-14 22:59 ..
    lrwx------ 1 root root 64 2011-05-14 23:29 0 -> /dev/console (deleted)
    lrwx------ 1 root root 64 2011-05-14 23:29 1 -> /dev/console (deleted)
    lrwx------ 1 root root 64 2011-05-14 23:29 2 -> /dev/console (deleted)
    lr-x------ 1 root root 64 2011-05-14 23:29 3 -> pipe:[6759]
    

    并将上述内容追溯到 fs/pipe.c(打印这些内容的 Linux 内核源代码):

    /*
     * pipefs_dname() is called from d_path().
     */
    static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
    {
            return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
                                    dentry->d_inode->i_ino);
    }
    
    static const struct dentry_operations pipefs_dentry_operations = {
            .d_dname        = pipefs_dname,
    };
    

    并阅读 fs/dcache.c:

    char *d_path(const struct path *path, char *buf, int buflen)
    {
            if (path->dentry->d_op && path->dentry->d_op->d_dname)
                    return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
    
    and since d_path() is an exported symbol,
    
    EXPORT_SYMBOL(d_path);
    

    您应该能够从任何地方调用它,并获取有关路径的信息 - 如果它是管道,则将调用相应的 pipefs_dname() - 它依赖于文件系统:

    ./fs/pipe.c:
        .d_dname    = pipefs_dname,
    

    阅读 inode.c: init_inode_always() 你可以看到 i_pipe 设置为 NULL。仅当 inode 是 PIPE 时,它才不为空。

    所以你总是可以循环遍历所有进程并获取它打开的文件描述符,并且如果将inode的i_pipe设置为非NULL,你就会知道该值是管道的inode编号。

    我认为这样的代码不会存在于内核源代码中(因此没有必要寻找它 - 我已经尝试过),因为在用户空间中执行它更有效且更安全(例如“ls -al "命令我之前解释过)然后在内核内部 - 通常内核越小,安全错误越少,因此稳定性更好等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-17
      • 2018-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2013-01-21
      相关资源
      最近更新 更多