免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2387 | 回复: 0
打印 上一主题 下一主题

[FreeBSD] freebsd9.2-对execve系统调用实现的支持-创建execsw数组 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-26 15:12 |只看该作者 |倒序浏览
本帖最后由 71v5 于 2014-06-26 15:13 编辑

[module_register_init]-初始化execsw数组:
  1. /*********************************************************************
  2. * static moduledata_t elf32_mod = {
  3.          "elf32", //name成员
  4.          elf32_modevent,//evhand成员
  5.          (void *)&elf32_execsw //priv成员
  6.    };

  7.    参数描述:
  8.    arg:&elf32_mod。
  9. *********************************/

  10.    109        void
  11.    110        module_register_init(const void *arg)
  12.    111        {
  13.    112                const moduledata_t *data = (const moduledata_t *)arg;
  14.    113                int error;
  15.    114                module_t mod;
  16.    115       
  17. /**********************************************************************************
  18. * "9.2-可执行文件格式-向内核注册-阶段01"中已经创建了name为"elf32"的
  19.     struct module对象,这里简记为elf32_module,并且已经对其成员进行了
  20.     初始化,也将其链接到了对应的链表上。

  21.     117-118:此时mod指向elf32_module。

  22.     #define MOD_EVENT(mod, type)        (mod)->handler((mod), (type), (mod)->arg)

  23.     123:这里的调用为elf32_modevent(mod,MOD_LOAD,&elf32_execsw);
  24.          宏EXEC_SET展开过程定义了函数elf32_modevent,一般情况下,该函数可以简化为
  25.          下面的函数调用:
  26.           exec_register(&elf32_execsw);

  27.     124-132:elf32_modevent函数执行失败。

  28.     133-147:elf32_modevent执行成功,进行一些无关紧要的处理。
  29. **********************************************/   
  30.    116                mtx_lock(&Giant);
  31.    117                MOD_SLOCK;
  32.    118                mod = module_lookupbyname(data->name);
  33.    119                if (mod == NULL)
  34.    120                        panic("module_register_init: module named %s not found\n",
  35.    121                            data->name);
  36.    122                MOD_SUNLOCK;
  37.    123                error = MOD_EVENT(mod, MOD_LOAD);
  38.    124                if (error) {
  39.    125                        MOD_EVENT(mod, MOD_UNLOAD);
  40.    126                        MOD_XLOCK;
  41.    127                        module_release(mod);
  42.    128                        MOD_XUNLOCK;
  43.    129                        printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
  44.    130                            " %d\n", data->name, (void *)data->evhand, data->priv,
  45.    131                            error);
  46.    132                } else {
  47.    133                        MOD_XLOCK;
  48.    134                        if (mod->file) {
  49.    135                                /*
  50.    136                                 * Once a module is succesfully loaded, move
  51.    137                                 * it to the head of the module list for this
  52.    138                                 * linker file.  This resorts the list so that
  53.    139                                 * when the kernel linker iterates over the
  54.    140                                 * modules to unload them, it will unload them
  55.    141                                 * in the reverse order they were loaded.
  56.    142                                 */
  57.    143                                TAILQ_REMOVE(&mod->file->modules, mod, flink);
  58.    144                                TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
  59.    145                        }
  60.    146                        MOD_XUNLOCK;
  61.    147                }
  62.    148                mtx_unlock(&Giant);
  63.    149        }
复制代码
[exec_register函数]:
  1. /*****************************************************************************************
  2. * 该函数主要初始化非常重要的指针数组execsw,execve系统调用会使用该数组。
  3.    static const struct execsw **execsw

  4.    函数exec_register没有社会么特别重要的操作,就是初始化数组execsw,这里不做过多的分析。

  5.    这里是以elf可执行文件格式来分析其初始化过程的,其它可执行文件格式类似,当全部都完成
  6.    了初始化后,非常重要的数组execsw被建立,这里数组元素的顺序不是很清楚,但是不影响说明
  7.    问题,就假设为下面的顺序:
  8.    execsw[0] = &linux_execsw;
  9.    execsw[1] = &coff_execsw;
  10.    execsw[2] = &gzip_execsw;
  11.    execsw[3] = &aout_execsw;
  12.    execsw[4] = &shell_execsw;
  13.    execsw[5] = &svr4_execsw;
  14.    execsw[6] = &elf32_execsw;
  15. ******************************************/
  16.   
  17.   1458        /*
  18.   1459         * Exec handler registration
  19.   1460         */
  20.   1461        int
  21.   1462        exec_register(execsw_arg)
  22.   1463                const struct execsw *execsw_arg;
  23.   1464        {
  24.   1465                const struct execsw **es, **xs, **newexecsw;
  25.   1466                int count = 2;        /* New slot and trailing NULL */
  26.   1467       
  27.   1468                if (execsw)
  28.   1469                        for (es = execsw; *es; es++)
  29.   1470                                count++;
  30.   1471                newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
  31.   1472                if (newexecsw == NULL)
  32.   1473                        return (ENOMEM);
  33.   1474                xs = newexecsw;
  34.   1475                if (execsw)
  35.   1476                        for (es = execsw; *es; es++)
  36.   1477                                *xs++ = *es;
  37.   1478                *xs++ = execsw_arg;
  38.   1479                *xs = NULL;
  39.   1480                if (execsw)
  40.   1481                        free(execsw, M_TEMP);
  41.   1482                execsw = newexecsw;
  42.   1483                return (0);
  43.   1484        }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP