一 binder設(shè)備打開的函數(shù)實(shí)現(xiàn) binder_open
路徑:kernel/drivers/android/binder.c
static int binder_open(struct inode *nodp, struct file *filp)
{
struct binder_proc *proc;
binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
current->group_leader->pid, current->pid);
/*分配binder_proc結(jié)構(gòu)體*/
proc = kzalloc(sizeof(*proc), GFP_KERNEL);
if (proc == NULL)
return -ENOMEM;
/*增加線程引用計(jì)數(shù)*/
get_task_struct(current);
proc->tsk = current;
/*初始化todo隊(duì)列深纲,用于存放待處理的請(qǐng)求(server端)*/
INIT_LIST_HEAD(&proc->todo);
/*初始化wait隊(duì)列*甜滨,這個(gè)隊(duì)列用于等待返回結(jié)果(client端)或者等待請(qǐng)求(server端)/
init_waitqueue_head(&proc->wait);
proc->default_priority = task_nice(current);
binder_lock(__func__);
/*類型為BINDER_STAT_PROC對(duì)象的創(chuàng)建個(gè)數(shù)加1*/
binder_stats_created(BINDER_STAT_PROC);
/*將創(chuàng)建的binder_proc鏈入binder_procs的哈希鏈表中*/
hlist_add_head(&proc->proc_node, &binder_procs);
/*記錄當(dāng)前進(jìn)程的pid*/
proc->pid = current->group_leader->pid;
INIT_LIST_HEAD(&proc->delivered_death);
/*將binder_proc存放在filp的private_data域瘪撇,以便于在之后的mmap、ioctl中獲取*/
filp->private_data = proc;
binder_unlock(__func__);
if (binder_debugfs_dir_entry_proc) {
char strbuf[11];
snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
/*創(chuàng)建/sys/kernel/debug/binde/proc/pid文件*/
proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
}
return 0;
}
總的來(lái)說(shuō)丁恭,binder_open
的實(shí)現(xiàn)相對(duì)比較直觀:
- 首先創(chuàng)建了
binder_proc
結(jié)構(gòu)體實(shí)例proc
- 接著開始初始化一系列成員:
tsk
,todo
,wait
,default_priority
,pid
,delivered_death
。 - 更新了統(tǒng)計(jì)數(shù)據(jù):
binder_proc
的創(chuàng)建個(gè)數(shù)加一 - 將
binder_proc
鏈入binder_procs
哈希鏈表中涂屁; - 緊接著將初始化好的
proc
,存放到filp->private_data
中灰伟,以便后續(xù)使用拆又。 - 最后查看是否創(chuàng)建的了
/sys/kernel/debug/binde/proc/
目錄儒旬,有的話再創(chuàng)建一個(gè)/sys/kernel/debug/binde/proc/pid
文件。