欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Netlink: 内核与用户空间传输数据的socket协议

程序员文章站 2022-04-24 22:32:35
...

https://en.wikipedia.org/wiki/Netlink

 

https://*.com/questions/12899055/how-kernel-notify-a-user-space-program-an-interrupt-occurrs

There is a similar question asked here:

Notify gpio interrupt to user space from a kernel module

Please check above question. However, i can provide my approach which i suggested there as well.

You can send a signal to user space thread from kernel API, which can help u run non-blocking:

send_sig(int sig, struct task_struct *p, int priv);

You need to be aware of pid of user thread in Kernel. You can over come this by writing pid of user process via /proc and then kernel reading the pid. With this arrangement, when there is an interrupt, kernel can send signal to user thread. In case your process restarts or gets killed, you will have to update the pid via proc. Just for status notification you can use this method; however if you like to transfer data along with status than Netlink or char driver mechanism is good way.

 

 

https://*.com/questions/19257624/interrupt-handling-and-user-space-notification

You don't need fancy kernel to userspace communication. A userspace application has access to GPIOs using Sysfs. Read about it in Documentation/gpio.txt.

First, export a GPIO pin like this (the actual number depends on your setup):

# echo 23 > /sys/class/gpio/export

This will export GPIO pin #23, and thus create /sys/class/gpio/gpio23.

Set its direction:

# echo in > /sys/class/gpio/gpio23/direction

If the hardware GPIO controller supports interrupts generation, the driver should also support it and you will see /sys/class/gpio/gpio23/edge. Write either risingfalling or both to this file to indicate the signal edge(s) that will create a "userspace interrupt". Now, to get interrupted, use the poll(2) system call on /sys/class/gpio/gpio23/value. Then, when the poll call unblocks, read the new value (/sys/class/gpio/gpio23/value), which will be '0' or '1' (ASCII).

ASK:

The main my question is: How to send notification with information from interrupt handler to user space application. I am using not only gpio interrupts. So in userspace I need to determine which interrupt occurred. If it possible, please present some examples how it can be done. Thanks... 
ANSWER:
In that case, you could do the same thing that GPIO does with poll(2) on a custom Sysfs entry for your driver. Otherwise, check out Netlink.