c - 如何从用户空间 C 读取内核模块 (/dev) 文件

标签 c file-io linux-kernel

在我的内核模块中,读取函数如下:

ssize_t aes_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { 

  unsigned long aes_reg[4];
  aes_reg[0] = leon_load_reg(output_mem_loc);
  aes_reg[1] = leon_load_reg(output_mem_loc+4);
  aes_reg[2] = leon_load_reg(output_mem_loc+8);
  aes_reg[3] = leon_load_reg(output_mem_loc+12);
  copy_to_user(buf, (char *)aes_reg, 16);
  ....

它出现在内核模块中,buf 被正确设置。在用户空间方面,我这样写:

int main(int argc, char* argv[]){
    FILE *fpaes;
    char *str;
    int buf[4];

    fpaes = fopen("/dev/aes", "r");
    fread(str, 16, 1, fpaes);
    p_long = (unsigned long *)str;
    ....

但是 str 没有更新为我期望的值。我可以用这种方式做一个 fread 还是我离题了?

最佳答案

str 未在您的程序中初始化。访问 *str 是未定义的行为。

关于c - 如何从用户空间 C 读取内核模块 (/dev) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098167/

相关文章:

c - 如何在 GTK3 中将焦点设置到窗口?

c++ - C 和 C++ 样式文件 IO 之间的性能差异

linux-kernel - 如何调试中断处理程序

linux-kernel - 在内核模块中分配大页面

linux - 如何导出修改后的内核头文件

c - 如何在ARM代码战士项目中调用C语言的汇编代码?

c - 解析 TCP 数据包数据

c - XC16 编译器 - 为 BTSTS(间接内存寻址)创建内存操作数的内联汇编约束

python - Appengine 上的 file.read() 返回垃圾字符

C# 文件异常 : cannot access the file because it is being used by another process