• Uncategorized

About c : Developing-SYSCALL-in-linux-to-access-the-procloadavg

Question Detail

I want to develop a SYSCALL in Linux that read the output of /proc/loadavg and prints the first column.

The idea is to print the first value of CPU load from cat /proc/loadavg i.e. 0.01 0.50 0.10 1/25 1800
using a syscall.

For example:

 #include <unistd.h>
 #include <sys/syscall.h>
 #include <stdio.h>

 int main() {
 printf("SYSCALL OUTPUT: %s\t", syscall(223, 1));  //(223) - syscall number (1) - for first cpu load

 return 0;
 } 

OUTPUT - SYSCALL OUTPUT: 0.01

Until now, using information on internet, I have made a simple Hello World kind of SYSCALL:

 #include <linux/kernel.h>    
 #include <linux/syscalls.h>   

 SYSCALL_DEFINE0(newprint)
 {
 printk("SYSCALL: Hello World!\n");

 return 0;
 }

But, I have no idea how to build a complex Syscall that does the things I want to. Therefore, I am looking for some help that includes things like –

  1. How to create complex syscalls in linux?
  2. How to import functions from ~/kernel/sched/loadavg.c to use in my SYSCALL program?
  3. After able to perform the above steps, How to compile the syscall program to get the output like a normal c program.

Question Answer

You need to distinguish two areas:

  • kernel space and
  • user space.

The new sys call will be implemented in kernel space. Once you have the new sys call implemented, you can issue the sys call request from an user space program.

To get you started:

  • you need to set up a kernel environment
  • add a new sys call. For this you need to read up on the net, there are
    good articles on how to achieve this. For instance.
    You cannot use the exact same code, but it should give you an idea how to get started. When designing the new system call you need to decide what parameters and data types you need.
  • create a user space program to invoke the new system call with syscall.
    You need to specify the new system call number that you added in the previous point.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.