Compile your own system call function in linux-general Linux technology-Linux programming and kernel information. For more information, see the following. 1. Compile the system call Function
Edit the sys. c file:
# Cd/usr/src/linux/kernel
# Vi sys. c
Add a system call function at the end of the file:
Asmlinkage int sys_print_info (int testflag)
{
Printk ("Its my syscall function! N ");
Return 0;
}
This function has an int entry parameter testflag and returns an integer 0.
2. Modify the file related to the system call number
Edit the entry table file:
# Cd/usr/src/linux/arch/i386/kernel
# Vi entry. S
Add the function entry address to the sys_call_table table:
The last few lines of source code in arch/i386/kernel/entry. S are changed:
......
. Long SYMBOL_NAME (sys_sendfile)
. Long SYMBOL_NAME (sys_ni_syscall)
. Long SYMBOL_NAME (sys_ni_syscall)
. Long SYMBOL_NAME (sys_vfork)
Rept NR_syscalls-190
. Long SYMBOL_NAME (sys_ni_syscall)
. Endr
After modification:
......
. Long SYMBOL_NAME (sys_sendfile)
. Long SYMBOL_NAME (sys_ni_syscall)
. Long SYMBOL_NAME (sys_ni_syscall)
. Long SYMBOL_NAME (sys_vfork)
. Long SYMBOL_NAME (sys_print_info)
. Rept NR_syscalls-191
. Endr
Modify the corresponding header file:
# Cd/usr/src/linux/include/asm
# Vi unistd. h
Make necessary declarations in include/asm/unistd. h for user and other system processes to query or call the vectors corresponding to the added sys_call_table table.
# Define _ NR_putpmsg 189
# Define _ NR_vfork 190
# Define _ NR_print_info 191
3. Compile the kernel and restart it.
4. Test
Write a user test program (test. c ):
# Vi test. c
# Include
# Include
Extern int errno;
_ Syscall1 (int, print_info, int, testflag)
Main ()
{
Int I;
I = print_info (0 );
If (I = 0)
Printf ("I = % d, syscall success! N ", I );
}
If you want to use the system call function in your program, you must declare the call _ syscall before the main function. 1 indicates that the system call has only one entry parameter, the first int indicates that the return value of the system call is an integer, print_info indicates the name of the system call function, the second int indicates that the type of the entry parameter is an integer, and testflag indicates the entry parameter name.
Compile the test program:
# Gcc-o test. c
Run the test program:
#./Test
Its my syscall function!
I = 0, syscall success!
OK! Added the system to call the function successfully.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service