简体   繁体   中英

unable to open filp_open file in kernel module

I'm building a Linux module in C. I have two folders kernel_mode and user_mode .

code in user_mode.c :

fd=open("/dev/iot-device", O_RDWR);
printf("call open() fd= %d \n\n",fd);
error("fd");

code in kernel_mode.c :

int iot_open(struct inode *inode, struct file *filp)
{
   file = filp_open("dev/ttyUSB0", O_RDWR, 0);
   printk(KERN_NOTICE "iot_open(): filp_open calls\n");
   if (IS_ERR(file) || file == NULL)
   {
       ret=PTR_ERR(file);
       printk(KERN_NOTICE "Cannot open destination file %s || ret=%d\n",path,ret);
       return ret;
   }
(...)
}
struct file_operations char_fops = 
{
    .owner =    THIS_MODULE,
    .open =     iot_open,(...)
}

kernel_mode.c I like this:

make (I have a makefile)

insmod kernel_mode.ko

user_mode.c I run like this:

gcc user_mode.c -o user_mode

./user_mode

terminal output:

call open() fd= -1 

fd: Permission denied

dmesg :

iot_open(): open() open function was called
iot_open(): filp_open calls
Cannot open destination file dev/ttyUSB0  ||  ret=-2

Permissions and access rights

crw-------   1 root root   (...) iot-device
crw-rw----   1 root dialout (...)  ttyUSB0

Used different flags in open() and filp_open() : O_RDONLY or O_RDWR

What am I missing? Please respond to my request! Any help is welcome

Inattention is to blame for everything.

  1. file = filp_open("/dev/ttyUSB0", O_RDONLY, 0); (did not have /dev)
  2. permission must be crw-rw---- iot-device

you can do chmod every time. But I added code to kernel_mode.c :

static int my_uevent(struct device *dev, struct kobj_uevent_env *env)
{
    add_uevent_var(env, "DEVMODE=%#o", 0660);
    printk(KERN_NOTICE "my_dev_uevent(): Permission is rw-rw---- 0660\n");
    return 0;
}
int init_module(void)
{[...]
    my_class = class_create(THIS_MODULE, CLASS_NAME);[...]
    my_class->dev_uevent = my_uevent;//this is a new code!

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM