如何唯一地识别尝试 open() 内核模块的用户?
我正在开发一个内核模块,并且试图唯一地标识每个尝试 open() 模块的用户(可以是进程或线程)。
识别它们的最佳方法是什么?我可以通过系统调用获取 ID 吗?
我希望将所有用户放在一个列表中,指定他们是否尝试打开模块进行读/写,并且我需要知道哪个用户尝试执行操作。
I'm working on a kernel module and I'm trying to uniquely identify each one of the users trying to open() the module (can be either processes or threads).
What is the best way to identify them? Is there an ID I can get from a system call?
I wish to get all users in a list that specifies whether they're trying to open the module for read/write, and I need to know which one tried acting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您正在创建一个简单的 Linux 字符设备,并在 /dev/mydev 等位置创建。如果是这种情况,那么下面应该为您提供一个很好的示例来说明如何执行此操作。但是,如果您对打开设备有不同的含义,那么这将不适用。
您的 char 设备文件操作
您的 mydev_open()
有关可以了解当前进程的更多信息,请查看头文件 include/linux/cred.h。
I'm assuming that you are creating a simple Linux character device, to be created in a location such as /dev/mydev. If this is the case then the following should give you a good example of how to do it. However, if you have a different meaning for open a device, then this won't be applicable.
Your char device file operations
Your mydev_open()
For more information about what you can find out about the current process, then check out the header file include/linux/cred.h.
当调用
open
方法时,current
将指向调用它的任务(~= 线程)的task_struct
。但这很少是正确的方法。
When your
open
method is called,current
will point at thetask_struct
of the task (~= thread) that is calling it.This is seldom the right approach, though.
您可以根据您的要求决定 unique 的含义: unique 元组可以仅包含 (pid)。或者它可以是(tid,uid),或(filp),或(inode)。
You decide what unique means, depending on your requirements: The unique tuple could just consist of (pid). Or it could be (tid, uid), or (filp), or (inode).