Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616999
  • 博文数量: 69
  • 博客积分: 1891
  • 博客等级: 上尉
  • 技术积分: 1359
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-20 23:38
文章分类

全部博文(69)

文章存档

2012年(46)

2011年(23)

分类: LINUX

2012-02-15 21:22:45

search_binary_handler的作用是遍历二进制格式handler列表,寻找合适的handler。
  1. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
其核心代码如下:
  1. for (try=0; try<2; try++) {
  2.         read_lock(&binfmt_lock);
  3.         list_for_each_entry(fmt, &formats, lh) {
  4.             int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  5.             if (!fn)
  6.                 continue;
  7.             if (!try_module_get(fmt->module))
  8.                 continue;
  9.             read_unlock(&binfmt_lock);
  10.             retval = fn(bprm, regs);
  11.             /*
  12.              * Restore the depth counter to its starting value
  13.              * in this call, so we don't have to rely on every
  14.              * load_binary function to restore it on return.
  15.              */
  16.             bprm->recursion_depth = depth;
  17.             if (retval >= 0) {
  18.                 if (depth == 0)
  19.                     tracehook_report_exec(fmt, bprm, regs);
  20.                 put_binfmt(fmt);
  21.                 allow_write_access(bprm->file);
  22.                 if (bprm->file)
  23.                     fput(bprm->file);
  24.                 bprm->file = NULL;
  25.                 current->did_exec = 1;
  26.                 proc_exec_connector(current);
  27.                 return retval;
  28.             }
  29.             read_lock(&binfmt_lock);
  30.             put_binfmt(fmt);
  31.             if (retval != -ENOEXEC || bprm->mm == NULL)
  32.                 break;
  33.             if (!bprm->file) {
  34.                 read_unlock(&binfmt_lock);
  35.                 return retval;
  36.             }
  37.         }
  38.         read_unlock(&binfmt_lock);
  39.         if (retval != -ENOEXEC || bprm->mm == NULL) {
  40.             break;
  41.         }
  42.     }
  43.     return retval;
  44. }
1、外层是一个循环,for (try=0; try<2; try++) 貌似是重试两次的意思,具体为什么是两次,暂时还没搞明白(貌似是待模块加载之后再遍历一次,还不是很明白,囧)
2、里层通过list_for_each_entry(fmt, &formats, lh)遍历二进制格式列表,寻找合适的handler
  formats的基本结构如下:
  1. struct linux_binfmt {
  2.     struct list_head lh;
  3.     struct module *module;
  4.     int (*load_binary)(struct linux_binprm *, struct pt_regs * regs);
  5.     int (*load_shlib)(struct file *);
  6.     int (*core_dump)(struct coredump_params *cprm);
  7.     unsigned long min_coredump;    /* minimal dump size */
  8. };
其中,load_binary用于加载普通程序
load_shlib用于加载共享库
core_dump用于在程序出错的情况下输出内存转储。

3、fn= fmt->load_binary,  retval = fn(bprm, regs);用于加载传入的文件,
阅读(4541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~