九鼎创展论坛
标题:
proc编程(proc_create_data)
[打印本页]
作者:
armeasy
时间:
2014-10-21 23:18
标题:
proc编程(proc_create_data)
[size=13.333333015441895px]test_main.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <net/net_namespace.h>
#include <linux/mm.h>
MODULE_LICENSE("GPL");
struct _DATA_INFO{
int data1;
int data2;
};
static struct _DATA_INFO data_info[2];
/* PROC stuff */
static void *dl_seq_start(struct seq_file *s, loff_t *pos)
{
static unsigned long counter = 0;
if ( *pos == 0 )
{
return &counter;
}
else
{
*pos = 0;
return NULL;
}
}
static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
return NULL;
}
static void dl_seq_stop(struct seq_file *s, void *v)
{
}
static int dl_seq_show(struct seq_file *s, void *v)
{
struct proc_dir_entry *pde = s->private;
struct _DATA_INFO *info = pde->data;
seq_printf(s, "%d----%d",info->data1,info->data2);
return 0;
}
static struct seq_operations dl_seq_ops = {
.start = dl_seq_start,
.next = dl_seq_next,
.stop = dl_seq_stop,
.show = dl_seq_show
};
static int dl_proc_open(struct inode *inode, struct file *file)
{
int ret = seq_open(file, &dl_seq_ops);
if (!ret) {
struct seq_file *sf = file->private_data;
sf->private = PDE(inode);
}
return ret;
}
static const struct file_operations dl_file_ops = {
.owner = THIS_MODULE,
.open = dl_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
void init_mem(void)
{
data_info[0].inflow=1;
data_info[0].upflow=2;
proc_create_data("proc_test1", 0, init_net.proc_net, &dl_file_ops, &data_info[0]);
data_info[1].inflow=3;
data_info[1].upflow=4;
proc_create_data("proc_test2", 0, init_net.proc_net, &dl_file_ops, &data_info[1]);
}
static int __init init_mem_pool(void)
{
init_mem();
return 0;
}
static void __exit exit_mem_pool(void)
{
remove_proc_entry("proc_test1", init_net.proc_net);
remove_proc_entry("proc_test2", init_net.proc_net);
}
module_init(init_mem_pool);
module_exit(exit_mem_pool);
Makefile:
TARGET = test_main
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
CF = *.o .*o.d .*.cmd .*.flags *.mod.c Module.* modules.* .tmp_*
obj-m := $(TARGET).o
default:
make -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
-rm -rf $(CF)
-include $(KDIR)/Rules.make
复制代码
[size=13.333333015441895px]运行:
insmod test_main.ko
cat /proc/net/proc_test1
cat /proc/net/proc_test2
复制代码
欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/)
Powered by Discuz! X3.2