免费注册 查看新帖 |

Chinaunix

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

[FreeBSD] freebsd9.2-系统调用机制-注册 [复制链接]

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

在前面简单分析了系统调用机制后,下面看看内核提供的用来注册系统调用的接口,这样就可以编写一个具有特殊功能的系统调用,以满足特定的需要,既然注册的是
系统调用,系统调用属于内核的一部分,所以编写这个系统调用,基本上使用都是内核中的变量,数据结构,函数等等。


内核的这个接口,是以模块的形式来提供的, 当需要注册的系统调用编写完成后,在文件中使用宏SYSCALL_MODULE向内核声明这个模块,然后将相应的源文件编译
成可共享的目标文件(一般是.ko结尾),然后就可以使用外部工具kldload将这个模块加载到内核中。


先来看一个SYSCALL_MODULE宏会用到的一个数据结构:
[struct syscall_module_data对象]:
  1. /********************************************************************************************
  2. * 该数据结构主要由函数syscall_module_handler来使用。

  3.    成员描述:
  4.    chainevh:
  5.    一个函数指针,当模块被加载或者卸载时,如果该函数非空,就会被调用。

  6.    chainarg:
  7.    chainevh成员指向的函数的参数。

  8.    new_sysent:
  9.    指向类型为struct sysent类型的对象,这个数据对象中的成员需要我们来显式初始化,
  10.    用来描述将要注册到内核中的系统调用。
  11.       
  12.    offset:
  13.    指向一个整形变量,该整形变量的值为数组sysent即系统调用方法表的索引,也就是所注册
  14.    系统调用将要使用的系统调用号。

  15.    在数组sysent中,.sy_call成员指向lkmnosys函数的数组元素共有10个,在选择系统调用号
  16.    时,优先选择这些数组元素的索引.

  17.    在数组sysent中,.sy_call成员指向lkmressys函数的数组元素共有50个,这些数组元素的索引
  18.    也能作为所注册系统调用的系统调用号,不过,这些数组元素索引的使用貌似已经约定好了,
  19.    每个索引都有对应的函数,而且这些函数在内核中都有相应的实现,所以即使这些数组元素
  20.    的.sy_call成员指向lkmressys函数,为了避免冲突,尽量不要使用这些数组元素的索引。

  21.    当保存在变量offset中的值为NO_SYSCALL时,内核会为我们选择一个合适的系统调用号。
  22.    #define        NO_SYSCALL (-1)

  23.    
  24.    old_sysent:
  25.    和new_sysent,offset成员结合起来,假设x为所选择的有效的系统调用号,那么当注册完成
  26.    后有:
  27.    old_sysent = sysent[x];
  28.    sysent[x] = *new_sysent;

  29.    offset,old_sysent,new_sysent三个成员可以结合函数syscall_register看会更清楚。
  30.    该函数简要分析如下。


  31.    一般情况下,当把有意义的参数传递给宏SYSCALL_MODULE后,该宏会帮我们声明并初始化
  32.    相关的数据对象。
  33.   ****************************************************************/
  34.    166        struct syscall_module_data {
  35.    167                int        (*chainevh)(struct module *, int, void *); /* next handler */
  36.    168                void        *chainarg;                /* arg for next event handler */
  37.    169                int        *offset;                /* offset into sysent */
  38.    170                struct sysent *new_sysent;        /* new sysent */
  39.    171                struct sysent old_sysent;        /* old sysent */
  40.    172        };
