【问题标题】:cant open device打不开设备
【发布时间】:2010-05-20 18:50:21
【问题描述】:

我有一个小问题。我将此模块安装到我的内核中,并将其写在 /proc 下

当我尝试从用户模式打开()它时,我收到以下消息:

“无法打开设备文件:my_dev”

static int module_permission(struct inode *inode, int op, struct nameidata *foo)
{
 //if its write
 if ((op == 2)&&(writer == DOESNT_EXIST)){
  writer = EXIST ;
  return 0;
 }
 //if its read
 if (op == 4 ){
  numOfReaders++;
  return 0;
 }
 return -EACCES;
}

int procfs_open(struct inode *inode, struct file *file)
{
 try_module_get(THIS_MODULE);
 return 0;
}

static struct file_operations File_Ops_4_Our_Proc_File = {
 .read   = procfs_read,
 .write   = procfs_write,
 .open   = procfs_open,
 .release = procfs_close,
};

static struct inode_operations Inode_Ops_4_Our_Proc_File = {
 .permission = module_permission, /* check for permissions */
};

int init_module()
{
 /* create the /proc file */
 Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
 /* check if the /proc file was created successfuly */
 if (Our_Proc_File == NULL){
  printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
         PROC_ENTRY_FILENAME);
  return -ENOMEM;
 }

 Our_Proc_File->owner = THIS_MODULE;
 Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
 Our_Proc_File->proc_fops = &File_Ops_4_Our_Proc_File;
 Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
 Our_Proc_File->uid = 0;
 Our_Proc_File->gid = 0;
 Our_Proc_File->size = 80;

 //i added init the writewr status
 writer = DOESNT_EXIST;
 numOfReaders = 0 ;
 printk(KERN_INFO "/proc/%s created\n", PROC_ENTRY_FILENAME);
 return 0;
}

【问题讨论】:

  • 您能否提供公开电话的strace 以查看返回的实际错误?您是否有权打开设备?如果以上所有方法都失败,请使用 ftrace。

标签: linux linux-kernel


【解决方案1】:
open("/proc/ex3mod", O_WRONLY) = -1 EACCES (Permission denied)

就像之前的评论者所说,您没有打开 proc 文件的权限。尝试使用chmod 修复权限。

【讨论】:

  • 虽然我使用 chmod o+w /proc/ex3mod 我仍然不允许我做错什么(很可能)?
  • Ising 在计算 op 时发现了我的 ERR,我使用 == 而不是使用 &(logical AND ) 作为示例:if (op & 4)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多