From 84b2af3cdb78ebd31b2b6e132dcabebce614c507 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 9 Mar 2019 12:49:59 +0800 Subject: [PATCH] refactor sys_close_internal to SocketWrapper::drop --- kernel/src/syscall/fs.rs | 17 ++++------------- kernel/src/syscall/net.rs | 19 ++++++++++--------- kernel/src/syscall/proc.rs | 11 +---------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index cb3a424..4905c80 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -326,15 +326,8 @@ pub fn sys_open(path: *const u8, flags: usize, mode: usize) -> SysResult { pub fn sys_close(fd: usize) -> SysResult { info!("close: fd: {:?}", fd); let mut proc = process(); - sys_close_internal(&mut proc, fd) -} - -pub fn sys_close_internal(proc: &mut Process, fd: usize) -> SysResult { - match proc.files.remove(&fd) { - Some(FileLike::File(_)) => Ok(0), - Some(FileLike::Socket(wrapper)) => sys_close_socket(proc, fd, wrapper.handle), - None => Err(SysError::EINVAL), - } + proc.files.remove(&fd).ok_or(SysError::EBADF)?; + Ok(0) } pub fn sys_access(path: *const u8, mode: usize) -> SysResult { @@ -450,10 +443,8 @@ pub fn sys_getdents64(fd: usize, buf: *mut LinuxDirent64, buf_size: usize) -> Sy pub fn sys_dup2(fd1: usize, fd2: usize) -> SysResult { info!("dup2: from {} to {}", fd1, fd2); let mut proc = process(); - if proc.files.contains_key(&fd2) { - // close fd2 first if it is opened - sys_close_internal(&mut proc, fd2)?; - } + // close fd2 first if it is opened + proc.files.remove(&fd2); match proc.files.get(&fd1) { Some(FileLike::File(file)) => { diff --git a/kernel/src/syscall/net.rs b/kernel/src/syscall/net.rs index e1f8cfc..eb03621 100644 --- a/kernel/src/syscall/net.rs +++ b/kernel/src/syscall/net.rs @@ -484,16 +484,17 @@ pub fn sys_recvfrom( } } -pub fn sys_close_socket(proc: &mut Process, fd: usize, handle: SocketHandle) -> SysResult { - let iface = &*(NET_DRIVERS.read()[0]); - let mut sockets = iface.sockets(); - sockets.release(handle); - sockets.prune(); +impl Drop for SocketWrapper { + fn drop(&mut self) { + let iface = &*(NET_DRIVERS.read()[0]); + let mut sockets = iface.sockets(); + sockets.release(self.handle); + sockets.prune(); - // send FIN immediately when applicable - drop(sockets); - iface.poll(); - Ok(0) + // send FIN immediately when applicable + drop(sockets); + iface.poll(); + } } pub fn sys_bind(fd: usize, addr: *const SockaddrIn, len: usize) -> SysResult { diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index c9cf31b..046b6f8 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -179,20 +179,11 @@ pub fn sys_getppid() -> SysResult { Ok(ppid) } -/// Exit the current process +/// Exit the current thread pub fn sys_exit(exit_code: isize) -> ! { let pid = thread::current().id(); info!("exit: {}, code: {}", pid, exit_code); - // close all files. - // TODO: close them in all possible ways a process can exit - let mut proc = process(); - let fds: Vec = proc.files.keys().cloned().collect(); - for fd in fds.into_iter() { - sys_close_internal(&mut proc, fd).unwrap(); - } - drop(proc); - processor().manager().exit(pid, exit_code as usize); processor().yield_now(); unreachable!();