mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 19:03:27 +00:00
Support update rusage in wait4 syscall
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
c85438353f
commit
0d82262beb
@ -49,7 +49,7 @@ pub type Pgid = u32;
|
|||||||
/// Session Id.
|
/// Session Id.
|
||||||
pub type Sid = u32;
|
pub type Sid = u32;
|
||||||
|
|
||||||
pub type ExitCode = i32;
|
pub type ExitCode = u32;
|
||||||
|
|
||||||
pub(super) fn init() {
|
pub(super) fn init() {
|
||||||
timer_manager::init();
|
timer_manager::init();
|
||||||
@ -632,7 +632,7 @@ impl Process {
|
|||||||
*self.status.lock() = ProcessStatus::Zombie(term_status);
|
*self.status.lock() = ProcessStatus::Zombie(term_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit_code(&self) -> Option<u32> {
|
pub fn exit_code(&self) -> Option<ExitCode> {
|
||||||
match &*self.status.lock() {
|
match &*self.status.lock() {
|
||||||
ProcessStatus::Runnable | ProcessStatus::Uninit => None,
|
ProcessStatus::Runnable | ProcessStatus::Uninit => None,
|
||||||
ProcessStatus::Zombie(term_status) => Some(term_status.as_u32()),
|
ProcessStatus::Zombie(term_status) => Some(term_status.as_u32()),
|
||||||
|
@ -27,9 +27,9 @@ impl WaitOptions {
|
|||||||
pub fn wait_child_exit(
|
pub fn wait_child_exit(
|
||||||
child_filter: ProcessFilter,
|
child_filter: ProcessFilter,
|
||||||
wait_options: WaitOptions,
|
wait_options: WaitOptions,
|
||||||
) -> Result<(Pid, ExitCode)> {
|
) -> Result<Option<Arc<Process>>> {
|
||||||
let current = current!();
|
let current = current!();
|
||||||
let (pid, exit_code) = current.children_pauser().pause_until(|| {
|
let zombie_child = current.children_pauser().pause_until(|| {
|
||||||
let unwaited_children = current
|
let unwaited_children = current
|
||||||
.children()
|
.children()
|
||||||
.lock()
|
.lock()
|
||||||
@ -54,29 +54,28 @@ pub fn wait_child_exit(
|
|||||||
|
|
||||||
if let Some(zombie_child) = zombie_child {
|
if let Some(zombie_child) = zombie_child {
|
||||||
let zombie_pid = zombie_child.pid();
|
let zombie_pid = zombie_child.pid();
|
||||||
let exit_code = zombie_child.exit_code().unwrap();
|
|
||||||
if wait_options.contains(WaitOptions::WNOWAIT) {
|
if wait_options.contains(WaitOptions::WNOWAIT) {
|
||||||
// does not reap child, directly return
|
// does not reap child, directly return
|
||||||
return Some(Ok((zombie_pid, exit_code)));
|
return Some(Ok(Some(zombie_child.clone())));
|
||||||
} else {
|
} else {
|
||||||
let exit_code = reap_zombie_child(¤t, zombie_pid);
|
reap_zombie_child(¤t, zombie_pid);
|
||||||
return Some(Ok((zombie_pid, exit_code)));
|
return Some(Ok(Some(zombie_child.clone())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if wait_options.contains(WaitOptions::WNOHANG) {
|
if wait_options.contains(WaitOptions::WNOHANG) {
|
||||||
return Some(Ok((0, 0)));
|
return Some(Ok(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait
|
// wait
|
||||||
None
|
None
|
||||||
})??;
|
})??;
|
||||||
|
|
||||||
Ok((pid, exit_code as _))
|
Ok(zombie_child)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Free zombie child with pid, returns the exit code of child process.
|
/// Free zombie child with pid, returns the exit code of child process.
|
||||||
fn reap_zombie_child(process: &Process, pid: Pid) -> u32 {
|
fn reap_zombie_child(process: &Process, pid: Pid) -> ExitCode {
|
||||||
let child_process = process.children().lock().remove(&pid).unwrap();
|
let child_process = process.children().lock().remove(&pid).unwrap();
|
||||||
assert!(child_process.is_zombie());
|
assert!(child_process.is_zombie());
|
||||||
child_process.root_vmar().destroy_all().unwrap();
|
child_process.root_vmar().destroy_all().unwrap();
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
use super::SyscallReturn;
|
use super::{getrusage::rusage_t, SyscallReturn};
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
process::{wait_child_exit, ProcessFilter, WaitOptions},
|
process::{wait_child_exit, ProcessFilter, WaitOptions},
|
||||||
util::write_val_to_user,
|
util::write_val_to_user,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn sys_wait4(wait_pid: u64, exit_status_ptr: u64, wait_options: u32) -> Result<SyscallReturn> {
|
pub fn sys_wait4(
|
||||||
|
wait_pid: u64,
|
||||||
|
exit_status_ptr: u64,
|
||||||
|
wait_options: u32,
|
||||||
|
rusage_addr: Vaddr,
|
||||||
|
) -> Result<SyscallReturn> {
|
||||||
let wait_options = WaitOptions::from_bits(wait_options)
|
let wait_options = WaitOptions::from_bits(wait_options)
|
||||||
.ok_or_else(|| Error::with_message(Errno::EINVAL, "unknown wait option"))?;
|
.ok_or_else(|| Error::with_message(Errno::EINVAL, "unknown wait option"))?;
|
||||||
debug!(
|
debug!(
|
||||||
@ -16,9 +21,26 @@ pub fn sys_wait4(wait_pid: u64, exit_status_ptr: u64, wait_options: u32) -> Resu
|
|||||||
);
|
);
|
||||||
debug!("wait4 current pid = {}", current!().pid());
|
debug!("wait4 current pid = {}", current!().pid());
|
||||||
let process_filter = ProcessFilter::from_id(wait_pid as _);
|
let process_filter = ProcessFilter::from_id(wait_pid as _);
|
||||||
let (return_pid, exit_code) = wait_child_exit(process_filter, wait_options)?;
|
|
||||||
if return_pid != 0 && exit_status_ptr != 0 {
|
let waited_process = wait_child_exit(process_filter, wait_options)?;
|
||||||
|
let Some(process) = waited_process else {
|
||||||
|
return Ok(SyscallReturn::Return(0 as _));
|
||||||
|
};
|
||||||
|
|
||||||
|
let (return_pid, exit_code) = (process.pid(), process.exit_code().unwrap());
|
||||||
|
if exit_status_ptr != 0 {
|
||||||
write_val_to_user(exit_status_ptr as _, &exit_code)?;
|
write_val_to_user(exit_status_ptr as _, &exit_code)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rusage_addr != 0 {
|
||||||
|
let rusage = rusage_t {
|
||||||
|
ru_utime: process.prof_clock().user_clock().read_time().into(),
|
||||||
|
ru_stime: process.prof_clock().kernel_clock().read_time().into(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
write_val_to_user(rusage_addr, &rusage)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(SyscallReturn::Return(return_pid as _))
|
Ok(SyscallReturn::Return(return_pid as _))
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ pub fn sys_waitid(
|
|||||||
// FIXME: what does infoq and rusage use for?
|
// FIXME: what does infoq and rusage use for?
|
||||||
let process_filter = ProcessFilter::from_which_and_id(which, upid);
|
let process_filter = ProcessFilter::from_which_and_id(which, upid);
|
||||||
let wait_options = WaitOptions::from_bits(options as u32).expect("Unknown wait options");
|
let wait_options = WaitOptions::from_bits(options as u32).expect("Unknown wait options");
|
||||||
let (exit_code, pid) = wait_child_exit(process_filter, wait_options)?;
|
let waited_process = wait_child_exit(process_filter, wait_options)?;
|
||||||
|
let pid = waited_process.map_or(0, |process| process.pid());
|
||||||
Ok(SyscallReturn::Return(pid as _))
|
Ok(SyscallReturn::Return(pid as _))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user