Skip to content
Advertisement

How to add poll function to the kernel module code?

As I know, to inform the user space from kernel space, one way is to using poll. That means kernel driver should provide poll method first. Below code is found from internet, and it really works!

JavaScript

I can do like this to make it work:

JavaScript

And then

JavaScript

to see the result.

But how to add poll method to it? I tried some times, but still failed.

Advertisement

Answer

You can find some good examples in kernel itself. Take a look at next files:

To add poll() function to your code follow next steps.

  1. Include needed headers:

    JavaScript
  2. Declare waitqueue variable:

    JavaScript
  3. Add fortune_poll() function and add it (as .poll callback) to your file operations structure:

    JavaScript

    Note that you should return POLLIN | POLLRDNORM if you have some new data to read, and 0 in case there is no new data to read (poll() call timed-out). See man 2 poll for details.

  4. Notify your waitqueue once you have new data:

    JavaScript

That’s the basic stuff about implementing poll() operation. Depending on your task, you may be needed to use some waitqueue API in your .read function (like wait_event_interruptible()).


See also related question: Implementing poll in a Linux kernel module.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement