process_list.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. use core::sync::atomic::Ordering;
  2. use super::{Process, ProcessGroup, Session, Thread, WaitObject, WaitType};
  3. use crate::rcu::rcu_sync;
  4. use alloc::{
  5. collections::btree_map::BTreeMap,
  6. sync::{Arc, Weak},
  7. };
  8. use eonix_runtime::task::Task;
  9. use eonix_sync::{AsProof as _, AsProofMut as _, RwLock};
  10. pub struct ProcessList {
  11. /// The init process.
  12. init: Option<Arc<Process>>,
  13. /// All threads.
  14. threads: BTreeMap<u32, Arc<Thread>>,
  15. /// All processes.
  16. processes: BTreeMap<u32, Weak<Process>>,
  17. /// All process groups.
  18. pgroups: BTreeMap<u32, Weak<ProcessGroup>>,
  19. /// All sessions.
  20. sessions: BTreeMap<u32, Weak<Session>>,
  21. }
  22. static GLOBAL_PROC_LIST: RwLock<ProcessList> = RwLock::new(ProcessList {
  23. init: None,
  24. threads: BTreeMap::new(),
  25. processes: BTreeMap::new(),
  26. pgroups: BTreeMap::new(),
  27. sessions: BTreeMap::new(),
  28. });
  29. impl ProcessList {
  30. pub fn get() -> &'static RwLock<Self> {
  31. &GLOBAL_PROC_LIST
  32. }
  33. pub fn add_session(&mut self, session: &Arc<Session>) {
  34. self.sessions.insert(session.sid, Arc::downgrade(session));
  35. }
  36. pub fn add_pgroup(&mut self, pgroup: &Arc<ProcessGroup>) {
  37. self.pgroups.insert(pgroup.pgid, Arc::downgrade(pgroup));
  38. }
  39. pub fn add_process(&mut self, process: &Arc<Process>) {
  40. self.processes.insert(process.pid, Arc::downgrade(process));
  41. }
  42. pub fn add_thread(&mut self, thread: &Arc<Thread>) {
  43. self.threads.insert(thread.tid, thread.clone());
  44. }
  45. pub fn remove_process(&mut self, pid: u32) {
  46. // Thread group leader has the same tid as the pid.
  47. if let Some(thread) = self.threads.remove(&pid) {
  48. self.processes.remove(&pid);
  49. // SAFETY: We wait until all references are dropped below with `rcu_sync()`.
  50. let session = unsafe { thread.process.session.swap(None) }.unwrap();
  51. let pgroup = unsafe { thread.process.pgroup.swap(None) }.unwrap();
  52. let _parent = unsafe { thread.process.parent.swap(None) }.unwrap();
  53. pgroup.remove_member(pid, self.prove_mut());
  54. Task::block_on(rcu_sync());
  55. if Arc::strong_count(&pgroup) == 1 {
  56. self.pgroups.remove(&pgroup.pgid);
  57. }
  58. if Arc::strong_count(&session) == 1 {
  59. self.sessions.remove(&session.sid);
  60. }
  61. } else {
  62. panic!("Process {} not found", pid);
  63. }
  64. }
  65. pub fn set_init_process(&mut self, init: Arc<Process>) {
  66. let old_init = self.init.replace(init);
  67. assert!(old_init.is_none(), "Init process already set");
  68. }
  69. pub fn init_process(&self) -> &Arc<Process> {
  70. self.init.as_ref().unwrap()
  71. }
  72. pub fn try_find_thread(&self, tid: u32) -> Option<&Arc<Thread>> {
  73. self.threads.get(&tid)
  74. }
  75. pub fn try_find_process(&self, pid: u32) -> Option<Arc<Process>> {
  76. self.processes.get(&pid).and_then(Weak::upgrade)
  77. }
  78. pub fn try_find_pgroup(&self, pgid: u32) -> Option<Arc<ProcessGroup>> {
  79. self.pgroups.get(&pgid).and_then(Weak::upgrade)
  80. }
  81. pub fn try_find_session(&self, sid: u32) -> Option<Arc<Session>> {
  82. self.sessions.get(&sid).and_then(Weak::upgrade)
  83. }
  84. /// Make the process a zombie and notify the parent.
  85. /// # Safety
  86. /// This function will destroy the process and all its threads.
  87. /// It is the caller's responsibility to ensure that the process is not
  88. /// running or will not run after this function is called.
  89. pub unsafe fn do_kill_process(&mut self, process: &Arc<Process>, status: WaitType) {
  90. if process.pid == 1 {
  91. panic!("init exited");
  92. }
  93. let inner = process.inner.access_mut(self.prove_mut());
  94. // TODO!!!!!!: When we are killing multiple threads, we need to wait until all
  95. // the threads are stopped then proceed.
  96. for thread in inner.threads.values().map(|t| t.upgrade().unwrap()) {
  97. assert!(thread.tid == Thread::current().tid);
  98. // TODO: Send SIGKILL to all threads.
  99. thread.files.close_all();
  100. thread.dead.store(true, Ordering::SeqCst);
  101. }
  102. // If we are the session leader, we should drop the control terminal.
  103. if process.session(self.prove()).sid == process.pid {
  104. if let Some(terminal) =
  105. Task::block_on(process.session(self.prove()).drop_control_terminal())
  106. {
  107. terminal.drop_session();
  108. }
  109. }
  110. // Release the MMList as well as the page table.
  111. unsafe {
  112. // SAFETY: We are exiting the process, so no one might be using it.
  113. process.mm_list.replace(None);
  114. }
  115. // Make children orphans (adopted by init)
  116. {
  117. let init = self.init_process();
  118. inner.children.retain(|_, child| {
  119. let child = child.upgrade().unwrap();
  120. // SAFETY: `child.parent` must be ourself. So we don't need to free it.
  121. unsafe { child.parent.swap(Some(init.clone())) };
  122. init.add_child(&child, self.prove_mut());
  123. false
  124. });
  125. }
  126. let mut init_notify = self.init_process().notify_batch();
  127. process
  128. .wait_list
  129. .drain_exited()
  130. .into_iter()
  131. .for_each(|item| init_notify.notify(item));
  132. init_notify.finish(self.prove());
  133. process.parent(self.prove()).notify(
  134. WaitObject {
  135. pid: process.pid,
  136. code: status,
  137. },
  138. self.prove(),
  139. );
  140. }
  141. }