免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1922 | 回复: 5
打印 上一主题 下一主题

内核与应用层交互 [复制链接]

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-30 09:20 |只看该作者 |倒序浏览
内核与应用层交互的方式都有哪几种,最好是能够有代码实例。
我先抛砖引玉了。
proc文件系统实现内核与应用层的交互。下面是代码,基于2.6.24.4内核。
#include <linux/spinlock.h>
#include <linux/version.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
#include <net/netfilter/nf_conntrack.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_layer7.h>
#include <linux/ctype.h>
#include <linux/proc_fs.h>


MODULE_LICENSE("GPL");
MODULE_AUTHOR("LTW <tingw.liu@gmail.com>");
MODULE_DESCRIPTION("interaction with proc file");
//static struct nf_hook_ops procfile;
static int procnum=20;

static struct proc_dir_entry* father;

/*unsigned int procfile_func(unsigned int hooknum,struct sk_buff *skb,const struct net_device *in,const struct net_device *out,int(*okfn)(struct sk_buff*))
{
        printk(KERN_ALERT "OK\n");
        return NF_ACCEPT;
}*/

static int my_atoi(const char *s)
{
        int val = 0;

        for (;; s++) {
                switch (*s) {
                        case '0'...'9':
                        val = 10*val+(*s-'0');
                        break;
                default:
                        return val;
                }
        }
}
/* write out num_packets to userland. */
static int procfile_read_proc(char* page, char ** start, off_t off, int count,int* eof, void * data)
{
        if(procnum>99)
                printk(KERN_ALERT "procnum is too big\n");
        page[0] = procnum/10 + '0';
        page[1] = procnum%10 + '0';
        page[2] = '\n';
        page[3] = '\0';

        *eof=1;

        return 3;
}

/* Read in num_packets from userland */
static int procfile_write_proc(struct file* file, const char* buffer,
                             unsigned long count, void *data)
{
        char * foo = kmalloc(count, GFP_ATOMIC);

        if(!foo){
                return count;
        }

        if(copy_from_user(foo, buffer, count)) {
                return -EFAULT;
        }


        procnum= my_atoi(foo);
        kfree (foo);
        if(procnum > 99) {
                printk(KERN_ALERT "procnum can't be > 99.\n");
                procnum = 99;
        } else if(procnum < 1) {
                printk(KERN_ALERT "procnum can't be < 1.\n");
                procnum = 1;
        }

        return count;
}


static void procfile_cleanup_proc(void)
{
        remove_proc_entry("procfile",father);
        remove_proc_entry("procdir",init_net.proc_net);
}

static int procfile_init_proc(void)
{
        struct proc_dir_entry* entry;
        father=proc_mkdir("procdir",init_net.proc_net);
        entry = create_proc_entry("procfile", 0644,father);
        entry->read_proc = procfile_read_proc;
        entry->write_proc = procfile_write_proc;
        return 1;
}

static int __init procfile_init(void)
{
        printk(KERN_ALERT "procfile init\n");
        return procfile_init_proc();
}
static void __exit procfile_exit(void)
{
        procfile_cleanup_proc();
        printk(KERN_ALERT "procfile exit\n");
}
module_init(procfile_init);
module_exit(procfile_exit);

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2009-10-30 09:24 |只看该作者

回复 #1 瀚海书香 的帖子

可多了,这贴可以慢慢总结出来

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
3 [报告]
发表于 2009-10-30 09:40 |只看该作者

回复 #2 dreamice 的帖子

dreamice兄也给小弟分享一下吧

论坛徽章:
0
4 [报告]
发表于 2009-10-30 15:05 |只看该作者
我都用netlink

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
5 [报告]
发表于 2009-10-30 17:53 |只看该作者

回复 #3 瀚海书香 的帖子

驱动版,我以前写过seq_file和proc,现在确实没时间,一直酝酿总结一下呢,呵呵

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
6 [报告]
发表于 2009-10-30 19:12 |只看该作者
还有netlink和sockopt。LZ可以去我的blog上看一下,总结了有proc,mmap,netlink和sockopt这几种方式。
http://blog.chinaunix.net/u/33048/
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP