Eswlnk Blog Eswlnk Blog
  • 资源
    • 精彩视频
    • 破解专区
      • WHMCS
      • WordPress主题
      • WordPress插件
    • 其他分享
    • 极惠VPS
    • PDF资源
  • 关于我
    • 论文阅读
    • 关于本站
    • 通知
    • 左邻右舍
    • 玩物志趣
    • 日志
    • 专题
  • 热议话题
    • 游戏资讯
  • 红黑
    • 渗透分析
    • 攻防对抗
    • 代码发布
  • 自主研发
    • 知识库
    • 插件
      • ToolBox
      • HotSpot AI 热点创作
    • 区块
    • 快乐屋
    • 卡密
  • 乱步
    • 文章榜单
    • 热门标签
  • 问答中心反馈
  • 注册
  • 登录
首页 › 代码发布 › Linux操作系统实验:模块编程

Linux操作系统实验:模块编程

Eswlnk的头像
Eswlnk
2023-02-11 13:12:38
Linux操作系统实验:模块编程-Eswlnk Blog
智能摘要 AI
本文详细介绍了编写、编译、加载和卸载Linux内核模块的过程。实验目标包括编写一个简单的内核模块并掌握相关命令(如`lsmod`、`insmod`、`rmmod`、`dmesg`)。模块代码实现了基本的文件操作功能,包括读取和打开设备,并在加载时显示设备主编号,卸载时输出“Goodbye, World!”。实验中遇到的问题主要是模块与内核版本不匹配,通过比对版本信息并重新编译解决了这一问题。此外,还编写了Makefile用于模块的编译和测试。最终,成功加载和卸载模块,达到了预期效果,加深了对Linux内核模块的理解和兴趣。

实验目的

  • 编写一个内核模块;
  • 编译该模块;
  • 加载、卸载该模块;(相关命令: lsmod,insmod,rmmod,dmesg)

实验过程

编写内核模块代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robert W. Oliver II");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");
 #define DEVICE_NAME "lkm_example"#define EXAMPLE_MSG "Hello, World!\n"#define MSG_BUFFER_LEN 15
 /* Prototypes for device functions */static int device_open(struct inode *, struct file *);static int device_release(struct inode *, struct file *);static ssize_t device_read(struct file *, char *, size_t, loff_t *);static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
 static int major_num;static int device_open_count = 0;static char msg_buffer[MSG_BUFFER_LEN];static char *msg_ptr;
 /* This structure points to all of the device functions */static struct file_operations file_ops = {
 .read = device_read,
 .write = device_write,
 .open = device_open,
 .release = device_release};
 /* When a process reads from our device, this gets called. */static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset) {
 int bytes_read = 0;
  /* If we’re at the end, loop back to the beginning */
  if (*msg_ptr == 0) {
   msg_ptr = msg_buffer;
  }
  /* Put data in the buffer */
  while (len && *msg_ptr) {
    /* Buffer is in user data, not kernel, so you can’t just reference
     * with a pointer. The function put_user handles this for us */
    put_user(*(msg_ptr++), buffer++);
    len--;
    bytes_read++;
 }
  return bytes_read;}
 /* Called when a process tries to write to our device */static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t *offset) {
 /* This is a read-only device */
  printk(KERN_ALERT "This operation is not supported.\n");
  return -EINVAL;}
 /* Called when a process opens our device */static int device_open(struct inode *inode, struct file *file) {
  /* If device is open, return busy */
  if (device_open_count) {
   return -EBUSY;
  }
  device_open_count++;
  try_module_get(THIS_MODULE);
  return 0;}
 /* Called when a process closes our device */static int device_release(struct inode *inode, struct file *file) {
  /* Decrement the open counter and usage count. Without this, the module would not unload. */
  device_open_count--;
  module_put(THIS_MODULE);
  return 0;}
 static int __init lkm_example_init(void) {
  /* Fill buffer with our message */
  strncpy(msg_buffer, EXAMPLE_MSG, MSG_BUFFER_LEN);
  /* Set the msg_ptr to the buffer */
  msg_ptr = msg_buffer;
  /* Try to register character device */
  major_num = register_chrdev(0, "lkm_example", &file_ops);
  if (major_num < 0) {
   printk(KERN_ALERT "Could not register device: %d\n", major_num);
   return major_num;
  } else {
   printk(KERN_INFO "lkm_example module loaded with device major number %d\n", major_num);
   return 0;
  }}
 static void __exit lkm_example_exit(void) {
  /* Remember — we have to clean up after ourselves. Unregister the character device. */
  unregister_chrdev(major_num, DEVICE_NAME);
  printk(KERN_INFO "Goodbye, World!\n");}
 /* Register module functions */
