一、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
评论