How to add a system call in Linux
1. Download Kernel Source code from kernel.org
2. Unzip and intar the Linux source code.
3. Make directory “mycall” at ./linux-2.6.X.x (mkdir mycall)
4. Create C file in mycall directory for the defination of the system call (sys_mycall in our
case)
myfile.c
#include<linux/linkage.h>
#include<linux/kernel.h>
asmlinkage long sys_mycall(int i)
{
printk("Hello World %d \n",i);
return i+10;
}
5. Create a Makefile in mycall directory for mycall.c
obj-y := mycall.o
6. Add sys_mycall in system call table in file arch/x86/kernel/syscall_table_32.S
7. Assign System Call number to sys_mycall funtion from file
arch/x86/include/asm/unistd_32.h
Add "#define __NR_mycall <Last_System_Call_Num + 1>" at the end of the list
Increment the "NR_syscalls" by 1
8. Declare sys_mycall in include/linux/syscalls.h
Add the following line at the end of the file:
asmlinkage long sys_mycall(int i);
9. Compile the kernel code
1. make menuconfig
2. make
3. make modules_install
4. make install
5. mkinitrd -o initrd.img-2.6.X.x 2.6.X.x
10. Write a test program for make system call
#include <sys/syscall.h>
#include <unistd.h>
#include<stdio.h>
int main()
printf("Return value is %d\n",syscall(338,21));
}
Output: On Screen
Return value is 31
In /var/log/kern.log
Oct 5 11:34:00 alok-laptop kernel: [ 326.170004] Hello World 21