c - LKM和procfs的解释

标签 c linux linux-kernel kernel proc

我正在学习用于将数据从内核读取到用户空间的 proc 和可加载内核模块 (LKM)。 I asked for some info regarding procfs in another post.

谁能告诉我 lkm 和 procfs 是什么,我在哪里可以编写 lkm 和 proc 的代码(内核源代码中的位置)?

最佳答案

您不会在内核源代码中为 LKM 编写代码(虽然这是可能的,但不建议这样做,除非您正在处理将成为正态分布模块的内容)。您改为创建自己的目录并提供您的代码。

您编写的用于提供 procfs 接口(interface)的函数只是作为 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_writeprocfile_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) clean

3) 使用命令 make 构建模块 4) 使用命令 insmod procfs2.ko 将模块加载到内存中(以 root 用户执行此操作)

教程中没有列出的是:如果你的模块有问题,期望重启。内核模块的崩溃通常会使您的系统瘫痪。

关于c - LKM和procfs的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23084681/

相关文章:

c++ - 为驱动程序定义 GUID 的推荐方法是什么?

CUnit:因动态生成的字符串而失败

c - Linux 上的 IOCTL_STORAGE_QUERY_PROPERTY 相当于什么?

linux - awk打印所需数量的字段

c++ - 共享库、库依赖

linux - 是否可以替换现有字符设备的字符驱动程序?

c++ - 为项目构建大型库时的最佳实践

c - mm_struct 中的 cpumask 是什么

c - 获取 kobject_add 的父级

python - 子进程.Popen : 'OSError: [Errno 2] No such file or directory' only on Linux