From f0764c5fa6a4d12733cf207daea6e4dabfb56453 Mon Sep 17 00:00:00 2001 From: hinus Date: Thu, 29 Jul 2021 10:53:49 +0800 Subject: [PATCH] Title: System call 'close' Issue: https://gitee.com/hinus/linux_kernel_011/issues/I42Q8A Description: To close a file. --- fs/exec.c | 7 +++++++ fs/open.c | 16 ++++++++++++++++ include/linux/sys.h | 4 ++-- include/unistd.h | 5 +++-- lib/Makefile | 5 ++++- lib/close.c | 5 +++++ 6 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 lib/close.c diff --git a/fs/exec.c b/fs/exec.c index 1ed1682..f839b67 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -11,6 +11,8 @@ #define MAX_ARG_PAGES 32 +extern int sys_close(int fd); + static unsigned long * create_tables(char * p,int argc,int envc) { unsigned long *argv,*envp; unsigned long * sp; @@ -199,6 +201,11 @@ int do_execve(unsigned long * eip,long tmp,char * filename, iput(current->executable); current->executable = inode; + for (i=0 ; iclose_on_exec>>i)&1) + sys_close(i); + current->close_on_exec = 0; + free_page_tables(get_base(current->ldt[1]),get_limit(0x0f)); free_page_tables(get_base(current->ldt[2]),get_limit(0x17)); diff --git a/fs/open.c b/fs/open.c index 0199bc2..999d441 100644 --- a/fs/open.c +++ b/fs/open.c @@ -102,3 +102,19 @@ int sys_fstat() { return 0; } +int sys_close(unsigned int fd) { + struct file * filp; + if (fd >= NR_OPEN) + return -EINVAL; + current->close_on_exec &= ~(1<filp[fd])) + return -EINVAL; + current->filp[fd] = NULL; + if (filp->f_count == 0) + panic("Close: file count is 0"); + if (--filp->f_count) + return 0; + iput(filp->f_inode); + return 0; +} + diff --git a/include/linux/sys.h b/include/linux/sys.h index 13846cf..5d66342 100644 --- a/include/linux/sys.h +++ b/include/linux/sys.h @@ -4,7 +4,7 @@ extern int sys_fork(); extern int sys_read(); extern int sys_write(); extern int sys_open(); -//extern int sys_close(); +extern int sys_close(); //extern int sys_waitpid(); //extern int sys_creat(); //extern int sys_link(); @@ -93,7 +93,7 @@ fn_ptr sys_call_table[] = { sys_read, sys_write, sys_open, - 0, /*sys_close,*/ + sys_close, 0, //sys_waitpid, 0, //sys_creat, 0, //sys_link, diff --git a/include/unistd.h b/include/unistd.h index a202200..a805ffd 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -151,7 +151,10 @@ __asm__ volatile("int $0x80\n\r"\ extern int errno; +int chdir(const char* pathname); +int close(int fildes); int dup(int fildes); +int execve(const char * filename, char ** argv, char ** envp); int fork(); int fstat(); int setup(void *BIOS); @@ -159,10 +162,8 @@ int sync(); int open(const char * filename, int flag, ...); int write(int fildes, const char * buf, off_t count); int read(int fildes, const char * buf, off_t count); -int execve(const char * filename, char ** argv, char ** envp); int rmdir(const char* pathname); -int chdir(const char* pathname); time_t time(time_t * tloc); diff --git a/lib/Makefile b/lib/Makefile index 331f5a5..9618874 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,7 +2,7 @@ AR := ar LD := ld GCC := gcc CCFLAG := -m32 -I../include -nostdinc -ffreestanding -fno-pic -Wall -fomit-frame-pointer -fno-stack-protector -c -OBJS := ctype.o write.o string.o open.o dup.o execve.o +OBJS := ctype.o write.o string.o open.o dup.o execve.o close.o lib.a : $(OBJS) $(AR) rcs $@ $^ @@ -26,6 +26,9 @@ dup.o : dup.c execve.o : execve.c $(GCC) $(CCFLAG) -o $@ $< +close.o : close.c + $(GCC) $(CCFLAG) -o $@ $< + clean : -rm *.o -rm lib.a diff --git a/lib/close.c b/lib/close.c new file mode 100644 index 0000000..0ba4677 --- /dev/null +++ b/lib/close.c @@ -0,0 +1,5 @@ +#define __LIBRARY__ +#include + +_syscall1(int,close,int,fd) + -- Gitee