一个使用 seq_file 接口的 proc_fs 例子

本文介绍了一个简单的Linux内核模块实现,通过该模块可以在/proc目录下创建名为fruits的虚拟文件。此文件用于展示一系列字符串数组内容,具体包括五种水果名称:苹果、橙子、香蕉、西瓜和梨。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

static struct proc_dir_entry *pfile;

static char *myfruits[5] = {"apple", "orange", "banana", "watermelon", "pear"};

static void *my_seq_start(struct seq_file *s, loff_t *pos)
{
        if (*pos >= 5)
                return NULL;
        return myfruits[*pos];
}

static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
        if (++*pos >= 5)
                return NULL;
        return myfruits[*pos];
}

static void my_seq_stop(struct seq_file *s, void *v)
{
    // nothing to do;
}

static int my_seq_show(struct seq_file *s, void *v)
{
        seq_printf(s, "%s ", (char *)v);
        return 0;
}

static struct seq_operations proc_seq_ops = {
        .start = my_seq_start,
        .next = my_seq_next,
        .stop = my_seq_stop,
        .show = my_seq_show,
};

static int my_seq_open(struct inode *inode, struct file *file)
{
        return seq_open(file, &proc_seq_ops);
}

static struct file_operations proc_fops = {
        .open = my_seq_open,
        .read = seq_read,
        .llseek = seq_lseek,
        .release = seq_release,
};

static int __init myproc_init(void)
{
        pfile = proc_create("fruits", 0666, NULL, &proc_fops);
        if (!pfile) {
                printk(KERN_ERR "Can't create /proc/fruits/n");
                remove_proc_entry("mydir", NULL);
                return -1;
        }

        return 0;
}

static void __exit myproc_exit(void)
{
        remove_proc_entry("fruits", NULL);
}

