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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM