Skip to content

Commit

Permalink
syscall: fix fd leak in epoll
Browse files Browse the repository at this point in the history
Signed-off-by: rayylee <[email protected]>
  • Loading branch information
hbuxiaofei committed Apr 23, 2024
1 parent 5a65115 commit a2a808b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ulib/axstarry/src/syscall_fs/ctype/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl EpollFile {
}
}

/// 判断fd是否在monitor_list中
pub fn contains(&self, fd: i32) -> bool {
let inner = self.inner.lock();
if inner.monitor_list.contains_key(&fd) {
return true;
}
false
}

/// 控制指定的事件,改变其对应的事件内容
///
/// 成功返回0,错误返回对应的编号
Expand Down
15 changes: 15 additions & 0 deletions ulib/axstarry/src/syscall_fs/imp/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::syscall_fs::ctype::{
dir::new_dir,
file::{new_fd, new_inode},
pipe::make_pipe,
epoll::{EpollCtl, EpollEvent, EpollEventType, EpollFile},
};
/// 功能:从一个文件描述符中读取;
/// # Arguments
Expand Down Expand Up @@ -432,6 +433,20 @@ pub fn syscall_close(args: [usize; 6]) -> SyscallResult {
return Err(SyscallError::EPERM);
}
// let file = process_inner.fd_manager.fd_table[fd].unwrap();
for i in 0..fd_table.len() {
if let Some(file) = fd_table[i].as_ref() {
if let Some(epoll_file) = file.as_any().downcast_ref::<EpollFile>() {
if epoll_file.contains(fd as i32) {
let ev = EpollEvent{
event_type: EpollEventType::EPOLLMSG,
data: 0
};
epoll_file.epoll_ctl(EpollCtl::DEL, fd as i32, ev)?;
}
}
}
}

fd_table[fd] = None;
// for i in 0..process_inner.fd_table.len() {
// if let Some(file) = process_inner.fd_table[i].as_ref() {
Expand Down

0 comments on commit a2a808b

Please sign in to comment.