为什么 IOCTL 命令号在整个系统中应该是唯一的?

Why IOCTL command numbers should be unique across the system?

我阅读了选择 ioctl 命令的说明(来自 O'Reilly linux 设备驱动程序):

The ioctl command numbers should be unique across the system in order to prevent 
errors caused by issuing the right command to the wrong device.

IOCTL 函数(来自用户space)的参数之一是文件描述符。 因此,如果我调用特定设备,为什么 ioctl 命令编号在整个系统中应该是唯一的?

IOCTL CMD 不必在整个系统中是唯一的。它对于特定的设备节点应该是唯一的。但通常的做法是保持跨系统唯一的CMD,以避免因向错误的设备发出正确的命令而导致错误。

如果您将特定命令(假设 device-1 的无效 cmd)传递给错误的 device-2,它能够处理 ioctl CMD 将导致成功,您将得到一些无效数据而不是错误。为了避免这种情况,我们在整个系统中使用唯一的 CMD。