asterinas/services/libs/jinux-std/src/process/mod.rs

403 lines
12 KiB
Rust
Raw Normal View History

2022-12-20 06:12:22 +00:00
use self::posix_thread::posix_thread_ext::PosixThreadExt;
2022-10-31 08:14:41 +00:00
use self::process_group::ProcessGroup;
2022-10-27 07:18:24 +00:00
use self::process_vm::user_heap::UserHeap;
use self::process_vm::ProcessVm;
2022-12-20 06:12:22 +00:00
use self::rlimit::ResourceLimits;
2022-11-02 11:35:39 +00:00
use self::signal::constants::SIGCHLD;
2022-10-31 08:14:41 +00:00
use self::signal::sig_disposition::SigDispositions;
use self::signal::sig_queues::SigQueues;
2022-11-02 11:35:39 +00:00
use self::signal::signals::kernel::KernelSignal;
use self::signal::signals::Signal;
2022-09-08 07:37:34 +00:00
use self::status::ProcessStatus;
2023-05-18 08:57:29 +00:00
use crate::device::tty::get_n_tty;
2022-11-15 06:08:21 +00:00
use crate::fs::file_table::FileTable;
2023-03-14 07:35:38 +00:00
use crate::fs::fs_resolver::FsResolver;
2023-03-09 09:07:30 +00:00
use crate::fs::utils::FileCreationMask;
2022-11-09 07:14:12 +00:00
use crate::prelude::*;
use crate::thread::{allocate_tid, thread_table, Thread};
use crate::vm::vmar::Vmar;
use jinux_frame::sync::WaitQueue;
use jinux_rights::Full;
2022-10-28 05:47:57 +00:00
pub mod clone;
pub mod fifo_scheduler;
2022-12-20 06:12:22 +00:00
pub mod posix_thread;
2022-09-27 11:51:18 +00:00
pub mod process_filter;
2022-10-31 08:14:41 +00:00
pub mod process_group;
2022-12-20 06:12:22 +00:00
pub mod process_table;
2022-10-27 07:18:24 +00:00
pub mod process_vm;
2023-03-14 07:35:38 +00:00
pub mod program_loader;
2022-12-20 06:12:22 +00:00
pub mod rlimit;
2022-10-31 08:14:41 +00:00
pub mod signal;
2022-09-08 07:37:34 +00:00
pub mod status;
2023-09-01 03:40:22 +00:00
mod term_status;
2022-09-27 11:51:18 +00:00
pub mod wait;
2023-09-01 03:40:22 +00:00
pub use term_status::TermStatus;
2022-11-15 06:08:21 +00:00
pub type Pid = i32;
pub type Pgid = i32;
2022-09-27 11:51:18 +00:00
pub type ExitCode = i32;
2022-09-08 07:37:34 +00:00
2023-03-07 06:24:29 +00:00
const INIT_PROCESS_PID: Pid = 1;
2022-12-20 06:12:22 +00:00
/// Process stands for a set of threads that shares the same userspace.
pub struct Process {
2022-09-08 07:37:34 +00:00
// Immutable Part
2022-09-27 11:51:18 +00:00
pid: Pid,
process_vm: ProcessVm,
2022-11-15 06:08:21 +00:00
/// wait for child status changed
2022-11-09 07:14:12 +00:00
waiting_children: WaitQueue,
2022-09-08 07:37:34 +00:00
// Mutable Part
/// The executable path.
executable_path: RwLock<String>,
2022-12-20 06:12:22 +00:00
/// The threads
threads: Mutex<Vec<Arc<Thread>>>,
2022-09-08 07:37:34 +00:00
/// Process status
status: Mutex<ProcessStatus>,
/// Parent process
2022-12-15 06:53:43 +00:00
parent: Mutex<Weak<Process>>,
2022-09-08 07:37:34 +00:00
/// Children processes
2022-11-15 06:08:21 +00:00
children: Mutex<BTreeMap<Pid, Arc<Process>>>,
2022-10-31 08:14:41 +00:00
/// Process group
2022-12-15 06:53:43 +00:00
process_group: Mutex<Weak<ProcessGroup>>,
2022-11-15 06:08:21 +00:00
/// File table
2022-12-20 06:12:22 +00:00
file_table: Arc<Mutex<FileTable>>,
/// FsResolver
fs: Arc<RwLock<FsResolver>>,
2023-03-09 09:07:30 +00:00
/// umask
umask: Arc<RwLock<FileCreationMask>>,
2022-12-20 06:12:22 +00:00
/// resource limits
resource_limits: Mutex<ResourceLimits>,
2022-10-31 08:14:41 +00:00
// Signal
2022-12-20 06:12:22 +00:00
/// sig dispositions
sig_dispositions: Arc<Mutex<SigDispositions>>,
/// Process-level signal queues
2022-10-31 08:14:41 +00:00
sig_queues: Mutex<SigQueues>,
}
impl Process {
2022-09-16 03:14:46 +00:00
/// returns the current process
pub fn current() -> Arc<Process> {
2022-12-15 06:53:43 +00:00
let current_thread = Thread::current();
2023-03-07 06:24:29 +00:00
if let Some(posix_thread) = current_thread.as_posix_thread() {
2022-12-20 06:12:22 +00:00
posix_thread.process()
} else {
2023-03-07 06:24:29 +00:00
panic!("[Internal error]The current thread does not belong to a process");
2022-12-20 06:12:22 +00:00
}
2022-09-16 03:14:46 +00:00
}
2022-09-27 07:15:12 +00:00
/// create a new process(not schedule it)
2023-09-04 03:04:42 +00:00
#[allow(clippy::too_many_arguments)]
2022-09-27 07:15:12 +00:00
pub fn new(
2022-09-27 11:51:18 +00:00
pid: Pid,
2023-03-07 06:24:29 +00:00
parent: Weak<Process>,
2022-12-15 06:53:43 +00:00
threads: Vec<Arc<Thread>>,
executable_path: String,
process_vm: ProcessVm,
2022-12-15 06:53:43 +00:00
process_group: Weak<ProcessGroup>,
2022-12-20 06:12:22 +00:00
file_table: Arc<Mutex<FileTable>>,
fs: Arc<RwLock<FsResolver>>,
2023-03-09 09:07:30 +00:00
umask: Arc<RwLock<FileCreationMask>>,
2022-12-20 06:12:22 +00:00
sig_dispositions: Arc<Mutex<SigDispositions>>,
2022-09-16 03:14:46 +00:00
) -> Self {
2022-09-27 11:51:18 +00:00
let children = BTreeMap::new();
let waiting_children = WaitQueue::new();
2022-12-20 06:12:22 +00:00
let resource_limits = ResourceLimits::default();
Self {
pid,
2022-12-20 06:12:22 +00:00
threads: Mutex::new(threads),
executable_path: RwLock::new(executable_path),
process_vm,
2022-09-27 11:51:18 +00:00
waiting_children,
2022-09-08 07:37:34 +00:00
status: Mutex::new(ProcessStatus::Runnable),
parent: Mutex::new(parent),
children: Mutex::new(children),
2022-10-31 08:14:41 +00:00
process_group: Mutex::new(process_group),
2022-12-20 06:12:22 +00:00
file_table,
fs,
2023-03-09 09:07:30 +00:00
umask,
2022-12-20 06:12:22 +00:00
sig_dispositions,
sig_queues: Mutex::new(SigQueues::new()),
resource_limits: Mutex::new(resource_limits),
}
}
2022-11-09 07:14:12 +00:00
pub fn waiting_children(&self) -> &WaitQueue {
2022-09-27 11:51:18 +00:00
&self.waiting_children
}
2022-12-20 06:12:22 +00:00
/// init a user process and run the process
2022-11-09 07:14:12 +00:00
pub fn spawn_user_process(
2023-03-14 07:35:38 +00:00
executable_path: &str,
2022-11-09 07:14:12 +00:00
argv: Vec<CString>,
envp: Vec<CString>,
) -> Result<Arc<Self>> {
// spawn user process should give an absolute path
debug_assert!(executable_path.starts_with('/'));
let process = Process::create_user_process(executable_path, argv, envp)?;
2022-11-21 11:09:23 +00:00
// FIXME: How to determine the fg process group?
let process_group = Weak::clone(&process.process_group.lock());
2023-01-06 03:14:34 +00:00
// FIXME: tty should be a parameter?
let tty = get_n_tty();
tty.set_fg(process_group);
2022-12-15 06:53:43 +00:00
process.run();
Ok(process)
2022-09-08 07:37:34 +00:00
}
2022-11-09 07:14:12 +00:00
fn create_user_process(
2023-03-14 07:35:38 +00:00
executable_path: &str,
2022-11-09 07:14:12 +00:00
argv: Vec<CString>,
envp: Vec<CString>,
) -> Result<Arc<Self>> {
let fs = FsResolver::new();
2023-03-09 09:07:30 +00:00
let umask = FileCreationMask::default();
let pid = allocate_tid();
let parent = Weak::new();
let process_group = Weak::new();
let process_vm = ProcessVm::alloc()?;
let file_table = FileTable::new_with_stdio();
let sig_dispositions = SigDispositions::new();
let user_process = Arc::new(Process::new(
pid,
parent,
vec![],
executable_path.to_string(),
process_vm,
process_group,
Arc::new(Mutex::new(file_table)),
Arc::new(RwLock::new(fs)),
2023-03-09 09:07:30 +00:00
Arc::new(RwLock::new(umask)),
Arc::new(Mutex::new(sig_dispositions)),
));
let thread = Thread::new_posix_thread_from_executable(
pid,
&user_process.process_vm,
&user_process.fs().read(),
executable_path,
Arc::downgrade(&user_process),
argv,
envp,
)?;
user_process.threads().lock().push(thread);
2022-10-31 08:14:41 +00:00
// Set process group
user_process.create_and_set_process_group();
2022-12-20 06:12:22 +00:00
process_table::add_process(user_process.clone());
Ok(user_process)
}
2022-09-27 11:51:18 +00:00
/// returns the pid of the process
pub fn pid(&self) -> Pid {
self.pid
}
2022-09-08 07:37:34 +00:00
2022-09-27 11:51:18 +00:00
/// returns the process group id of the process
pub fn pgid(&self) -> Pgid {
2022-12-15 06:53:43 +00:00
if let Some(process_group) = self.process_group.lock().upgrade() {
2022-10-31 08:14:41 +00:00
process_group.pgid()
} else {
0
}
}
2022-12-15 06:53:43 +00:00
pub fn process_group(&self) -> &Mutex<Weak<ProcessGroup>> {
2022-10-31 08:14:41 +00:00
&self.process_group
2022-09-27 11:51:18 +00:00
}
2022-09-27 07:15:12 +00:00
/// add a child process
pub fn add_child(&self, child: Arc<Process>) {
2022-09-27 11:51:18 +00:00
let child_pid = child.pid();
self.children.lock().insert(child_pid, child);
2022-09-08 07:37:34 +00:00
}
2022-12-20 06:12:22 +00:00
pub fn set_parent(&self, parent: Weak<Process>) {
2022-12-15 06:53:43 +00:00
*self.parent.lock() = parent;
2022-09-08 07:37:34 +00:00
}
2022-11-10 09:57:39 +00:00
/// Set process group for current process. If old process group exists,
/// remove current process from old process group.
2022-10-31 08:14:41 +00:00
pub fn set_process_group(&self, process_group: Weak<ProcessGroup>) {
2022-12-15 06:53:43 +00:00
if let Some(old_process_group) = self.process_group.lock().upgrade() {
old_process_group.remove_process(self.pid());
2022-10-31 08:14:41 +00:00
}
2022-12-15 06:53:43 +00:00
*self.process_group.lock() = process_group;
2022-10-31 08:14:41 +00:00
}
2022-12-20 06:12:22 +00:00
pub fn file_table(&self) -> &Arc<Mutex<FileTable>> {
2022-11-15 06:08:21 +00:00
&self.file_table
}
pub fn fs(&self) -> &Arc<RwLock<FsResolver>> {
&self.fs
}
2023-03-09 09:07:30 +00:00
pub fn umask(&self) -> &Arc<RwLock<FileCreationMask>> {
&self.umask
}
2022-10-31 08:14:41 +00:00
/// create a new process group for the process and add it to globle table.
/// Then set the process group for current process.
fn create_and_set_process_group(self: &Arc<Self>) {
let process_group = Arc::new(ProcessGroup::new(self.clone()));
let pgid = process_group.pgid();
self.set_process_group(Arc::downgrade(&process_group));
2022-12-20 06:12:22 +00:00
process_table::add_process_group(process_group);
2022-10-31 08:14:41 +00:00
}
2022-11-09 07:14:12 +00:00
pub fn parent(&self) -> Option<Arc<Process>> {
2022-12-15 06:53:43 +00:00
self.parent.lock().upgrade()
2022-09-27 11:51:18 +00:00
}
2022-12-20 06:12:22 +00:00
/// Exit thread group(the process).
2022-11-09 07:14:12 +00:00
/// Set the status of the process as Zombie and set exit code.
2022-10-31 08:14:41 +00:00
/// Move all children to init process.
/// Wake up the parent wait queue if parent is waiting for self.
2023-09-01 03:40:22 +00:00
pub fn exit_group(&self, term_status: TermStatus) {
2022-12-20 06:12:22 +00:00
debug!("exit group was called");
2023-09-01 03:40:22 +00:00
if self.status.lock().is_zombie() {
return;
}
self.status.lock().set_zombie(term_status);
let threads = self.threads.lock().clone();
for thread in threads {
if thread.is_exited() {
continue;
}
2022-12-20 06:12:22 +00:00
thread.exit();
2023-09-01 03:40:22 +00:00
if let Some(posix_thread) = thread.as_posix_thread() {
let tid = thread.tid();
if let Err(e) = posix_thread.exit(tid, term_status) {
debug!("Ignore error when call exit: {:?}", e);
}
}
2022-12-20 06:12:22 +00:00
}
2023-05-29 05:29:53 +00:00
// close all files then exit the process
let files = self.file_table().lock().close_all();
for file in files {
let _ = file.clean_for_close();
}
2022-09-08 07:37:34 +00:00
// move children to the init process
2022-11-09 07:14:12 +00:00
if !self.is_init_process() {
2023-03-07 06:24:29 +00:00
if let Some(init_process) = get_init_process() {
2023-08-02 04:11:42 +00:00
for (_, child_process) in self.children.lock().extract_if(|_, _| true) {
2023-03-07 06:24:29 +00:00
child_process.set_parent(Arc::downgrade(&init_process));
init_process.add_child(child_process);
}
2022-09-08 07:37:34 +00:00
}
}
2022-09-27 11:51:18 +00:00
2022-11-09 07:14:12 +00:00
if let Some(parent) = self.parent() {
2022-11-02 11:35:39 +00:00
// set parent sig child
let signal = Box::new(KernelSignal::new(SIGCHLD));
parent.sig_queues().lock().enqueue(signal);
// wake up parent waiting children, if any
2022-11-09 07:14:12 +00:00
parent.waiting_children().wake_all();
2022-09-27 11:51:18 +00:00
}
2022-09-08 07:37:34 +00:00
}
2022-09-27 07:15:12 +00:00
/// if the current process is init process
pub fn is_init_process(&self) -> bool {
2022-09-08 07:37:34 +00:00
self.pid == 0
}
/// start to run current process
2022-12-15 06:53:43 +00:00
pub fn run(&self) {
2022-12-20 06:12:22 +00:00
let threads = self.threads.lock();
// when run the process, the process should has only one thread
debug_assert!(threads.len() == 1);
let thread = threads[0].clone();
// should not hold the lock when run thread
drop(threads);
thread.run();
}
pub fn threads(&self) -> &Mutex<Vec<Arc<Thread>>> {
&self.threads
2022-09-08 07:37:34 +00:00
}
2022-09-27 07:15:12 +00:00
/// returns the user_vm
pub fn process_vm(&self) -> &ProcessVm {
&self.process_vm
2022-09-27 07:15:12 +00:00
}
/// returns the root vmar
pub fn root_vmar(&self) -> &Vmar<Full> {
2023-09-04 03:04:42 +00:00
self.process_vm.root_vmar()
}
2022-09-27 05:52:21 +00:00
/// returns the user heap if the process does have, otherwise None
pub fn user_heap(&self) -> &UserHeap {
self.process_vm.user_heap()
2022-09-16 03:14:46 +00:00
}
2022-11-02 11:35:39 +00:00
/// free zombie child with pid, returns the exit code of child process.
/// remove process from process group.
2023-09-01 03:40:22 +00:00
pub fn reap_zombie_child(&self, pid: Pid) -> u32 {
2022-09-27 11:51:18 +00:00
let child_process = self.children.lock().remove(&pid).unwrap();
2022-10-31 08:14:41 +00:00
assert!(child_process.status().lock().is_zombie());
child_process.root_vmar().destroy_all().unwrap();
2022-12-20 06:12:22 +00:00
for thread in &*child_process.threads.lock() {
thread_table::remove_thread(thread.tid());
}
process_table::remove_process(child_process.pid());
2022-12-15 06:53:43 +00:00
if let Some(process_group) = child_process.process_group().lock().upgrade() {
process_group.remove_process(child_process.pid);
2022-11-02 11:35:39 +00:00
}
2023-09-01 03:40:22 +00:00
child_process.exit_code().unwrap()
2022-09-27 11:51:18 +00:00
}
2022-11-15 06:08:21 +00:00
pub fn children(&self) -> &Mutex<BTreeMap<Pid, Arc<Process>>> {
2022-11-09 07:14:12 +00:00
&self.children
2022-09-27 11:51:18 +00:00
}
2023-09-01 03:40:22 +00:00
pub fn exit_code(&self) -> Option<u32> {
match &*self.status.lock() {
ProcessStatus::Runnable => None,
ProcessStatus::Zombie(term_status) => Some(term_status.as_u32()),
}
2022-09-27 11:51:18 +00:00
}
2022-09-27 05:52:21 +00:00
/// whether the process has child process
2022-09-08 07:37:34 +00:00
pub fn has_child(&self) -> bool {
self.children.lock().len() != 0
}
pub fn executable_path(&self) -> &RwLock<String> {
&self.executable_path
2022-09-16 03:14:46 +00:00
}
2022-09-27 11:51:18 +00:00
2022-10-31 08:14:41 +00:00
pub fn status(&self) -> &Mutex<ProcessStatus> {
&self.status
}
2022-12-20 06:12:22 +00:00
pub fn resource_limits(&self) -> &Mutex<ResourceLimits> {
&self.resource_limits
}
pub fn sig_dispositions(&self) -> &Arc<Mutex<SigDispositions>> {
2022-10-31 08:14:41 +00:00
&self.sig_dispositions
}
pub fn sig_queues(&self) -> &Mutex<SigQueues> {
&self.sig_queues
}
pub fn enqueue_signal(&self, signal: Box<dyn Signal>) {
if !self.status().lock().is_zombie() {
self.sig_queues.lock().enqueue(signal);
}
}
2022-09-08 07:37:34 +00:00
}
/// Get the init process
2023-03-07 06:24:29 +00:00
pub fn get_init_process() -> Option<Arc<Process>> {
process_table::pid_to_process(INIT_PROCESS_PID)
}