Linux驱动 | procfs接口创建
上篇介绍了Linux驱动中sysfs接口的创建,今天介绍procfs接口的创建。
procfs
:可实现类似cat /proc/cpuinfo
的操作procfs
:可实现类似cat /proc/cpuinfo
的操作
procfs接口创建
实现效果:
例如, 在/proc
下创建一个clk节点,通过cat /proc/clk
procfs接口创建
实现效果:
/proc
下创建一个clk节点,通过cat /proc/clk
可查看内容:系统 | |
---|---|
Linux |
在驱动中添加以下代码:
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { //cat显示的内容 seq_printf(m, "pll0: %u Mhz\n" "pll1: %u Mhz\n" "pll2: %u Mhz\n", 100, 200, 300); return 0; } static int clk_info_open(struct inode *inode, struct file *filp) { return single_open(filp, proc_clk_show, NULL); } static struct file_operations myops = { .owner = THIS_MODULE, .open = clk_info_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, }; static int __init my_module_init(void) { //注册proc接口 my_proc_entry = proc_create("clk", 0644, NULL, &myops); return 0; } static void __exit my_module_exit(void) { //注销proc接口 proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
procfs接口的创建,主要是实现struct file_operations
结构体,然后通过proc_create
函数进行注册,通过proc_remove
函数进行注销。struct file_operations
结构体,然后通过proc_create
函数进行注册,通过proc_remove
函数进行注销。
procfs通常是用来获取CPU、内存、进程等各种信息,例如cat /proc/cpuinfo
、cat /proc/meminfo
,所以我们只需要实现.open成员函数。当使用cat
命令查看/proc
下的信息时,会调用到.open
对应的实现函数。
这里我们使用了seq_file
接口,需要记住的是,procfs通常会和seq_file接口一起使用。seq_file是一个序列文件接口,当我们创建的proc数据内容由一系列数据顺序组合而成或者是比较大的proc文件系统时,都建议使用seq_file接口,例如cat /proc/meminfo
cat /proc/cpuinfo
、cat /proc/meminfo
,所以我们只需要实现.open成员函数。当使用cat
命令查看/proc
下的信息时,会调用到.open
对应的实现函数。🎜🎜这里我们使用了seq_file
接口,需要记住的是,procfs通常会和seq_file接口一起使用。seq_file是一个序列文件接口,当我们创建的proc数据内容由一系列数据顺序组合而成或者是比较大的proc文件系统时,都建议使用seq_file接口,例如cat /proc/meminfo
就会显示很多内容。🎜seq_file接口主要就是解决proc接口编程存在的问题,推荐在proc接口编程时使用seq_file接口,另外.read、.llseek、.release成员函数也可以直接用seq_read
、seq_lseek
和seq_release
。seq_read
、seq_lseek
和seq_release
。
proc新接口
注意,在较新版本的内核中,procfs
proc新接口
注意,在较新版本的内核中,procfs
的函数接口有所变化。系统 | 内核版本 |
---|---|
Linux | 5.10.111 |
在驱动中添加以下代码:
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { seq_printf(m, "pll0: %lu Mhz\n" "pll1: %lu Mhz\n" "pll2: %lu Mhz\n", 100, 200, 300); return 0; } static int clk_info_open(struct inode *inode, struct file *filp) { return single_open(filp, proc_clk_show, NULL); } static const struct proc_ops clk_stat_proc_fops = { .proc_open = clk_info_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release, }; static int __init my_module_init(void) { my_proc_entry = proc_create("clk", 0, NULL, &clk_stat_proc_fops); return 0; } static void __exit my_module_exit(void) { proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
新的proc
接口中,将原来的struct file_operations
换成了struct proc_ops
,其中成员函数也添加了对应的前缀proc
,但本质还是一样的,只是换了名字,更加规范了一些。
以上是Linux驱动 | procfs接口创建的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

错误日志位于 /var/log/nginx(Linux)或 /usr/local/var/log/nginx(macOS),使用命令行清理步骤:1. 备份原日志;2. 创建空文件作为新日志;3. 重启 Nginx 服务。也可使用第三方工具(如 logrotate)或配置自动清理。
