#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/fs.h>
#define PROC_NAME "myproc"
static int proc_open(struct inode *inodp, struct file *filp);
static struct file_operations proc_fops = {
.owner = THIS_MODULE,
.open = proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
void * seq_start(struct seq_file *m, loff_t *pos)
{
loff_t *item;
item = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (!item)
goto err;
*item = *pos;
err:
return item;
}
void seq_stop(struct seq_file *m, void *v)
{
kfree(v);
}
void * seq_next(struct seq_file *m, void *v, loff_t *pos)
{
loff_t *item = (loff_t *) v;
*pos = ++(*item);
return item;
}
int seq_show(struct seq_file *m, void *v)
{
loff_t *item = (loff_t *) v;
seq_printf(m, "%Ld\n", *item);
return 0;
}
static struct seq_operations seq_ops = {
.start = seq_start,
.stop = seq_stop,
.next = seq_next,
.show = seq_show,
};
static int proc_open(struct inode *inodp, struct file *filp)
{
return seq_open(filp, &seq_ops);
}
static int __init proc_init(void)
{
struct proc_dir_entry *entry;
entry = create_proc_entry(PROC_NAME, 0, NULL);
if (!entry)
printk(KERN_ALERT "create failure.\n");
entry->proc_fops = &proc_fops;
return 0;
}
static void __exit proc_exit(void)
{
remove_proc_entry(PROC_NAME, NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_exit);
|