实验楼每次启动环境都是原始状态。
#进入oslab
$ cd ~/oslab
# 解压,并指定解压到 /home/shiyanlou/
# 这样的话,在 /home/shiyanlou/oslab/ 中就能找到解压后的所有文件
$ tar -zxvf hit-oslab-linux-20110823.tar.gz -C /home/shiyanlou/
copy
#define __NR_iam 72
#deifne __NR_whoami 73
copy
int iam(const char * name);
int whoami(char * name,unsigned int size);
copy
nr_system_calls = 73
copy
extern int sys_iam();
extern int sys_whoami();
copy
#define __LIBRARY__
#include <errno.h>
#include <asm/segment.h>
#include <unistd.h>
char username[64]={0};
int sys_iam(const char* name){
int result=0,cnt;
while(get_fs_byte(result+name) !='\0' && result<64)
result++;
if(result>23)
return -EINVAL;
for(cnt=0;cnt<=result;cnt++)
username[cnt]=get_fs_byte(name+cnt);
return result;
}
int sys_whoami(char *name, unsigned int size){
int result=0,cnt;
while(username[result] !='\0' && result<64)
result++;
if(result>size)
return -EINVAL;
for(cnt=0;cnt<=result;cnt++)
put_fs_byte(username[cnt], name+cnt);
return result;
}
copy
OBJS中添加who.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
copy
Dependencies中添加
who.s who.o: who.c ../include/linux/kernel.h ../include/unistd.h
copy
Makefile 修改后,和往常一样 make all 就能自动把 who.c 加入到内核中了。
$ make all
copy
sudo ./mount-hdc
copy
cp linux-0.11/include/unistd.h hdc/usr/include
cp linux-0.11/include/linux/sys.h hdc/usr/include/linux
copy
#define __LIBRARY__
#include <unistd.h>
#include <errno.h>
#include <asm/segment.h>
_syscall1(int, iam, const char*, name);
int main(int argc, char * argv[]){
if(argc>1 && iam(argv[1])>=0)
return 0;
return -1;
}
copy
#define __LIBRARY__
#include <unistd.h>
#include <errno.h>
#include <asm/segment.h>
#include <stdio.h>
_syscall2(int, whoami, char *, name, unsigned int, size);
int main(void){
char str[64];
if(whoami(str, 24)<0)
return -1;
printf("%s\n",str);
return 0;
}
copy
sudo umount hdc
copy
./run
copy
gcc –o testlab2 testlab2.c
gcc -o iam iam.c -Wall
gcc -o whoami whoami.c -Wall
copy
./testlab2
copy
./iam bluefly-yyq
./whoami
copy
./testlab2.sh
copy
实验完成。
学习时间 112分钟
操作时间 91分钟
按键次数 1092次
实验次数 6次
报告字数 5088字
是否完成 完成