内核模块中 读写文件

#include <linux/uaccess.h>
#include <linux/init.h>
#include <asm/fcntl.h>
#include <asm/processor.h>
#include <asm/uaccess.h>


struct file* filp = NULL;
mm_segment_t  old_fs;
loff_t pos;


filp = filp_open("/usr/sniffer/mytest",O_RDWR | O_CREAT,0644);

    if(IS_ERR(filp))
    {
        printk("Create New Log file failtrue!!\n");
    }    


//shensh 调试用
    if (1)
    {            
        if (0 < dataLen)
        {                        
            
                char protoData[2000];
                int  copyLen;

                copyLen = dataLen > 1999 ? 1999 : dataLen;
                memcpy(protoData, dataArea, copyLen);
                protoData[copyLen] = '\0';
                if (strlen(protoData) > 0)
                {
                    printk("[dataArea]strlen(protoData)>0 protocolCode=%u  protoData=%s dataLen=%d \n", connectTreeData->protocolCode, protoData, dataLen);
                }

                {
                    if(copyLen >1400)
                    {
                        printk("[dataArea]copyLen >1400 protocolCode=%u  protoData=%s copyLen=%d  dataLen=%d \n", connectTreeData->protocolCode, protoData, copyLen,dataLen);                        
                        if(filp!=NULL)
                        {
                            old_fs = get_fs();
                            set_fs(KERNEL_DS);
                        
                            pos = filp->f_pos;
                            int ret=vfs_write(filp, protoData, 100, &pos);

                            if(ret>0)
                            {
                                printk("New Log Write OK Length:%d \n", ret);
                            }else
                            {
                                printk("Write Log File Error \n");
                            }

                            filp->f_pos = pos;
                            
                            set_fs(old_fs);

                            filp_close(filp, NULL);
                            filp = NULL;                                    
                        }
                    }
                
            }

            AuditFTPConnect(connectTreeData, (char *)ipHeader, dataArea, dataLen);
        }
    }            

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值