ssize_t read_reversion(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
if(copy_to_user(buf, "kernel", sizeof("kernel"))){
printk(KERN_ERR "reversion:can't set date to user space\n");
return EFAULT;
}
return 0;
}
ssize_t write_reversion(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
char auBuf[128] = {0};
if(copy_from_user(auBuf, buf, count)){
printk(KERN_ERR "reversion:can't get date form user space\n");
return EFAULT;
}
printk(KERN_DEBUG "reversion:get data form user space:%s\n", auBuf);
return 0;
}
long ioctl_reversion(struct file *filp, unsigned int cmd, unsigned long arg)
{
return 0;
}
pct@ubuntu:~$ cat /proc/devices | grep reversion
246 reversion
mknod [OPTION]... NAME TYPE [MAJOR MINOR]
pct@ubuntu:~$ cat /proc/devices | awk '{if($2=="reversion")print $1}'
246
pct@ubuntu:~$ sudo mknod /dev/reversion c 246 0
[sudo] password for pct:
pct@ubuntu:~$ ls /dev/reversion -l
crw-r--r-- 1 root root 246, 0 Jan 3 17:24 /dev/reversion
#define DEV "/dev/reversion"
int dev_file = 0;
int dev_open()
{
if(access(DEV,F_OK)){
FILE *fp = popen("cat /proc/devices | awk '{if($2==\"reversion\")print $1}'", "r");
char auMajor[5] = {0};
if(0 == fread(auMajor, 1, sizeof(auMajor), fp))
{
printf("fread error:%s\n", strerror(errno));
return -1;
}
int iMajor = atoi(auMajor);
printf("the device major is:%d\n", iMajor);
char auCommand[256] = {0};
snprintf(auCommand, sizeof(auCommand), "mknod /dev/reversion c %d 0", iMajor);
printf("command:%s\n", auCommand);
system(auCommand);
}
dev_file = open(DEV,O_RDWR);
if(dev_file == -1){
printf("open error:%s\n", strerror(errno));
return -1;
}
return 0;
}
char auRead[256] = {0};
if(read(dev_file, auRead, sizeof(auRead)) == -1){
printf("read error:%s\n", strerror(errno));
return -1;
}
printf("read:%s\n", auRead);