复制代码
[列举部分数组sysent中的元素]:
  1. struct sysent sysent[] = {
  2. ...............................................................................................................
  3. ...............................................................................................................
  4. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 210 = lkmnosys */
  5. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 211 = lkmnosys */
  6. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 212 = lkmnosys */
  7. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 213 = lkmnosys */
  8. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 214 = lkmnosys */
  9. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 215 = lkmnosys */
  10. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 216 = lkmnosys */
  11. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 217 = lkmnosys */
  12. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 218 = lkmnosys */
  13. { AS(nosys_args), (sy_call_t *)lkmnosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 219 = lkmnosys */
  14. ..............................................................................................................
  15. ..............................................................................................................
  16. { AS(nfssvc_args), (sy_call_t *)lkmressys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 155 = nfssvc */
  17. { AS(semsys_args), (sy_call_t *)lkmressys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 169 = semsys */
  18. { AS(msgsys_args), (sy_call_t *)lkmressys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 170 = msgsys */
  19. { AS(shmsys_args), (sy_call_t *)lkmressys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT },        /* 171 = shmsys */
  20. ..............................................................................................................
  21. ..............................................................................................................
  22. };
复制代码
下面将创建并注册一个简单的系统调用来看一下注册过程,这样就会对系统调用有更深刻的理解。

该系统调用名字为cu_printpid,cu_printpid用来输出系统中每个进程的name和pid,以及每个进程的子进程的name和pid,以及内核中
全部struct module对象的name;该系统调用不需要参数,选择的系统调用号为212,系统调用cu_printpid没有对返回值进行处理,对互斥
这方面的处理也不是很到位,所以不是很健壮,不过从测试过程来开,貌似没有出问题,系统调用cu_printpid简单实现如下所示:
[将要注册的系统调用cu_printpid简单分析]:
  1. /***********************************************************
  2. * 1-13:
  3.    需要用到的内核头文件。
  4. ************************************/
  5.      1        #include <sys/ctype.h>
  6.      2        #include <sys/param.h>
  7.      3        #include <sys/kernel.h>
  8.      4        #include <sys/module.h>
  9.      5        #include <sys/proc.h>
  10.      6        #include <sys/sysent.h>
  11.      7        #include <sys/sysproto.h>
  12.      8        #include <sys/systm.h>
  13.      9        #include <sys/lock.h>
  14.     10        #include <sys/sx.h>
  15.     11        #include <sys/mutex.h>
  16.     12        #include <sys/module.h>
  17.     13        #include <sys/linker.h>
  18.     14       
  19. /********************************************************************
  20. * 15-27:
  21.    自定义的函数cu_mod_event,在实现系统调用cu_printpid的模块
  22.    被加载和卸载时,将调用该函数。
  23.    当被加载时,将输出"register syscall"。
  24.    当被卸载时,将输出"unregister syscall"。
  25. *************************/
  26.     15        static int cu_mod_event(struct module *cu_mod,int cu_what,void *cu_data)
  27.     16        {
  28.     17           switch (cu_what) {
  29.     18           case MOD_LOAD:
  30.     19                uprintf("register syscall\n");
  31.     20                break;
  32.     21           case MOD_UNLOAD:
  33.     22                uprintf("unregister syscall\n");
  34.     23                break;
  35.     24           }
  36.     25       
  37.     26           return 0;
  38.     27        }
  39.     28       
  40. /****************************************************************************************
  41. *  29-37:
  42.     自定义的传递给内核函数linker_file_foreach的回调函数。用来输出系统中全部
  43.     module的name。

  44.     这里简单说明一下,当一个模块被加载到内核时,实际上是实现模块的可共享
  45.     目标文件被加载,内核会为该目标文件创建一个struct linker_file类型的数据对象
  46.     来描述该目标文件,所有的struct linker_file对象都链接到以linker_files
  47.     为head的链表上,struct linker_file对象的modules成员链接了包含在该文件
  48.     中的所有module。

  49.     内核函数linker_file_foreach遍历链表linker_files上的每一个struct linker_file对象,
  50.     然后我们这里自定义的回掉函数cu_predicate遍历每个struct linker_file对象的modules成员。
  51. *****************************/
  52.     29        static int cu_predicate(linker_file_t cu_linker_file,void *cu_data)
  53.     30        {
  54.     31           module_t cu_mod;
  55.     32           for (cu_mod = TAILQ_FIRST(&cu_linker_file->modules); cu_mod;cu_mod = module_getfnext(cu_mod)) {
  56.     33                  uprintf("%s\n",module_getname(cu_mod));
  57.     34            }
  58.     35       
  59.     36           return 0;
  60.     37        }
  61.     38       
  62. /************************************************************
  63. * 39:
  64.    cu_index变量是我们这里局部定义的,保存的是系统调用号,
  65.    这里我们选择的系统调用号为212,即显示指定。
  66. *****************************/
  67.     39        int cu_index = 212;
  68.     40       
  69.     41       
  70. /***********************************************************
  71. * 42-44:
  72.    freebsd内核中系统调用实现函数的参数的标准结构。
  73.    这里我们将其定义为struct cu_printpid_args类型。
  74. ************************/
  75.     42        struct cu_printpid_args {
  76.     43            int dummy;
  77.     44        };
  78.     45       
  79. /*************************************************************************
  80. * 实现系统调用cu_printpid的内核函数,将要被注册到内核。

  81.    参数描述:
  82.    cu_td:系统调用异常处理程序将控制传递给实现系统调用cu_printpid的内核
  83.           函数时,cu_td指向当前正在运行线程对应的struct thread对象。
  84.    
  85.    uap:参数,sys_cu_printpid不会使用这个参数。
  86. ***********************/
  87.     46        static int sys_cu_printpid(struct thread *cu_td, struct cu_printpid_args *uap)
  88.     47        {
  89.     48          
  90.     49           struct proc *cu_p;
  91.     50       
  92.     51         
  93. /*************************************************************************************
  94. *  53-66:
  95.     遍历链表allproc。
  96.     链表allproc链接了系统中全部的进程(状态不是PRS_ZOMBIE)。
  97.    
  98.     53:
  99.     链表allproc由共享独占锁allproc_lock保护,为了防止并发访问,先要申请
  100.     加锁。

  101.     55:
  102.     内核提供了宏FOREACH_PROC_IN_SYSTEM来简化对链表allporc的访问,遍历时,
  103.     cu_p指向链接到链表allproc上进程对应的struct proc对象。

  104.     57:
  105.     使用内核函数uprintf输出struct proc对象中的相应成员。这里需要使用函数uprintf,
  106.     内核中对该函数的描述如下:
  107.     Uprintf prints to the controlling terminal for the current process。

  108.     59-63:
  109.     访问进程cu_p的p_children成员,该成员是链表的head,链接了进程cu_p
  110.     的全部子进程,该成员由共享独占锁proctree_lock保护,因此需要先加锁。
  111.   
  112.     66:执行相应的解锁。
  113. *******************************/
  114.     52          
  115.     53           sx_slock(&allproc_lock);
  116.     54       
  117.     55           FOREACH_PROC_IN_SYSTEM(cu_p) {
  118.     56             struct proc *cu_child;
  119.     57             uprintf("My name:%s, My pid:%d\n",cu_p->p_comm,cu_p->p_pid);
  120.     58       
  121.     59             sx_slock(&proctree_lock);     
  122.     60             LIST_FOREACH(cu_child, &cu_p->p_children, p_sibling) {
  123.     61                    uprintf("         process%d's child: [name: %s], [pid: %d]\n",cu_p->p_pid,cu_child->p_comm,cu_child->p_pid);
  124.     62             }
  125.     63             sx_sunlock(&proctree_lock);
  126.     64           }
  127.     65       
  128.     66           sx_sunlock(&allproc_lock);
  129.     67       
  130. /********************************************************************************
  131. * 68:
  132.    调用内核函数linker_file_foreach输出内核中全部模块的name,注意,这里的模块
  133.    是由struct module对象描述的,输出的name为struct module对象name成员指向的
  134.    字符串,该字符串才是模块的实际name。
  135. ******************************************/
  136.     68           linker_file_foreach(cu_predicate,NULL);
  137.     69       
  138. /*****************************************************************
  139. * 70:设置相应的返回值。
  140. ********************************/
  141.     70           cu_td->td_retval[0] = 0;
  142.     71       
  143.     72           return 0;
  144.     73        }
  145.     74       
  146. /****************************************************************
  147. * 75-84:
  148.    这里就是我们静态定义的用来描述系统调用cu_printpid的
  149.    struct sysent对象。
  150. ****************************/
  151.     75        struct sysent cu_printpid_sysent = {
  152.     76            .sy_narg = 0,
  153.     77            .sy_call = (sy_call_t *)sys_cu_printpid,
  154.     78            .sy_auevent = AUE_NULL,
  155.     79            .sy_systrace_args_func = NULL,
  156.     80            .sy_entry = 0,
  157.     81            .sy_return = 0,
  158.     82            .sy_flags = 0,
  159.     83            .sy_thrcnt = 0,
  160.     84        };
  161.     85        SYSCALL_MODULE(cu_printpid_syscall, &cu_index, &cu_printpid_sysent,cu_mod_event, NULL);
复制代码
[宏SYSCALL_MODULE展开后如下所示]:
  1. static struct syscall_module_data cu_printpid_syscall_syscall_mod = {       
  2.     .chainevh = cu_mod_event,
  3.     .chainarg = NULL,
  4.     .offset = &cu_index,
  5.     .new_sysent = &cu_printpid_sysent,                 
  6.     { 0, NULL, AUE_NULL } // old_sysent.sy_narg, old_sysent.sy_call, old_sysent .sy_auevent
  7. };                                                               
  8.                                                                
  9. static moduledata_t cu_printpid_syscall_mod = {               
  10.     .name = "sys/cu_printpid_syscall",                               
  11.     .evhand = syscall_module_handler,                               
  12.     .priv = &cu_printpid_syscall_syscall_mod                       
  13. };                                                               
  14. MODULE_DEPEND(cu_printpid_syscall, kernel, __FreeBSD_version, __FreeBSD_version, MODULE_KERNEL_MAXVER);                       
  15. static struct mod_metadata _mod_metadata_md_cu_printpid_syscall = {
  16.     .md_version = MDT_STRUCT_VERSION,                                       
  17.     .md_type = MDT_MODULE,                                               
  18.     .md_data = &cu_printpid_syscall_mod,                                       
  19.     .md_cval = "cu_printpid_syscall"                                               
  20. };                                                               
  21. DATA_SET(modmetadata_set, _mod_metadata_md_cu_printpid_syscall)       
  22. SYSINIT(cu_printpid_syscallmodule, SI_SUB_SYSCALLS, SI_ORDER_MIDDLE, module_register_init, &cu_printpid_syscall_mod);       
  23. struct __hack
复制代码
从宏SYSCALL_MODULE的展开结果来看,如下:
1:内核对实现系统调用的代码是以模块的方式管理。

2:定义了一个对该模块具体事件进行响应的回调函数。

3:定义并初始化了几个描述模块的数据结构。

4:将类型为struct mod_metadata数据对象的地址保存到name为"set_modmetadata_set"的section中。

5:通过SYSINIT机制来对模块进行相应的初始化。


当实现系统调用cu_printpid的文件被编译成成可共享目标文件cu_printpid.ko时,就可以使用外部工具kldload将其加载到内核,最终都是通过系统调用kldload
来处理的,上面第4步,第5步的工作是由系统调用kldload来处理的,这个系统调用较复杂,要完成符号解析等等很多工作,暂是先不用关心其内部实现。

第四步,会创建一个struct module对象来描述cu_printpid.ko,并调用module_register将该模块注册到内核,该函数的实现可以参考"freebsd9.2-创建elf文件格式对应的struct module对象"
中的描述;第五步,确定cu_printpid.ko中set_sysinit_set节的起始和中止地址,然后依次执行相应的初始化函数,这里只有一个初始化函数,即module_register_init函数,该函数可以参考
"freebsd9.2-对execve系统调用实现的支持-创建execsw数组"中的描述。



现在我们只需要了解函数syscall_module_handler就行了,因为在这里,我们感兴趣的是系统调用对应的struct sysent对象是怎样被insert到系统调用方法表即数组sysent中以及怎样从系统调用
调用方法表中delete的,这两个过程都由函数syscall_module_handler来处理.

[函数syscall_module_handler]:
  1. /******************************************************************************************************
  2. * 参数描述:
  3.    mod:即上面第四步创建的描述cu_printpid.ko的struct module对象,这里忽略。
  4.   
  5.    arg:这个参数比较重要,调用该函数时,传递都是上面定义的cu_printpid_syscall_syscall_mod对象。

  6.         static struct syscall_module_data cu_printpid_syscall_syscall_mod = {       
  7.         .chainevh = cu_mod_event,
  8.         .chainarg = NULL,
  9.         .offset = &cu_index,
  10.         .new_sysent = &cu_printpid_sysent,                 
  11.           { 0, NULL, AUE_NULL } // old_sysent.sy_narg, old_sysent.sy_call, old_sysent .sy_auevent
  12.         };

  13.   153-167:
  14.   加载模块时进行相应的处理。
  15.   
  16.   154:
  17.   调用函数syscall_register完成系统调用的注册,该函数简要分析如下。
  18.   
  19.   161-164:模块相关的处理,这里忽略。

  20.   165-166:
  21.   这里将调用我们自定义的函数cu_mod_event。


  22.   168-182:
  23.   卸载模块时进行相应的处理。

  24.   这里就不分析了,和syscall_register完成的是相反的工作,很容易理解。       
  25. ***************************************************/
  26.    145        int
  27.    146        syscall_module_handler(struct module *mod, int what, void *arg)
  28.    147        {
  29.    148                struct syscall_module_data *data = arg;
  30.    149                modspecific_t ms;
  31.    150                int error;
  32.    151       
  33.    152                switch (what) {
  34.    153                case MOD_LOAD:
  35.    154                        error = syscall_register(data->offset, data->new_sysent,
  36.    155                            &data->old_sysent);
  37.    156                        if (error) {
  38.    157                                /* Leave a mark so we know to safely unload below. */
  39.    158                                data->offset = NULL;
  40.    159                                return (error);
  41.    160                        }
  42.    161                        ms.intval = *data->offset;
  43.    162                        MOD_XLOCK;
  44.    163                        module_setspecific(mod, &ms);
  45.    164                        MOD_XUNLOCK;
  46.    165                        if (data->chainevh)
  47.    166                                error = data->chainevh(mod, what, data->chainarg);
  48.    167                        return (error);
  49.    168                case MOD_UNLOAD:
  50.    169                        /*
  51.    170                         * MOD_LOAD failed, so just return without calling the
  52.    171                         * chained handler since we didn't pass along the MOD_LOAD
  53.    172                         * event.
  54.    173                         */
  55.    174                        if (data->offset == NULL)
  56.    175                                return (0);
  57.    176                        if (data->chainevh) {
  58.    177                                error = data->chainevh(mod, what, data->chainarg);
  59.    178                                if (error)
  60.    179                                        return error;
  61.    180                        }
  62.    181                        error = syscall_deregister(data->offset, &data->old_sysent);
  63.    182                        return (error);
  64.    183                default:
  65.    184                        if (data->chainevh)
  66.    185                                return (data->chainevh(mod, what, data->chainarg));
  67.    186                        return (EOPNOTSUPP);
  68.    187                }
  69.    188       
  70.    189                /* NOTREACHED */
  71.    190        }
复制代码
[函数syscall_register]:
  1. /*********************************************************************************************************
  2. * 参数描述:
  3.    offset:
  4.    指向的整形变量包含了所注册的系统调用将要使用的系统调用号,这里我们选择的是212.
  5.   
  6.    new_sysent:
  7.    描述所注册系统调用的struct sysent对象,这里我们要注册的系统调用为cu_printpid,其对应的struct sysent
  8.    对象如下:
  9.     60        struct sysent cu_printpid_sysent = {
  10.     61            .sy_narg = 0,
  11.     62            .sy_call = (sy_call_t *)sys_cu_printpid,
  12.     63            .sy_auevent = AUE_NULL,
  13.     64            .sy_systrace_args_func = NULL,
  14.     65            .sy_entry = 0,
  15.     66            .sy_return = 0,
  16.     67            .sy_flags = 0,
  17.     68            .sy_thrcnt = 0,
  18.     69        };

  19.     old_sysent:
  20.     如果非空,将把之前数组元素sysent[212]的值保存到old_sysent指向的数据对象中,从我们的注册
  21.     过程来看,该参数非空。
  22. *****************************************/               
  23.    106        int
  24.    107        syscall_register(int *offset, struct sysent *new_sysent,
  25.    108            struct sysent *old_sysent)
  26.    109        {
  27.    110                int i;
  28.    111       
  29. /*********************************************************************************************
  30. * 112-123:
  31.    结合系统调用方法表即数组sysent看会更清楚。
  32.    112-119:系统调用号是NO_SYSCALL,此时由内核自动选择一个。
  33.   
  34.    119-120:系统调用号无效时的处理。

  35.    121-123:系统调用号已经显示指定,检查对象sysent[*offset]的sy_call成员。如果该成员
  36.             既不是lkmnosys也不是lkmressys,那么将不能使用该系统调用号,因为此时该系统
  37.             调用号已经被使用。

  38.    从这里的处理来看,选择未被使用的系统调用号最好的办法就是结合系统调用方法表即数组sysent。
  39. *************************************.
  40.    112                if (*offset == NO_SYSCALL) {
  41.    113                        for (i = 1; i < SYS_MAXSYSCALL; ++i)
  42.    114                                if (sysent[i].sy_call == (sy_call_t *)lkmnosys)
  43.    115                                        break;
  44.    116                        if (i == SYS_MAXSYSCALL)
  45.    117                                return (ENFILE);
  46.    118                        *offset = i;
  47.    119                } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
  48.    120                        return (EINVAL);
  49.    121                else if (sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
  50.    122                    sysent[*offset].sy_call != (sy_call_t *)lkmressys)
  51.    123                        return (EEXIST);
  52.    124       
  53.    125                KASSERT(sysent[*offset].sy_thrcnt == SY_THR_ABSENT,
  54.    126                    ("dynamic syscall is not protected"));
  55. /****************************************************************************************
  56. * 127:
  57.    此时先将数组元素sysent[212]的旧值保存到old_sysent指向的对象中,在实现系统调用的模块
  58.    被卸载时,恢复使用。

  59.    128-130:
  60.    执行完后,数组元素sysent[212]的值已经被成功更新,此时如果执行系统调用,系统调用号
  61.    为212时,将调用函数sys_cu_printpid。
  62. **************************************/
  63.    127                *old_sysent = sysent[*offset];
  64.    128                new_sysent->sy_thrcnt = SY_THR_ABSENT;
  65.    129                sysent[*offset] = *new_sysent;
  66.    130                atomic_store_rel_32(&sysent[*offset].sy_thrcnt, 0);
  67.    131                return (0);
  68.    132        }
复制代码
下来我们测试一下。

测试系统是freebsd9.2,需要安装源代码树,系统版本不一样的话,内核相关宏或函数的实现有可能不同,所以这里测试的系统调用cu_printpid有可能不能正常运行,大家测试如果出现问题的话,及时联系一下,
还有一个,就是注意一下权限。

创建一个测试使用的目录,这里为/71-kernel,将系统调用cu_printpid的源代码保存到文件cu_printpid.c中,该文件的文件名可以任意,只要保证和Makefile文件中一致就行,文件cu_printpid.c
和Makefile文件要在一个目录下,这里在/71-kernel下:
  1. root@:/71-kernel # ls
  2. Makefile        cu_printpid.c
复制代码
Makefile文件内容如下:
  1. root@:/71-kernel # cat  Makefile
  2. KMOD    = cu_printpid
  3. SRCS    =  cu_printpid.c
  4. .include <bsd.kmod.mk>
复制代码
然后执行make,输出如下:
  1. root@:/71-kernel # make
  2. Warning: Object directory not changed from original /71-kernel
  3. @ -> /usr/src/sys
  4. machine -> /usr/src/sys/i386/include
  5. x86 -> /usr/src/sys/x86/include
  6. cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc   -I. -I@ -I@/contrib/altq -finline-limit=8000 --param inline-unit-growth=100 --param large-function-growth=1000 -fno-common   -mno-align-long-strings -mpreferred-stack-boundary=2 -mno-mmx -mno-sse -msoft-float -ffreestanding -fstack-protector -std=iso9899:1999 -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual  -Wundef -Wno-pointer-sign -fformat-extensions  -Wmissing-include-dirs -fdiagnostics-show-option   -c cu_printpid.c
  7. ld  -d -warn-common -r -d -o cu_printpid.kld cu_printpid.o
  8. :> export_syms
  9. awk -f /sys/conf/kmod_syms.awk cu_printpid.kld  export_syms | xargs -J% objcopy % cu_printpid.kld
  10. ld -Bshareable  -d -warn-common -o cu_printpid.ko cu_printpid.kld
  11. objcopy --strip-debug cu_printpid.ko
复制代码
make完后,/71-kernel目录内容如下:
  1. root@:/71-kernel # ls
  2. @                Makefile        cu_printpid.c        cu_printpid.kld        cu_printpid.ko        cu_printpid.o        export_syms        machine                x86
复制代码
可以看到,已经成功创建了相应的模块cu_printpid.ko,加载前:
  1. root@:/71-kernel # kldstat
  2. Id Refs Address    Size     Name
  3. 1    1 0xc0400000 1205064  kernel
复制代码
加载后:
  1. root@:/71-kernel # kldload /71-kernel/cu_printpid.ko
  2. register syscall
  3. root@:/71-kernel # kldstat
  4. Id Refs Address    Size     Name
  5. 1    3 0xc0400000 1205064  kernel
  6. 2    1 0xcb65b000 2000     cu_printpid.ko
复制代码
此时我们编写一个测试用的程序,因为系统调用cu_printpid是我们自己实现并注册到内核的,所以就得我们发出一个"int 0x80"软中断.

测试程序很简单,使用内联汇编来完成,文件名为cu_syscall.c,这里的212就是系统调用cu_printpid的系统调用号,我们将其保存到EAX寄存器中:
  1. root@:/71-kernel # cat cu_syscall.c
  2. int main(void)
  3. {
  4.    __asm __volatile(
  5.            "movl $212,%eax;"
  6.            "int $0x80;"
  7.   );
  8. }
复制代码
编译并运行,输出很多,省略了一些模块名,注意一下输出中最后一个模块名"sys/cu_printpid_syscall",和上面kldstat的输出不同,因为系统调用
kldstat访问的不是struct module对象:
  1. root@:/71-kernel # gcc -o cu cu_syscall.c
  2. root@:/71-kernel # ./cu
  3. My name:cu, My pid:3126
  4. My name:more, My pid:2907
  5. My name:csh, My pid:2739
  6. My name:sshd, My pid:2737
  7.          process2737's child: [name: csh], [pid: 2739]
  8. My name:csh, My pid:2335
  9. My name:sshd, My pid:2333
  10.          process2333's child: [name: csh], [pid: 2335]
  11. My name:csh, My pid:2292
  12. My name:sshd, My pid:2290
  13.          process2290's child: [name: csh], [pid: 2292]
  14. My name:csh, My pid:2278
  15. My name:sshd, My pid:2276
  16.          process2276's child: [name: csh], [pid: 2278]
  17. My name:more, My pid:2095
  18. My name:cat, My pid:2094
  19. My name:csh, My pid:1500
  20.          process1500's child: [name: cu], [pid: 3126]
  21. My name:sshd, My pid:1498
  22.          process1498's child: [name: csh], [pid: 1500]
  23. My name:csh, My pid:1485
  24.          process1485's child: [name: more], [pid: 2907]
  25. My name:sshd, My pid:1483
  26.          process1483's child: [name: csh], [pid: 1485]
  27. My name:csh, My pid:1443
  28. My name:sshd, My pid:1441
  29.          process1441's child: [name: csh], [pid: 1443]
  30. My name:csh, My pid:1439
  31. My name:sshd, My pid:1437
  32.          process1437's child: [name: csh], [pid: 1439]
  33. My name:csh, My pid:1435
  34.          process1435's child: [name: more], [pid: 2095]
  35.          process1435's child: [name: cat], [pid: 2094]
  36. My name:sshd, My pid:1433
  37.          process1433's child: [name: csh], [pid: 1435]
  38. My name:csh, My pid:1228
  39. My name:sshd, My pid:1226
  40.          process1226's child: [name: csh], [pid: 1228]
  41. My name:csh, My pid:1157
  42. My name:sshd, My pid:1155
  43.          process1155's child: [name: csh], [pid: 1157]
  44. My name:getty, My pid:1154
  45. My name:getty, My pid:1153
  46. My name:getty, My pid:1152
  47. My name:getty, My pid:1151
  48. My name:getty, My pid:1150
  49. My name:getty, My pid:1149
  50. My name:getty, My pid:1148
  51. My name:getty, My pid:1147
  52. My name:cron, My pid:1113
  53. My name:sendmail, My pid:1109
  54. My name:sendmail, My pid:1106
  55. My name:sshd, My pid:1103
  56.          process1103's child: [name: sshd], [pid: 2737]
  57.          process1103's child: [name: sshd], [pid: 2333]
  58.          process1103's child: [name: sshd], [pid: 2290]
  59.          process1103's child: [name: sshd], [pid: 2276]
  60.          process1103's child: [name: sshd], [pid: 1498]
  61.          process1103's child: [name: sshd], [pid: 1483]
  62.          process1103's child: [name: sshd], [pid: 1441]
  63.          process1103's child: [name: sshd], [pid: 1437]
  64.          process1103's child: [name: sshd], [pid: 1433]
  65.          process1103's child: [name: sshd], [pid: 1226]
  66.          process1103's child: [name: sshd], [pid: 1155]
  67. My name:syslogd, My pid:1002
  68. My name:devd, My pid:883
  69. My name:moused, My pid:866
  70. My name:dhclient, My pid:849
  71. My name:dhclient, My pid:809
  72. My name:softdepflush, My pid:17
  73. My name:syncer, My pid:16
  74. My name:vnlru, My pid:15
  75. My name:bufdaemon, My pid:9
  76. My name:pagezero, My pid:8
  77. My name:vmdaemon, My pid:7
  78. My name:pagedaemon, My pid:6
  79. My name:xpt_thrd, My pid:5
  80. My name:sctp_iterator, My pid:4
  81. My name:fdc0, My pid:3
  82. My name:usb, My pid:14
  83. My name:mpt_recovery0, My pid:2
  84. My name:geom, My pid:13
  85. My name:intr, My pid:12
  86. My name:idle, My pid:11
  87. My name:init, My pid:1
  88.          process1's child: [name: getty], [pid: 1154]
  89.          process1's child: [name: getty], [pid: 1153]
  90.          process1's child: [name: getty], [pid: 1152]
  91.          process1's child: [name: getty], [pid: 1151]
  92.          process1's child: [name: getty], [pid: 1150]
  93.          process1's child: [name: getty], [pid: 1149]
  94.          process1's child: [name: getty], [pid: 1148]
  95.          process1's child: [name: getty], [pid: 1147]
  96.          process1's child: [name: cron], [pid: 1113]
  97.          process1's child: [name: sendmail], [pid: 1109]
  98.          process1's child: [name: sendmail], [pid: 1106]
  99.          process1's child: [name: sshd], [pid: 1103]
  100.          process1's child: [name: syslogd], [pid: 1002]
  101.          process1's child: [name: devd], [pid: 883]
  102.          process1's child: [name: moused], [pid: 866]
  103.          process1's child: [name: dhclient], [pid: 809]
  104.          process1's child: [name: dhclient], [pid: 849]
  105. My name:audit, My pid:10
  106. My name:kernel, My pid:0
  107.          process0's child: [name: softdepflush], [pid: 17]
  108.          process0's child: [name: syncer], [pid: 16]
  109.          process0's child: [name: vnlru], [pid: 15]
  110.          process0's child: [name: bufdaemon], [pid: 9]
  111.          process0's child: [name: pagezero], [pid: 8]
  112.          process0's child: [name: vmdaemon], [pid: 7]
  113.          process0's child: [name: pagedaemon], [pid: 6]
  114.          process0's child: [name: xpt_thrd], [pid: 5]
  115.          process0's child: [name: sctp_iterator], [pid: 4]
  116.          process0's child: [name: fdc0], [pid: 3]
  117.          process0's child: [name: usb], [pid: 14]
  118.          process0's child: [name: mpt_recovery0], [pid: 2]
  119.          process0's child: [name: geom], [pid: 13]
  120.          process0's child: [name: intr], [pid: 12]
  121.          process0's child: [name: idle], [pid: 11]
  122.          process0's child: [name: init], [pid: 1]
  123.          process0's child: [name: audit], [pid: 10]
  124. if_lo
  125. newreno
  126. elf32
  127. shell
  128. ..........................................................................
  129. ..........................................................................
  130. sys/cu_printpid_syscall
复制代码
为了方便大家测试,未加注释的系统调用cu_printpid代码如下:
  1. #include <sys/ctype.h>
  2. #include <sys/param.h>
  3. #include <sys/kernel.h>
  4. #include <sys/module.h>
  5. #include <sys/proc.h>
  6. #include <sys/sysent.h>
  7. #include <sys/sysproto.h>
  8. #include <sys/systm.h>
  9. #include <sys/lock.h>
  10. #include <sys/sx.h>
  11. #include <sys/mutex.h>
  12. #include <sys/module.h>
  13. #include <sys/linker.h>

  14. static int cu_mod_event(struct module *cu_mod,int cu_what,void *cu_data)
  15. {
  16.    switch (cu_what) {
  17.    case MOD_LOAD:
  18.         uprintf("register syscall\n");
  19.         break;
  20.    case MOD_UNLOAD:
  21.         uprintf("unregister syscall\n");
  22.         break;
  23.    }

  24.    return 0;
  25. }

  26. static int cu_predicate(linker_file_t cu_linker_file,void *cu_data)
  27. {
  28.    module_t cu_mod;
  29.    for (cu_mod = TAILQ_FIRST(&cu_linker_file->modules); cu_mod;cu_mod = module_getfnext(cu_mod)) {
  30.           uprintf("%s\n",module_getname(cu_mod));
  31.     }

  32.    return 0;
  33. }

  34. int cu_index = 212;


  35. struct cu_printpid_args {
  36.     int dummy;
  37. };

  38. static int sys_cu_printpid(struct thread *cu_td, struct cu_printpid_args *uap)
  39. {
  40.   
  41.    struct proc *cu_p;


  42.    
  43.    sx_slock(&allproc_lock);

  44.    FOREACH_PROC_IN_SYSTEM(cu_p) {
  45.      struct proc *cu_child;
  46.      uprintf("My name:%s, My pid:%d\n",cu_p->p_comm,cu_p->p_pid);

  47.      sx_slock(&proctree_lock);     
  48.      LIST_FOREACH(cu_child, &cu_p->p_children, p_sibling) {
  49.             uprintf("         process%d's child: [name: %s], [pid: %d]\n",cu_p->p_pid,cu_child->p_comm,cu_child->p_pid);
  50.      }
  51.      sx_sunlock(&proctree_lock);
  52.    }

  53.    sx_sunlock(&allproc_lock);

  54.    linker_file_foreach(cu_predicate,NULL);

  55.    cu_td->td_retval[0] = 0;

  56.    return 0;
  57. }

  58. struct sysent cu_printpid_sysent = {
  59.     .sy_narg = 0,
  60.     .sy_call = (sy_call_t *)sys_cu_printpid,
  61.     .sy_auevent = AUE_NULL,
  62.     .sy_systrace_args_func = NULL,
  63.     .sy_entry = 0,
  64.     .sy_return = 0,
  65.     .sy_flags = 0,
  66.     .sy_thrcnt = 0,
  67. };
  68. SYSCALL_MODULE(cu_printpid_syscall, &cu_index, &cu_printpid_sysent,cu_mod_event, NULL);
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-12-21 01:51 |只看该作者
写的不错,比我写的好
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP