0

I am new to linux kernel programming.

Currently, I use debugfs to output the value of a kernel variable (say myKernelVariable) to a file, say, debugfs/myFile

What I want to do is this: I want to use a user-level program (prefer C, but python also works for me) that when the value of myKernelVariable in debugfs/myFile is changed, my user-level program will be notified.

A very inefficient way is that I can set up a timer in my user-level program and repeatedly check if the value in debugfs/myFile is changed.

Are there any trigger/notify - based lightweight methods to do this?

Thanks very much.

  • Write a third party server that will watch the change in the file & then alert/notify othet processes using a certain IPC mechanism like dbus. – Karim Manaouil Oct 07 '17 at 02:39
  • @KarimManaouil How to implement the "watch" function in the server? Does it also require some endless loop to check the value? Thx – Huanle Zhang Oct 07 '17 at 04:12
  • I'm not sure about other ideas to capture the change but periodically checking the file is the most obvious one. Why would you care about the deamon ? Its only job is watching the change and then notify other processe s. I mean it's OK if he spends all his life inside an endless loop. – Karim Manaouil Oct 07 '17 at 12:33
  • @KarimManaouil Hi Karim. I don't like the idea of setting a timer and to repeatedly check the value because it cannot guarantee that every change in the file will be captured. Also, the overhead is not small. I prefer some signal-based method, so that every time a change is made on the file, my user-level program will be triggered. I don't know how to add a signal event to value in a file. – Huanle Zhang Oct 07 '17 at 20:18
  • I know. But the loop that will capture the change in your file is implemented in another standalone program (called deamon) and not your main program. The loop deamon will send a signal to your main program whenever it captures the change. That means your program can do whatever it wants to do until it is signaled by the deamon when a change in the file is captured. The communication between the deamon and your program could be accomplished using Dbus or other IPC mechanism. – Karim Manaouil Oct 08 '17 at 02:43
  • @KarimManaouil I found the following link about setting hardware checkpoints to memory address: [Watch a variable (memory address) change in Linux kernel, and print stack trace when it changes?](https://stackoverflow.com/questions/19725900/watch-a-variable-memory-address-change-in-linux-kernel-and-print-stack-trace). I am still figuring out how to use it. – Huanle Zhang Oct 08 '17 at 15:27
  • @HuanleZhang, from my point of view, you just need a way to communicate between application and kernel, such as semaphone, fd ... when the kernel try to change the value, notice the application. – Forward Oct 18 '17 at 07:56
  • @Forward Thanks for your suggestion. I will look into semaphone to check if it is lightweight, because I want to reduce the overhead to system performance as possible as I can. – Huanle Zhang Oct 21 '17 at 04:12

0 Answers0