linux模块编写

一、linux模块化机制简介

  模块化的优势: linux内核是单内核结构,由于所有内容都集成在一起,效率很高,但可扩展性和可维护性相对较差,模块机制弥补这一缺陷。

Linux模块可以通过静态或动态的方法加载到内核空间,静态加载是指在内核启动过程中加载;动态加载是指在内核运行的过程中随时加载。一个模块被加载到内核中时,就成为内核代码的一部分。模块加载入系统时,系统修改内核中的符号表,将新加载的模块提供的资源和符号添加到内核符号表中,以便模块间的通信。

从代码的特征上来看,模块就是可以完成一个独立功能的一组函数的集合,但以特殊的方法来编译,从而使之可以在需要时随时安装,在不需要时随时卸载。它们扩展了操作系统内核功能却不需要重新编译内核、启动系统。

  Linux 内核模块主要由以下几个部分组成: 

模块加载函数(必须):当通过insmod命令加载内核模块时,模块的加载函数会自动被内核执行,完成本模块相关初始化工作;
      模块卸载函数(必须):当通过rmmod命令卸载模块时,模块的卸载函数会自动被内核执行,完成与模块加载函数相反的功能;
      模块许可证声明(必须):模块许可证(LICENCE)声明描述内核模块的许可权限,如果不声明LICENCE,模块被加载时将收到内核被污染的警告。大多数

模块参数(可选):模块参数是模块被加载的时候可以被传递给他的值,它本身对应模块内部的全局变量;

模块导出符号(可选):内核模块可以导出符号(symbol,对应于函数或变量),这样其他模块可以使用本模块中的变量或函数;
      模块作者等信息声明(可选)。

二、模块的内核描述

每一个内核模块在内核中都对应一个数据结构module,所有的模块通过一个链表维护

在线查看内核源码:https://elixir.bootlin.com

v2.6.28内核include/linux/module.h

struct module
{
    enum module_state state;              //模块状态,枚举类型的变量,可取的值为MODULE_STATE_LIVE、MODULE_STATE_COMING、MODULE_STATE_GOING,分为当前正常使用中(存活状态)、模块当前正在被加载和模块当前正在被卸载三种状态。

    /* Member of list of modules */
    struct list_head list;                //所有的模块构成双链表,包头为全局变量modules

    /* Unique handle for this module */
    char name[MODULE_NAME_LEN];           //模块名称,一般不包含.ko

    /* Sysfs stuff. */
    struct module_kobject mkobj;
    struct module_attribute *modinfo_attrs;
    const char *version;
    const char *srcversion;
    struct kobject *holders_dir;

    /* Exported symbols */
    const struct kernel_symbol *syms;
    const unsigned long *crcs;
    unsigned int num_syms;

    /* GPL-only exported symbols. */
    unsigned int num_gpl_syms;
    const struct kernel_symbol *gpl_syms;
    const unsigned long *gpl_crcs;

#ifdef CONFIG_UNUSED_SYMBOLS
    /* unused exported symbols. */
    const struct kernel_symbol *unused_syms;
    const unsigned long *unused_crcs;
    unsigned int num_unused_syms;

    /* GPL-only, unused exported symbols. */
    unsigned int num_unused_gpl_syms;
    const struct kernel_symbol *unused_gpl_syms;
    const unsigned long *unused_gpl_crcs;
#endif

    /* symbols that will be GPL-only in the near future. */
    const struct kernel_symbol *gpl_future_syms;
    const unsigned long *gpl_future_crcs;
    unsigned int num_gpl_future_syms;

    /* Exception table */
    unsigned int num_exentries;
    struct exception_table_entry *extable;

    /* Startup function. */
    int (*init)(void);       //模块初始化函数指针

    /* If this is non-NULL, vfree after init() returns */
    void *module_init;       //如果该函数不为空,则init结束后就可以调用进行适当释放

    /* Here is the actual code + data, vfree‘d on unload. */
    void *module_core;

    /* Here are the sizes of the init and core sections */
    unsigned int init_size, core_size;

    /* The size of the executable code in each section.  */
    unsigned int init_text_size, core_text_size;

    /* The handle returned from unwind_add_table. */
    void *unwind_info;

    /* Arch-specific module values */
    struct mod_arch_specific arch;

    unsigned int taints;    /* same bits as kernel:tainted */

#ifdef CONFIG_GENERIC_BUG
    /* Support for BUG */
    unsigned num_bugs;
    struct list_head bug_list;
    struct bug_entry *bug_table;
#endif

#ifdef CONFIG_KALLSYMS
    /* We keep the symbol and string tables for kallsyms. */
    Elf_Sym *symtab;
    unsigned int num_symtab;
    char *strtab;

    /* Section attributes */
    struct module_sect_attrs *sect_attrs;

    /* Notes attributes */
    struct module_notes_attrs *notes_attrs;
#endif

    /* Per-cpu data. */
    void *percpu;

    /* The command line arguments (may be mangled).  People like
       keeping pointers to this stuff */
    char *args;
#ifdef CONFIG_MARKERS
    struct marker *markers;
    unsigned int num_markers;
#endif
#ifdef CONFIG_TRACEPOINTS
    struct tracepoint *tracepoints;
    unsigned int num_tracepoints;
#endif

#ifdef CONFIG_MODULE_UNLOAD
    /* What modules depend on me? */
    struct list_head modules_which_use_me;

    /* Who is waiting for us to be unloaded */
    struct task_struct *waiter;

    /* Destruction function. */
    void (*exit)(void);

    /* Reference counts */
    struct module_ref ref[NR_CPUS];
#endif
};

三、实验

     hello模块

