1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*
*
* Copyright (C) 2012 Simon Kagstrom
*
* Author: Simon Kagstrom <simon.kagstrom@gmail.com>
*
* Most of the stuff comes from Jonathan Corbets seqfile example here:
*
* http://lwn.net/Articles/22359/
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
/*
* The sequence iterator functions. We simply use the count of the
* next line as our internal position.
*/
static void *ct_seq_start(struct seq_file *s, loff_t *pos)
{
loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (! spos)
return NULL;
*spos = *pos;
return spos;
}
static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
loff_t *spos = (loff_t *) v;
*pos = ++(*spos);
return spos;
}
static void ct_seq_stop(struct seq_file *s, void *v)
{
kfree (v);
}
/*
* The show function.
*/
static int ct_seq_show(struct seq_file *s, void *v)
{
loff_t *spos = (loff_t *) v;
seq_printf(s, "%Ld\n", *spos);
return 0;
}
/*
* Tie them all together into a set of seq_operations.
*/
static struct seq_operations ct_seq_ops = {
.start = ct_seq_start,
.next = ct_seq_next,
.stop = ct_seq_stop,
.show = ct_seq_show
};
/*
* Time to set up the file operations for our /proc file. In this case,
* all we need is an open function which sets up the sequence ops.
*/
static int ct_open(struct inode *inode, struct file *file)
{
return seq_open(file, &ct_seq_ops);
};
/*
* The file operations structure contains our open function along with
* set of the canned seq_ ops.
*/
static struct file_operations ct_file_ops = {
.owner = THIS_MODULE,
.open = ct_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
static int __init test_init(void)
{
struct proc_dir_entry *entry;
printk(KERN_INFO "initing test module!\n");
entry = create_proc_entry("test_module", 0, NULL);
if (entry)
entry->proc_fops = &ct_file_ops;
return 0;
}
static void __exit test_exit(void)
{
printk(KERN_INFO "Exiting test module!\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("Simon Kagstrom <simon.kagstrom@gmail.com>");
MODULE_DESCRIPTION("Test");
MODULE_LICENSE("GPL");
|