让我们看一下另一个write(2)例程并看看它是如何进行预处理的。
[root@plaguez kernel]# cat no2.c #include #include #include #include #include #include #include #include #include _syscall3(ssize_t,write,int,fd,const void *,buf,size_t,count);/*构建一个write调用*/ main() { char *t = "this is a test.\n"; write(0, t, strlen(t)); } [root@plaguez kernel]# gcc -E no2.c > no2.C [root@plaguez kernel]# indent no2.C -kr indent:no2.C:3304: Warning: old style assignment ambiguity in "=-". Assuming "= -" [root@plaguez kernel]# tail -n 50 no2.C#9 "no2.c" 2 ssize_t write(int fd, const void *buf, size_t count) { long __res; __asm__ __volatile("int $0x80":"=a"(__res):"0"(4), "b"((long) (fd)),
"c"((long) (buf)), "d"((long) (count))); if (__res >= 0) return (ssize_t) __res; errno = -__res; return -1; }; main() { char *t = "this is a test.\n"; write(0, t, strlen(t)); } [root@plaguez kernel]# exit
|
注意那个write()里的"0"这个参数匹配SYS_write,在/usr/include/sys/syscall.h中定义。
* 构建你自己的系统调用。
这里给出了几个构建你自己的系统调用的方法。举个例子, 你可以修改内核代码并且加入你自己的代码。一个比较简单可行的方法, 不过, 一定要被写成可加载内核模块。
没有一个代码可以象可加载内核模块那样可以当内核需要的时候被随时加载的。
我们的主要意图是需要一个很小的内核,当我们需要的时候运行insmod命令,给定的驱动就可以被自动加载。这样卸除来的lkm程序一定比在内核代码树里写代码要简单易行多了。
* 写lkm程序
一个lkm程序可以用c来很容易编写出来。它包含了大量的 #defines, 一些函数, 一个初始化模块的函数,叫做init_module(),和一个卸载函数:cleanup_module()。 |