Linux的内核模块.doc_第1页
Linux的内核模块.doc_第2页
Linux的内核模块.doc_第3页
Linux的内核模块.doc_第4页
Linux的内核模块.doc_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验5 Linux的内核模块邢卫 2008-11-12 修订实验目的学习如何编写Linux中的内核模块程序,如何安装和卸载;初步了解Linux内核模块的实现机理;学习虚拟文件系统。实验内容1. 学习内核模块的编写、编译、安装、使用、卸载过程2. 学习通过内核模块,增加虚拟文件系统 /proc的功能3. 学习虚拟文件系统的初步概念和使用4. 选做:通过阅读Linux内核源程序,学习Linux内核模块的实现机制实验步骤一、编写一个简单的内核模块 参阅边干边学第4.1.4节 1以stu帐号或者root帐号,创建模块helloworld1) 编辑 vi helloworld.cl 在源程序中增加一行:MODULE_LICENSE(GPL);2) 编译 gcc DMODULE c helloworld.c2以root帐号完成模块的安装和卸载1) 查看系统中现有的内核模块的安装情况使用 lsmod 或者 cat /proc/modules 命令2) 安装内核模块insmod helloworld.o若遇到版本不匹配问题,可使用 insmod -f helloworld.o3) 查看内核模块的安装情况,验证安装成功4) 卸载内核模块 rmmod helloworld5) 再次查看内核模块的安装情况,验证卸载成功二、通过内核模块在 /proc文件系统中增加应用 参阅边干边学第4.3.2节 1以stu帐号或者root帐号,创建模块proc_example1) 编辑 vi proc_example.cl 第15行,修改为:#define FOOBAR_LEN 255l 第197行,修改为:MODULE_LICENSE(GPL);2) 编译gcc DMODULE D_KERNEL_ I/usr/src/linux/include c proc_example.c2以root帐号完成模块的安装和卸载,以stu帐号完成使用1) (以root帐号)准备工作:确认内核源代码目录树中存在恰当的头文件# cd /usr/src# tar zxvf linux-2.4.18.tar.gz# cd linux# make config均选缺省选项(一直“回车”)2) 查看系统中现有的内核模块的安装情况使用 lsmod 或者 cat /proc/modules 命令3) (以root帐号)安装内核模块insmod proc_example.o若遇到版本不匹配问题,可使用 insmod -f proc_example.o4) 查看内核模块的安装情况,验证安装成功5) 使用 /proc/proc_example 中的虚拟文件l cd /procl 使用 ls l 验证存在 proc_example目录l cd proc_examplel 使用 ls l 验证存在foo,bar,jiffies等文件l 使用cat foo和cat bar命令查看文件内容l (以root帐号)使用vi foo命令修改foo文件内容,注意文件总大小不要超过200字节长度l 再次使用cat foo命令查看foo文件内容l 反复使用cat jiffies命令查看jiffies文件内容,注意观察系统时钟的变化l 其他你认为合适的操作6) (以root帐号)卸载内核模块 rmmod proc_example7) 再次查看内核模块的安装情况,验证卸载成功8) 验证 /proc目录下,proc_example目录及其文件均已消失三、(选做)课外预习或课后自学边干边学第4章“内核模块”参考资料l 边干边学第4章,“内核模块”l 边干边学第1.2节,“查看Linux内核状况”附:源程序边干边学(2002年8月第1版),第4.1.4节,helloworld.c#include MODULE_LICENSE(GPL);int init_module(void)printk(Hello, world!n);return 0;void cleanup_module(void)printk(goodbye!n);边干边学(2002年8月第1版),第4.3.2节,proc_example.c/* proc_example.c*/#define _NO_VERSION_#include #include #include #include #include #include #define MODULE_VERSION 1.0#define MODULE_NAME proc_example#define FOOBAR_LEN 255/ 8struct fb_data_t char nameFOOBAR_LEN + 1; char valueFOOBAR_LEN + 1;static struct proc_dir_entry *example_dir, *foo_file, *bar_file;static struct proc_dir_entry *jiffies_file, *tty_device, *symlink;struct fb_data_t foo_data, bar_data;static int proc_read_jiffies(char *page, char *start, off_t off, int count, int *eof, void *data) int len; MOD_INC_USE_COUNT; len = sprintf(page, jiffies = %ldn, jiffies); MOD_DEC_USE_COUNT; return len;static int proc_read_foobar(char *page, char *start, off_t off, int count, int *eof, void *data) int len; struct fb_data_t *fb_data = (struct fb_data_t *)data; MOD_INC_USE_COUNT; len = sprintf(page, %s = %sn, fb_data-name, fb_data-value); MOD_DEC_USE_COUNT; return len;static int proc_write_foobar(struct file *file, const char *buffer, unsigned long count, void *data) int len; struct fb_data_t *fb_data = (struct fb_data_t *)data; MOD_INC_USE_COUNT; if(count FOOBAR_LEN) len = FOOBAR_LEN; else len = count; if(copy_from_user(fb_data-value, buffer, len) MOD_DEC_USE_COUNT; return -EFAULT; fb_data-valuelen = 0; MOD_DEC_USE_COUNT; return len;int init_module() int rv = 0; /* create the directory */ example_dir = proc_mkdir(MODULE_NAME, NULL); if(example_dir = NULL) rv = -ENOMEM; goto out; example_dir-owner = THIS_MODULE; /* create the read-only file jiffies */ jiffies_file = create_proc_read_entry(jiffies, 0444, example_dir, proc_read_jiffies, NULL); if(jiffies_file = NULL) rv = -ENOMEM; goto no_jiffies; jiffies_file-owner = THIS_MODULE; /* create file foo and bar */ foo_file = create_proc_entry(foo, 0644, example_dir); if(foo_file = NULL) rv = -ENOMEM; goto no_foo; strcpy(foo_, foo); strcpy(foo_data.value, foo); foo_file-data = &foo_data; foo_file-read_proc = proc_read_foobar; foo_file-write_proc = proc_write_foobar; foo_file-owner = THIS_MODULE; bar_file = create_proc_entry(bar, 0644, example_dir); if(bar_file = NULL) rv = -ENOMEM; goto no_bar; strcpy(bar_, bar); strcpy(bar_data.value, bar); bar_file-data = &bar_data; bar_file-read_proc = proc_read_foobar; bar_file-write_proc = proc_write_foobar; bar_file-owner = THIS_MODULE; /* create device file tty */ tty_device = proc_mknod(tty, S_IFCHR | 0666, example_dir, MKDEV(5, 0); if(tty_device = NULL) rv = -ENOMEM; goto no_tty; tty_device-owner = THIS_MODULE; /* create link file jiffies_too */ symlink = proc_symlink(jiffies_too, example_dir, jiffies); if(symlink = NULL) rv = -ENOMEM; goto no_symlink; symlink-owner = THIS_MODULE; /* all creation are sucessful */ printk(KERN_INFO %s %s initialisedn, MODULE_NAME, MODULE_VERSION); return 0;/* error handling */no_symlink: remove_proc_entry(tty, example_dir);no_tty: remove_proc_entry(bar, example_dir);no_bar: remove_proc_entry(foo, example_dir);no_foo: remove_proc_entry(jiffies, example_dir);no_jiffies: remove_proc_entry(MODULE_NAME, NULL);out: return rv;void cleanup_module() remove_proc_entry(jiffies_too, example_dir); remove_proc_entry(tty, example_dir); remove_proc_entry(bar, example_dir); remove

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论