简体   繁体   English

C:在内核模块中读取文件指针的filp-> private_data

[英]C: read filp->private_data of filepointer in kernel module

I'am wondering if it is possible to read the private_data of a filepointer? 我想知道是否可以读取文件指针的private_data吗? Or is it really "private"?! 还是真的“私有”?

I am in a kernel module! 我在内核模块中!

struct file *intercepted_fp;

intercepted_fp = filp_open("/dev/snd/pcmC0D0p_bak", O_RDWR, (S_IRWXU | S_IRWXG | S_IRWXO));

if (intercepted_fp == NULL) {
    LOGI("Cannot open intercepted device!");
    return -ENODEV;
}

mm_segment_t old_fs = get_fs();
set_fs(get_ds());

LOGI("private data: %p", intercepted_fp->private_data); // -> prints "private data: ffffffff"
LOGI("buffer: %d", ((struct snd_pcm_file*) intercepted_fp->private_data)->substream->runtime->buffer_size); // ->  Unable to handle kernel paging request at virtual address ffffffff
set_fs(old_fs);

Of course you can. 当然可以。 It is just a field in struct file . 它只是struct file一个字段。

It is intended to pass around data known only to a particular device driver from a file system interface. 它旨在从文件系统接口传递仅特定设备驱动程序已知的数据。

The private_data pointer is a useful resource for preserving state information by the module across system calls. private_data指针是有用的资源,用于模块在系统调用之间保留状态信息。 The open system call sets this pointer to NULL before calling the open method for the driver. 在调用驱动程序的open方法之前,open系统调用将此指针设置为NULL。 The kernel module code needs to allocate the memory and free it. 内核模块代码需要分配内存并释放它。 So the memory is allocated in kernel space - not so accessible from user space, but easily accessible from kernel space. 因此,内存是在内核空间中分配的 -无法从用户空间访问,但可以从内核空间轻松访问。 You can access it, but changing it can disturb functioning of the driver. 您可以访问它,但是更改它可能会影响驱动程序的功能。 You can find more about private_data pointer here (archive) in a great book Linux Device Drivers, 3rd Edition (archive) . 您可以在出色的书《 Linux设备驱动程序,第三版》 (存档)中找到有关private_data指针的更多信息 (存档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM