在不知道返回的错误的情况下,很难说...我的第一个问题是您对文件描述符的权限。我以前见过类似的问题。首先,如果您查看 ioctl 的返回,您可以获得有关失败的更多信息:
#include <errno.h>
int main(int argc, char* argv[])
{
    long ret;     
    int fd = fopen("/dev/aes", "r+");
    ret = ioctl(fd, 0, 1);
    if (ret < 0)
        printf("ioctl failed. Return code: %d, meaning: %s\n", ret, strerror(errno));
    fclose(fd); 
} 
检查返回值,这应该有助于您搜索。为什么要检查?请参阅帖子的底部...
接下来要检查是否是权限问题,请运行以下命令:
ls -l /dev/aes
如果你看到类似的东西:
crw------- 1 root root 10, 57 Aug 21 10:24 /dev/aes
然后只需发出一个:
sudo chmod 777 /dev/aes
再试一次。我敢打赌它会为你工作。(请注意,我使用 root 权限运行它,因为 root 是我的 mod 版本的所有者)
如果权限已经OK,那么我还有一些建议:
1) 对我来说,fopen/fclose 的使用很奇怪。你真的只需要这样做:
int fd = open("/dev/aes");
close(fd);
我的系统甚至不允许您的代码按原样编译。
2)您的 IOCTL 参数列表很旧,我不知道您编译的内核版本是什么,但最近的内核使用这种格式:
long aes_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param){   
注意 inode 的移除和返回类型的改变。当我在我的系统上运行您的代码时,我进行了这些更改。
祝你好运!
注意:当我们“没有进入 ioctl”时,为什么要检查返回?让我给你举个例子:
//Kernel Code:
//assume include files, other fops, exit, miscdev struct, etc. are present
long hello_ioctl(struct file *file, unsigned long ioctl_num, unsigned long ioctl_param) {
    long ret = 0;
    printk("in ioctl");
    return ret;
}
static const struct file_operations hello_fops = {
    owner: THIS_MODULE,
    read: hello_read,
    unlocked_ioctl: hello_ioctl,
};
static int __init hello_init(void) {
    int ret;
    printk("hello!\n");
    ret = misc_register(&hello_dev); //assume it worked...
    return ret;
}
用户空间代码:
//assume includes
void main() {
  int fd;
  long ret;
  fd = open("/dev/hello");
  if(fd) {
    c = ioctl(fd, 0, 1);
    if (c < 0)
      printf("error: %d, errno: %d, meaning: %s\n", c, errno, strerror(errno));
    close(fd);
  }
  return;
}
那么输出是什么?让我们假设 /dev/hello 上有错误的文件权限(这意味着我们的用户空间程序无法访问 /dev/hello)。
dmesg | 尾部显示:
[ 2388.051660] Hello!
所以看起来我们没有“进入”ioctl。程序的输出是什么?
error: -1, errno: 9, meaning: Bad file descriptor
很多有用的输出。显然 ioctl 调用做了一些事情,只是不是我们想要的。现在更改权限并重新运行,我们可以看到新的 dmesg:
[ 2388.051660] Hello!
[ 2625.025339] in ioctl