系统调用–系列一(添加)
序言
最近在研究
Linux Kernel
, 发现系统调用的整个过程不是很清楚, 决定自己动手弄懂它. 这次先添加一个系统调用, 把整个过程撸一遍.
环境
system: Ubuntu 12.04 lts 32bit
tools: vim, make, apt-get ...
过程
1. 下载源码
apt-get source linux-image-$(uname -r)
2. 添加声明
/*
* file : arch/x86/syscalls/syscall_32.tbl
*/
352 i386 sched_getattr sys_ni_syscall
353 i386 renameat2 sys_ni_syscall
354 i386 seccomp sys_seccomp
// add a system call, which printf a string "hello, world"
355 i386 hello sys_hello
/*
* file : include/linux/syscalls.h
*/
asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,
const char __user *uargs);
//added
asmlinkage long sys_hello(void);
#endif
3. 系统调用实现
在源代码根目录创建目录
hello
/*
* create a file, named syscalls.c
*/
#include <linux/kernel.h>
#include <linux/syscalls.h>
asmlinkage long sys_hello(void){
printk("hello, world\n");
return 0;
}
对应Makefile
/*
* filename: Makefile
*/
obj-y := syscalls.o
4.配置源代码根目录下的Makefile
/*
* 大约785行
*/
- core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
+ core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/
5. 编译, 安装
make -j4
make modules
make modules_install install
make install
之后选择你安装的内核重新启动系统.
6. 测试
/*
* create a file: hello.c
*/
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main(){
long int result = syscall(355);
printf("System call: %ld\n", result);
return 0;
}
编译:
gcc test.c -o test
运行:
扫描二维码关注公众号,回复:
1644731 查看本文章
./test
System call sys_hello returned 0
执行dmesg
[83996.842542] hello, world
[84477.164610] hello, world
问题
Ubuntu 12.04
启动菜单上面没有内核选择选项, 这里需要我们手动修改一下/etc/default/grub
/*
* filename: /etc/default/grub
*/
GRUB_DEFAULT=0
#将下面一行注释掉, 执行update-grub命令
# GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10