0

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 ?

Brain
  • 311
  • 2
  • 12

1 Answers1

1

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.

  • And for completeness, here is a tutorial to trigger the script on module load from `udev`: https://mohammadthalif.wordpress.com/2010/01/02/udev-rules-to-create-a-dev-node-when-inserting-a-module/ – Brain Sep 11 '17 at 16:26