To add a system call into linux kernel, follow the steps: 1. Preparation: insert a new entry into the interrupt table edit the file:. Linux / arch / i386 / kernel / entry.S in which lines look like: .data ENTRY (sys_call_table) .long SYMBOL_NAME (sys_ni_call) / * 0 * / .long SYMBOL_NAME (sys_exit) .long SYMBOL_NAME (sys_fork) ... .long SYMBOL_NAME (sys_vfork) / * 190 * / after the line "long SYMBOL_NAME (sys_vfork)" you can add your own entry for your system call:. long SYMBOL_NAME (sys_myService) Next, we define the the Num for our system call as a "stub" edit the file: linux / include / asm / unistd.h in which lines look Like: / * * this file number. * / #define __nr_exit 1 #define __nr_fork 2 ... #define __nr_vfork 190 Add your num define: #define __nr_myservice 1912 Place Fo R Your System Call Source File
After inserting your new system call entry in the interrupt table and preparing a stub for it, you will need to define (or implement) the system call. It will be easiest to have the system call definitions in your own source code files, say myservice .h and myservice.c.
In general, header files for machine architecture independent system calls and functions are kept under linux / include / linux / and machine architecture dependent ones are kept in linux / include / asm /. Therefore, it would be a good idea to follow this convention. for example, the header file for the system calls for your new synchronization method, of which the implementation is machine architecture specific, would be placed in linux / include / asm /, while the header file for your machine architecture independent system call that access the superblock of one or more of your file systems would be placed under linux / include / linux.The place for actual implementation file (myservice.c in this example) could vary. for example, if you are implementing a new process synchronization method, linux / IPC / Would Be The Best Place for It. if you areware, linux / fs / would be the best place.
Remember that you will need to modify the Makefile in the directory you placed your .c file so that your code gets compiled and linked in properly. Modify the Makefile line to have a .o of your source code. For example, adding myservice.o :
O_OBJS = ... MyService.o
The Rest of the Makefiles Can Remain Untouched (Makefile Changes Will Be Similar if You Choose to Add Your Code Elsewhere) .3. Implement The System Call Source File:
Here is an esample myService.h and myservice.c, assuming myservice.h is under Linux / include / Linux /
MyService.h # ifndef __linux_myservice_h
#define __linux_myservice_h
#include
#include
_syscall2 (int, myservice, int, arg1, char *, arg2);
/ * The "2" in "_syscall2" indicates the para number of the syscall here the first para is the return type of syscall 2nd para is the name of the syscall the next 2 para are paras passed to the syscall * / # endif
MyService.c # include
ASMLINKAGE INT SYS_MYSERVICE (int Arg1, char * arg2) {
Return (1); / * Here Asmlinkage INDICES The Linkage Style of The SyscallWhich IS Defined in Linux / Linkage.H * /
}