1

(最初のステップでは) すべてのファイル操作を別のファイルに転送するだけの Linux カーネル ドライバーを実装する必要があります (後のステップでは、これを管理および操作する必要がありますが、ここでは説明しません)。

私の考えは次のとおりですが、読み取り時にカーネルがクラッシュします。

static struct {
    struct file *file;
    char *file_name;
    int open;
} file_out_data = {
.file_name  = "/any_file",
.open       = 0, 
};


int memory_open(struct inode *inode, struct file *filp) {
  PRINTK("<1>open memory module\n");
    /* 
     * We don't want to talk to two processes at the same time 
     */
    if (file_out_data.open)
        return -EBUSY;
    /*
     * Initialize the message 
     */
    Message_Ptr = Message;
    try_module_get(THIS_MODULE);

        file_out_data.file = filp_open(file_out_data.file_name, filp->f_flags, filp->f_mode); //here should be another return handling in case of fail
    file_out_data.open++;

  /* Success */
  return 0;
}

int memory_release(struct inode *inode, struct file *filp) {
      PRINTK("<1>release memory module\n");
    /* 
     * We're now ready for our next caller 
     */
    file_out_data.open--;
    filp_close(file_out_data.file,NULL);

    module_put(THIS_MODULE);
  /* Success */
  return 0;
}

ssize_t memory_read(struct file *filp, char *buf, 
                    size_t count, loff_t *f_pos) { 
   PRINTK("<1>read memory module \n");
ret=file_out_data.file->f_op->read(file_out_data.file,buf,count,f_pos); //corrected one, false one is to find in the history
    return ret;

}

だから、誰か私に理由を教えてもらえますか?

4

3 に答える 3

2

なぜ2回インクリメントfile_out_data.openして1回デクリメントするのですか?これにより、file_out_data.file閉じた後に使用する可能性があります。

于 2012-04-13T13:02:09.293 に答える
2
  1. set_fs()使用する理由がないので使用しないでください。
  2. file->f_fop->read()の代わりに使用しvfs_readます。fileおよびfile_operations構造を見てください。
于 2012-04-13T14:02:12.780 に答える
0

読み込んだファイルにメモリを書き込みたいですか? あなたは読んでいて書いていないので...私は間違っている可能性があります

于 2013-05-27T13:12:56.397 に答える