search_binary_handler的作用是遍历二进制格式handler列表,寻找合适的handler。
- int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
其核心代码如下:
- for (try=0; try<2; try++) {
-
read_lock(&binfmt_lock);
-
list_for_each_entry(fmt, &formats, lh) {
-
int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
-
if (!fn)
-
continue;
-
if (!try_module_get(fmt->module))
-
continue;
-
read_unlock(&binfmt_lock);
-
retval = fn(bprm, regs);
-
/*
-
* Restore the depth counter to its starting value
-
* in this call, so we don't have to rely on every
-
* load_binary function to restore it on return.
-
*/
-
bprm->recursion_depth = depth;
-
if (retval >= 0) {
-
if (depth == 0)
-
tracehook_report_exec(fmt, bprm, regs);
-
put_binfmt(fmt);
-
allow_write_access(bprm->file);
-
if (bprm->file)
-
fput(bprm->file);
-
bprm->file = NULL;
-
current->did_exec = 1;
-
proc_exec_connector(current);
-
return retval;
-
}
-
read_lock(&binfmt_lock);
-
put_binfmt(fmt);
-
if (retval != -ENOEXEC || bprm->mm == NULL)
-
break;
-
if (!bprm->file) {
-
read_unlock(&binfmt_lock);
-
return retval;
-
}
-
}
-
read_unlock(&binfmt_lock);
-
if (retval != -ENOEXEC || bprm->mm == NULL) {
-
break;
-
}
-
}
-
return retval;
-
}
1、外层是一个循环,for (try=0; try<2; try++) 貌似是重试两次的意思,具体为什么是两次,暂时还没搞明白(貌似是待模块加载之后再遍历一次,还不是很明白,囧)
2、里层通过list_for_each_entry(fmt, &formats, lh)遍历二进制格式列表,寻找合适的handler
formats的基本结构如下:
- struct linux_binfmt {
-
struct list_head lh;
-
struct module *module;
-
int (*load_binary)(struct linux_binprm *, struct pt_regs * regs);
-
int (*load_shlib)(struct file *);
-
int (*core_dump)(struct coredump_params *cprm);
-
unsigned long min_coredump; /* minimal dump size */
-
};
其中,load_binary用于加载普通程序
load_shlib用于加载共享库
core_dump用于在程序出错的情况下输出内存转储。
3、fn= fmt->load_binary, retval = fn(bprm, regs);用于加载传入的文件,
阅读(4605) | 评论(0) | 转发(0) |