module_init(myproc_init);
module_exit(myproc_exit);
#include <linux/module.h> #include <linux/kernel.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <linux/ip.h> #include <linux/icmp.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/timekeeping.h> #include <linux/version.h> #define MAX_ENTRIES 1000 // 最大统计条目数 #define PROC_NAME "ping_stat" // proc文件名 // 统计数据结构 struct ping_stat_entry { struct timespec64 timestamp; // 时间戳 __be32 src_ip; // 源IP地址 struct list_head list; // 链表指针 }; // 全局变量 static struct list_head stat_list; // 统计链表 static DEFINE_SPINLOCK(stat_lock); // 保护链表的自旋锁 static unsigned int param_n = 100; // 默认数据大小 static unsigned int packet_count = 0; // 总包计数 static struct proc_dir_entry *proc_entry; // proc文件入口 // 模块参数声明 module_param(param_n, uint, 0); MODULE_PARM_DESC(param_n, "ICMP data size to filter (100-1000)"); // Netfilter钩子函数 static unsigned int hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { struct iphdr *ip_header; struct icmphdr *icmp_header; unsigned int data_size; struct timespec64 now; struct ping_stat_entry *new_entry; // 获取IP头 ip_header = ip_hdr(skb); if (!ip_header || ip_header->version != 4) return NF_ACCEPT; // 检查是否为ICMP协议 if (ip_header->protocol != IPPROTO_ICMP) return NF_ACCEPT; // 获取ICMP头 icmp_header = (struct icmphdr *)((char *)ip_header + (ip_header->ihl * 4)); if (!icmp_header) return NF_ACCEPT; // 检查是否为回显请求(ping) if (icmp_header->type != ICMP_ECHO) return NF_ACCEPT; // 计算数据部分大小 data_size = ntohs(ip_header->tot_len) - (ip_header->ihl * 4) - sizeof(struct icmphdr); // 检查数据大小是否符合要求 if (data_size != param_n) return NF_ACCEPT; // 获取当前时间 ktime_get_real_ts64(&now); // 创建新条目 new_entry = kmalloc(sizeof(*new_entry), GFP_ATOMIC); if (!new_entry) return NF_ACCEPT; // 填充数据 new_entry->timestamp = now; new_entry->src_ip = ip_header->saddr; // 加锁保护链表 spin_lock(&stat_lock); // 更新统计信息 packet_count++; // 维护链表大小 if (packet_count > MAX_ENTRIES) { struct ping_stat_entry *old_entry; old_entry = list_first_entry(&stat_list, struct ping_stat_entry, list); list_del(&old_entry->list); kfree(old_entry); } // 添加到链表尾部 list_add_tail(&new_entry->list, &stat_list); spin_unlock(&stat_lock); return NF_ACCEPT; } // Netfilter钩子配置 static struct nf_hook_ops nfho = { .hook = hook_func, .pf = NFPROTO_IPV4, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP_PRI_FIRST, }; // seq_file操作:开始迭代 static void *ping_seq_start(struct seq_file *s, loff_t *pos) { spin_lock(&stat_lock); return seq_list_start(&stat_list, *pos); } // seq_file操作:下一个元素 static void *ping_seq_next(struct seq_file *s, void *v, loff_t *pos) { return seq_list_next(v, &stat_list, pos); } // seq_file操作:结束迭代 static void ping_seq_stop(struct seq_file *s, void *v) { spin_unlock(&stat_lock); } // seq_file操作:显示元素 static int ping_seq_show(struct seq_file *s, void *v) { struct ping_stat_entry *entry = list_entry(v, struct ping_stat_entry, list); char ip_str[16]; // 格式化IP地址 snprintf(ip_str, sizeof(ip_str), "%pI4", &entry->src_ip); // 输出统计信息 seq_printf(s, "Time: %lld.%09ld | Source: %s\n", (long long)entry->timestamp.tv_sec, entry->timestamp.tv_nsec, ip_str); return 0; } // seq_file操作结构 static const struct seq_operations ping_seq_ops = { .start = ping_seq_start, .next = ping_seq_next, .stop = ping_seq_stop, .show = ping_seq_show, }; // 打开proc文件 static int ping_proc_open(struct inode *inode, struct file *file) { return seq_open(file, &ping_seq_ops); } // proc文件操作 static const struct proc_ops ping_proc_ops = { .proc_open = ping_proc_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release, }; // 模块初始化 static int __init ping_stat_init(void) { // 检查参数范围 if (param_n < 100 || param_n > 1000) { printk(KERN_ERR "Invalid param_n value (%d), must be 100-1000\n", param_n); return -EINVAL; } // 初始化链表和锁 INIT_LIST_HEAD(&stat_list); // 注册Netfilter钩子 if (nf_register_net_hook(&init_net, &nfho)) { printk(KERN_ERR "Failed to register netfilter hook\n"); return -ENODEV; } // 创建proc文件 proc_entry = proc_create(PROC_NAME, 0, NULL, &ping_proc_ops); if (!proc_entry) { nf_unregister_net_hook(&init_net, &nfho); printk(KERN_ERR "Failed to create proc entry\n"); return -ENOMEM; } printk(KERN_INFO "Ping stat module loaded, filtering %d byte packets\n", param_n); return 0; } // 模块退出 static void __exit ping_stat_exit(void) { struct ping_stat_entry *entry, *tmp; // 移除proc文件 if (proc_entry) proc_remove(proc_entry); // 注销Netfilter钩子 nf_unregister_net_hook(&init_net, &nfho); // 清理链表内存 spin_lock(&stat_lock); list_for_each_entry_safe(entry, tmp, &stat_list, list) { list_del(&entry->list); kfree(entry); } spin_unlock(&stat_lock); printk(KERN_INFO "Ping stat module unloaded\n"); } module_init(ping_stat_init); module_exit(ping_stat_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("ICMP Ping Packet Statistics Module");改进代码,我需要在ping_seq_show里显示报文数目
08-05
#include <linux/module.h> #include <linux/kernel.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <linux/ip.h> #include <linux/icmp.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/timekeeping.h> #include <linux/version.h> #define MAX_ENTRIES 1000 // 最大统计条目数 #define PROC_NAME “ping_stat” // proc文件名 // 统计数据结构 struct ping_stat_entry { struct timespec64 timestamp; // 时间戳 __be32 src_ip; // 源IP地址 struct list_head list; // 链表指针 }; // 全局变量 static struct list_head stat_list; // 统计链表 static DEFINE_SPINLOCK(stat_lock); // 保护链表的自旋锁 static unsigned int param_n = 100; // 默认数据大小 static unsigned int packet_count = 0; // 总包计数 static struct proc_dir_entry *proc_entry; // proc文件入口 // 模块参数声明 module_param(param_n, uint, 0); MODULE_PARM_DESC(param_n, “ICMP data size to filter (100-1000)”); // Netfilter钩子函数 static unsigned int hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { struct iphdr *ip_header; struct icmphdr *icmp_header; unsigned int data_size; struct timespec64 now; struct ping_stat_entry *new_entry; // 获取IP头 ip_header = ip_hdr(skb); if (!ip_header || ip_header->version != 4) return NF_ACCEPT; // 检查是否为ICMP协议 if (ip_header->protocol != IPPROTO_ICMP) return NF_ACCEPT; // 获取ICMP头 icmp_header = (struct icmphdr *)((char *)ip_header + (ip_header->ihl * 4)); if (!icmp_header) return NF_ACCEPT; // 检查是否为回显请求(ping) if (icmp_header->type != ICMP_ECHO) return NF_ACCEPT; // 计算数据部分大小 data_size = ntohs(ip_header->tot_len) - (ip_header->ihl * 4) - sizeof(struct icmphdr); // 检查数据大小是否符合要求 if (data_size != param_n) return NF_ACCEPT; // 获取当前时间 ktime_get_real_ts64(&now); // 创建新条目 new_entry = kmalloc(sizeof(*new_entry), GFP_ATOMIC); if (!new_entry) return NF_ACCEPT; // 填充数据 new_entry->timestamp = now; new_entry->src_ip = ip_header->saddr; // 加锁保护链表 spin_lock(&stat_lock); // 更新统计信息 packet_count++; // 维护链表大小 if (packet_count > MAX_ENTRIES) { struct ping_stat_entry *old_entry; old_entry = list_first_entry(&stat_list, struct ping_stat_entry, list); list_del(&old_entry->list); kfree(old_entry); } // 添加到链表尾部 list_add_tail(&new_entry->list, &stat_list); spin_unlock(&stat_lock); return NF_ACCEPT; } // Netfilter钩子配置 static struct nf_hook_ops nfho = { .hook = hook_func, .pf = NFPROTO_IPV4, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP_PRI_FIRST, }; // seq_file操作:开始迭代 static void *ping_seq_start(struct seq_file *s, loff_t *pos) { spin_lock(&stat_lock); return seq_list_start(&stat_list, *pos); } // seq_file操作:下一个元素 static void *ping_seq_next(struct seq_file *s, void *v, loff_t *pos) { return seq_list_next(v, &stat_list, pos); } // seq_file操作:结束迭代 static void ping_seq_stop(struct seq_file *s, void *v) { spin_unlock(&stat_lock); } // seq_file操作:显示元素 static int ping_seq_show(struct seq_file *s, void *v) { struct ping_stat_entry *entry = list_entry(v, struct ping_stat_entry, list); char ip_str[16]; // 格式化IP地址 snprintf(ip_str, sizeof(ip_str), "%pI4", &entry->src_ip); // 输出统计信息 seq_printf(s, "Time: %lld.%09ld | Source: %s\n", (long long)entry->timestamp.tv_sec, entry->timestamp.tv_nsec, ip_str); return 0; } // seq_file操作结构 static const struct seq_operations ping_seq_ops = { .start = ping_seq_start, .next = ping_seq_next, .stop = ping_seq_stop, .show = ping_seq_show, }; // 打开proc文件 static int ping_proc_open(struct inode *inode, struct file *file) { return seq_open(file, &ping_seq_ops); } // proc文件操作 static const struct proc_ops ping_proc_ops = { .proc_open = ping_proc_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release, }; // 模块初始化 static int __init ping_stat_init(void) { // 检查参数范围 if (param_n < 100 || param_n > 1000) { printk(KERN_ERR “Invalid param_n value (%d), must be 100-1000\n”, param_n); return -EINVAL; } // 初始化链表和锁 INIT_LIST_HEAD(&stat_list); // 注册Netfilter钩子 if (nf_register_net_hook(&init_net, &nfho)) { printk(KERN_ERR "Failed to register netfilter hook\n"); return -ENODEV; } // 创建proc文件 proc_entry = proc_create(PROC_NAME, 0, NULL, &ping_proc_ops); if (!proc_entry) { nf_unregister_net_hook(&init_net, &nfho); printk(KERN_ERR "Failed to create proc entry\n"); return -ENOMEM; } printk(KERN_INFO "Ping stat module loaded, filtering %d byte packets\n", param_n); return 0; } // 模块退出 static void __exit ping_stat_exit(void) { struct ping_stat_entry *entry, *tmp; // 移除proc文件 if (proc_entry) proc_remove(proc_entry); // 注销Netfilter钩子 nf_unregister_net_hook(&init_net, &nfho); // 清理链表内存 spin_lock(&stat_lock); list_for_each_entry_safe(entry, tmp, &stat_list, list) { list_del(&entry->list); kfree(entry); } spin_unlock(&stat_lock); printk(KERN_INFO "Ping stat module unloaded\n"); } module_init(ping_stat_init); module_exit(ping_stat_exit); MODULE_LICENSE(“GPL”); MODULE_AUTHOR(“Your Name”); MODULE_DESCRIPTION(“ICMP Ping Packet Statistics Module”);我希望在ping_seq_show函数中加入显示报文数目,包字节大小,时间用年月日时分秒的格式显示
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值