Access data from proc file in kernel module

I need to access some proc files in a module on the Android kernel. Basically, I need the information provided in the cat command, for example cat /proc/uptime . However, I need to do this programmatically.

I tried working with proc_fs functions, but for me it was just a little fuzzy, as a rule, these are examples of creating a proc file and then reading it and that’s it. I need to actually use the data from the proc files.

I also added a good fopen , but it doesn't seem to work with modules.

How can i do this? I am really new to this. I am working on Android goldfish core.

Thanks.

+4
source share
2 answers

Procfs is an in-memory file system. This is an interface for user space for collecting information and entering (configuring) information into kernel data structures. In other words, procfs allows user space to interact and analyze kernel data structures as they exist at run time.

Therefore, any file inside / proc is not intended to be read from within the kernel or kernel module. And why should this be done? In a monolithic kernel, such as Linux, you can access the data structures of one subsystem in the kernel through another directly or through a predefined function.

The following function call may help:

 struct timespec uptime; do_posix_clock_monotonic_gettime(&uptime); 

You can refer to the / proc / uptime implementation from the link below, it is essentially a seq_file .

http://lxr.free-electrons.com/source/fs/proc/uptime.c

+4
source

I used top for this because it actually gives CPU%. The code I used is as follows

 Process process = Runtime.getRuntime().exec("top -n 1"); //Get the output of top so that it can be read BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; //Read every line of the output of top that contains data while (((line = bufferedReader.readLine()) != null)) { //Break the line into parts. Any number of spaces greater than 0 defines a new element numbersC = line.split("[ ]+"); if (i > 6) { //Some lines start with a space, so their indices are different than others if (numbersC[0].equals("")){ //If name contains the string com.android, then it is a process that we want to take values for if (numbersC[numbersC.length - 1].toLowerCase().contains("com.android".toLowerCase())){ //Add the name of the process to the Name arraylist, excluding the com.android. part Name.add(numbersC[numbersC.length - 1].replace("com.android.", "")); //Add the CPU value of the process to the CPU arraylist, without the % at the end CPU.add(Long.parseLong(numbersC[3].replace("%", ""))); } } else { //This is basically the same as above, except with different index values, as there is no leading space in the numbers array if (numbersC[numbersC.length - 1].toLowerCase().contains("com.android.".toLowerCase())){ Name.add(numbersC[numbersC.length - 1].replace("com.android.", "")); CPU.add(Long.parseLong(numbersC[2].replace("%", ""))); } } } i++; } 
0
source

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


All Articles