Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1070866
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2010-07-30 09:07:40

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/net.h>
#include <net/sock.h>
#include <linux/in.h>
#include <linux/types.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <asm-generic/unaligned.h>
#include <linux/sysctl.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <asm/checksum.h>
#include <linux/ip.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <net/net_namespace.h>
#include <net/route.h>
#include <linux/route.h>
#include <linux/stddef.h>
#include <linux/mutex.h>

#define err(msg) printk(KERN_ERR "%s failed.\n", msg)

struct list_data_struct
{
    struct list_head n_list;
    int v;
};

static spinlock_t lock;
static struct list_head head;

static void * myproc_seq_start(struct seq_file *seq, loff_t *pos)
{
    spin_lock_bh(&lock);
    return seq_list_start(&head, *pos);
}

static void * myproc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
    return seq_list_next(v, &head, pos);
}

static void myproc_seq_stop(struct seq_file *seq, void *v)
{
    spin_unlock_bh(&lock);
}

static int myproc_seq_show(struct seq_file *seq, void *v)
{
    struct list_data_struct *item = list_entry(v, struct list_data_struct, n_list);

    seq_printf(seq, "%d\n", item->v);
    return 0;
}

static struct seq_operations myproc_seq_ops =
{
    .start = myproc_seq_start,
    .next = myproc_seq_next,
    .stop = myproc_seq_stop,
    .show = myproc_seq_show,
};

static int myproc_open(struct inode *inodp, struct file *filp)
{
    return seq_open(filp, &myproc_seq_ops);
}

static ssize_t myproc_write(struct file *filp, const char __user *buff, size_t len, loff_t *pos)
{
    struct list_data_struct *item;

    if ((item = kzalloc(sizeof(struct list_data_struct), GFP_ATOMIC)) == NULL)
    {
        err("kzalloc");
        goto out;
    }

    sscanf(buff, "%d", &item->v);

    spin_lock_bh(&lock);
    list_add(&item->n_list, &head);
    spin_unlock_bh(&lock);

    return len;
out:
    return -1;
}

static struct file_operations myproc_fops =
{
    .owner     = THIS_MODULE,
    .open     = myproc_open,
    .read     = seq_read,
    .write        = myproc_write,
    .llseek     = seq_lseek,
    .release     = seq_release,
};

static int __init myproc_init(void)
{
    spin_lock_init(&lock);
    INIT_LIST_HEAD(&head);

    proc_net_fops_create(&init_net, "myproc", 0, &myproc_fops);
    return 0;
}

static void __exit myproc_exit(void)
{
    struct list_data_struct *pos, *n;

    proc_net_remove(&init_net, "myproc");

    spin_lock_bh(&lock);
    list_for_each_entry_safe(pos, n, &head, n_list)
    {
        kfree(pos);
    }
    spin_unlock_bh(&lock);
}

MODULE_LICENSE("GPL");

module_init(myproc_init);
module_exit(myproc_exit);


阅读(695) | 评论(0) | 转发(0) |
0

上一篇:netlink接口

下一篇:获取时间

给主人留下些什么吧!~~