#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");
阅读(305) | 评论(0) | 转发(0) |