Linux kernel Linux 2.6.x内核的“procfs1.c”的现代版本是什么?

Linux kernel Linux 2.6.x内核的“procfs1.c”的现代版本是什么?,linux-kernel,kernel-module,Linux Kernel,Kernel Module,当我遇到第5章中的第一个示例时,我一直在遵循,名为procfs1.c 它不会开箱即用地编译,在检查了各种相关问题之后,我仍然花了相当长的时间来找出正确的更改,使它能够按照预期编译和工作 因此,我将为未来的人们发布我的更新代码。我正在运行内核2.6.32-279.el6.x86\u 64 这里是该示例的更新版本,它与内核版本2.6.32-279.el6.x86_64一起使用 /* * procfs1.c - create a "file" in /proc * */ #include

当我遇到第5章中的第一个示例时,我一直在遵循,名为
procfs1.c

它不会开箱即用地编译,在检查了各种相关问题之后,我仍然花了相当长的时间来找出正确的更改,使它能够按照预期编译和工作


因此,我将为未来的人们发布我的更新代码。我正在运行内核
2.6.32-279.el6.x86\u 64

这里是该示例的更新版本,它与内核版本
2.6.32-279.el6.x86_64
一起使用

/*
 *  procfs1.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 */

#define procfs_name "helloworld"

/**
 * This structure hold information about the /proc file
 *
 */
struct proc_dir_entry *Our_Proc_File;


static ssize_t procfile_read(struct file *filp,   
               char *buffer,    
               size_t length,   
               loff_t * offset)
{
    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 */
        ret = sprintf(buffer, "HelloWorld!\n");
        *offset = ret;
    }

    return ret;
}

static struct file_operations fops = {
    .owner = THIS_MODULE,
    .read = procfile_read,
};

int init_module()
{

    Our_Proc_File = proc_create(procfs_name, S_IFREG | S_IRUGO, NULL, &fops);
    if (Our_Proc_File == NULL) {
        remove_proc_entry(procfs_name, NULL);
        printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
               procfs_name);
        return -ENOMEM;
    }

    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 */
}

void cleanup_module()
{
    remove_proc_entry(procfs_name, NULL);
    printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
/*
*c-在/proc中创建一个“文件”
*
*/
#包括/*特别是模块*/
#include/*我们正在做内核工作*/
#包括/*必要,因为我们使用proc fs*/
#定义procfs_名称“helloworld”
/**
*此结构保存有关/proc文件的信息
*
*/
struct proc\u dir\u entry*我们的\u proc\u文件;
静态ssize\u t procfile\u read(结构文件*filp,
字符*缓冲区,
大小和长度,
loff_t*偏移量)
{
int ret;
printk(KERN\u INFO“procfile\u read(/proc/%s)调用\n”,procfs\u name);
如果(*偏移量>0){
/*我们已完成读取,返回0*/
ret=0;
}否则{
/*填充缓冲区,返回缓冲区大小*/
ret=sprintf(缓冲区,“HelloWorld!\n”);
*偏移量=ret;
}
返回ret;
}
静态结构文件\u操作fops={
.owner=此_模块,
.read=procfile\u read,
};
int init_模块()
{
我们的_Proc_File=Proc_create(procfs_name、S_IFREG、S|u IRUGO、NULL和&fops);
if(我们的_Proc_文件==NULL){
删除\u proc\u条目(procfs\u名称,空);
printk(内核警报“错误:无法初始化/proc/%s\n”,
procfs_name);
return-ENOMEM;
}
我们的程序文件->uid=0;
我们的程序文件->gid=0;
我们的程序文件->大小=37;
printk(KERN\u INFO)/proc/%s已创建\n“,procfs\u名称);
返回0;/*一切正常*/
}
void cleanup_模块()
{
删除\u proc\u条目(procfs\u名称,空);
printk(内核信息“/proc/%s已删除\n”,procfs\u名称);
}