Linux Kernel Programming(1)

1.1什么是Kernel Module?

内核模块是可以按需加载或卸载的内核代码,可以不重启系统就扩充内核的功能。例如,一种内核模块叫做设备驱动程序(Device driver),它允许内核访问连接到系统的硬件。如果没有内核模块,我们将必须构建整体内核,并将新功能直接添加到内核映像中。除了具有更大的内核之外,这还有一个缺点,即每次我们想要新功能时都要求我们重建并重新启动内核。

1.2关于Kernel的基本操作

  • 显示当前装入的内核模块:
$ lsmod
  • 显示模块信息:
$ modinfo module_name
  • 显示所有模块的配置信息
$ modprobe -c | less
  • 显示某个模块的配置信息:
$ modprobe -c | grep module_name

2.1 Hello World (part1): 最简单的Module


/*  
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */

int init_module(void)
{
	printk(KERN_INFO "Hello world 1.\n");

	/* 
	 * A non 0 return means init_module failed; module can't be loaded. 
	 */
	return 0;
}

void cleanup_module(void)
{
	printk(KERN_INFO "Goodbye world 1.\n");
}
  • 头文件中的#include <linux/module.h> 和 #include <linux/kernel.h>是所有module都需要添加的头文件。
  • Kernel modules必须包含至少两个函数:初始化函数init_module() 和 结束函数cleanup_module()。
  • 初始化函数init_module()作用是将module写入kernel。
  • 结束函数cleanup_module()作用是清理内存结束module。
  • printk()函数:用来记录信息或给出warning,该函数有8个优先级,其中第一个就是KERN_ALERT,如果你不写优先级会被默认为DEFAULT_MESSAGE_LOGLEVEL (在后文中会具体讲解printk函数)

2.2 编译Kernel Modules

  1. 编写makefile文件 (下文中讲解如何编写makefile文件)
  2. 在相应的文件目录下使用make命令来编译module,如果编译成功,则resulting module code 包含在 <module_name>.ko 文件中
  3. 通过输入su -变成superuser,因此你必须输入密码来操作下一步
  4. 通过输入insmod <module_name>.ko命令,将module插入到Kernel中,任何通过printk()函数将会显示在 /var/log/messages 文件中。查看输出的最好方法是通过输入tail -f /var/log/messages命令。
  5. 你可以删除掉已存在的module,通过输入rmmod <module_name>命令。

2.2.1如何编写makefile文件

一个基础的makefile文件:

obj-m += hello-1.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  • 第一行obj-m += hello-1.o: 指定编译模块名称,会自动寻找hello-1.c
  • make -C /lib/modules/$(shell uname -r)/build M=$(PWD)表示C进入内核目录读取Makefile,M表明后回到当前目录读取Makefile

2.3 Hello World (part2) : 宏定义1⃣️

module_init()module_exit()
The only caveat is that your init and cleanup functions must be defined before calling the macros, otherwise you’ll get compilation errors.

/*  
 *  hello-2.c - Demonstrating the module_init() and module_exit() macros.
 *  This is preferred over using init_module() and cleanup_module().
 */
#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */
#include <linux/init.h>		/* Needed for the macros */

static int __init hello_2_init(void)
{
	printk(KERN_INFO "Hello, world 2\n");
	return 0;
}

static void __exit hello_2_exit(void)
{
	printk(KERN_INFO "Goodbye, world 2\n");
}

module_init(hello_2_init); //宏定义
module_exit(hello_2_exit); //宏定义

2.4 Hello World (part3) : 宏定义2⃣️

还有一个__initdata与__init相似,但用于init变量而不是函数。


/*  
 *  hello-3.c - Illustrating the __init, __initdata and __exit macros.
 */
#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */
#include <linux/init.h>		/* Needed for the macros */

static int hello3_data __initdata = 3;

static int __init hello_3_init(void)
{
	printk(KERN_INFO "Hello, world %d\n", hello3_data);
	return 0;
}

static void __exit hello_3_exit(void)
{
	printk(KERN_INFO "Goodbye, world 3\n");
}

module_init(hello_3_init);
module_exit(hello_3_exit);

2.5 传递参数到Module中

  1. step1: 声明头文件 #include<linux/moduleparam.h>
  2. step2: 创建参数
  3. step3:module_param(name_var, type, permissions);
    permissions有五种:
    S_IRUSR 其中第一个R表示Reading,USR表示user
    S_IWUSR 其中W表示Writing,USR表示user
    S_IXUSR 其中X表示Execution,USR表示user
    S_IWGRP 其中W表示Writing,GRP表示group
    S_IRGRP 其中R表示Reading,GRP表示group

/*
 *  hello-5.c - Demonstrates command line argument passing to a module.
 */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>

static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { -1, -1 };
static int arr_argc = 0;

/* 
 * module_param(foo, int, 0000)
 * The first param is the parameters name
 * The second param is it's data type
 * The final argument is the permissions bits, 
 * for exposing parameters in sysfs (if non-zero) at a later stage.
 */

module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
module_param(mylong, long, S_IRUSR);
module_param(mystring, charp, 0000);

/*
 * module_param_array(name, type, num, perm);
 * The first param is the parameter's (in this case the array's) name
 * The second param is the data type of the elements of the array
 * The third argument is a pointer to the variable that will store the number 
 * of elements of the array initialized by the user at module loading time
 * The fourth argument is the permission bits
 */
module_param_array(myintArray, int, &arr_argc, 0000);

static int __init hello_5_init(void)
{
	int i;
	printk(KERN_INFO "Hello, world 5\n=============\n");
	printk(KERN_INFO "myshort is a short integer: %hd\n", myshort);
	printk(KERN_INFO "myint is an integer: %d\n", myint);
	printk(KERN_INFO "mylong is a long integer: %ld\n", mylong);
	printk(KERN_INFO "mystring is a string: %s\n", mystring);
	for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
	{
		printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]);
	}
	printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);
	return 0;
}

static void __exit hello_5_exit(void)
{
	printk(KERN_INFO "Goodbye, world 5\n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值