登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

ideality

@linux c 编程@

 
 
 

日志

 
 
 
 

(三)一个用户程序来执行和测试(二)的system call  

2010-02-07 08:40:27|  分类: freebsd |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
    一、modfind 函数
modfind 函数根据模块的名字返回这个模块的识别线(modid)。
#include <sys/param.h>
#include <sys/module.h>
int modfind(const char *modname);
其返回值modid是一个整数,能唯一的标识每一个登录进系统的模块。
       二、modstat 函数
modstat 函数根据modid返回核心模块的状态。
#include <sys/param.h>
#include <sys/module.h>
int modstat(int modid,struct module_stat *stat);其返回值被存储在一个module_stat的结构体 stat 中,其被定义的头函数在<sys/module.h>中如下:
struct module_stat {
          int            version;
          char         name[MAXMODNAME];        /*module name*/
          int            refs;                            /*number of references*/
          int            id;                             /*module id number*/
          modspecific_t  data;                  /*module specific data*/
};
typedef union modspecific {
            int                intval;        /*offset value*/
            u_int            uintval;
            long             longval;
            u_long         ulongval;
} modspecific_t;
       三、syscall 函数
syscall 函数通过她的system call number来执行the system call.
#include <sys/syscall.h>
#include <unistd.h>
int syscall(int number,...);
        四、执行system call
#include <stdio.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/module.h>
int main(int argc,char *argv[])
{
        int  syscall_num;
        struct module_stat stat;
        if  (argc != 2) {
                      printf("usage:\n%s <string>\n",argv[0]);
                      exit(0);
         }
         /*determine sc_example's offset value.*/
         stat.version = sizeof(stat);
         modstat(modfind("sc_example"),&stat);
         syscall_num = stat.data.intval;
         /*call sc_example.*/
         return(syscall(syscall_num,argv[1]));

}
                   /*interface.c*/
执行:
$:./interface  hello,\kernel!
$:dmesg | tail -n 1
执行 system call without c code
$:sudo kldload ./sc_example.ko
$:perl -e 'str = "hello,kernel!";' -e 'syscall(210,$str);'
 
$:dmesg | tail -n 1
 
  评论这张
 
阅读(608)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018