Linux系统调用--系列一(添加)

匿名 (未验证) 提交于 2019-12-02 21:59:42

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

运行:

./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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!