# Kernel Space / User Space bi-directional communication



## perleo (May 18, 2012)

Hi,

Just looking for some guidance, I want the kernel to be able to send data to a userspace program (daemon?) and for the daemon to process that data and return the processed data back to the kernel.

What ways in [Free]BSD are there for doing this? I've seen a lot about netlink and such for Linux but unsure about Unix.

Thanks in advance


----------



## anemos (May 19, 2012)

One option is the way the Security Event Auditing system is implemented. The kernel sends data to the audit daemon (auditd) via the special device /dev/audit. Any user space program can send audit-related data to the kernel via the audit system calls. Even if user space programs need to send data to the daemon they will have to go through the kernel.

Check out sys/security/audit/* for the kernel part as well as contrib/openbsm/bin/auditd/* for the daemon sources.


----------



## Maelstorm (Jul 20, 2012)

There are a couple of options that come to mind.

1. A KLM that creates a character device on /dev and have the program read/write to that device.
2. A KLM that implements a new system call.  Then you can use functions like copyin(9), copyinstr(9), and copyout(9) to pass data back and forth between user space and kernel space.

If you are going to spontainiously write data to the userland program from the kernel, then you will need to signal the program by either passing a message to it or sending a signal which your program will have to catch.  Take a look at signal(3) and sigaction(2) for more info.


----------



## fluca1978 (Jul 23, 2012)

Als have a look at devd(8) and this thread.


----------



## AmbrSb (Mar 23, 2021)

I know this is an old thread, but for the sake of reference if anyone reading this later on is interested, I have implemented a kernel module and a userland library that does exactly what the OP has asked for.
https://github.com/AmbrSb/KUP

It provides a shared-memory based bi-directional communication mechanism between kernel / modules and a userland process.


----------

