Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel /proc FIFO/pipe

I'm currently trying to create a kernel module that will produce data based on kernel events and push them to a file. After reading that this is bad (and I agree), I decided it would make more sense to have the data in a /proc file that a user program could pull from when necessary. However, this idea led to all sorts of problems, particularly when and how to clear out this file. So I thought... "why don't I make a named pipe in /proc and read from that?"

I've got the general gist of setting a read function and a write function for a proc file, but I'm still having conceptual trouble with how I'd go about this. Namely, how would I write such a function to take arbitrary data and write it to such a pipe from the kernel? Does anyone have any idea how you'd push data to a named pipe from kernel-space? In the end, it doesn't have to be a /proc file (particularly if it's wrong of me to do so), but that was the conclusion I came to. Then I'll have to figure out how to attach to it from a userspace program, but I feel that's a separate issue.

like image 555
Dan Fego Avatar asked Mar 10 '26 06:03

Dan Fego


2 Answers

Instead of making a named pipe, what you want to do is create a "Character device". If you want simple interaction or streaming data from kernel to userspace and back, this is the usual method. I'd recommend looking up similar devices in the Linux kernel and seeing what they do.

like image 134
Ana Betts Avatar answered Mar 12 '26 00:03

Ana Betts


I think the way this is generally done is to use a netlink socket; one or more userspace processes can bind to a "netlink" address, and your kernel facility can broadcast messages to any / all of them as necessary.

This is certainly what some things do, the networking subsystem especially. It is possible for a userspace program to watch for changes in network interfaces (e.g. new IP addresses, link status change) using this method.

like image 37
MarkR Avatar answered Mar 11 '26 23:03

MarkR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!