refactor sys_close_internal to SocketWrapper::drop

master
WangRunji 6 years ago
parent 1645451749
commit 84b2af3cdb

@ -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)) => {

@ -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 {

@ -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<usize> = 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!();

Loading…
Cancel
Save