📄 readme.syscall
字号:
This is a sample implementation of a system call - system calls are fairlyfast compared to device open/read/close operations that need to traversethe VFS and execute a few system calls sequescially, but it is the mostsnon-portable and the most dangorous solution to a problem posible, changinga system call or introducing a new one makes your system as a whole incompatible to all other linux system. Adding a system call can introducea serious security problem in your system. Adding a system call will requireyou to patch every kernel releas when updating. So the best solution is notto write your own system calls.... but they solve problems some times ;)The actual syscall code:/usr/src/linux/arch/i386/kernel/sys_i386.casmlinkage int sys_test_call(void){ printk("Test System Call called \n"); return 0;}This system call will only produce a printk output and thats it - system callshave a fixed number of parameters and types that must be declared, in the abovecase the system call takes no arguments at all. The number of arguments notonly needs to be given with the declaration of the system call but also with the prototype declaration which is a littl bit different than regular prototypedeclarations (see below).The kernel has a "jump-matrix" for the system calls - the position of a system call in the syscall table is absolut so you can't add in your system call at the beginning or in the middle or your will break the entire system, if at alladd it at the end of the syscall table. The position in the syscall table is the syscall number. So put it into the syscall table like:/usr/src/linux/arch/i386/kernel/entry.S ... .long SYMBOL_NAME(sys_test_call)after recompiling your kernel you could now call it with the absolute system call number, to be a bit more user frindly you need to add some entries tomake it available to user space apps via asm/unistd.h:/usr/include/asm/unistd.h#define __NR_test_call 222 /* this number better be the same as the position in entry.S !! */static inline _syscall0(int,test_call) some syscalls are inlined - for your home-brew system call this probably isnot relevant.Now a regular system call like open is simply called by fd=open(".....our system call could also be called in this way but that would requirerecompiling libc aswell, as during the build process of libc the kernels syscall table is read - if you do recompile libc then you have reached the maximum posible incompatibility to any other linux system. If you don'twant to recompile libc then you need to put the prototype declaration foryour system call into the source file.so assuming we did not recompile libc, call it in in a c-source file:---syscall.c---#include <asm/unistd.h>#include <errno.h>_syscall0(int,test_call);main(){ syscall(222); /* call it via syscal(SYSCAL_NUMBER) */ test_call(); /* call it by name */ return 0;}compile with simple cc syscall.c -o syscallrun this program as ./syscall and check kernel output via the dmesg command - it should have produced:Test System Call called Test System Call called the two calls are via syscall(222) and test_call() - note that you don't needthe headerfiles errno.h and asm/unistd.h to use syscall(222) but you do needthese includes for the named call test_call();hofrat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -