加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

来自Linux内核模块中fd inode的可执行路径

发布时间:2020-12-14 01:52:55 所属栏目:Linux 来源:网络整理
导读:给定/ proc / ** / fd / *中存在的inode 还有一个 Linux内核模块需要从符号链接/ proc / ** / exe中找到可执行文件路径 我怎么能实现这一点,以便从inode编号我使用fd获得可执行文件的路径? 解决方法 proc_inode struct和 PROC_I macro都是内部的.见 [PATCH
>给定/ proc / ** / fd / *中存在的inode
>还有一个 Linux内核模块需要从符号链接/ proc / ** / exe中找到可执行文件路径

我怎么能实现这一点,以便从inode编号我使用fd获得可执行文件的路径?

解决方法

proc_inode struct和 PROC_I macro都是内部的.见 [PATCH 27/28] proc: Make the PROC_I() and PDE() macros internal toprocfs [RFC].

相反,如何迭代inode的dentry列表呢?您可以使用dentry_path_raw()查找/ * / fd / *路径名称:

//struct inode *proc_inode;

struct dentry *dentry;
pid_t pid;
int found_match = 0;

printk(KERN_DEBUG "superblock type name: %sn",proc_inode->i_sb->s_type->name);

// An inode's dentry list is protected by the i_lock. See:
// - "dcache->d_inode->i_lock protects: i_dentry,d_u.d_alias,d_inode of aliases"
//   http://lxr.free-electrons.com/source/fs/dcache.c?v=4.0#L48
// - The implementation of d_prune_aliases()
//   http://lxr.free-electrons.com/source/fs/dcache.c?v=4.0#L882
spin_lock(&proc_inode->i_lock);
hlist_for_each_entry(dentry,&proc_inode->i_dentry,d_u.d_alias) {
    char buf[64];
    const char *path_raw;
    char c;

    path_raw = dentry_path_raw(dentry,buf,sizeof(buf));

    // dentry_path_raw() places the path into `buf'. If `buf' is not large
    // enough,then continue on to the next dentry.
    if (!(buf <= path_raw && path_raw <= buf + sizeof(buf) - 1)) {
        printk(KERN_DEBUG "`buf' not large enough,dentry_path_raw() returned %ldn",PTR_ERR(path_raw));
        continue;
    }

    printk(KERN_DEBUG "path_raw = %sn",path_raw);

    // We're looking to match: ^/(d*)/fd/

    if (*path_raw++ != '/') continue;

    pid = 0;
    for (c = *path_raw; c; c = *++path_raw) {
        if ('0' <= c && c <= '9') {
            pid = 10 * pid + (c - '0');
        } else {
            break;
        }
    }

    if (*path_raw++ != '/') continue;
    if (*path_raw++ != 'f') continue;
    if (*path_raw++ != 'd') continue;
    if (*path_raw != '/' && *path_raw != '') continue;

    // Found a match. Break the dentry list loop.
    found_match = 1;
    printk(KERN_DEBUG "breaking dentry list loopn");
    break;
}
spin_unlock(&proc_inode->i_lock);

if (found_match) {
    printk(KERN_DEBUG "pid = %dn",(int)pid);
}

编辑:我已经向GitHub上传了一个示范项目:
https://github.com/dtrebbien/so16317923-proc-fs-kernel-module

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读