【问题标题】:How to get the owner process of a socket?如何获取套接字的所有者进程?
【发布时间】:2014-07-24 03:35:06
【问题描述】:

给定一个套接字结构,有什么方法可以在内核模块中获取该套接字的所有者进程?也就是说,给定一个套接字结构,我试图获取其所有者进程的进程描述符(task_struct struct)?

我最初的想法是遍历任务列表。对于遍历的每个进程 p,只需检查 p 的打开文件描述符是否包含与与套接字结构关联的文件描述符相同的一个。 (在struct socket中,有一个字段struct file *file)。

我不确定内核是否允许我遍历任务列表。任何人都知道如何做到这一点?谢谢。

【问题讨论】:

    标签: sockets linux-kernel


    【解决方案1】:

    我认为这不是一个坏主意。看看我哪天写的这段代码:

    struct task_struct *task;
    struct list_head *t, *t1;
    ...
    list_for_each(t, t1) {
    #ifdef CONFIG_UIDGID_STRICT_TYPE_CHECKS
        if ((task_uid(task)).val == 0) /* no root processes, only userspace*/
    #else
        if ((task_uid(task)) == 0)
    #endif
        continue;
        task = list_entry(t, struct task_struct, tasks);
        /*Now you have task_struct of userspace program*/
        /*and may do whatever you want ))) */
        /*E.g. to figure out opened file descriptors*/
    }
    

    一点补充:我想你知道这段代码应该在单内核线程中运行。无需执行内核模块的init函数。

    【讨论】:

    • 谢谢,这有帮助。顺便说一句,您的模块代码运行的内核版本是什么?
    • 反对?可能你的意思是……继续跑?我在内核版本 3.2.0、3.10.2 上测试过
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多