gpt4 book ai didi

file - 如何从 Linux 内核空间访问系统中的所有管道

转载 作者:行者123 更新时间:2023-12-02 04:09:34 27 4
gpt4 key购买 nike

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

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

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

最佳答案

首先我去/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”命令前面解释过)然后在内核内部 - 通常内核越小,安全错误越少,因此稳定性越好等。

关于file - 如何从 Linux 内核空间访问系统中的所有管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com