通过miscdevice获得驱动私有数据结构体时file->private_data为空的解决方法

本文讨论了在Linux内核中通过file->private_data传递misc设备私有数据的方法,提出了一种更高效的实现方式,以解决驱动与misc设备内部数据断开链接的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PS:实际上标准的做法是在调用misc_open之前将file->private_data置为空,然后在调用misc_open的时候通过container_of函数得到驱动私有数据结构体的地址,然后让file->private_data指向私有数据结构体以供fops中的其他函数使用。尝试了一些办法,还是这个方法比较高效。
Author:     Samu Onkalo <samu.p.onkalo@nokia.com>
AuthorDate: Mon May 24 14:33:10 2010 -0700
Committer:  Linus Torvalds <torvalds@linux-foundation.org>
CommitDate: Tue May 25 08:07:03 2010 -0700

    drivers: misc: pass miscdevice pointer via file private data
    For misc devices, inode->i_cdev doesn't point to the device drivers own
    data.  Link between file operations and device driver internal data is
    lost.  Pass pointer to misc device struct via file private data for driver
    open function use.

    Signed-off-by: Samu Onkalo <samu.p.onkalo@nokia.com>
    Cc: Al Viro <viro@zeniv.linux.org.uk>
    Cc: Christoph Hellwig <hch@lst.de>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 drivers/char/misc.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 92ab03d..cd650ca 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -144,6 +144,7 @@ static int misc_open(struct inode * inode, struct file * file)
 	old_fops = file->f_op;
 	file->f_op = new_fops;
 	if (file->f_op->open) {
+		/* 重要:这里最好加一个name的判断,或者打印出申请者的name,用于DEBUG */
+		file->private_data = c; //增加这句,c指向misc_register时传进来的参数
 		err=file->f_op->open(inode,file);
 		if (err) {
 			fops_put(file->f_op);
--
 
原文地址:http://kerneltrap.org/mailarchive/git-commits-head/2010/5/25/39820
#include <linux/init.h> /* __init and __exit macroses */ #include <linux/kernel.h> /* KERN_INFO macros */ #include <linux/module.h> /* required for all kernel modules */ #include <linux/moduleparam.h> /* module_param() and MODULE_PARM_DESC() */ #include <linux/fs.h> /* struct file_operations, struct file */ #include <linux/miscdevice.h> /* struct miscdevice and misc_[de]register() */ #include <linux/slab.h> /* kzalloc() function */ #include <linux/uaccess.h> /* copy_{to,from}_user() */ #include <linux/init_task.h> //init_task再次定义 #include "proc_relate.h" MODULE_LICENSE("GPL"); MODULE_AUTHOR("Wu Yimin>"); MODULE_DESCRIPTION("proc_relate kernel modoule"); static int proc_relate_open(struct inode *inode, struct file *file) { struct proc_info *buf; int err = 0; buf=kmalloc(sizeof(struct proc_info)*30,GFP_KERNEL); file->private_data = buf; return err; } static ssize_t proc_relate_read(struct file *file, char __user * out,size_t size, loff_t * off) { struct proc_info *buf = file->private_data; /* 你需要补充的代码 */ } static int proc_relate_close(struct inode *inode, struct file *file) { struct buffer *buf = file->private_data; kfree(buf); return 0; } static struct file_operations proc_relate_fops = { .owner = THIS_MODULE, .open = proc_relate_open, .read = proc_relate_read, .release = proc_relate_close, .llseek = noop_llseek }; static struct miscdevice proc_relate_misc_device = { .minor = MISC_DYNAMIC_MINOR, .name = "proc_relate", .fops = &proc_relate_fops }; static int __init proc_relate_init(void) { misc_register(&proc_relate_misc_device); printk(KERN_INFO "proc_relate device has been registered.\n"); return 0; } static void __exit proc_relate_exit(void) { misc_deregister(&proc_relate_misc_device); printk(KERN_INFO "proc_relate device has been unregistered\n"); } module_init(proc_relate_init); module_exit(proc_relate_exit);补充这段代码需要补充的函数部分,使其能编译为内核模块,安装该内核模块后测试程序,运行结果类似如下:Here is parent process,pid = 7329 this is a child,pid is 7330 this is another child,pid is 7331 this is a child,pid is 7333 In thread,pid=7331 tid=7334 thread id=1254224352 this is a child,pid is 7332 this is a child,pid is 7335 ------------------------------------------------------- pid=2616 tgid=2616 comm=sshd sessionid=4 mm=ffff8000fae19000 activeMM=ffff8000fae19000 parent =1971 real_parent=1971 group_leader2616 ------------------------------------------------------- pid=2670 tgid=2670 comm=sshd sessionid=4 mm=ffff8000fa477500 activeMM=ffff8000fa477500 parent =2616 real_parent=2616 group_leader2670 -------------------------------------------------------
05-17
这段代码中缺少的部分是 proc_relate_read() 函数的实现。这个函数需要完成从内核间读取信息并将其复制到用户间的功能。 以下是一个可能的实现: static ssize_t proc_relate_read(struct file *file, char __user * out, size_t size, loff_t * off) { struct proc_info *buf = file->private_data; struct task_struct *task; int count = 0; char *tmp_buf; if (*off > 0) { return 0; /* End of file */ } tmp_buf = kmalloc(size, GFP_KERNEL); if (!tmp_buf) { return -ENOMEM; } /* Traverse the process tree and copy information to buffer */ for_each_process(task) { snprintf(tmp_buf + count, size - count, "this is a child,pid is %d\n", task->pid); count += strlen(tmp_buf + count); if (list_empty(&task->children)) { continue; } /* Traverse the children of the current task */ list_for_each_entry(task, &task->children, sibling) { snprintf(tmp_buf + count, size - count, "this is a child,pid is %d\n", task->pid); count += strlen(tmp_buf + count); } } /* Copy buffer to user space */ if (copy_to_user(out, tmp_buf, count)) { kfree(tmp_buf); return -EFAULT; } *off += count; kfree(tmp_buf); return count; } 这个函数使用了 for_each_process() 宏来遍历进程树,并将每个进程的 PID 写入缓冲区。然后,它使用 copy_to_user() 函数将缓冲区的内容复制到用户间。 注意,该实现并不完美。例如,它没有处理缓冲区溢出的情况,并且只返回进程的 PID,而不是更有用的信息。但是,它可以作为一个起点,让你了解如何在内核模块中读取和复制信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值