module_init(lkm_example_init);
module_exit(lkm_example_exit);

以上代码实现,当模块加载后,显示设备主要编号,卸载后显示“Goodbye, World”

Linux操作系统实验:模块编程-Eswlnk Blog

问题与解决办法

Disagrees about version of symbol symbol_name after insmod

此处问题出现原因是linux模块与内核版本不同,通过modinfo和uname -a 比对后选择相同的linux模块,重新编译即可

编译

安装开发工具集合:

sudo apt-get install build-essential linux-headers-`uname -r`

编写Makefile

obj-m += lkm_example.o

all:

make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

clean:

make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

test:

  # We put a — in front of the rmmod command to tell make to ignore

  # an error in case the module isn’t loaded.

sudo rmmod lkm_example

  # Clear the kernel log without echo

sudo dmesg -C

  # Insert the module

sudo insmod lkm_example.ko

dmesg

编译模块

Sudo make

Sudo make test

执行成功后回显:

Linux操作系统实验:模块编程-Eswlnk Blog

成功加载卸载模块

Lsmod | grep “lkm_example”

列出加载模块

Linux操作系统实验:模块编程-Eswlnk Blog

实验心得

实验结论

本次实验已获得预期输出结果,但是在实验过程中却花费了不少的时间去学习相关知识与函数用法。

个人收获

通过这次小实验,让我进一步了解了linux如何编写内核模块,以及如何加载卸载编译内核模块,激起了我们对linux学习的兴趣

本站默认网盘访问密码:1166
本站默认网盘访问密码:1166
0
0
Eswlnk的头像
Eswlnk
一个有点倒霉的研究牲站长
赞赏
脚本分享:某教育挂机刷课脚本
上一篇
如何用Python判断回文数?
下一篇

评论 (0)

请登录以参与评论
现在登录
    发表评论

猜你喜欢

  • 「日志记录」逆向必应翻译网页版API实现免费调用
  • 「代码分享」第三方平台VIP视频解析API接口
  • 「至臻原创」某系统网站登录功能监测
  • 「开发日志」在Vue3中如何为路由Query参数标注类型
  • 「其他分享」分享一个在Tun模式下可用的脚本
Eswlnk的头像

Eswlnk

一个有点倒霉的研究牲站长
1108
文章
319
评论
679
获赞

随便看看

Apple 发布安全更新以修补关键的 iOS 和 macOS 安全漏洞
2023-12-12 22:17:10
如何关闭移动浏览器的云端加速?
2022-07-19 18:34:46
总结安装ffmpeg拓展命令
2021-04-28 1:05:57

专题展示

WordPress53

工程实践37

热门标签

360 AI API CDN java linux Nginx PDF PHP python SEO Windows WordPress 云服务器 云服务器知识 代码 免费 安全 安卓 工具 开发日志 微信 微软 手机 插件 攻防 攻防对抗 教程 日志 渗透分析 源码 漏洞 电脑 破解 系统 编程 网站优化 网络 网络安全 脚本 苹果 谷歌 软件 运维 逆向
  • 首页
  • 知识库
  • 地图
Copyright © 2023-2025 Eswlnk Blog. Designed by XiaoWu.
本站CDN由 壹盾安全 提供高防CDN安全防护服务
蜀ICP备20002650号-10
页面生成用时 0.890 秒   |  SQL查询 26 次
本站勉强运行:
友情链接: Eswlnk Blog 网站渗透 倦意博客 特资啦!个人资源分享站 祭夜博客 iBAAO壹宝头条
  • WordPress142
  • 网络安全64
  • 漏洞52
  • 软件52
  • 安全48
现在登录
  • 资源
    • 精彩视频
    • 破解专区
      • WHMCS
      • WordPress主题
      • WordPress插件
    • 其他分享
    • 极惠VPS
    • PDF资源
  • 关于我
    • 论文阅读
    • 关于本站
    • 通知
    • 左邻右舍
    • 玩物志趣
    • 日志
    • 专题
  • 热议话题
    • 游戏资讯
  • 红黑
    • 渗透分析
    • 攻防对抗
    • 代码发布
  • 自主研发
    • 知识库
    • 插件
      • ToolBox
      • HotSpot AI 热点创作
    • 区块
    • 快乐屋
    • 卡密
  • 乱步
    • 文章榜单
    • 热门标签
  • 问答中心反馈