ioctl.h 分析

2024-06-06 09:58
文章标签 分析 ioctl

本文主要是介绍ioctl.h 分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ioctl.h 分析





我自己画了个解析图。。。不要嫌弃丑啊。。。哈哈

 
type

The magic number. Just choose one number (after consultingioctl-number.txt ) and use it throughout the driver. This field is eight bits wide (_IOC_TYPEBITS).


number.

          The ordinal (sequential) number. It’s eight bits ( _IOC_NRBITS) wide.


direction

The direction of data transfer,if the particular command involves a data transfer. The possible values are _IOC_NONE (no data transfer), _IOC_READ, _IOC_WRITE, and _IOC_READ|_IOC_WRITE(data is transferred both ways). Data transfer is seen from the application’s point of view; _IOC_READ means reading from the device, so the driver must write to user space. Note that the field is a bitmask,so _IOC_ READ and _IOC_WRITE can be extracted using a logical AND operation.

size

The size of user data involved.The width of this field is architecture dependent, but is usually 13 or 14 bits.You can find its value  for your specific architecture in the macro _IOC_SIZEBITS. It’s not mandatory that you use the size field—the kernel does not check it—but it is a goodidea.Proper use of this field can help detect user-space programming errors and enable you to implement backward compatibility if you ever need to change the size of the relevant data item. If you need larger data structures, however,you can just ignore the size field. We’ll see how this field is used soon.


#ifndef _UAPI_ASM_GENERIC_IOCTL_H
#define _UAPI_ASM_GENERIC_IOCTL_H/* ioctl command encoding: 32 bits total, command in lower 16 bits,* size of the parameter structure in the lower 14 bits of the* upper 16 bits.* Encoding the size of the parameter structure in the ioctl request* is useful for catching programs compiled with old versions* and to avoid overwriting user space outside the user buffer area.* The highest 2 bits are reserved for indicating the ``access mode''.* NOTE: This limits the max parameter size to 16kB -1 !*//** The following is for compatibility across the various Linux* platforms.  The generic ioctl numbering scheme doesn't really enforce* a type field.  De facto, however, the top 8 bits of the lower 16* bits are indeed used as a type field, so we might just as well make* this explicit here.  Please be sure to use the decoding macros* below from now on.*/
#define _IOC_NRBITS	8
#define _IOC_TYPEBITS	8/** Let any architecture override either of the following before* including this file.*/#ifndef _IOC_SIZEBITS
# define _IOC_SIZEBITS	14
#endif#ifndef _IOC_DIRBITS
# define _IOC_DIRBITS	2
#endif#define _IOC_NRMASK	((1 << _IOC_NRBITS)-1)           //0xff
#define _IOC_TYPEMASK	((1 << _IOC_TYPEBITS)-1)     //0xff
#define _IOC_SIZEMASK	((1 << _IOC_SIZEBITS)-1)     //0x3fff
#define _IOC_DIRMASK	((1 << _IOC_DIRBITS)-1)      //0x3/*这部分是NR TYPE SIZE DIR 段在32bit数据中储存位置相对于起始0位置的偏移量*/
#define _IOC_NRSHIFT	0
#define _IOC_TYPESHIFT	(_IOC_NRSHIFT+_IOC_NRBITS)   //0x8
#define _IOC_SIZESHIFT	(_IOC_TYPESHIFT+_IOC_TYPEBITS)//0x10
#define _IOC_DIRSHIFT	(_IOC_SIZESHIFT+_IOC_SIZEBITS)//0x1E/** Direction bits, which any architecture can choose to override* before including this file.*/
/*io 读写权限宏*/
#ifndef _IOC_NONE
# define _IOC_NONE	0U
#endif#ifndef _IOC_WRITE
# define _IOC_WRITE	1U
#endif#ifndef _IOC_READ
# define _IOC_READ	2U
#endif/*_IOC 适用于将dir, type nr size 这四个信息合成到一个32bit的数据中*/
#define _IOC(dir,type,nr,size) \(((dir)  << _IOC_DIRSHIFT) | \((type) << _IOC_TYPESHIFT) | \((nr)   << _IOC_NRSHIFT) | \((size) << _IOC_SIZESHIFT))#ifndef __KERNEL__
#define _IOC_TYPECHECK(t) (sizeof(t))
#endif/* used to create numbers */
#define _IO(type,nr)		_IOC(_IOC_NONE,(type),(nr),0)
#define _IOR(type,nr,size)	_IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOW(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOWR(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))/*一下三个BAD结尾的宏定义我也没看明白为什么最后一个参数是sizeof(size) 就得跟一个BAD*/
#define _IOR_BAD(type,nr,size)	_IOC(_IOC_READ,(type),(nr),sizeof(size))
#define _IOW_BAD(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),sizeof(size))
#define _IOWR_BAD(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))/* used to decode ioctl numbers.. *//*从32bit的数据中解码出DIR TYPE NR SIZE,很简单没啥讲的*/
#define _IOC_DIR(nr)		(((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
#define _IOC_TYPE(nr)		(((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
#define _IOC_NR(nr)		(((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
#define _IOC_SIZE(nr)		(((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)/* ...and for the drivers/sound files... */#define IOC_IN		(_IOC_WRITE << _IOC_DIRSHIFT)
#define IOC_OUT		(_IOC_READ << _IOC_DIRSHIFT)
#define IOC_INOUT	((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
#define IOCSIZE_MASK	(_IOC_SIZEMASK << _IOC_SIZESHIFT)
#define IOCSIZE_SHIFT	(_IOC_SIZESHIFT)#endif /* _UAPI_ASM_GENERIC_IOCTL_H */








这篇关于ioctl.h 分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035782

相关文章

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Linux中的HTTPS协议原理分析

《Linux中的HTTPS协议原理分析》文章解释了HTTPS的必要性:HTTP明文传输易被篡改和劫持,HTTPS通过非对称加密协商对称密钥、CA证书认证和混合加密机制,有效防范中间人攻击,保障通信安全... 目录一、什么是加密和解密?二、为什么需要加密?三、常见的加密方式3.1 对称加密3.2非对称加密四、

MySQL中读写分离方案对比分析与选型建议

《MySQL中读写分离方案对比分析与选型建议》MySQL读写分离是提升数据库可用性和性能的常见手段,本文将围绕现实生产环境中常见的几种读写分离模式进行系统对比,希望对大家有所帮助... 目录一、问题背景介绍二、多种解决方案对比2.1 原生mysql主从复制2.2 Proxy层中间件:ProxySQL2.3

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1