开发者

How can I uniquely identify users trying to open() a kernel module?

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 g开发者_高级运维et 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.


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.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