ubuntu 请安装源代码

sudo apt-get install linux-source

centos安装

yum install kernel*

hello.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init init_hello(void)
{
        printk(KERN_ALERT "my first module test,output \"hello!\"\n");
        return 0;
}

static void __exit exit_hello(void)
{
        printk(KERN_DEBUG "bye bye test module!\n");
}
module_init(init_hello);
module_exit(exit_hello);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xiaoan");
MODULE_VERSION("v0.1");
MODULE_DESCRIPTION("TEST FOR MODULE");

Makefile

obj-m := hello.o
KERNELBUILD :=/lib/modules/$(shell uname -r)/build
default:
        make -C $(KERNELBUILD) M=$(shell pwd) modules

执行make 即可

验证:

    

$ inmod ./hello.ko$ dmesg...  [ 2502.490509] my first module test,output "hello!"

$ rmmod heelo.ko$ dmesg...   [ 2568.969806] bye bye test module!

原文地址:https://www.cnblogs.com/linuxxl/p/11391133.html

时间: 2025-01-29 05:26:58

linux模块编写的相关文章

linux内核hello world模块编写

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> int param = 0; /* 设备模块注册时执行的初始化函数 */ static int __init initialization_module(void) { printk("Hello world.\n"); printk("param = %d.\n", param)

Linux内核模块编写详解

内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了.Linux内核和它的用户空间是大不相同的:抛开漫不经心,你必须小心翼翼,因为你编程中的一个bug就会影响到整个系统,本文给大家介绍linux内核模块编写,需要的朋友可以参考下 内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了.Linux内核和它的用户空间是大不相同的:抛开漫不经心,你必须小心翼翼,因为你编程中的一个bug就会影响到整个系统.浮点运算做起来可不容易,堆栈固定而狭小,而你写的代码总是异步的,

Linux 脚本编写基础

1. Linux 脚本编写基础1.1 语法基本介绍1.1.1 开头程序必须以下面的行开始(必须放在文件的第一行):#!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执行:编译 chmod +x filename 这样才能用./filename 来运行1.1.2 注释 在进行shell编程时,以#开头的句子表示注释,直到这一行的结束.我们真诚地建议您在程序中使用

Linux模块编程框架

Linux模块编程框架 Linux是单内核系统,可通用计算平台的外围设备是频繁变化的,不可能将所有的(包括将来即将出现的)设备的驱动程序都一次性编译进内核,为了解决这个问题,Linux提出了可加载内核模块(Loadable Kernel Module,LKM)的概念,允许一个设备驱动通过模块加载的方式,在内核运行起来之后"融入"内核,加载进内核的模块和本身就编译进内核的模块一模一样.一个程序在编译的地址的相对关系就已经确定了,运行的时候只是进行简单的偏移,为了使模块加载进内核后能够被放

基于Asterisk的VoIP开发指南——Asterisk 模块编写指南(1)

原文:基于Asterisk的VoIP开发指南--Asterisk 模块编写指南(1) 1 开源项目概述 Asterisk是一个开源的软件包,通常运行在Linux操作系统平台上.Asterisk可以用三种协议来实现VoIP,同时可以与目前电话使用的标准硬件进行交互通信,Asterisk在实现VoIP时,不需要任何附加硬件,本文所采用的也是这种使用方式.但是,如果企业没有与VoIP语音网关运营商建立合作关系,想要自己构建这样的一个平台,那么要和数字电话设备与模拟电话设备进行交互通信,Asterisk

Linux下编写驱动程序(VFS)

转:http://hi.baidu.com/firstm25/item/8fe022155e1fa78988a9568f 摘要:设备驱动程序是操作系统内核与机器硬件之间的接口.设备驱动程序为应用程序屏蔽了硬件的细节.那么驱动程序如何书写实现这一接口功能是本文讨论的重点,并以一简单的驱动程序介绍书写细节. 在用户进程调用驱动程序时,系统进入核心态,这时不再是抢先式调度.(应用程序一般是在用户态下进行)也就是说系统必须在驱动程序的子函数返回后才能进行其它的工作,即驱动程序不能进入死循环. 字符型设备

nginx自定义模块编写-根据post参数路由到不同服务器

nginx可以轻松实现根据不同的url 或者 get参数来转发到不同的服务器,然而当我们需要根据http包体来进行请求路由时,nginx默认的配置规则就捉襟见肘了,但是没关系,nginx提供了强大的自定义模块功能,我们只要进行需要的扩展就行了. 我们来理一下思路,我们的需求是: nginx根据http包体的参数,来选择合适的路由 在这之前,我们先来考虑另一个问题: 在nginx默认配置的支持下,能否实现服务器间的跳转呢?即类似于状态机,从一个服务器执行OK后,跳转到另一台服务器,按照规则依次传递

在Linux下编写Daemon

在Linux(以Redhat Linux Enterprise Edition 5.3为例)下,有时需要编写Service.Service也是程序,一般随系统启动用户不干预就不退出的程序,可以称为Service.Linux下的Service一般称为Daemon. 以上是广义的Service的定义.Linux下的Service一般放在/etc/init.d文件夹下.浏览一下这个文件夹下的文件,可以发现在Linux下编写Service一般遵循的原则. 一 Linux下编写Service一般遵循的原则

scapy编写简单的ARP扫描脚本 本课程基于 Python 的 scapy 模块编写,适合有 Python 基础的同学学习,最终完成一个简单的 ARP 扫描脚本。

scapy编写简单的ARP扫描脚本 本课程基于 Python 的 scapy 模块编写,适合有 Python 基础的同学学习,最终完成一个简单的 ARP 扫描脚本.