gpt4 book ai didi

c - 如何为 proc_dir_entry 定义 inode_operation?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:47 27 4
gpt4 key购买 nike

我正在尝试学习如何使用 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 文件系统的一般目的是让内核及其模块能够轻松创建文件,其中包含动态生成的“内容” group 这些文件的目录。可以通过使用 proc_create() 和 friend 来做到这一点。

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

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


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

关于c - 如何为 proc_dir_entry 定义 inode_operation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com