linux下ioctl遇到的坑

在驱动编程里面经常会用到ioctl的系统调用,发现cmd = 2的时候,用户ioctl直接返回-1。

原因在于在linux-x.xx/fs/ioctl.c定义的do_vfs_ioctl函数

 1 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 2          unsigned long arg)
 3 {
 4     int error = 0;
 5     int __user *argp = (int __user *)arg;
 6     struct inode *inode = file_inode(filp);
 7 
 8     switch (cmd) {
 9         ......
10         ......
11         ......
12     case FS_IOC_FIEMAP:
13         return ioctl_fiemap(filp, arg);
14 
15     case FIGETBSZ:
16         return put_user(inode->i_sb->s_blocksize, argp);
17 
18     default:
19         if (S_ISREG(inode->i_mode))
20             error = file_ioctl(filp, cmd, arg);
21         else
22             error = vfs_ioctl(filp, cmd, arg);
23         break;
24     }
25     return error;
26 }

发现do_vfs_ioctl实现先判断系统的cmd不匹配后才判断用户的cmd。

 

从以下代码可以得出 FIGETBSZ = 2。

 1 #define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */
 2 
 3 
 4 * used to create numbers * /
 5 #define _IO(type,nr)        _IOC(_IOC_NONE,(type),(nr),0)
 6 #define _IOR(type,nr,size)    _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
 7 #define _IOW(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
 8 #define _IOWR(type,nr,size)    _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
 9 #define _IOR_BAD(type,nr,size)    _IOC(_IOC_READ,(type),(nr),sizeof(size))
10 #define _IOW_BAD(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
11 #define _IOWR_BAD(type,nr,size)    _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
12 
13 
14 #define _IOC(dir,type,nr,size) \
15      (((dir)  << _IOC_DIRSHIFT) | \
16       ((type) << _IOC_TYPESHIFT) | \
17       ((nr)   << _IOC_NRSHIFT) | \
18       ((size) << _IOC_SIZESHIFT))
19 
20 
21 #define _IOC_NRSHIFT    0
22 #define _IOC_TYPESHIFT    (_IOC_NRSHIFT+_IOC_NRBITS)
23 #define _IOC_SIZESHIFT    (_IOC_TYPESHIFT+_IOC_TYPEBITS)
24 #define _IOC_DIRSHIFT    (_IOC_SIZESHIFT+_IOC_SIZEBITS)
25 
26 #define _IOC_NRBITS    8
27 #define _IOC_TYPEBITS    8
28 
29 
30 #ifndef _IOC_SIZEBITS
31 #define _IOC_SIZEBITS    14
32 #endif
33 
34 #ifndef _IOC_DIRBITS
35 #define _IOC_DIRBITS    2
36 #endif

其实内核cmd有一个格式,使用户cmd不与系统cmd冲突,解决办法就是用_IO、_IOW、_IOR和_IOWR产生cmd。

cmd的格式为:

DIR:SIZE:TYPE:NUM = 2:14:8:8

其中TYPE可以自定义一个ASCII码,假设是一条输入命令,NUM = 1,SIZE为char。则可以利用_IOW('a', 1, char)自动生成cmd

posted @ 2016-07-13 00:52  Kevin_Hwang  阅读(6627)  评论(0编辑  收藏  举报