如何为proc_dir_entry定义inode_operation?

How to define inode_operation for proc_dir_entry?

我正在尝试学习如何使用 Linux 内核模块编程指南

编写 linux 内核模块

但是我意识到这本书中的示例已经过时了。 以下是书中的示例之一。

static struct proc_dir_entry *Our_Proc_File;
static int module_permission(struct inode *inode, int op, struct nameidata *nd)
{

    if (op == 4 || (op == 2 && current−>euid == 0))
            return 0;
    return −EACCES;
}
static struct inode_operations Inode_Ops_4_Our_Proc_File = {
    .permission = module_permission
}
static struct file_operations File_Ops_4_Our_Proc_File = {
    // ... 
};

int init_module()
{
    Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
    // above line should use proc_create() 
    if (Our_Proc_File == NULL) {
            remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
            return −ENOMEM;
    }

    Our_Proc_File−>owner = THIS_MODULE;
    Our_Proc_File−>proc_iops = &Inode_Ops_4_Our_Proc_File;
    Our_Proc_File−>proc_fops = &File_Ops_4_Our_Proc_File;
}

当我查看源代码时,我发现 proc_iops 正在从 Linux 4.x

中的 proc_dir_entry 结构中删除

那么我应该如何为 proc_dir_entry

定义 inode_operations

/proc 文件系统的一般目的是让内核及其模块能够轻松创建 文件,"content" 即时生成 and 目录 group 这些文件。可以通过使用 proc_create() 和朋友来做到这一点。

至于 inode 和 dentry,它们是文件系统内部的一部分:最好不要修改它们和它们的操作。

其实file_operations本身就很强大。如果您发现 proc_create()mode 参数不足以反映访问权限,您可以在 .open() 文件操作 .

中检查访问权限

至于结构体proc_dir_entry中的字段proc_iops,它仍然存在。但是结构本身在 internal header fs/proc/internal.h 中定义 - 另一个信号,即不希望从外部访问其字段。