How to communicate with a Linux kernel module from user space without littering /dev with new nodes?

后端 未结 6 1685
孤城傲影
孤城傲影 2020-12-28 21:26

What are the ways to communicate with a kernel module from user space? By communication i mean sending information and commands between the kernel module and a user space pr

相关标签:
6条回答
  • 2020-12-28 22:05

    You could also use Shared Memory and IOCTL

    0 讨论(0)
  • 2020-12-28 22:06

    You could also read/write from /dev device nodes.

    IMHO, /dev is already littered with stuff and adding your own nodes there isn't a big issue. Don't forget that you can have lots of ioctl codes for a single device node, and the ioctl paramters are passed by reference so can be as big as you like.

    0 讨论(0)
  • 2020-12-28 22:20

    There's also the /sys filesystem (sysfs):

    Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

    (from Wikipedia)

    0 讨论(0)
  • 2020-12-28 22:22

    debugfs is another good possibility for APIs that are less stable than sysfs, but the API is basically the same. Here is a minimal runnable example.

    configfs is another one. It allows easy dynamic creation of kernel objects from userspace through the filesystem: https://www.kernel.org/doc/Documentation/filesystems/configfs/configfs.txt

    In any case, you will have to dirty some namespace... a filesystem entry in case of sysfs and debugfs. Just choose your poison.

    Also, udev rules make /dev very similar to sysfs and debugfs: How to create a device in /dev automatically upon loading of the kernel module for a device driver?

    0 讨论(0)
  • 2020-12-28 22:24

    Netlink sockets are designed for that kind of requirements, too...

    Also see

    • man 7 netlink
    • libnl - Netlink library
    • The libnl Archives
    0 讨论(0)
  • 2020-12-28 22:24

    Third one is add a new syscall, but the two you have written are the preferred ones, I think. I've found this document that might help, but I still think this option is unadvised: http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html

    Another acceptable option might be sharing memory.

    0 讨论(0)
提交回复
热议问题