Skip to content

Commit

Permalink
add file flush in sys_close and sys_exit
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw2002426 committed Apr 1, 2024
1 parent 8c43a6a commit 135c431
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/ruxos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ pub const RUX_FILE_LIMIT: usize = 1024;
pub trait FileLike: Send + Sync {
fn read(&self, buf: &mut [u8]) -> LinuxResult<usize>;
fn write(&self, buf: &[u8]) -> LinuxResult<usize>;
fn flush(&self) -> LinuxResult;
fn stat(&self) -> LinuxResult<ctypes::stat>;
fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync>;
fn poll(&self) -> LinuxResult<PollState>;
fn set_nonblocking(&self, nonblocking: bool) -> LinuxResult;
}

lazy_static::lazy_static! {
static ref FD_TABLE: RwLock<FlattenObjects<Arc<dyn FileLike>, RUX_FILE_LIMIT>> = {
pub static ref FD_TABLE: RwLock<FlattenObjects<Arc<dyn FileLike>, RUX_FILE_LIMIT>> = {
let mut fd_table = FlattenObjects::new();
fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin
fd_table.add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
Expand Down Expand Up @@ -67,7 +68,10 @@ pub fn sys_close(fd: c_int) -> c_int {
if (0..=2).contains(&fd) {
return 0; // stdin, stdout, stderr
}
syscall_body!(sys_close, close_file_like(fd).map(|_| 0))
syscall_body!(sys_close, {
let _ = get_file_like(fd)?.flush();
close_file_like(fd).map(|_| 0)
})
}

fn dup_fd(old_fd: c_int) -> LinuxResult<c_int> {
Expand Down
8 changes: 8 additions & 0 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl FileLike for File {
Ok(self.inner.lock().write(buf)?)
}

fn flush(&self) -> LinuxResult {
Ok(self.inner.lock().flush()?)
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
Expand Down Expand Up @@ -117,6 +121,10 @@ impl FileLike for Directory {
Err(LinuxError::EACCES)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
Expand Down
4 changes: 4 additions & 0 deletions api/ruxos_posix_api/src/imp/io_mpx/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ impl FileLike for EpollInstance {
Err(LinuxError::ENOSYS)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let st_mode = 0o600u32; // rw-------
Ok(ctypes::stat {
Expand Down
4 changes: 4 additions & 0 deletions api/ruxos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl FileLike for Socket {
self.send(buf)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
// not really implemented
let st_mode = 0o140000 | 0o777u32; // S_IFSOCK | rwxrwxrwx
Expand Down
4 changes: 4 additions & 0 deletions api/ruxos_posix_api/src/imp/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ impl FileLike for Pipe {
}
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let st_mode = 0o10000 | 0o600u32; // S_IFIFO | rw-------
Ok(ctypes::stat {
Expand Down
15 changes: 15 additions & 0 deletions api/ruxos_posix_api/src/imp/pthread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ruxtask::AxTaskRef;
use spin::RwLock;

use crate::ctypes;
use crate::imp::fd_ops::{get_file_like, FD_TABLE, RUX_FILE_LIMIT};

pub mod condvar;
pub mod mutex;
Expand Down Expand Up @@ -208,6 +209,20 @@ pub unsafe fn sys_pthread_create(
/// Exits the current thread. The value `retval` will be returned to the joiner.
pub fn sys_pthread_exit(retval: *mut c_void) -> ! {
debug!("sys_pthread_exit <= {:#x}", retval as usize);
let mut now_fd_count = 0;
let mut fd_index: usize = 0;
while fd_index < RUX_FILE_LIMIT {
let file_like = get_file_like(fd_index.try_into().unwrap()).expect("");
if file_like.flush().is_err() {
fd_index += 1;
continue;
}
now_fd_count += 1;
if now_fd_count == FD_TABLE.read().count() {
break;
}
fd_index += 1;
}
#[cfg(feature = "musl")]
{
use core::sync::atomic::Ordering;
Expand Down
8 changes: 8 additions & 0 deletions api/ruxos_posix_api/src/imp/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl super::fd_ops::FileLike for Stdin {
Err(LinuxError::EPERM)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<crate::ctypes::stat> {
let st_mode = 0o20000 | 0o440u32; // S_IFCHR | r--r-----
Ok(crate::ctypes::stat {
Expand Down Expand Up @@ -156,6 +160,10 @@ impl super::fd_ops::FileLike for Stdout {
Ok(self.inner.lock().write(buf)?)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<crate::ctypes::stat> {
let st_mode = 0o20000 | 0o220u32; // S_IFCHR | -w--w----
Ok(crate::ctypes::stat {
Expand Down
15 changes: 15 additions & 0 deletions api/ruxos_posix_api/src/imp/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* See the Mulan PSL v2 for more details.
*/

use crate::imp::fd_ops::{get_file_like, FD_TABLE, RUX_FILE_LIMIT};
use core::ffi::c_int;

/// Relinquish the CPU, and switches to another task.
Expand Down Expand Up @@ -42,6 +43,20 @@ pub fn sys_getpid() -> c_int {
/// Exit current task
pub fn sys_exit(exit_code: c_int) -> ! {
debug!("sys_exit <= {}", exit_code);
let mut now_fd_count = 0;
let mut fd_index: usize = 0;
while fd_index < RUX_FILE_LIMIT {
let file_like = get_file_like(fd_index.try_into().unwrap()).expect("");
if file_like.flush().is_err() {
fd_index += 1;
continue;
}
now_fd_count += 1;
if now_fd_count == FD_TABLE.read().count() {
break;
}
fd_index += 1;
}
#[cfg(feature = "multitask")]
ruxtask::exit(exit_code);
#[cfg(not(feature = "multitask"))]
Expand Down
6 changes: 6 additions & 0 deletions modules/ruxfs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,10 @@ impl Disk {
};
Ok(write_size)
}

///flush device cache
pub fn do_flush(&mut self) -> DevResult {
let _ = self.dev.flush();
Ok(())
}
}
7 changes: 7 additions & 0 deletions modules/ruxfs/src/fs/fatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl FatFileSystem {
impl VfsNodeOps for FileWrapper<'static> {
axfs_vfs::impl_vfs_non_dir_default! {}

fn fsync(&self) -> VfsResult {
let mut file = self.0.lock();
let _ = file.flush();
Ok(())
}

fn get_attr(&self) -> VfsResult<VfsNodeAttr> {
let size = self.0.lock().seek(SeekFrom::End(0)).map_err(as_vfs_err)?;
let blocks = (size + BLOCK_SIZE as u64 - 1) / BLOCK_SIZE as u64;
Expand Down Expand Up @@ -272,6 +278,7 @@ impl Write for Disk {
Ok(write_len)
}
fn flush(&mut self) -> Result<(), Self::Error> {
let _ = self.do_flush();
Ok(())
}
}
Expand Down

0 comments on commit 135c431

Please sign in to comment.