From d9e05012f07a81c628f99d4d7636a1e08ef771c6 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 1 Mar 2019 08:03:10 +0800 Subject: [PATCH] Implement socket sys_close --- kernel/src/syscall/fs.rs | 6 ++++-- kernel/src/syscall/net.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 1b996e1..7473e17 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -107,8 +107,10 @@ pub fn sys_open(path: *const u8, flags: usize, mode: usize) -> SysResult { pub fn sys_close(fd: usize) -> SysResult { info!("close: fd: {:?}", fd); - match process().files.remove(&fd) { - Some(_) => Ok(0), + let mut proc = process(); + match proc.files.remove(&fd) { + Some(FileLike::File(_)) => Ok(0), + Some(FileLike::Socket(handle)) => sys_close_socket(&mut proc, fd, handle), None => Err(SysError::EINVAL), } } diff --git a/kernel/src/syscall/net.rs b/kernel/src/syscall/net.rs index a0428bc..2ff7371 100644 --- a/kernel/src/syscall/net.rs +++ b/kernel/src/syscall/net.rs @@ -206,3 +206,15 @@ pub fn sys_recvfrom( warn!("sys_recvfrom is unimplemented"); Err(SysError::EINVAL) } + +pub fn sys_close_socket(proc: &mut Process, fd: usize, handle: SocketHandle) -> SysResult { + let mut socket = proc.sockets.remove(handle); + match socket { + Socket::Tcp(ref mut tcp_socket) => { + tcp_socket.close(); + } + _ => {} + } + + Ok(0) +} \ No newline at end of file