1.添加iam和whoami系统调用编号的宏定义只会实现具体功能不涉及理论知识
文件路径:/oslab/linux-0.11/include/unistd.h 在131行代码之后添加一下内容:
#define __NR_whoami 72 #define __NR_iam 732.修改系统调用总数,
文件:kernel/system_call.s
nr_system_calls = 72 改为 nr_system_calls = 743.为新增的系统调用添加系统调用名并维护系统调用表
文件:include/linux/sys.h
extern int sys_whoami(); extern int sys_iam(); 注! 要在fn_ptr sys_call_table[]添加以上内容4.为新增的系统调用编写代码实现
在linux-0.11/kernel目录下,创建一个文件 who.c 并添加以下代码
#include #include4.修改 Makefile#include char _myname[24]; int sys_iam(const char *name) { char str[25]; int i = 0; do { // get char from user input str[i] = get_fs_byte(name + i); } while (i <= 25 && str[i++] != ' '); if (i > 24) { errno = EINVAL; i = -1; } else { // copy from user mode to kernel mode strcpy(_myname, str); } return i; } int sys_whoami(char *name, unsigned int size) { int length = strlen(_myname); printk("%sn", _myname); if (size < length) { errno = EINVAL; length = -1; } else { int i = 0; for (i = 0; i < length; i++) { // copy from kernel mode to user mode put_fs_byte(_myname[i], name + i); } } return length; }
第一处要想让我们添加的 kernel/who.c 可以和其它 Linux 代码编译链接到一起,必须要修改 两处Makefile 文件。路径:kernel/Makefile
vim +27 Makefile
OBJS = sched.o system_call.o traps.o asm.o fork.o
panic.o printk.o vsprintf.o sys.o exit.o
signal.o mktime.o
修改为
OBJS = sched.o system_call.o traps.o asm.o fork.o
panic.o printk.o vsprintf.o sys.o exit.o
signal.o mktime.o who.o
第二处修改结果:
原基础添加了 who.o
### Dependencies: exit.s exit.o: exit.c ../include/errno.h ../include/signal.h ../include/sys/types.h ../include/sys/wait.h ../include/linux/sched.h ../include/linux/head.h ../include/linux/fs.h ../include/linux/mm.h ../include/linux/kernel.h ../include/linux/tty.h ../include/termios.h ../include/asm/segment.h
修改为:
### Dependencies: who.s who.o: who.c ../include/linux/kernel.h ../include/unistd.h exit.s exit.o: exit.c ../include/errno.h ../include/signal.h ../include/sys/types.h ../include/sys/wait.h ../include/linux/sched.h ../include/linux/head.h ../include/linux/fs.h ../include/linux/mm.h ../include/linux/kernel.h ../include/linux/tty.h ../include/termios.h ../include/asm/segment.h
修改结果。添加…/include/linux/kernel.h …/include/unistd.h
Makefile 修改后和往常一样 make all 就能自动把 who.c 加入到内核中了。
shiyanlou@41a34909ea01:~/oslab/linux-0.11$ make all到此,内核修改部分完成,接下来是编写测试程序了 5.编写测试程序 iam.c,whoami.c
#define __LIBRARY__ #include#include #include #include _syscall1(int, iam, const char*, name); int main(int argc, char *argv[]) { iam(argv[1]); return 0; }
#define __LIBRARY__ #include#include #include #include #include _syscall2(int, whoami,char *,name,unsigned int,size); int main(int argc, char *argv[]) { char username[64] = {0}; whoami(username, 24); printf("%sn", username); return 0; }
然后把编写好的测试程序通过挂载到虚拟机操作系统
shiyanlou@41a34909ea01:~/oslab$ sudo ./mount-hdc shiyanlou@41a34909ea01:~/oslab$ cp iam.c whoami.c hdc/usr/root
执行脚本./run 进入虚拟机操作系统修改虚拟机系统调用号跟第一步是一样的
#define __NR_whoami 72 #define __NR_iam 73
在虚拟机 gcc 编译执行测试程序
[/usr/root]# gcc -o iam iam.c [/usr/root]# gcc -o whoami whoami.c [/usr/root]# ./iam wcf [/usr/root]# ./whoami实验到此结束



