1

我正在摆弄一个 scull 驱动程序代码,然后试图让它打印输入的整个字符,而不是一个。那就是:

# insmod memory.ko
# chmod 666 /dev/memory
$ echo -n abcdef >/dev/memory
$ cat /dev/memory

应该打印 abcdef,但只打印 f

为此,我将 scull 代码修改为:

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

  /* Transfering data to user space */ 
  copy_to_user(buf,memory_buffer,5);

  /* Changing reading position as best suits */ 
  if (*f_pos == 0) { 
    *f_pos+=1; 
    return 1; 
  } else { 
    return 0; 
  }
}

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

  char *tmp;

  tmp=buf+count-1;
  copy_from_user(memory_buffer,tmp,5);
  return 1;
}

int memory_init(void) {
  int result;

  /* Registering device */
  result = register_chrdev(memory_major, "memory", &memory_fops);
  if (result < 0) {
    printk(
      "<1>memory: cannot obtain major number %d\n", memory_major);
    return result;
  }

  /* Allocating memory for the buffer */
  memory_buffer = kmalloc(5, GFP_KERNEL); 
  if (!memory_buffer) { 
    result = -ENOMEM;
    goto fail; 
  } 
  memset(memory_buffer, 0, 5);

  printk("<1>Inserting memory module\n"); 
  return 0;

  fail: 
    memory_exit(); 
    return result;
}
4

1 回答 1

6

read正确实施。它不应该向用户写入更多count字节(他可以有更小的缓冲区)。更相关的是,它应该返回写入用户的字节数,并且应该0只在文件结束的情况下返回。它还可以将错误代码作为负数返回,就像-EIO发生错误一样。

你写了 5 个字符,但返回 1,所以cat认为它是不完整的读取,它读取了 1 个字节。我尝试再次读取并得到 0,因此它将其解释为文件结尾。

write有类似的问题,它还应该检查用户缓冲区的大小和返回的字节数。

这或多或少是我read为固定大小缓冲区实现的方式:

ssize_t memory_read(struct file *filp, char *buf, 
                    size_t count, loff_t *f_pos) { 
  ssize_t retval;

  if(down_interruptible(&sem)) // some mutex is probably necessary
    return -ERESTARTSYS;
  count = MIN(count, MEMORY_SIZE - *f_pos);
  if(count == 0) { // this if is probably not necessary
    retval = 0;
    goto out;
  }
  /* Transfering data to user space */ 
  if(copy_to_user(buf, memory_buffer + *f_pos, count)) {
    retval = -EFAULT;
    goto out;
  }
  *f_pos += count;
  retval = count;
out:
  up(&sem);
  return retval;
}
于 2013-06-11T15:22:46.220 回答