Binder进程间通信(二)---- 驱动程序初始化

微笑、不失礼 提交于 2020-02-29 11:53:53

Binder驱动程序的初始化是在方法binder_init中,和罗老师列出的代码有些区别,不过内容基本相同。 

static int __init binder_init(void)
{
	int ret;
    //创建一个工作队列
	binder_deferred_workqueue = create_singlethread_workqueue("binder");
	if (!binder_deferred_workqueue)
		return -ENOMEM;
    //创建 /proc/binder 文件夹
	binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
	if (binder_debugfs_dir_entry_root)//创建 /proc/binder/proc文件夹
		binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
						 binder_debugfs_dir_entry_root);
	ret = misc_register(&binder_miscdev);
	if (binder_debugfs_dir_entry_root) {
		debugfs_create_file("state",
				    S_IRUGO,
				    binder_debugfs_dir_entry_root,
				    NULL,
				    &binder_state_fops);
		debugfs_create_file("stats",
				    S_IRUGO,
				    binder_debugfs_dir_entry_root,
				    NULL,
				    &binder_stats_fops);
		debugfs_create_file("transactions",
				    S_IRUGO,
				    binder_debugfs_dir_entry_root,
				    NULL,
				    &binder_transactions_fops);
		debugfs_create_file("transaction_log",
				    S_IRUGO,
				    binder_debugfs_dir_entry_root,
				    &binder_transaction_log,
				    &binder_transaction_log_fops);
		debugfs_create_file("failed_transaction_log",
				    S_IRUGO,
				    binder_debugfs_dir_entry_root,
				    &binder_transaction_log_failed,
				    &binder_transaction_log_fops);
	}
	return ret;
}

device_initcall(binder_init);

    首先在目标设备上创建了一个/proc/binder/proc目录,每一个使用了Binder进程间通信机制的进程在该目录下都对应有一个文件,这些文件是以进程ID来命名的,通过它们就可以读取到各个进程的Binder线程池、Binder实体对象、Binder引用对象以及内核缓冲区等信息。

    然后if语句块又在/proc/binder目录下创建了五个文件state、stats、transactions、transaction_log和failed_transaction_log,通过这五个文件就可以读取到Binder驱动程序的运行状况。例如,各个命令协议(BinderDriverCommandProtocol)和返回协议(BinderDriverReturnProtocol)的请求次数、日志记录信息,以及正在执行进程间通信过程的进程信息等。

    剩下就一个misc_register方法,它是用来创建一个misc类型的字符设备的,这个设备的属性是根据如下参数来决定的

static const struct file_operations binder_fops = {
	.owner = THIS_MODULE,
	.poll = binder_poll,
	.unlocked_ioctl = binder_ioctl,
	.compat_ioctl = binder_ioctl,
	.mmap = binder_mmap,
	.open = binder_open,
	.flush = binder_flush,
	.release = binder_release,
};

static struct miscdevice binder_miscdev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "binder",
	.fops = &binder_fops
};

    Binder驱动程序在目标设备上创建了一个Binder设备文件/dev/binder,这个设备文件的操作方法列表是由全局变量binder_fops指定的。全局变量binder_fops为Binder设备文件/dev/binder指定文件打开、内存映射和IO控制函数分别为binder_open、binder_mmap和binder_ioctl。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!