c - Linux 内核代码中 create_proc_entry() 和 read_proc 的替代方案

标签 c linux-kernel procfs

几年前,Linux 3.8 被移植到 TI-nSpire 图形计算器 ( https://github.com/tangrs/linux )。虽然有些更改是上游的,但大多数不是。我一直在为最新的内核源代码重写补丁,但我陷入了一个已弃用的函数:

create_proc_entry()

我收到两条与此相关的 GCC 错误消息。我通过将 create_proc_entry(BOOT1_PROCFS_NAME, 0644, NULL); 更改为 proc_create_data(BOOT1_PROCFS_NAME, 0644, NULL, NULL, NULL); 修复了第一个问题 (如果有更好的,请告诉我。)

第二个错误,

arch/arm/mach-nspire/boot1.c:37:18: error: dereferencing pointer to incomplete type ‘struct proc_dir_entry’
boot1_proc_entry->read_proc = boot1_read;

我一直无法修复。我查看了 git 历史记录,其中 read_proc_t *read_proc; (struct proc_dir_entry 的一部分;然后位于 include/linux/proc_fs.h 中,现在位于 fs/proc/internal.h 中的文件被删除 ( https://github.com/torvalds/linux/commit/3cb5bf1bf947d325fcf6e9458952b51cfd7e6677#diff-a2f17c99c50d86d5160c8f7f0261fbbd ),期望看到其他东西放在它的位置,但事实并非如此。相反,它与 create_proc_entry() 一起被弃用。

那么我应该如何重写该行 boot1_proc_entry->read_proc = boot1_read; (此处的完整文件: https://github.com/tangrs/linux/blob/nspire/arch/arm/mach-nspire/boot1.c )以便它可以使用当前的内核源进行编译?

最佳答案

函数proc_create_data有原型(prototype)

struct proc_dir_entry *proc_create_data(const char *, umode_t,
                       struct proc_dir_entry *,
                       const struct file_operations *,
                       void *);

第4个参数是文件操作的结构体。

要指定如何从文件中读取数据,您需要设置 .read该结构的字段:这是(旧)分配 read_proc替换 proc_dir_entry 中的字段对象。

同时签名.read函数(回调)非常通用:

ssize_t (*read) (struct file * file, char __user * buf, size_t size, loff_t * ppos);

Linux 内核有几个帮助程序可以在简单情况下实现此功能。

例如,如果您想将某个缓冲区“映射”为文件内容,您可以使用simple_read_from_buffer助手:

ssize_t my_read (struct file * file, char __user * buf, size_t size, loff_t * ppos)
{
    return simple_read_from_buffer(
        buf, size, ppos, // first 3 arguments are just ones for .read function
        NSPIRE_BOOT1_VIRT_BASE, // address of the buffer's start
        NSPIRE_BOOT1_SIZE // size of the buffer
    )
}

其余代码:

// Define file operations for given file.
static const struct file_operations my_fops = {
    .owner = THIS_MODULE, // This is useful almost in any case
    .read = my_read, // Setup .read function
};

// And create the file itself
entry = proc_create_data(BOOT1_PROCFS_NAME, 0644, NULL, &my_fops, NULL);

关于c - Linux 内核代码中 create_proc_entry() 和 read_proc 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868218/

相关文章:

c - IP如何使用 "inet_ntop()"存储在Buffer中

c - 在 c 位域中添加额外的位

c++ - AT命令响应解析器

javascript - 将字符串作为 Uint8 缓冲区发送

memory - 不可逐出的页面有什么特殊之处?

新内核编译问题

linux - 使用 perf 确定进程何时以及为何进入不间断 sleep

linux - 如何从内核空间读取/写入 linux/proc 文件?

/proc/self/exe 可以映射吗?

android - ARM 处理器的 CPU Revision 和 Revision 字段有什么区别?