Search code examples
linux-kernelmknod

How to access a dynamic character device from userspace?


The register_chrdev() function in kernel registers a character device:

int register_chrdev(unsigned  int  major,  const  char*name,
 struct file_operations*ops));

If major is 0 the kernel dynamically allocates a major number and the register function returns it.

Now, let's assume a module foo.ko wants to use /dev/foo with a dynamic major number. How does the userspace learn what major number to pass to mknod to create /dev/foo ?


Solution

  • As soon as a character device gets registered with a dynamic major number, the corresponding information appears in /proc/devices and thus can be retrieved by a user-space application/script in order to create an appropriate node.

    For a better example you may refer to Linux Device Drivers book (3rd edition), for instance, a script to read /proc/devices is shown on this page.