我正在学习用于将数据从内核读取到用户空间的proc和可加载内核模块(LKM)。I asked for some info regarding procfs in another post.
请有人告诉我lkm和procfs是什么,我可以在哪里为lkm和proc (内核源代码中的位置)编写代码?
发布于 2014-04-15 13:18:50
您不会在内核源代码中为LKM编写代码(虽然这是可能的,但除非您正在研究将成为正态分布的模块),否则不建议这样做。相反,您可以创建自己的目录并提供代码。
为提供procfs接口而编写的函数只是LKM源代码的一部分。
http://linux.die.net/lkmpg/x769.html有一个使用procfs的简单示例,在这里复制:
/**
* procfs2.c - create a "file" in /proc
*
*/
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#include <asm/uaccess.h> /* for copy_from_user */
#define PROCFS_MAX_SIZE 1024
#define PROCFS_NAME "buffer1k"
/**
* This structure hold information about the /proc file
*
*/
static struct proc_dir_entry *Our_Proc_File;
/**
* The buffer used to store character for this module
*
*/
static char procfs_buffer[PROCFS_MAX_SIZE];
/**
* The size of the buffer
*
*/
static unsigned long procfs_buffer_size = 0;
/**
* This function is called then the /proc file is read
*
*/
int
procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);
if (offset > 0) {
/* we have finished to read, return 0 */
ret = 0;
} else {
/* fill the buffer, return the buffer size */
memcpy(buffer, procfs_buffer, procfs_buffer_size);
ret = procfs_buffer_size;
}
return ret;
}
/**
* This function is called with the /proc file is written
*
*/
int procfile_write(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
return procfs_buffer_size;
}
/**
*This function is called when the module is loaded
*
*/
int init_module()
{
/* create the /proc file */
Our_Proc_File = create_proc_entry(PROCFS_NAME, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(PROCFS_NAME, &proc_root);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
PROCFS_NAME);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->write_proc = procfile_write;
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
return 0; /* everything is ok */
}
/**
*This function is called when the module is unloaded
*
*/
void cleanup_module()
{
remove_proc_entry(PROCFS_NAME, &proc_root);
printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
}模块初始化使用create_proc_entry()建立procfs条目。对函数procfile_write和procfile_read进行初始化,以处理该条目上的写入和读取。当模块卸载时调用模块的cleanup_module()函数,通过调用cleanup_module()删除procfs条目。
您将在http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html上找到构建内核模块的教程。这方面的摘要如下:
1)确保在/usr/src中安装了内核源代码。
2)创建一个makefile,如下所示:
obj-m = procfs2.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean3)使用命令make 4构建模块,4)使用命令insmod procfs2.ko将模块加载到内存中(作为根用户执行此操作)
本教程中没有列出的内容是:如果您的模块有问题,请重新启动。内核模块中的崩溃通常会导致系统崩溃。
https://stackoverflow.com/questions/23084681
复制相似问题