如何唯一地识别尝试 open() 内核模块的用户?

发布于 2024-09-02 22:31:50 字数 156 浏览 6 评论 0原文

我正在开发一个内核模块,并且试图唯一地标识每个尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荆棘i 2024-09-09 22:31:50

我假设您正在创建一个简单的 Linux 字符设备,并在 /dev/mydev 等位置创建。如果是这种情况,那么下面应该为您提供一个很好的示例来说明如何执行此操作。但是,如果您对打开设备有不同的含义,那么这将不适用。

您的 char 设备文件操作

struct file_operations mydev_fops = {
    .open = mydev_open,
};

您的 mydev_open()

static int mydev_open(struct inode *inode, struct file *filp)
{
    pid_t pid;
    int user_id;

    /* This is the thread-ID, traditional PID is found in current->pgid */
    pid = current->pid;

    /* The current user-id (as of 2.6.29) */
    user_id = current_uid();
}

有关可以了解当前进程的更多信息,请查看头文件 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

struct file_operations mydev_fops = {
    .open = mydev_open,
};

Your mydev_open()

static int mydev_open(struct inode *inode, struct file *filp)
{
    pid_t pid;
    int user_id;

    /* This is the thread-ID, traditional PID is found in current->pgid */
    pid = current->pid;

    /* The current user-id (as of 2.6.29) */
    user_id = current_uid();
}

For more information about what you can find out about the current process, then check out the header file include/linux/cred.h.

屌丝范 2024-09-09 22:31:50

当调用 open 方法时,current 将指向调用它的任务(~= 线程)的 task_struct

但这很少是正确的方法。

When your open method is called, current will point at the task_struct of the task (~= thread) that is calling it.

This is seldom the right approach, though.

沉默的熊 2024-09-09 22:31:50

您可以根据您的要求决定 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文