0

Here is my code:

my_device = device_create(my_class, NULL, devno, NULL, "iot_device");

appears in /dev folder

crw------- 1 root root (...) iot_device

I would like it to be created with a resolution of 666 (crw-rw-rw- ) I mean without using chmod 666 iot_device

Yael
  • 93
  • 7
  • 1
    This is what you want: [Linux Kernel Module Character Device Permissions](https://stackoverflow.com/questions/21767061/linux-kernel-module-character-device-permissions) – Marco Bonelli Aug 04 '22 at 11:52
  • 1
    Another option is to use `device_class->devnode`, as done for example in `drivers/char/mem.c`: https://elixir.bootlin.com/linux/latest/source/drivers/char/mem.c#L743 – Marco Bonelli Aug 04 '22 at 11:55
  • [Setting device permission from driver code fails](https://stackoverflow.com/questions/47303639/setting-device-permission-from-driver-code-fails) **great answer!!** – Yael Aug 04 '22 at 20:42

1 Answers1

1

You can create a file /etc/udev/rules.d/my.rules with content:

KERNEL=="iot_device", MODE="0666"

and refresh udev or reboot. There are many examples in /usr/lib/udev/rules.d/50-udev-default.rules on my system.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    The kernel can very well do that too. It would be silly if it had to rely on an userspace file to be edited. – Marco Bonelli Aug 04 '22 at 11:51
  • @MarcoBonelli What would you propose? The kernel filters the mode settings when creating sysfs files. UDEV rules are more flexible because ownership and group ownership can be changed. I would recommend setting `MODE="0660"` and `GROUP=""` in the UDEV rules for most devices. – Ian Abbott Aug 04 '22 at 12:14
  • @IanAbbott well of course that makes sense, just saying that it's not the only option and kernel obviously still has control. – Marco Bonelli Aug 04 '22 at 12:18
  • @IanAbbott My original answer had that it was not possible to do in kernel - I was wrong, hence the comment. – KamilCuk Aug 04 '22 at 12:27