1

我为设备编写了模块,但我的读取功能出现问题:

ssize_t my_sys_read(struct file *f, char __user *buffer, size_t s, loff_t *off){

    char * myBuffer = "ossec buffer";
    size_t read_bytes;

    if (s > ( sizeof(char) * 13 ) ) s = ( sizeof(char) * 13 );

    if (!access_ok(%VERIFY_WRITE, (void *) buffer, s)) return -EFAULT;
        read_bytes = copy_to_user((void *) buffer, (void *) myBuffer, s);

    printk(KERN_INFO "myBuffer %s", myBuffer);
    printk(KERN_INFO "buffer %s", buffer);

    read_bytes = s - ( sizeof(char) * 13 );

    return read_bytes;

}

我真的不知道为什么,但是它的副本不起作用,并且打印的缓冲区对我来说毫无意义。

 [10038.885838] buffer \xffffff81\xffffffc3\xffffffcb\x1a

我想问题出在副本上,因为使用该设备的程序很简单。

int main(void)
  {
    int fd = open(device_name, O_RDONLY);
    if(fd < 0)
    {
      printf("Error: Impossible to open device, action not permited.\n");
      return 0;
    }

    char * buff;
    int read_bytes;
    read_bytes = read(fd, buff, (13 * sizeof(char) ) );

    printf(" %s\n", buff);


  }

谢谢!

4

1 回答 1

1
char * buff;
int read_bytes;
read_bytes = read(fd, buff, (13 * sizeof(char) ) );

您正在使用指针 buff 而不为其分配内存,然后它是一个具有随机值的悬空指针。你需要做

char *buff = new char();或者

  char buff;
  int read_bytes;
  read_bytes = read(fd, &buff, (13 * sizeof(char)) );
于 2013-01-30T20:21:27.440 回答