Chinaunix首页 | 论坛 | 博客
  • 博客访问: 325532
  • 博文数量: 101
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 774
  • 用 户 组: 普通用户
  • 注册时间: 2018-10-15 14:13
个人简介

搭建一个和linux开发者知识共享和学习的平台

文章分类

全部博文(101)

文章存档

2024年(15)

2023年(24)

2022年(27)

2019年(8)

2018年(27)

分类: LINUX

2022-10-21 15:04:57

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


 




typedef unsigned char    UI_08;
typedef unsigned short  UI_16;
typedef unsigned int    UI_32;
typedef char            SI_08;
typedef short            SI_16;
typedef int              SI_32;


static struct cdev *pCdev;
static dev_t ndev;
static struct class *fasync_class;
static struct device *fasync_dev;


static unsigned long flag=0;
static struct fasync_struct *sig_list;


static ssize_t read_flag(struct device *dev, struct device_attribute *attr, char *buf)
{
size_t count=0;

count+=sprintf(&buf[count],"%lu\n",flag);
return count;
}


static ssize_t write_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
flag=buf[0]-'0';
kill_fasync(&sig_list,SIGIO,POLL_IN);
return count;
}


static struct device_attribute flag_attr=__ATTR(flag,S_IRUGO|S_IWUSR,read_flag,write_flag);


static int fasync_test_open(struct inode *inode, struct file *filp)
{
return 0;
}


static int fasync_test_async(int fd, struct file *filp, int onflag)
{
return fasync_helper(fd,filp,onflag,&sig_list);
}


static struct file_operations ops=
{
.owner=THIS_MODULE,
.open=fasync_test_open,
.fasync=fasync_test_async,
};


static int umh_test( void )
{
char *argv[] = { "/usr/bin/logger", "the world is terrible!", NULL };
static char *envp[] = {
"HOME=/",
"TERM=linux",
"PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
printk("----umh_test------\n");
return call_usermodehelper( argv[0], argv, envp, UMH_WAIT_PROC );
}


static int readf_test(void)
{
char *buf;
struct file *filp = NULL;
struct inode *inode=NULL;
    mm_segment_t old_fs;
unsigned long magic=0;
unsigned int fsize=0;

printk("----readf_test--111-\n");

filp = filp_open("/data/fw_env.txt", O_RDONLY, 0);
if (IS_ERR(filp)) {  
        printk("create file error\n");  
        return -1;  
    } 


inode=filp->f_inode;


magic=inode->i_sb->s_magic;
printk("<1>file system magic:%li \n",magic);
printk("<1>super blocksize:%li \n",inode->i_sb->s_blocksize);
printk("<1>inode %li \n",inode->i_ino);
fsize=inode->i_size;
printk("<1>file size:%i \n",(int)fsize);


buf=(char *) kmalloc(fsize+1,GFP_ATOMIC);
    old_fs = get_fs();
     
    set_fs(KERNEL_DS);
if (filp->f_op->read)
{
printk("-----------filp->f_op->read-----------\n");
filp->f_op->read(filp, buf, fsize, &filp->f_pos);
}
else
vfs_read(filp, buf, fsize, &filp->f_pos);
buf[fsize]='\0';
printk("----readf_test--%s-\n",buf);

kfree(buf);
set_fs(old_fs);
filp_close(filp,NULL);
return 0;
}


static int __init fasync_test_init(void)
{
    int ret=-1;

    ret=alloc_chrdev_region(&ndev,0,1,"fasync_test_dev");
if(ret<0)
return ret;
pCdev=cdev_alloc();
cdev_init(pCdev,&ops);
pCdev->owner=THIS_MODULE;
cdev_add(pCdev,ndev,1);

fasync_class=class_create(THIS_MODULE,"fasync_dev");
if(IS_ERR(fasync_class))
return PTR_ERR(fasync_class);
fasync_dev=device_create(fasync_class,NULL,ndev,NULL,"fasync_dev");
if(IS_ERR(fasync_dev))
return PTR_ERR(fasync_dev);
ret=device_create_file(fasync_dev,&flag_attr);
umh_test();
readf_test();
    return ret;    
}


static void __exit fasync_test_exit(void)
{
    device_remove_file(fasync_dev,&flag_attr);
device_destroy(fasync_class,ndev);
class_destroy(fasync_class);
cdev_del(pCdev);
unregister_chrdev_region(ndev,1);
}




module_init(fasync_test_init);
module_exit(fasync_test_exit);




MODULE_LICENSE("GPL");








阅读(413) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~