linux-kernel - 内核模块的 proc_create() 示例

标签 linux-kernel linux-device-driver

谁能给我proc_create()例子?

早些时候他们使用了create_proc_entry()在内核中,但现在他们使用 proc_create() .

最佳答案

这是一个“hello_proc”代码,它使用了较新的“proc_create()”接口(interface)。

#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

static int hello_proc_show(struct seq_file *m, void *v) {
  seq_printf(m, "Hello proc!\n");
  return 0;
}

static int hello_proc_open(struct inode *inode, struct  file *file) {
  return single_open(file, hello_proc_show, NULL);
}

static const struct file_operations hello_proc_fops = {
  .owner = THIS_MODULE,
  .open = hello_proc_open,
  .read = seq_read,
  .llseek = seq_lseek,
  .release = single_release,
};

static int __init hello_proc_init(void) {
  proc_create("hello_proc", 0, NULL, &hello_proc_fops);
  return 0;
}

static void __exit hello_proc_exit(void) {
  remove_proc_entry("hello_proc", NULL);
}

MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);
此代码取自 http://pointer-overloading.blogspot.com/2013/09/linux-creating-entry-in-proc-file.html

关于linux-kernel - 内核模块的 proc_create() 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8516021/

相关文章:

c - 如何在 C 中以任何预处理器指令形式定义一组配置

linux - 为什么我们需要在poll中调用poll_wait?

android - Uinput虚拟设备在android上被检测为物理键盘

linux - 完全消除现代 Linux >=5.0 中的计时器滴答

multithreading - futex 工具返回了意外的错误代码?

c - 使用pmap分析进程的内存映射。 [堆栈]

c - 内核模块中的进程描述符

linux-device-driver - .o 和 .ko 文件之间的区别

linux - 如何使用系统内核二进制文件本身作为转储捕获内核?

linux-device-driver - Linux 设备驱动程序基础知识