1

I want to add a system call to print CPU utilization to stdout. But I find the function "system()" in "stdlib.h" seem can't work in linux kernel? Is there another method to simulate "cat /proc/stat" ?

Boyou Yu
  • 11
  • 2
  • have you tried opening the file normally and `printk`ing the contents? – Chris Turner Mar 20 '17 at 10:17
  • @ChrisTurner Sorry for this newbie question. How to open a file within system call implementation? I have tried to use filp_open, it pass the compile but the system behave strange after switch to the modified kernel. – Kit Fung Mar 20 '17 at 10:31
  • I'm not sure, but I think this might count as a duplicate of this question http://stackoverflow.com/questions/1184274/how-to-read-write-files-within-a-linux-kernel-module – Chris Turner Mar 20 '17 at 10:46
  • `but printk contents with "dmesg"` - What does this mean? Do you want to prepend content with current time, cpu number and so? – Tsyvarev Mar 20 '17 at 13:20
  • @ChrisT I want to write a system call, and it can implement `cat /froc/stat`. At the beginning, I try `system("cat /froc/stat")`, but it can't work. Is there another way to do it ? – Boyou Yu Mar 20 '17 at 13:46
  • @Tsyvarev Sorry for my poor English. I have edited it. – Boyou Yu Mar 20 '17 at 13:49
  • @BoyouYu I already linked to a similar question that describes how to read a file from within the kernel - did that not help? – Chris Turner Mar 20 '17 at 14:11
  • @nos I want to write a system call. In `unistd_32.h`, there is `#define__NR_cpu_utilization 337`. then I write a test program. there is a line `syscall(337)` Then, I compile the test program and execute it, it can do the same thing just like typing `cat /froc/stat` in the terminal. – Boyou Yu Mar 20 '17 at 14:44
  • @ChrisTurner Sorry for reply late. It really helpful. Thx~ – Boyou Yu Mar 20 '17 at 15:55

1 Answers1

1

You can check how linux kernel populates information in /proc/stat file.

Check the implementation of function show_stat defined in fs/proc/stat.c inside linux kernel source code. You will get some idea.

I hope this will help you.

Akshay Patole
  • 454
  • 3
  • 14