How can I share data between C kernel programs and C user level programs?

I am using Ubuntu 9.04 with a kernel 2.8.32. I created a simple system call that counts the number of clone and execve . When the user / shell calls this system call, I would like it to pass these two values ​​to the user. At the moment I am using:

#include <linux/sched.h> #include <linux/asmlinkage> /* These two variables are extern longs which are defined in sched.h and initialized in process_32.c */ total_execve; total_clones; long mycall* (int i){ int array[2]; array[0] = total_execve; array[1] = total_clones; return array; } 

I can not compile this because I get an undefined link.

Regarding the return of the array: will my new call be able to access the array, will the array be in kernel memory?

+4
source share
2 answers

Answering your last question: the array is really in the “core” of the memory, but the stack is allocated, which means that it “leaves” when the mycall () function completes. Perhaps your function works, but it may not work in cases where the memory is quickly used.

To return multiple values, the generic template is designed so that the caller can pass pointers to user space and fill them with the kernel routine. For example, you can pass two pointers for two values ​​or define one structure that contains the values ​​you need and will be populated by the kernel program.

+4
source

Do not push pointers onto the stack. Ask the pointer to pass pointers to the function. In addition, the syntax for declaring a function is incorrect. Here is what you want to do:

 void mycall (int *execve, int *clones) { *execve = total_execve; *clones = total_clones; } 
+5
source

Source: https://habr.com/ru/post/1339410/


All Articles