Browse Source

feat: implement namespace proxy and PID namespace support (#1244)

  - Add nsproxy module for unified namespace management across process types
  - Implement complete PID namespace isolation with process ID mapping and management
  - Refactor credential system to use Arc<Cred> for improved performance and memory safety
  - Unify namespace common field management to enhance code reusability
  - Optimize PID namespace process lookup and management mechanisms
  - Enhance TTY subsystem with improved terminal job control and virtual terminal management
  - Improve signal handling system with namespace-level signal isolation
  - Add sys_getpgrp system call and refine process group management
  - Refactor signal processing modules across x86 and RISC-V architectures
LoGin 3 weeks ago
parent
commit
5038bf86d5
75 changed files with 2500 additions and 1107 deletions
  1. 2 2
      kernel/src/arch/loongarch64/process/kthread.rs
  2. 2 4
      kernel/src/arch/riscv64/process/kthread.rs
  3. 34 13
      kernel/src/arch/x86_64/ipc/signal.rs
  4. 1 1
      kernel/src/arch/x86_64/mm/fault.rs
  5. 3 3
      kernel/src/arch/x86_64/process/kthread.rs
  6. 2 2
      kernel/src/arch/x86_64/process/syscall.rs
  7. 2 2
      kernel/src/arch/x86_64/syscall/mod.rs
  8. 2 2
      kernel/src/debug/klog/mm.rs
  9. 7 2
      kernel/src/debug/panic/mod.rs
  10. 10 1
      kernel/src/driver/base/device/device_number.rs
  11. 45 11
      kernel/src/driver/tty/tty_core.rs
  12. 60 11
      kernel/src/driver/tty/tty_device.rs
  13. 6 0
      kernel/src/driver/tty/tty_driver.rs
  14. 161 54
      kernel/src/driver/tty/tty_job_control.rs
  15. 3 2
      kernel/src/driver/tty/tty_ldisc/ntty.rs
  16. 4 0
      kernel/src/driver/tty/tty_port.rs
  17. 2 2
      kernel/src/driver/tty/virtual_terminal/virtual_console.rs
  18. 1 1
      kernel/src/filesystem/devfs/mod.rs
  19. 44 20
      kernel/src/filesystem/procfs/mod.rs
  20. 3 3
      kernel/src/filesystem/vfs/file.rs
  21. 4 0
      kernel/src/ipc/generic_signal.rs
  22. 27 8
      kernel/src/ipc/kill.rs
  23. 5 2
      kernel/src/ipc/pipe.rs
  24. 3 3
      kernel/src/ipc/shm.rs
  25. 27 16
      kernel/src/ipc/signal.rs
  26. 43 5
      kernel/src/ipc/signal_types.rs
  27. 17 13
      kernel/src/ipc/syscall/sys_kill.rs
  28. 2 2
      kernel/src/ipc/syscall/sys_restart.rs
  29. 1 1
      kernel/src/ipc/syscall/sys_sigaction.rs
  30. 5 4
      kernel/src/libs/elf.rs
  31. 4 4
      kernel/src/libs/futex/futex.rs
  32. 4 4
      kernel/src/libs/mutex.rs
  33. 1 1
      kernel/src/libs/semaphore.rs
  34. 2 2
      kernel/src/libs/wait_queue.rs
  35. 1 1
      kernel/src/mm/mmio_buddy.rs
  36. 3 3
      kernel/src/net/socket/mod.rs
  37. 24 7
      kernel/src/process/cred.rs
  38. 46 2
      kernel/src/process/exec.rs
  39. 0 17
      kernel/src/process/execve.rs
  40. 73 24
      kernel/src/process/exit.rs
  41. 148 65
      kernel/src/process/fork.rs
  42. 8 8
      kernel/src/process/kthread.rs
  43. 251 100
      kernel/src/process/mod.rs
  44. 37 0
      kernel/src/process/namespace/mod.rs
  45. 179 0
      kernel/src/process/namespace/nsproxy.rs
  46. 220 0
      kernel/src/process/namespace/pid_namespace.rs
  47. 88 0
      kernel/src/process/namespace/user_namespace.rs
  48. 515 5
      kernel/src/process/pid.rs
  49. 36 345
      kernel/src/process/process_group.rs
  50. 68 213
      kernel/src/process/session.rs
  51. 16 0
      kernel/src/process/signal.rs
  52. 2 2
      kernel/src/process/stdio.rs
  53. 3 0
      kernel/src/process/syscall/mod.rs
  54. 5 5
      kernel/src/process/syscall/sys_clone.rs
  55. 23 9
      kernel/src/process/syscall/sys_getpgid.rs
  56. 41 0
      kernel/src/process/syscall/sys_getpgrp.rs
  57. 2 7
      kernel/src/process/syscall/sys_getpid.rs
  58. 6 1
      kernel/src/process/syscall/sys_getppid.rs
  59. 1 1
      kernel/src/process/syscall/sys_getrlimit.rs
  60. 12 14
      kernel/src/process/syscall/sys_getsid.rs
  61. 1 1
      kernel/src/process/syscall/sys_gettid.rs
  62. 4 4
      kernel/src/process/syscall/sys_prlimit64.rs
  63. 0 3
      kernel/src/process/syscall/sys_set_tid_address.rs
  64. 7 2
      kernel/src/process/syscall/sys_setfsgid.rs
  65. 5 1
      kernel/src/process/syscall/sys_setfsuid.rs
  66. 10 6
      kernel/src/process/syscall/sys_setgid.rs
  67. 72 22
      kernel/src/process/syscall/sys_setpgid.rs
  68. 8 2
      kernel/src/process/syscall/sys_setresgid.rs
  69. 7 2
      kernel/src/process/syscall/sys_setresuid.rs
  70. 5 6
      kernel/src/process/syscall/sys_setsid.rs
  71. 9 4
      kernel/src/process/syscall/sys_setuid.rs
  72. 4 4
      kernel/src/process/timer.rs
  73. 18 17
      kernel/src/syscall/mod.rs
  74. 1 1
      kernel/src/tracepoint/basic_macro.rs
  75. 2 2
      kernel/src/virt/vm/kvm_host/vcpu.rs

+ 2 - 2
kernel/src/arch/loongarch64/process/kthread.rs

@@ -5,7 +5,7 @@ use alloc::sync::Arc;
 use crate::process::{
     fork::CloneFlags,
     kthread::{KernelThreadCreateInfo, KernelThreadMechanism},
-    Pid,
+    RawPid,
 };
 
 impl KernelThreadMechanism {
@@ -17,7 +17,7 @@ impl KernelThreadMechanism {
     pub fn __inner_create(
         info: &Arc<KernelThreadCreateInfo>,
         clone_flags: CloneFlags,
-    ) -> Result<Pid, SystemError> {
+    ) -> Result<RawPid, SystemError> {
         // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
         let create_info: *const KernelThreadCreateInfo =
             KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());

+ 2 - 4
kernel/src/arch/riscv64/process/kthread.rs

@@ -1,11 +1,9 @@
-use core::arch::asm;
-
 use crate::{
     arch::{asm::csr::CSR_SSTATUS, interrupt::TrapFrame},
     process::{
         fork::CloneFlags,
         kthread::{kernel_thread_bootstrap_stage2, KernelThreadCreateInfo, KernelThreadMechanism},
-        Pid, ProcessManager,
+        ProcessManager, RawPid,
     },
 };
 use alloc::sync::Arc;
@@ -24,7 +22,7 @@ impl KernelThreadMechanism {
     pub fn __inner_create(
         info: &Arc<KernelThreadCreateInfo>,
         clone_flags: CloneFlags,
-    ) -> Result<Pid, SystemError> {
+    ) -> Result<RawPid, SystemError> {
         // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
         let create_info: *const KernelThreadCreateInfo =
             KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());

+ 34 - 13
kernel/src/arch/x86_64/ipc/signal.rs

@@ -20,7 +20,7 @@ use crate::{
     exception::InterruptArch,
     ipc::{
         signal::{restore_saved_sigmask, set_current_blocked},
-        signal_types::{SaHandlerType, SigInfo, Sigaction, SigactionType, SignalArch},
+        signal_types::{SaHandlerType, SigInfo, Sigaction, SigactionType, SignalArch, SignalFlags},
     },
     mm::MemoryManagementArch,
     process::ProcessManager,
@@ -208,7 +208,10 @@ unsafe fn do_signal(frame: &mut TrapFrame, got_signal: &mut bool) {
         return;
     }
 
-    let sig_guard = sig_guard.unwrap();
+    let sig_guard: crate::libs::spinlock::SpinLockGuard<
+        '_,
+        crate::ipc::signal_types::SignalStruct,
+    > = sig_guard.unwrap();
     let mut siginfo_mut_guard = siginfo_mut.unwrap();
     loop {
         (sig_number, info) = siginfo_mut_guard.dequeue_signal(&sig_block, &pcb);
@@ -222,7 +225,7 @@ unsafe fn do_signal(frame: &mut TrapFrame, got_signal: &mut bool) {
         match sa.action() {
             SigactionType::SaHandler(action_type) => match action_type {
                 SaHandlerType::Error => {
-                    error!("Trying to handle a Sigerror on Process:{:?}", pcb.pid());
+                    error!("Trying to handle a Sigerror on Process:{:?}", pcb.raw_pid());
                     return;
                 }
                 SaHandlerType::Default => {
@@ -236,6 +239,22 @@ unsafe fn do_signal(frame: &mut TrapFrame, got_signal: &mut bool) {
             SigactionType::SaSigaction(_) => todo!(),
         }
 
+        /*
+         * Global init gets no signals it doesn't want.
+         * Container-init gets no signals it doesn't want from same
+         * container.
+         *
+         * Note that if global/container-init sees a sig_kernel_only()
+         * signal here, the signal must have been generated internally
+         * or must have come from an ancestor namespace. In either
+         * case, the signal cannot be dropped.
+         */
+        // todo: https://code.dragonos.org.cn/xref/linux-6.6.21/include/linux/signal.h?fi=sig_kernel_only#444
+        if siginfo_mut_guard.flags().contains(SignalFlags::UNKILLABLE) && !sig_number.kernel_only()
+        {
+            continue;
+        }
+
         if sigaction.is_some() {
             break;
         }
@@ -264,7 +283,7 @@ unsafe fn do_signal(frame: &mut TrapFrame, got_signal: &mut bool) {
         error!(
             "Error occurred when handling signal: {}, pid={:?}, errcode={:?}",
             sig_number as i32,
-            ProcessManager::current_pcb().pid(),
+            ProcessManager::current_pcb().raw_pid(),
             res.as_ref().unwrap_err()
         );
     }
@@ -326,7 +345,7 @@ impl SignalArch for X86_64SignalArch {
         if UserBufferWriter::new(frame, size_of::<SigFrame>(), true).is_err() {
             error!("rsp doesn't from user level");
             let _r = crate::ipc::kill::kill_process(
-                ProcessManager::current_pcb().pid(),
+                ProcessManager::current_pcb().raw_pid(),
                 Signal::SIGSEGV,
             )
             .map_err(|e| e.to_posix_errno());
@@ -338,7 +357,7 @@ impl SignalArch for X86_64SignalArch {
         if !unsafe { &mut (*frame).context }.restore_sigcontext(trap_frame) {
             error!("unable to restore sigcontext");
             let _r = crate::ipc::kill::kill_process(
-                ProcessManager::current_pcb().pid(),
+                ProcessManager::current_pcb().raw_pid(),
                 Signal::SIGSEGV,
             )
             .map_err(|e| e.to_posix_errno());
@@ -430,11 +449,11 @@ fn setup_frame(
                     } else {
                         error!(
                             "pid-{:?} forgot to set SA_FLAG_RESTORER for signal {:?}",
-                            ProcessManager::current_pcb().pid(),
+                            ProcessManager::current_pcb().raw_pid(),
                             sig as i32
                         );
                         let r = crate::ipc::kill::kill_process(
-                            ProcessManager::current_pcb().pid(),
+                            ProcessManager::current_pcb().raw_pid(),
                             Signal::SIGSEGV,
                         );
                         if r.is_err() {
@@ -445,7 +464,7 @@ fn setup_frame(
                     if sigaction.restorer().is_none() {
                         error!(
                             "restorer in process:{:?} is not defined",
-                            ProcessManager::current_pcb().pid()
+                            ProcessManager::current_pcb().raw_pid()
                         );
                         return Err(SystemError::EINVAL);
                     }
@@ -473,8 +492,10 @@ fn setup_frame(
     if r.is_err() {
         // 如果地址区域位于内核空间,则直接报错
         // todo: 生成一个sigsegv
-        let r =
-            crate::ipc::kill::kill_process(ProcessManager::current_pcb().pid(), Signal::SIGSEGV);
+        let r = crate::ipc::kill::kill_process(
+            ProcessManager::current_pcb().raw_pid(),
+            Signal::SIGSEGV,
+        );
         if r.is_err() {
             error!("In setup frame: generate SIGSEGV signal failed");
         }
@@ -486,7 +507,7 @@ fn setup_frame(
     info.copy_siginfo_to_user(unsafe { &mut ((*frame).info) as *mut SigInfo })
         .map_err(|e| -> SystemError {
             let r = crate::ipc::kill::kill_process(
-                ProcessManager::current_pcb().pid(),
+                ProcessManager::current_pcb().raw_pid(),
                 Signal::SIGSEGV,
             );
             if r.is_err() {
@@ -503,7 +524,7 @@ fn setup_frame(
             .setup_sigcontext(oldset, trap_frame)
             .map_err(|e: SystemError| -> SystemError {
                 let r = crate::ipc::kill::kill_process(
-                    ProcessManager::current_pcb().pid(),
+                    ProcessManager::current_pcb().raw_pid(),
                     Signal::SIGSEGV,
                 );
                 if r.is_err() {

+ 1 - 1
kernel/src/arch/x86_64/mm/fault.rs

@@ -179,7 +179,7 @@ impl X86_64MMArch {
             address.data(),
             regs.rip,
             error_code,
-            pcb.pid().data()
+            pcb.raw_pid().data()
         );
         //TODO https://code.dragonos.org.cn/xref/linux-6.6.21/arch/x86/mm/fault.c#do_kern_addr_fault
     }

+ 3 - 3
kernel/src/arch/x86_64/process/kthread.rs

@@ -9,7 +9,7 @@ use crate::{
     process::{
         fork::CloneFlags,
         kthread::{kernel_thread_bootstrap_stage2, KernelThreadCreateInfo, KernelThreadMechanism},
-        Pid, ProcessManager,
+        ProcessManager, RawPid,
     },
 };
 
@@ -22,7 +22,7 @@ impl KernelThreadMechanism {
     pub fn __inner_create(
         info: &Arc<KernelThreadCreateInfo>,
         clone_flags: CloneFlags,
-    ) -> Result<Pid, SystemError> {
+    ) -> Result<RawPid, SystemError> {
         // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
         let create_info: *const KernelThreadCreateInfo =
             KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());
@@ -44,7 +44,7 @@ impl KernelThreadMechanism {
             unsafe { KernelThreadCreateInfo::parse_unsafe_arc_ptr(create_info) };
         })?;
 
-        ProcessManager::find(pid)
+        ProcessManager::find_task_by_vpid(pid)
             .unwrap()
             .set_name(info.name().clone());
 

+ 2 - 2
kernel/src/arch/x86_64/process/syscall.rs

@@ -92,13 +92,13 @@ impl Syscall {
             ARCH_SET_FS => {
                 arch_info.fsbase = arg2;
                 // 如果是当前进程则直接写入寄存器
-                if pcb.pid() == ProcessManager::current_pcb().pid() {
+                if pcb.raw_pid() == ProcessManager::current_pcb().raw_pid() {
                     unsafe { arch_info.restore_fsbase() }
                 }
             }
             ARCH_SET_GS => {
                 arch_info.gsbase = arg2;
-                if pcb.pid() == ProcessManager::current_pcb().pid() {
+                if pcb.raw_pid() == ProcessManager::current_pcb().raw_pid() {
                     unsafe { arch_info.restore_gsbase() }
                 }
             }

+ 2 - 2
kernel/src/arch/x86_64/syscall/mod.rs

@@ -52,7 +52,7 @@ macro_rules! syscall_return {
         $regs.rax = ret as u64;
 
         if $show {
-            let pid = ProcessManager::current_pcb().pid();
+            let pid = ProcessManager::current_pcb().raw_pid();
             debug!("syscall return:pid={:?},ret= {:?}\n", pid, ret as isize);
         }
 
@@ -88,7 +88,7 @@ pub extern "sysv64" fn syscall_handler(frame: &mut TrapFrame) {
         frame.r9 as usize,
     ];
     mfence();
-    let pid = ProcessManager::current_pcb().pid();
+    let pid = ProcessManager::current_pcb().raw_pid();
     let show = false;
     // let show = if syscall_num != SYS_SCHED && pid.data() >= 9{
     //     true

+ 2 - 2
kernel/src/debug/klog/mm.rs

@@ -4,7 +4,7 @@ use core::sync::atomic::{compiler_fence, Ordering};
 
 use klog_types::{AllocatorLog, AllocatorLogType, LogSource, MMLogChannel};
 
-use crate::{arch::CurrentTimeArch, libs::spinlock::SpinLock, process::Pid, time::TimeArch};
+use crate::{arch::CurrentTimeArch, libs::spinlock::SpinLock, process::RawPid, time::TimeArch};
 
 /// 全局的内存分配器日志通道
 ///
@@ -50,7 +50,7 @@ impl MMDebugLogManager {
     /// - `source`:日志来源
     /// - `pid`:日志来源的pid
     #[allow(dead_code)]
-    pub fn log(log_type: AllocatorLogType, source: LogSource, pid: Option<Pid>) {
+    pub fn log(log_type: AllocatorLogType, source: LogSource, pid: Option<RawPid>) {
         let id = __MM_DEBUG_LOG_IDA.lock_irqsave().alloc().unwrap();
         let log = AllocatorLog::new(
             id as u64,

+ 7 - 2
kernel/src/debug/panic/mod.rs

@@ -16,6 +16,8 @@ cfg_if! {
     }
 }
 
+const MAX_PANIC_COUNT: u8 = 2;
+
 #[derive(Debug)]
 struct PanicGuard;
 
@@ -39,7 +41,10 @@ impl Drop for PanicGuard {
 #[no_mangle]
 pub fn panic(info: &PanicInfo) -> ! {
     PANIC_COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
-    error!("Kernel Panic Occurred.");
+    error!(
+        "Kernel Panic Occurred. raw_pid: {}",
+        process::ProcessManager::current_pid().data()
+    );
 
     match info.location() {
         Some(loc) => {
@@ -55,7 +60,7 @@ pub fn panic(info: &PanicInfo) -> ! {
         }
     }
     println!("Message:\n\t{}", info.message());
-    if PANIC_COUNTER.load(core::sync::atomic::Ordering::Relaxed) > 8 {
+    if PANIC_COUNTER.load(core::sync::atomic::Ordering::Relaxed) > MAX_PANIC_COUNT {
         println!(
             "Panic Counter: {}, too many panics, halt.",
             PANIC_COUNTER.load(core::sync::atomic::Ordering::Relaxed)

+ 10 - 1
kernel/src/driver/base/device/device_number.rs

@@ -1,4 +1,7 @@
-use core::hash::{Hash, Hasher};
+use core::{
+    fmt::Display,
+    hash::{Hash, Hasher},
+};
 
 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
 pub struct Major(u32);
@@ -96,3 +99,9 @@ impl From<u32> for DeviceNumber {
         Self { data: x }
     }
 }
+
+impl Display for DeviceNumber {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        write!(f, "{}:{}", self.major().data(), self.minor())
+    }
+}

+ 45 - 11
kernel/src/driver/tty/tty_core.rs

@@ -19,13 +19,14 @@ use crate::{
         wait_queue::EventWaitQueue,
     },
     mm::VirtAddr,
-    process::{process_group::Pgid, session::Sid, ProcessControlBlock},
+    process::{pid::Pid, ProcessControlBlock},
     syscall::user_access::{UserBufferReader, UserBufferWriter},
 };
 
 use super::{
     termios::{ControlMode, PosixTermios, Termios, TtySetTermiosOpt, WindowSize},
     tty_driver::{TtyCorePrivateField, TtyDriver, TtyDriverSubType, TtyDriverType, TtyOperation},
+    tty_job_control::TtyJobCtrlManager,
     tty_ldisc::{
         ntty::{NTtyData, NTtyLinediscipline},
         TtyLineDiscipline,
@@ -69,7 +70,7 @@ impl TtyCore {
             port: RwLock::new(None),
             index,
             vc_index: AtomicUsize::new(usize::MAX),
-            ctrl: SpinLock::new(TtyContorlInfo::default()),
+            ctrl: SpinLock::new(TtyControlInfo::default()),
             closing: AtomicBool::new(false),
             flow: SpinLock::new(TtyFlowState::default()),
             link: RwLock::default(),
@@ -279,21 +280,35 @@ impl TtyCore {
 }
 
 #[derive(Debug, Default)]
-pub struct TtyContorlInfo {
+pub struct TtyControlInfo {
     /// 当前会话的SId
-    pub session: Option<Sid>,
+    pub session: Option<Arc<Pid>>,
     /// 前台进程组id
-    pub pgid: Option<Pgid>,
+    pub pgid: Option<Arc<Pid>>,
 
     /// packet模式下使用,目前未用到
     pub pktstatus: TtyPacketStatus,
     pub packet: bool,
 }
 
-impl TtyContorlInfo {
+impl TtyControlInfo {
     pub fn set_info_by_pcb(&mut self, pcb: Arc<ProcessControlBlock>) {
-        self.session = Some(pcb.sid());
-        self.pgid = Some(pcb.pgid());
+        self.session = pcb.task_session();
+        self.pgid = pcb.task_pgrp();
+    }
+
+    /// 清除当前session的pid
+    ///
+    /// 如果当前session已经死了,则将其清除。
+    pub fn clear_dead_session(&mut self) {
+        if self.session.is_none() {
+            return;
+        }
+
+        let clean = self.session.as_ref().map(|s| s.dead()).unwrap();
+        if clean {
+            self.session = None;
+        }
     }
 }
 
@@ -324,7 +339,7 @@ pub struct TtyCoreData {
     /// 端口
     port: RwLock<Option<Arc<dyn TtyPort>>>,
     /// 前台进程
-    ctrl: SpinLock<TtyContorlInfo>,
+    ctrl: SpinLock<TtyControlInfo>,
     /// 是否正在关闭
     closing: AtomicBool,
     /// 流控状态
@@ -400,12 +415,23 @@ impl TtyCoreData {
         self.count.load(Ordering::SeqCst)
     }
 
+    pub fn count_valid(&self) -> bool {
+        let cnt = self.count();
+        // 暂时认为有效的count范围是(0, usize::MAX / 2)
+        cnt > 0 && cnt < usize::MAX / 2
+    }
+
     #[inline]
     pub fn add_count(&self) {
         self.count
             .fetch_add(1, core::sync::atomic::Ordering::SeqCst);
     }
 
+    pub fn dec_count(&self) -> usize {
+        self.count
+            .fetch_sub(1, core::sync::atomic::Ordering::SeqCst)
+    }
+
     #[inline]
     pub fn read_wq(&self) -> &EventWaitQueue {
         &self.read_wq
@@ -417,7 +443,7 @@ impl TtyCoreData {
     }
 
     #[inline]
-    pub fn contorl_info_irqsave(&self) -> SpinLockGuard<TtyContorlInfo> {
+    pub fn contorl_info_irqsave(&self) -> SpinLockGuard<TtyControlInfo> {
         self.ctrl.lock_irqsave()
     }
 
@@ -624,7 +650,15 @@ impl TtyOperation for TtyCore {
     }
 
     fn close(&self, tty: Arc<TtyCore>) -> Result<(), SystemError> {
-        self.core().tty_driver.driver_funcs().close(tty)
+        let r = self.core().tty_driver.driver_funcs().close(tty.clone());
+        self.core().dec_count();
+        // let cnt = self.core().count();
+        // log::debug!("TtyCore close: count: {cnt}, tty: {:?}", tty.core().name());
+        if !self.core().count_valid() {
+            // 如果计数为0或者无效,表示tty已经关闭
+            TtyJobCtrlManager::remove_session_tty(&tty);
+        }
+        r
     }
 
     fn resize(&self, tty: Arc<TtyCore>, winsize: WindowSize) -> Result<(), SystemError> {

+ 60 - 11
kernel/src/driver/tty/tty_device.rs

@@ -143,6 +143,29 @@ impl TtyDevice {
         };
         Ok(tty)
     }
+
+    /// tty_open_current_tty - get locked tty of current task
+    #[inline(never)]
+    fn open_current_tty(
+        &self,
+        dev_num: DeviceNumber,
+        data: &mut FilePrivateData,
+    ) -> Option<Arc<TtyCore>> {
+        // log::debug!("open_current_tty: dev_num: {}", dev_num);
+        if dev_num != DeviceNumber::new(Major::TTYAUX_MAJOR, 0) {
+            // log::debug!("Not a tty device, dev_num: {}", dev_num);
+            return None;
+        }
+
+        let current_tty = TtyJobCtrlManager::get_current_tty()?;
+
+        if let FilePrivateData::Tty(tty_priv) = data {
+            tty_priv.mode.insert(FileMode::O_NONBLOCK);
+        }
+
+        current_tty.reopen().ok()?;
+        Some(current_tty)
+    }
 }
 
 impl Debug for TtyDevice {
@@ -187,18 +210,24 @@ impl IndexNode for TtyDevice {
         mut data: SpinLockGuard<FilePrivateData>,
         mode: &crate::filesystem::vfs::file::FileMode,
     ) -> Result<(), SystemError> {
-        if let FilePrivateData::Tty(_) = &*data {
-            return Ok(());
-        }
         if self.tty_type == TtyType::Pty(PtyType::Ptm) {
             return ptmx_open(data, mode);
         }
         let dev_num = self.metadata()?.raw_dev;
+        // log::debug!(
+        //     "TtyDevice::open: dev_num: {}, current pid: {}",
+        //     dev_num,
+        //     ProcessManager::current_pid()
+        // );
+        let mut tty = self.open_current_tty(dev_num, &mut data);
+        if tty.is_none() {
+            let (index, driver) =
+                TtyDriverManager::lookup_tty_driver(dev_num).ok_or(SystemError::ENODEV)?;
+
+            tty = Some(driver.open_tty(Some(index))?);
+        }
 
-        let (index, driver) =
-            TtyDriverManager::lookup_tty_driver(dev_num).ok_or(SystemError::ENODEV)?;
-
-        let tty = driver.open_tty(Some(index))?;
+        let tty = tty.unwrap();
 
         // 设置privdata
         *data = FilePrivateData::Tty(TtyFilePrivateData {
@@ -206,6 +235,8 @@ impl IndexNode for TtyDevice {
             mode: *mode,
         });
 
+        tty.core().contorl_info_irqsave().clear_dead_session();
+
         let ret = tty.open(tty.core());
         if let Err(err) = ret {
             if err == SystemError::ENOSYS {
@@ -223,7 +254,12 @@ impl IndexNode for TtyDevice {
         {
             let pcb = ProcessManager::current_pcb();
             let pcb_tty = pcb.sig_info_irqsave().tty();
-            if pcb_tty.is_none() && tty.core().contorl_info_irqsave().session.is_none() {
+
+            let cond1 = pcb_tty.is_none();
+            let _cond2 = tty.core().contorl_info_irqsave().session.is_none();
+
+            // 注意!!这里为了debug,临时把cond2的判断去掉了,其实要cond1 && cond2才对
+            if cond1 {
                 TtyJobCtrlManager::proc_set_tty(tty);
             }
         }
@@ -353,6 +389,7 @@ impl IndexNode for TtyDevice {
         } else {
             return Err(SystemError::EIO);
         };
+
         drop(data);
         tty.close(tty.clone())
     }
@@ -610,6 +647,14 @@ impl TtyFilePrivateData {
 #[unified_init(INITCALL_DEVICE)]
 #[inline(never)]
 pub fn tty_init() -> Result<(), SystemError> {
+    let tty_device = TtyDevice::new(
+        "tty".to_string(),
+        IdTable::new(
+            String::from("tty"),
+            Some(DeviceNumber::new(Major::TTYAUX_MAJOR, 0)),
+        ),
+        TtyType::Tty,
+    );
     let console = TtyDevice::new(
         "console".to_string(),
         IdTable::new(
@@ -619,9 +664,13 @@ pub fn tty_init() -> Result<(), SystemError> {
         TtyType::Tty,
     );
 
-    // 将设备注册到devfs,TODO:这里console设备应该与tty在一个设备group里面
-    device_register(console.clone())?;
-    devfs_register(&console.name.clone(), console)?;
+    let devs = [tty_device, console];
+
+    for dev in devs {
+        // 将设备注册到devfs,TODO:这里console设备应该与tty在一个设备group里面
+        device_register(dev.clone())?;
+        devfs_register(&dev.name.clone(), dev)?;
+    }
 
     serial_init()?;
 

+ 6 - 0
kernel/src/driver/tty/tty_driver.rs

@@ -55,6 +55,12 @@ impl TtyDriverManager {
     pub fn lookup_tty_driver(dev_num: DeviceNumber) -> Option<(usize, Arc<TtyDriver>)> {
         let drivers_guard = TTY_DRIVERS.lock();
         for driver in drivers_guard.iter() {
+            // log::debug!(
+            //     "driver.major: {}, minor_start: {}, device_count: {}",
+            //     driver.major.data(),
+            //     driver.minor_start,
+            //     driver.device_count
+            // );
             let base = DeviceNumber::new(driver.major, driver.minor_start);
             if dev_num < base || dev_num.data() > base.data() + driver.device_count {
                 continue;

+ 161 - 54
kernel/src/driver/tty/tty_job_control.rs

@@ -4,7 +4,10 @@ use system_error::SystemError;
 use crate::{
     arch::ipc::signal::{SigSet, Signal},
     mm::VirtAddr,
-    process::{process_group::Pgid, Pid, ProcessFlags, ProcessManager},
+    process::{
+        pid::{Pid, PidType},
+        ProcessControlBlock, ProcessFlags, ProcessManager, ProcessSignalInfo, RawPid,
+    },
     syscall::user_access::{UserBufferReader, UserBufferWriter},
 };
 
@@ -20,47 +23,75 @@ impl TtyJobCtrlManager {
 
         let mut ctrl = core.contorl_info_irqsave();
         ctrl.set_info_by_pcb(pcb.clone());
+
         drop(ctrl);
 
         let mut singal = pcb.sig_info_mut();
         singal.set_tty(Some(tty.clone()));
     }
 
+    /// ### 清除进程的tty
+    ///
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/tty/tty_jobctrl.c#75
+    pub fn proc_clear_tty(pcb: &Arc<ProcessControlBlock>) {
+        let mut singal = pcb.sig_info_mut();
+        Self::__proc_clear_tty(&mut singal);
+    }
+
+    pub fn __proc_clear_tty(wguard: &mut ProcessSignalInfo) {
+        // log::debug!("Clearing tty");
+        wguard.set_tty(None);
+    }
+
+    pub fn remove_session_tty(tty: &Arc<TtyCore>) {
+        let mut ctrl = tty.core().contorl_info_irqsave();
+        ctrl.session = None;
+    }
+
     /// ### 检查tty
+    ///
+    /// check for POSIX terminal changes
+    ///
+    /// Reference: https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/tty/tty_jobctrl.c#33
     pub fn tty_check_change(tty: Arc<TtyCore>, sig: Signal) -> Result<(), SystemError> {
         let pcb = ProcessManager::current_pcb();
-
-        if pcb.sig_info_irqsave().tty().is_none()
-            || !Arc::ptr_eq(&pcb.sig_info_irqsave().tty().unwrap(), &tty)
-        {
+        let current_tty = pcb.sig_info_irqsave().tty();
+        if current_tty.is_none() || !Arc::ptr_eq(&current_tty.unwrap(), &tty) {
             return Ok(());
         }
 
-        let pgid = pcb.pgid();
+        let pgid = pcb.task_pgrp();
 
         let ctrl = tty.core().contorl_info_irqsave();
-        let tty_pgid = ctrl.pgid;
+        let tty_pgid = ctrl.pgid.clone();
         drop(ctrl);
 
-        if tty_pgid.is_some() && tty_pgid.unwrap() != pgid {
-            if pcb
-                .sig_info_irqsave()
-                .sig_blocked()
-                .contains(SigSet::from(sig))
-                || pcb.sig_struct_irqsave().handlers[sig as usize - 1].is_ignore()
-            {
+        // log::debug!(
+        //     "tty_check_change: pid: {},pgid: {:?}, tty_pgid: {:?}",
+        //     pcb.raw_pid().data(),
+        //     pgid.as_ref().map(|p| p.pid_vnr().data()),
+        //     tty_pgid.as_ref().map(|p| p.pid_vnr().data())
+        // );
+        if tty_pgid.is_some() && tty_pgid != pgid {
+            let pgid = pgid.unwrap();
+            if Self::sig_is_ignored(sig) {
                 // 忽略该信号
                 if sig == Signal::SIGTTIN {
                     return Err(SystemError::EIO);
                 }
             } else if ProcessManager::is_current_pgrp_orphaned() {
+                log::debug!("tty_check_change: orphaned pgrp");
                 return Err(SystemError::EIO);
             } else {
-                crate::ipc::kill::kill_process_group(pgid, sig)?;
+                crate::ipc::kill::kill_process_group(&pgid, sig)?;
                 ProcessManager::current_pcb()
                     .flags()
                     .insert(ProcessFlags::HAS_PENDING_SIGNAL);
-                log::debug!("job_ctrl_ioctl: kill. pgid: {pgid}, tty_pgid: {tty_pgid:?}");
+                log::debug!(
+                    "job_ctrl_ioctl: kill. pgid: {}, tty_pgid: {:?}",
+                    pgid.pid_vnr().data(),
+                    tty_pgid.map(|p| p.pid_vnr().data())
+                );
                 return Err(SystemError::ERESTARTSYS);
             }
         }
@@ -68,6 +99,13 @@ impl TtyJobCtrlManager {
         Ok(())
     }
 
+    fn sig_is_ignored(sig: Signal) -> bool {
+        let pcb = ProcessManager::current_pcb();
+        let siginfo_guard = pcb.sig_info_irqsave();
+        siginfo_guard.sig_blocked().contains(SigSet::from(sig))
+            || pcb.sig_struct_irqsave().handler(sig).unwrap().is_ignore()
+    }
+
     pub fn job_ctrl_ioctl(
         real_tty: Arc<TtyCore>,
         cmd: u32,
@@ -77,35 +115,57 @@ impl TtyJobCtrlManager {
             TtyIoctlCmd::TIOCSPGRP => Self::tiocspgrp(real_tty, arg),
             TtyIoctlCmd::TIOCGPGRP => Self::tiocgpgrp(real_tty, arg),
             TtyIoctlCmd::TIOCGSID => Self::tiocgsid(real_tty, arg),
-            TtyIoctlCmd::TIOCSCTTY => Self::tiocsctty(real_tty),
+            TtyIoctlCmd::TIOCSCTTY => Self::tiocsctty(real_tty, arg),
             _ => {
                 return Err(SystemError::ENOIOCTLCMD);
             }
         }
     }
 
-    fn tiocsctty(real_tty: Arc<TtyCore>) -> Result<usize, SystemError> {
+    // https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/tty/tty_jobctrl.c#tiocsctty
+    fn tiocsctty(real_tty: Arc<TtyCore>, arg: usize) -> Result<usize, SystemError> {
         let current = ProcessManager::current_pcb();
-        // log::debug!("job_ctrl_ioctl: TIOCSCTTY,current: {:?}", current.pid());
-        if current.is_session_leader()
-            && real_tty.core().contorl_info_irqsave().session.unwrap() == current.sid()
-        {
-            return Ok(0);
-        }
+        let siginfo_guard = current.sig_info_irqsave();
 
-        if !current.is_session_leader() || current.sig_info_irqsave().tty().is_some() {
+        // 只有会话首进程才能设置控制终端
+        if !siginfo_guard.is_session_leader {
             return Err(SystemError::EPERM);
         }
 
-        //todo 权限检查?
-        // https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/tty/tty_jobctrl.c#tiocsctty
-        if let Some(sid) = real_tty.core().contorl_info_irqsave().session {
-            //todo 目前只有一个tty设备,所以选择复用1号进程的tty,因此修改1号进程的tty暂时被允许
-            if sid != Pid::new(1) {
+        // 如果当前进程已经有控制终端,则返回错误(除非是同一个tty且会话相同)
+        if let Some(current_tty) = siginfo_guard.tty() {
+            if Arc::ptr_eq(&current_tty, &real_tty)
+                && current.task_session() == real_tty.core().contorl_info_irqsave().session
+            {
+                // 如果已经是当前tty且会话相同,直接返回成功
+                return Ok(0);
+            } else {
+                // 已经有其他控制终端,返回错误
                 return Err(SystemError::EPERM);
             }
         }
 
+        drop(siginfo_guard);
+
+        let tty_ctrl_guard = real_tty.core().contorl_info_irqsave();
+
+        if let Some(ref sid) = tty_ctrl_guard.session {
+            // 如果当前进程是会话首进程,且tty的会话是当前进程的会话,则允许设置
+            if current.task_session() == Some(sid.clone()) {
+                // 这是正常情况:会话首进程要设置自己会话的tty为控制终端
+            } else {
+                // tty被其他会话占用,需要强制标志或CAP_SYS_ADMIN权限
+                // todo: 这里要补充对CAP_SYS_ADMIN的检查
+                if arg == 1 {
+                    Self::session_clear_tty(sid.clone());
+                } else {
+                    return Err(SystemError::EPERM);
+                }
+            }
+        }
+
+        drop(tty_ctrl_guard);
+
         Self::proc_set_tty(real_tty);
         Ok(0)
     }
@@ -119,25 +179,21 @@ impl TtyJobCtrlManager {
             return Err(SystemError::ENOTTY);
         }
 
+        let pgid = real_tty.core().contorl_info_irqsave().pgid.clone();
+        let pgrp = pgid.map(|p| p.pid_vnr()).unwrap_or(RawPid::new(0)).data();
+        // log::debug!("pid: {},tiocgpgrp: {}", current.raw_pid().data(), pgrp);
         let mut user_writer = UserBufferWriter::new(
             VirtAddr::new(arg).as_ptr::<i32>(),
             core::mem::size_of::<i32>(),
             true,
         )?;
 
-        user_writer.copy_one_to_user(
-            &(real_tty
-                .core()
-                .contorl_info_irqsave()
-                .pgid
-                .unwrap_or(Pid::new(1))
-                .data() as i32),
-            0,
-        )?;
+        user_writer.copy_one_to_user(&(pgrp as i32), 0)?;
 
         return Ok(0);
     }
 
+    /// Get session ID
     fn tiocgsid(real_tty: Arc<TtyCore>, arg: usize) -> Result<usize, SystemError> {
         // log::debug!("job_ctrl_ioctl: TIOCGSID");
         let current = ProcessManager::current_pcb();
@@ -151,8 +207,10 @@ impl TtyJobCtrlManager {
         if guard.session.is_none() {
             return Err(SystemError::ENOTTY);
         }
-        let sid = guard.session.unwrap();
+        let session = guard.session.clone();
+        // 先释放guard,免得阻塞了
         drop(guard);
+        let sid = session.unwrap().pid_vnr();
 
         let mut user_writer = UserBufferWriter::new(
             VirtAddr::new(arg).as_ptr::<i32>(),
@@ -170,6 +228,7 @@ impl TtyJobCtrlManager {
             Ok(_) => {}
             Err(e) => {
                 if e == SystemError::EIO {
+                    log::debug!("sss-1");
                     return Err(SystemError::ENOTTY);
                 }
                 return Err(e);
@@ -182,32 +241,80 @@ impl TtyJobCtrlManager {
             true,
         )?;
 
-        let pgrp = user_reader.read_one_from_user::<i32>(0)?;
+        let pgrp_nr = *user_reader.read_one_from_user::<i32>(0)?;
 
         let current = ProcessManager::current_pcb();
 
         let mut ctrl = real_tty.core().contorl_info_irqsave();
 
-        if current.sig_info_irqsave().tty().is_none()
-            || !Arc::ptr_eq(
-                &current.sig_info_irqsave().tty().clone().unwrap(),
-                &real_tty,
-            )
-            || ctrl.session.is_none()
-            || ctrl.session.unwrap() != current.sid()
         {
-            return Err(SystemError::ENOTTY);
+            // if current.sig_info_irqsave().tty().is_none()
+            //     || !Arc::ptr_eq(
+            //         &current.sig_info_irqsave().tty().clone().unwrap(),
+            //         &real_tty,
+            //     )
+            //     || ctrl.session != current.task_session()
+            // {
+            //     log::debug!("sss-2");
+            //     return Err(SystemError::ENOTTY);
+            // }
+
+            // 拆分判断条件以便调试
+            let current_tty = current.sig_info_irqsave().tty();
+            let condition1 = current_tty.is_none();
+
+            let condition2 = if let Some(ref tty) = current_tty {
+                !Arc::ptr_eq(tty, &real_tty)
+            } else {
+                false // 如果 tty 为 None,这个条件就不用检查了
+            };
+
+            let condition3 = ctrl.session != current.task_session();
+
+            if condition1 || condition2 || condition3 {
+                if condition1 {
+                    log::debug!("sss-2: 失败原因 - 当前进程没有关联的 tty");
+                } else if condition2 {
+                    log::debug!("sss-2: 失败原因 - 当前进程的 tty 与目标 tty 不匹配");
+                } else if condition3 {
+                    log::debug!("sss-2: 失败原因 - 会话不匹配");
+                }
+                return Err(SystemError::ENOTTY);
+            }
         }
+        let pgrp =
+            ProcessManager::find_vpid(RawPid::new(pgrp_nr as usize)).ok_or(SystemError::ESRCH)?;
 
-        let pg = ProcessManager::find_process_group(Pgid::from(*pgrp as usize));
-        if pg.is_none() {
-            return Err(SystemError::ESRCH);
-        } else if !Arc::ptr_eq(&pg.unwrap().session().unwrap(), &current.session().unwrap()) {
+        if Self::session_of_pgrp(&pgrp) != current.task_session() {
             return Err(SystemError::EPERM);
         }
 
-        ctrl.pgid = Some(Pid::from(*pgrp as usize));
+        ctrl.pgid = Some(pgrp);
 
         return Ok(0);
     }
+
+    fn session_of_pgrp(pgrp: &Arc<Pid>) -> Option<Arc<Pid>> {
+        let mut p = pgrp.pid_task(PidType::PGID);
+        if p.is_none() {
+            // this should not be None
+            p = pgrp.pid_task(PidType::PID);
+        }
+        let p = p.unwrap();
+
+        let sid = p.task_session();
+
+        return sid;
+    }
+
+    pub(super) fn session_clear_tty(sid: Arc<Pid>) {
+        // 清除会话的tty
+        for task in sid.tasks_iter(PidType::SID) {
+            TtyJobCtrlManager::proc_clear_tty(&task);
+        }
+    }
+
+    pub fn get_current_tty() -> Option<Arc<TtyCore>> {
+        ProcessManager::current_pcb().sig_info_irqsave().tty()
+    }
 }

+ 3 - 2
kernel/src/driver/tty/tty_ldisc/ntty.rs

@@ -787,9 +787,10 @@ impl NTtyData {
     ) {
         // 先处理信号
         let ctrl_info = tty.core().contorl_info_irqsave();
-        let pg = ctrl_info.pgid;
+        let pg = ctrl_info.pgid.clone();
+        drop(ctrl_info);
         if let Some(pg) = pg {
-            let _ = crate::ipc::kill::kill_process_group(pg, signal);
+            let _ = crate::ipc::kill::kill_process_group(&pg, signal);
         }
 
         if !termios.local_mode.contains(LocalMode::NOFLSH) {

+ 4 - 0
kernel/src/driver/tty/tty_port.rs

@@ -90,6 +90,10 @@ pub trait TtyPort: Sync + Send + Debug {
         EventPoll::wakeup_epoll(tty.core().eptiems(), pollflag)?;
         ret
     }
+
+    fn internal_tty(&self) -> Option<Arc<TtyCore>> {
+        self.port_data().internal_tty()
+    }
 }
 
 #[derive(Debug)]

+ 2 - 2
kernel/src/driver/tty/virtual_terminal/virtual_console.rs

@@ -13,7 +13,7 @@ use crate::{
         tty::{console::ConsoleSwitch, ConsoleFont, KDMode},
     },
     libs::{font::FontDesc, rwlock::RwLock},
-    process::Pid,
+    process::RawPid,
 };
 
 use super::{
@@ -91,7 +91,7 @@ pub struct VirtualConsoleData {
 
     pub cursor_blink_ms: u16,
 
-    pub pid: Option<Pid>,
+    pub pid: Option<RawPid>,
     pub index: usize,
 
     pub vc_state: VirtualConsoleState,

+ 1 - 1
kernel/src/filesystem/devfs/mod.rs

@@ -143,7 +143,7 @@ impl DevFS {
                     .unwrap();
 
                 // 特殊处理 tty 设备,挂载在 /dev 下
-                if name.starts_with("tty") && name.len() > 3 {
+                if name.starts_with("tty") && name.len() >= 3 {
                     dev_root_inode.add_dev(name, device.clone())?;
                 } else if name.starts_with("hvc") && name.len() > 3 {
                     // 特殊处理 hvc 设备,挂载在 /dev 下

+ 44 - 20
kernel/src/filesystem/procfs/mod.rs

@@ -24,7 +24,7 @@ use crate::{
         spinlock::{SpinLock, SpinLockGuard},
     },
     mm::allocator::page_frame::FrameAllocator,
-    process::{Pid, ProcessManager},
+    process::{ProcessManager, ProcessState, RawPid},
     time::PosixTimeSpec,
 };
 
@@ -73,7 +73,7 @@ impl From<u8> for ProcFileType {
 #[derive(Debug)]
 pub struct InodeInfo {
     ///进程的pid
-    pid: Pid,
+    pid: RawPid,
     ///文件类型
     ftype: ProcFileType,
     //其他需要传入的信息在此定义
@@ -148,7 +148,7 @@ impl ProcFSInode {
     fn open_status(&self, pdata: &mut ProcfsFilePrivateData) -> Result<i64, SystemError> {
         // 获取该pid对应的pcb结构体
         let pid = self.fdata.pid;
-        let pcb = ProcessManager::find(pid);
+        let pcb = ProcessManager::find_task_by_vpid(pid);
         let pcb = if let Some(pcb) = pcb {
             pcb
         } else {
@@ -158,6 +158,12 @@ impl ProcFSInode {
             );
             return Err(SystemError::ESRCH);
         };
+
+        // ::log::debug!(
+        //     "ProcFS: Opening 'status' file for pid {:?} (cnt: {})",
+        //     pcb.raw_pid(),
+        //     Arc::strong_count(&pcb)
+        // );
         // 传入数据
         let pdata: &mut Vec<u8> = &mut pdata.data;
         // name
@@ -181,24 +187,42 @@ impl ProcFSInode {
         pdata.append(&mut format!("\nState:\t{:?}", state).as_bytes().to_owned());
 
         // Tgid
-        pdata.append(&mut format!("\nTgid:\t{}", pcb.tgid().into()).into());
+        pdata.append(
+            &mut format!(
+                "\nTgid:\t{}",
+                pcb.task_tgid_vnr().unwrap_or(RawPid::new(0)).into()
+            )
+            .into(),
+        );
 
         // pid
         pdata.append(
-            &mut format!("\nPid:\t{}", pcb.pid().into())
+            &mut format!("\nPid:\t{}", pcb.task_pid_vnr().data())
                 .as_bytes()
                 .to_owned(),
         );
 
         // ppid
         pdata.append(
-            &mut format!("\nPpid:\t{}", pcb.basic().ppid().into())
-                .as_bytes()
-                .to_owned(),
+            &mut format!(
+                "\nPpid:\t{}",
+                pcb.parent_pcb()
+                    .map(|p| p.task_pid_vnr().data() as isize)
+                    .unwrap_or(-1)
+            )
+            .as_bytes()
+            .to_owned(),
         );
 
         // fdsize
-        pdata.append(&mut format!("\nFDSize:\t{}", pcb.fd_table().read().fd_open_count()).into());
+        if matches!(state, ProcessState::Exited(_)) {
+            // 进程已经退出,fdsize为0
+            pdata.append(&mut format!("\nFDSize:\t{}", 0).into());
+        } else {
+            pdata.append(
+                &mut format!("\nFDSize:\t{}", pcb.fd_table().read().fd_open_count()).into(),
+            );
+        }
 
         // tty
         let name = if let Some(tty) = pcb.sig_info_irqsave().tty() {
@@ -287,10 +311,10 @@ impl ProcFSInode {
     fn read_link(&self, buf: &mut [u8]) -> Result<usize, SystemError> {
         // 判断是否有记录pid信息,有的话就是当前进程的exe文件,没有则是当前进程的exe文件
         let pid = self.fdata.pid;
-        let pcb = if pid == Pid::from(0) {
+        let pcb = if pid == RawPid::from(0) {
             ProcessManager::current_pcb()
         } else {
-            ProcessManager::find(pid).ok_or(SystemError::ESRCH)?
+            ProcessManager::find_task_by_vpid(pid).ok_or(SystemError::ESRCH)?
         };
         let exe = pcb.execute_path();
         let exe_bytes = exe.as_bytes();
@@ -379,7 +403,7 @@ impl ProcFS {
                 },
                 fs: Weak::default(),
                 fdata: InodeInfo {
-                    pid: Pid::new(0),
+                    pid: RawPid::new(0),
                     ftype: ProcFileType::Default,
                 },
                 dname: DName::default(),
@@ -410,7 +434,7 @@ impl ProcFS {
                 .as_any_ref()
                 .downcast_ref::<LockedProcFSInode>()
                 .unwrap();
-            meminfo_file.0.lock().fdata.pid = Pid::new(0);
+            meminfo_file.0.lock().fdata.pid = RawPid::new(0);
             meminfo_file.0.lock().fdata.ftype = ProcFileType::ProcMeminfo;
         } else {
             panic!("create meminfo error");
@@ -423,7 +447,7 @@ impl ProcFS {
                 .as_any_ref()
                 .downcast_ref::<LockedProcFSInode>()
                 .unwrap();
-            kmsg_file.0.lock().fdata.pid = Pid::new(1);
+            kmsg_file.0.lock().fdata.pid = RawPid::new(1);
             kmsg_file.0.lock().fdata.ftype = ProcFileType::ProcKmsg;
         } else {
             panic!("create ksmg error");
@@ -464,7 +488,7 @@ impl ProcFS {
                 .as_any_ref()
                 .downcast_ref::<LockedProcFSInode>()
                 .unwrap();
-            exe_file.0.lock().fdata.pid = Pid::new(0);
+            exe_file.0.lock().fdata.pid = RawPid::new(0);
             exe_file.0.lock().fdata.ftype = ProcFileType::ProcExe;
         } else {
             panic!("create exe error");
@@ -475,7 +499,7 @@ impl ProcFS {
 
     /// @brief 进程注册函数
     /// @usage 在进程中调用并创建进程对应文件
-    pub fn register_pid(&self, pid: Pid) -> Result<(), SystemError> {
+    pub fn register_pid(&self, pid: RawPid) -> Result<(), SystemError> {
         // 获取当前inode
         let inode: Arc<dyn IndexNode> = self.root_inode();
         // 创建对应进程文件夹
@@ -519,7 +543,7 @@ impl ProcFS {
 
     /// @brief 解除进程注册
     ///
-    pub fn unregister_pid(&self, pid: Pid) -> Result<(), SystemError> {
+    pub fn unregister_pid(&self, pid: RawPid) -> Result<(), SystemError> {
         // 获取当前inode
         let proc: Arc<dyn IndexNode> = self.root_inode();
         // 获取进程文件夹
@@ -728,7 +752,7 @@ impl IndexNode for LockedProcFSInode {
                 },
                 fs: inode.fs.clone(),
                 fdata: InodeInfo {
-                    pid: Pid::new(0),
+                    pid: RawPid::new(0),
                     ftype: ProcFileType::Default,
                 },
                 dname: name.clone(),
@@ -898,7 +922,7 @@ impl IndexNode for LockedProcFSInode {
 }
 
 /// @brief 向procfs注册进程
-pub fn procfs_register_pid(pid: Pid) -> Result<(), SystemError> {
+pub fn procfs_register_pid(pid: RawPid) -> Result<(), SystemError> {
     let procfs_inode = ROOT_INODE().find("proc")?;
 
     let procfs_inode = procfs_inode
@@ -914,7 +938,7 @@ pub fn procfs_register_pid(pid: Pid) -> Result<(), SystemError> {
 }
 
 /// @brief 在ProcFS中,解除进程的注册
-pub fn procfs_unregister_pid(pid: Pid) -> Result<(), SystemError> {
+pub fn procfs_unregister_pid(pid: RawPid) -> Result<(), SystemError> {
     // 获取procfs实例
     let procfs_inode: Arc<dyn IndexNode> = ROOT_INODE().find("proc")?;
 

+ 3 - 3
kernel/src/filesystem/vfs/file.rs

@@ -128,7 +128,7 @@ pub struct File {
     readdir_subdirs_name: SpinLock<Vec<String>>,
     pub private_data: SpinLock<FilePrivateData>,
     /// 文件的凭证
-    cred: Cred,
+    cred: Arc<Cred>,
 }
 
 impl File {
@@ -505,7 +505,7 @@ impl Drop for File {
         if r.is_err() {
             error!(
                 "pid: {:?} failed to close file: {:?}, errno={:?}",
-                ProcessManager::current_pcb().pid(),
+                ProcessManager::current_pcb().raw_pid(),
                 self,
                 r.as_ref().unwrap_err()
             );
@@ -642,7 +642,7 @@ impl FileDescriptorVec {
                     if let Err(r) = self.drop_fd(i as i32) {
                         error!(
                             "Failed to close file: pid = {:?}, fd = {}, error = {:?}",
-                            ProcessManager::current_pcb().pid(),
+                            ProcessManager::current_pcb().raw_pid(),
                             i,
                             r
                         );

+ 4 - 0
kernel/src/ipc/generic_signal.rs

@@ -129,6 +129,10 @@ impl GenericSignal {
             Self::SIGRTMAX => sig_terminate(*self),
         }
     }
+
+    pub fn kernel_only(&self) -> bool {
+        matches!(self, Self::SIGKILL | Self::SIGSTOP)
+    }
 }
 
 impl From<GenericSignal> for usize {

+ 27 - 8
kernel/src/ipc/kill.rs

@@ -1,11 +1,13 @@
 use crate::arch::ipc::signal::{SigCode, Signal};
 use crate::ipc::signal_types::{SigInfo, SigType};
-use crate::process::{process_group::Pgid, Pid, ProcessManager};
+use crate::process::pid::{Pid, PidType};
+use crate::process::{ProcessControlBlock, ProcessManager, RawPid};
+use alloc::sync::Arc;
 use core::sync::atomic::compiler_fence;
 use system_error::SystemError;
 
 /// ### 杀死一个进程
-pub fn kill_process(pid: Pid, sig: Signal) -> Result<usize, SystemError> {
+pub fn kill_process(pid: RawPid, sig: Signal) -> Result<usize, SystemError> {
     // 初始化signal info
     let mut info = SigInfo::new(sig, 0, SigCode::User, SigType::Kill(pid));
     compiler_fence(core::sync::atomic::Ordering::SeqCst);
@@ -18,12 +20,29 @@ pub fn kill_process(pid: Pid, sig: Signal) -> Result<usize, SystemError> {
     ret
 }
 
+/// 直接向指定进程发送信号,绕过PID namespace查找
+///
+/// 注意!这个函数不会检查目标进程是否在本pidns内,慎用!可能造成安全问题。
+pub fn kill_process_by_pcb(
+    pcb: Arc<ProcessControlBlock>,
+    sig: Signal,
+) -> Result<usize, SystemError> {
+    // 初始化signal info
+    let mut info = SigInfo::new(sig, 0, SigCode::User, SigType::Kill(pcb.raw_pid()));
+
+    return sig
+        .send_signal_info_to_pcb(Some(&mut info), pcb)
+        .map(|x| x as usize);
+}
 /// ### 杀死一个进程组
-pub fn kill_process_group(pgid: Pgid, sig: Signal) -> Result<usize, SystemError> {
-    let pg = ProcessManager::find_process_group(pgid).ok_or(SystemError::ESRCH)?;
-    let inner = pg.process_group_inner.lock();
-    for pcb in inner.processes.values() {
-        kill_process(pcb.pid(), sig)?; // Call the new common function
+///
+/// 注意!这个函数的实现跟Linux不一致。
+///
+/// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/signal.c?fi=kill_pgrp#1921
+#[inline(never)]
+pub fn kill_process_group(pgid: &Arc<Pid>, sig: Signal) -> Result<usize, SystemError> {
+    for pcb in pgid.tasks_iter(PidType::PGID) {
+        kill_process(pcb.raw_pid(), sig)?;
     }
     Ok(0)
 }
@@ -31,7 +50,7 @@ pub fn kill_process_group(pgid: Pgid, sig: Signal) -> Result<usize, SystemError>
 /// ### 杀死所有进程
 /// - 该函数会杀死所有进程,除了当前进程和init进程
 pub fn kill_all(sig: Signal) -> Result<usize, SystemError> {
-    let current_pid = ProcessManager::current_pcb().pid();
+    let current_pid = ProcessManager::current_pcb().raw_pid();
     let all_processes = ProcessManager::get_all_processes();
 
     for pid_val in all_processes {

+ 5 - 2
kernel/src/ipc/pipe.rs

@@ -412,12 +412,15 @@ impl IndexNode for LockedPipeInode {
                             sig,
                             0,
                             SigCode::Kernel,
-                            SigType::Kill(ProcessManager::current_pid()),
+                            SigType::Kill(ProcessManager::current_pcb().task_pid_vnr()),
                         );
                         compiler_fence(core::sync::atomic::Ordering::SeqCst);
 
                         let _retval = sig
-                            .send_signal_info(Some(&mut info), ProcessManager::current_pid())
+                            .send_signal_info(
+                                Some(&mut info),
+                                ProcessManager::current_pcb().task_pid_vnr(),
+                            )
                             .map(|x| x as usize);
 
                         compiler_fence(core::sync::atomic::Ordering::SeqCst);

+ 3 - 3
kernel/src/ipc/shm.rs

@@ -10,7 +10,7 @@ use crate::{
         page::{page_manager_lock_irqsave, PageFlags, PageType},
         PhysAddr,
     },
-    process::{Pid, ProcessManager},
+    process::{ProcessManager, RawPid},
     syscall::user_access::{UserBufferReader, UserBufferWriter},
     time::PosixTimeSpec,
 };
@@ -396,9 +396,9 @@ pub struct KernelShm {
     /// 最后一次更改信息的时间
     shm_ctim: PosixTimeSpec,
     /// 创建者进程id
-    shm_cprid: Pid,
+    shm_cprid: RawPid,
     /// 最后操作者进程id
-    shm_lprid: Pid,
+    shm_lprid: RawPid,
 }
 
 impl KernelShm {

+ 27 - 16
kernel/src/ipc/signal.rs

@@ -10,7 +10,7 @@ use crate::{
     libs::spinlock::SpinLockGuard,
     mm::VirtAddr,
     process::{
-        pid::PidType, Pid, ProcessControlBlock, ProcessFlags, ProcessManager, ProcessSignalInfo,
+        pid::PidType, ProcessControlBlock, ProcessFlags, ProcessManager, ProcessSignalInfo, RawPid,
     },
     time::Instant,
 };
@@ -59,7 +59,7 @@ impl Signal {
     pub fn send_signal_info(
         &self,
         info: Option<&mut SigInfo>,
-        pid: Pid,
+        pid: RawPid,
     ) -> Result<i32, SystemError> {
         // TODO:暂时不支持特殊的信号操作,待引入进程组后补充
         // 如果 pid 大于 0,那么会发送信号给 pid 指定的进程
@@ -67,7 +67,7 @@ impl Signal {
         // 如果 pid 小于 -1,那么会向组 ID 等于该 pid 绝对值的进程组内所有下属进程发送信号。向一个进程组的所有进程发送信号在 shell 作业控制中有特殊有途
         // 如果 pid 等于 -1,那么信号的发送范围是:调用进程有权将信号发往的每个目标进程,除去 init(进程 ID 为 1)和调用进程自身。如果特权级进程发起这一调用,那么会发送信号给系统中的所有进程,上述两个进程除外。显而易见,有时也将这种信号发送方式称之为广播信号
         // 如果并无进程与指定的 pid 相匹配,那么 kill() 调用失败,同时将 errno 置为 ESRCH(“查无此进程”)
-        if pid.lt(&Pid::from(0)) {
+        if pid.lt(&RawPid::from(0)) {
             warn!("Kill operation not support: pid={:?}", pid);
             return Err(SystemError::ENOSYS);
         }
@@ -81,8 +81,8 @@ impl Signal {
         if !self.is_valid() {
             return Err(SystemError::EINVAL);
         }
-        let mut retval = Err(SystemError::ESRCH);
-        let pcb = ProcessManager::find(pid);
+        let retval = Err(SystemError::ESRCH);
+        let pcb = ProcessManager::find_task_by_vpid(pid);
 
         if pcb.is_none() {
             warn!("No such process: pid={:?}", pid);
@@ -90,11 +90,22 @@ impl Signal {
         }
 
         let pcb = pcb.unwrap();
-        // println!("Target pcb = {:?}", pcb.as_ref().unwrap());
+        return self.send_signal_info_to_pcb(info, pcb);
+    }
+
+    /// 直接向指定进程发送信号,绕过PID namespace查找
+    pub fn send_signal_info_to_pcb(
+        &self,
+        info: Option<&mut SigInfo>,
+        pcb: Arc<ProcessControlBlock>,
+    ) -> Result<i32, SystemError> {
+        // 检查sig是否符合要求,如果不符合要求,则退出。
+        if !self.is_valid() {
+            return Err(SystemError::EINVAL);
+        }
         compiler_fence(core::sync::atomic::Ordering::SeqCst);
         // 发送信号
-        retval = self.send_signal(info, pcb.clone(), PidType::PID);
-
+        let retval = self.send_signal(info, pcb, PidType::PID);
         compiler_fence(core::sync::atomic::Ordering::SeqCst);
         return retval;
     }
@@ -154,7 +165,7 @@ impl Signal {
                         *self,
                         0,
                         SigCode::User,
-                        SigType::Kill(ProcessManager::current_pcb().pid()),
+                        SigType::Kill(ProcessManager::current_pcb().raw_pid()),
                     )
                 }
             };
@@ -337,8 +348,8 @@ fn signal_wake_up(pcb: Arc<ProcessControlBlock>, _guard: SpinLockGuard<SignalStr
             wakeup_ok = false;
             warn!(
                 "Current pid: {:?}, signal_wake_up target {:?} error: {:?}",
-                ProcessManager::current_pcb().pid(),
-                pcb.pid(),
+                ProcessManager::current_pcb().raw_pid(),
+                pcb.raw_pid(),
                 e
             );
         });
@@ -347,8 +358,8 @@ fn signal_wake_up(pcb: Arc<ProcessControlBlock>, _guard: SpinLockGuard<SignalStr
             wakeup_ok = false;
             warn!(
                 "Current pid: {:?}, signal_wake_up target {:?} error: {:?}",
-                ProcessManager::current_pcb().pid(),
-                pcb.pid(),
+                ProcessManager::current_pcb().raw_pid(),
+                pcb.raw_pid(),
                 e
             );
         });
@@ -606,10 +617,10 @@ fn retarget_shared_pending(pcb: Arc<ProcessControlBlock>, which: SigSet) {
     };
 
     // 暴力遍历每一个线程,找到相同的tgid
-    let tgid = pcb.tgid();
+    let tgid = pcb.task_tgid_vnr();
     for &pid in pcb.children_read_irqsave().iter() {
-        if let Some(child) = ProcessManager::find(pid) {
-            if child.tgid() == tgid {
+        if let Some(child) = ProcessManager::find_task_by_vpid(pid) {
+            if child.task_tgid_vnr() == tgid {
                 thread_handling_function(child, &retarget);
             }
         }

+ 43 - 5
kernel/src/ipc/signal_types.rs

@@ -5,6 +5,7 @@ use core::{
     sync::atomic::AtomicI64,
 };
 
+use alloc::sync::Arc;
 use alloc::vec::Vec;
 use system_error::SystemError;
 
@@ -15,7 +16,10 @@ use crate::{
         ipc::signal::{SigCode, SigFlags, SigSet, Signal, MAX_SIG_NUM},
     },
     mm::VirtAddr,
-    process::Pid,
+    process::{
+        pid::{Pid, PidType},
+        RawPid,
+    },
     syscall::user_access::UserBufferWriter,
 };
 
@@ -84,6 +88,8 @@ pub struct InnerSignalStruct {
     /// 如果对应linux,这部分会有一个引用计数,但是没发现在哪里有用到需要计算引用的地方,因此
     /// 暂时删掉,不然这个Arc会导致其他地方的代码十分丑陋
     pub handlers: Vec<Sigaction>,
+
+    pub pids: [Option<Arc<Pid>>; PidType::PIDTYPE_MAX],
 }
 
 impl SignalStruct {
@@ -95,6 +101,11 @@ impl SignalStruct {
 
         r
     }
+
+    pub fn reset_sighandlers(&mut self) {
+        // 重置信号处理程序
+        self.inner.handlers = default_sighandlers();
+    }
 }
 
 impl Default for SignalStruct {
@@ -122,10 +133,17 @@ impl Default for InnerSignalStruct {
         Self {
             cnt: Default::default(),
             handlers: default_sighandlers(),
+            pids: core::array::from_fn(|_| None),
         }
     }
 }
 
+impl InnerSignalStruct {
+    pub fn handler(&self, sig: Signal) -> Option<&Sigaction> {
+        self.handlers.get(sig as usize - 1)
+    }
+}
+
 #[derive(Debug, Copy, Clone)]
 #[allow(dead_code)]
 pub enum SigactionType {
@@ -354,8 +372,8 @@ impl SigInfo {
 
 #[derive(Copy, Clone, Debug)]
 pub enum SigType {
-    Kill(Pid),
-    Alarm(Pid),
+    Kill(RawPid),
+    Alarm(RawPid),
     // 后续完善下列中的具体字段
     // Timer,
     // Rt,
@@ -443,8 +461,8 @@ impl SigPending {
             return info;
         } else {
             // 信号不在sigqueue中,这意味着当前信号是来自快速路径,因此直接把siginfo设置为0即可。
-            let mut ret = SigInfo::new(sig, 0, SigCode::User, SigType::Kill(Pid::from(0)));
-            ret.set_sig_type(SigType::Kill(Pid::new(0)));
+            let mut ret = SigInfo::new(sig, 0, SigCode::User, SigType::Kill(RawPid::from(0)));
+            ret.set_sig_type(SigType::Kill(RawPid::new(0)));
             return ret;
         }
     }
@@ -562,3 +580,23 @@ pub trait SignalArch {
 
     fn sys_rt_sigreturn(trap_frame: &mut TrapFrame) -> u64;
 }
+
+bitflags! {
+
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/include/linux/sched/signal.h#253
+    pub struct SignalFlags: u32 {
+        const STOP_STOPPED = 0x00000001; /* job control stop in effect */
+        const STOP_CONTINUED = 0x00000002; /* SIGCONT since WCONTINUED reap */
+        const GROUP_EXIT = 0x00000004; /* group exit in progress */
+        const CLD_STOPPED = 0x00000010; /* Pending notifications to parent */
+        const CLD_CONTINUED = 0x00000020;
+        const UNKILLABLE = 0x00000040; /* for init: ignore fatal signals */
+    }
+}
+
+impl SignalFlags {
+    pub const CLD_MASK: SignalFlags = SignalFlags::CLD_STOPPED.union(SignalFlags::CLD_CONTINUED);
+    pub const STOP_MASK: SignalFlags = SignalFlags::CLD_MASK
+        .union(SignalFlags::STOP_STOPPED)
+        .union(SignalFlags::STOP_CONTINUED);
+}

+ 17 - 13
kernel/src/ipc/syscall/sys_kill.rs

@@ -1,13 +1,15 @@
 use alloc::string::ToString;
+use alloc::sync::Arc;
 use alloc::vec::Vec;
 use core::ffi::c_int;
 
 use crate::arch::interrupt::TrapFrame;
+use crate::process::pid::Pid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use crate::{
     arch::{ipc::signal::Signal, syscall::nr::SYS_KILL},
-    process::{process_group::Pgid, Pid, ProcessManager},
+    process::{ProcessManager, RawPid},
 };
 use log::warn;
 use system_error::SystemError;
@@ -19,25 +21,27 @@ use crate::ipc::kill::{kill_all, kill_process, kill_process_group};
 /// - 如果id == -1,则为所有进程
 /// - 如果id == 0,则为当前进程组
 /// - 如果id > 0,则为pid
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[derive(Debug, Clone, PartialEq, Eq)]
 pub enum PidConverter {
     All,
-    Pid(Pid),
-    Pgid(Pgid),
+    Pid(Arc<Pid>),
+    Pgid(Option<Arc<Pid>>),
 }
 
 impl PidConverter {
     /// ### 为 `wait` 和 `kill` 调用使用
-    pub fn from_id(id: i32) -> Self {
+    pub fn from_id(id: i32) -> Option<Self> {
         if id < -1 {
-            PidConverter::Pgid(Pgid::from(-id as usize))
+            let pgid = ProcessManager::find_vpid(RawPid::from(-id as usize));
+            Some(PidConverter::Pgid(pgid))
         } else if id == -1 {
-            PidConverter::All
+            Some(PidConverter::All)
         } else if id == 0 {
-            let pgid = ProcessManager::current_pcb().pgid();
-            PidConverter::Pgid(pgid)
+            let pgid = ProcessManager::current_pcb().task_pgrp().unwrap();
+            Some(PidConverter::Pgid(Some(pgid)))
         } else {
-            PidConverter::Pid(Pid::from(id as usize))
+            let pid = ProcessManager::find_vpid(RawPid::from(id as usize))?;
+            Some(PidConverter::Pid(pid))
         }
     }
 }
@@ -65,7 +69,7 @@ impl Syscall for SysKillHandle {
         let id = Self::pid(args);
         let sig_c_int = Self::sig(args);
 
-        let converter = PidConverter::from_id(id);
+        let converter = PidConverter::from_id(id).ok_or(SystemError::ESRCH)?;
         let sig = Signal::from(sig_c_int);
         if sig == Signal::INVALID {
             warn!("Not a valid signal number");
@@ -73,8 +77,8 @@ impl Syscall for SysKillHandle {
         }
 
         match converter {
-            PidConverter::Pid(pid) => kill_process(pid, sig),
-            PidConverter::Pgid(pgid) => kill_process_group(pgid, sig),
+            PidConverter::Pid(pid) => kill_process(pid.pid_vnr(), sig),
+            PidConverter::Pgid(pgid) => kill_process_group(&pgid.ok_or(SystemError::ESRCH)?, sig),
             PidConverter::All => kill_all(sig),
         }
     }

+ 2 - 2
kernel/src/ipc/syscall/sys_restart.rs

@@ -4,7 +4,7 @@ use crate::{
     alloc::vec::Vec,
     arch::ipc::signal::{SigCode, Signal},
     arch::syscall::nr::SYS_RESTART_SYSCALL,
-    process::{Pid, ProcessManager},
+    process::{ProcessManager, RawPid},
     syscall::table::{FormattedSyscallParam, Syscall},
 };
 use syscall_table_macros::declare_syscall;
@@ -22,7 +22,7 @@ pub(super) fn do_kernel_restart_syscall() -> Result<usize, SystemError> {
         return restart_block.restart_fn.call(&mut restart_block.data);
     } else {
         // 不应该走到这里,因此kill掉当前进程及同组的进程
-        let pid = Pid::new(0);
+        let pid = RawPid::new(0);
         let sig = Signal::SIGKILL;
         let mut info = SigInfo::new(sig, 0, SigCode::Kernel, SigType::Kill(pid));
 

+ 1 - 1
kernel/src/ipc/syscall/sys_sigaction.rs

@@ -83,7 +83,7 @@ pub(super) fn do_kernel_sigaction(
         } else if new_ka.action().is_customized() {
             error!(
                 "pid:{:?}: in sys_sigaction: User must manually sprcify a sa_restorer for signal {}.",
-                ProcessManager::current_pcb().pid(),
+                ProcessManager::current_pcb().raw_pid(),
                 sig
             );
             return Err(SystemError::EINVAL);

+ 5 - 4
kernel/src/libs/elf.rs

@@ -246,7 +246,7 @@ impl ElfLoader {
             if err == SystemError::EEXIST {
                 error!(
                     "Pid: {:?}, elf segment at {:p} overlaps with existing mapping",
-                    ProcessManager::current_pcb().pid(),
+                    ProcessManager::current_pcb().raw_pid(),
                     addr_to_map.as_ptr::<u8>()
                 );
             }
@@ -808,9 +808,10 @@ impl BinaryLoader for ElfLoader {
         }
         Self::parse_gnu_property()?;
 
-        // todo: 设置exec信息的功能 https://code.dragonos.org.cn/xref/linux-6.1.9/fs/binfmt_elf.c#1002
-        crate::process::execve::begin_new_exec(param)
-            .map_err(|e| ExecError::Other(format!("Failed to begin new exec: {:?}", e)))?;
+        param.begin_new_exec()?;
+
+        // todo: 补充逻辑:https://code.dragonos.org.cn/xref/linux-6.6.21/fs/binfmt_elf.c#1007
+        param.setup_new_exec();
 
         let mut elf_brk = VirtAddr::new(0);
         let mut elf_bss = VirtAddr::new(0);

+ 4 - 4
kernel/src/libs/futex/futex.rs

@@ -18,7 +18,7 @@ use crate::{
     exception::InterruptArch,
     libs::spinlock::{SpinLock, SpinLockGuard},
     mm::{ucontext::AddressSpace, MemoryManagementArch, VirtAddr},
-    process::{Pid, ProcessControlBlock, ProcessManager},
+    process::{ProcessControlBlock, ProcessManager, RawPid},
     sched::{schedule, SchedMode},
     syscall::user_access::{UserBufferReader, UserBufferWriter},
     time::{
@@ -561,7 +561,7 @@ impl Futex {
         if (encoded_op & (FutexOP::FUTEX_OP_OPARG_SHIFT.bits() << 28) != 0) && oparg > 31 {
             warn!(
                 "futex_wake_op: pid:{} tries to shift op by {}; fix this program",
-                ProcessManager::current_pcb().pid().data(),
+                ProcessManager::current_pcb().raw_pid().data(),
                 oparg
             );
 
@@ -708,7 +708,7 @@ impl RobustListHead {
         let pcb: Arc<ProcessControlBlock> = if pid == 0 {
             ProcessManager::current_pcb()
         } else {
-            ProcessManager::find(Pid::new(pid)).ok_or(SystemError::ESRCH)?
+            ProcessManager::find_task_by_vpid(RawPid::new(pid)).ok_or(SystemError::ESRCH)?
         };
 
         // TODO: 检查当前进程是否能ptrace另一个进程
@@ -752,7 +752,7 @@ impl RobustListHead {
         };
         // 遍历当前进程/线程的robust list
         for futex_uaddr in head.futexes() {
-            let ret = Self::handle_futex_death(futex_uaddr, pcb.pid().into() as u32);
+            let ret = Self::handle_futex_death(futex_uaddr, pcb.raw_pid().into() as u32);
             if ret.is_err() {
                 return;
             }

+ 4 - 4
kernel/src/libs/mutex.rs

@@ -10,7 +10,7 @@ use crate::{
     arch::CurrentIrqArch,
     exception::InterruptArch,
     libs::spinlock::SpinLockGuard,
-    process::{Pid, ProcessControlBlock, ProcessManager},
+    process::{ProcessControlBlock, ProcessManager, RawPid},
     sched::{schedule, SchedMode},
 };
 
@@ -65,7 +65,7 @@ impl<T> Mutex<T> {
             // 当前mutex已经上锁
             if inner.is_locked {
                 // 检查当前进程是否处于等待队列中,如果不在,就加到等待队列内
-                if !self.check_pid_in_wait_list(&inner, ProcessManager::current_pcb().pid()) {
+                if !self.check_pid_in_wait_list(&inner, ProcessManager::current_pcb().raw_pid()) {
                     inner.wait_list.push_back(ProcessManager::current_pcb());
                 }
 
@@ -132,9 +132,9 @@ impl<T> Mutex<T> {
 
     /// @brief 检查进程是否在该mutex的等待队列内
     #[inline]
-    fn check_pid_in_wait_list(&self, inner: &MutexInner, pid: Pid) -> bool {
+    fn check_pid_in_wait_list(&self, inner: &MutexInner, pid: RawPid) -> bool {
         for p in inner.wait_list.iter() {
-            if p.pid() == pid {
+            if p.raw_pid() == pid {
                 // 在等待队列内
                 return true;
             }

+ 1 - 1
kernel/src/libs/semaphore.rs

@@ -54,7 +54,7 @@ impl Semaphore {
                 //如果唤醒失败,打印错误信息
                 debug!(
                     "Semaphore wakeup failed: current pid= {}, semaphore={:?}",
-                    ProcessManager::current_pcb().pid().into(),
+                    ProcessManager::current_pcb().raw_pid().into(),
                     self
                 );
             }

+ 2 - 2
kernel/src/libs/wait_queue.rs

@@ -313,7 +313,7 @@ impl WaitQueue {
 
             if wake {
                 ProcessManager::wakeup(&to_wakeup).unwrap_or_else(|e| {
-                    error!("wakeup pid: {:?} error: {:?}", to_wakeup.pid(), e);
+                    error!("wakeup pid: {:?} error: {:?}", to_wakeup.raw_pid(), e);
                 });
                 continue;
             } else {
@@ -348,7 +348,7 @@ fn before_sleep_check(max_preempt: usize) {
     if unlikely(pcb.preempt_count() > max_preempt) {
         warn!(
             "Process {:?}: Try to sleep when preempt count is {}",
-            pcb.pid().data(),
+            pcb.raw_pid().data(),
             pcb.preempt_count()
         );
     }

+ 1 - 1
kernel/src/mm/mmio_buddy.rs

@@ -499,7 +499,7 @@ impl MmioBuddyMemPool {
             Err(_) => {
                 error!(
                     "failed to create mmio. pid = {:?}",
-                    ProcessManager::current_pcb().pid()
+                    ProcessManager::current_pcb().raw_pid()
                 );
                 return Err(SystemError::ENOMEM);
             }

+ 3 - 3
kernel/src/net/socket/mod.rs

@@ -29,7 +29,7 @@ use crate::{
         spinlock::{SpinLock, SpinLockGuard},
         wait_queue::EventWaitQueue,
     },
-    process::{Pid, ProcessManager},
+    process::{ProcessManager, RawPid},
     sched::{schedule, SchedMode},
 };
 
@@ -495,9 +495,9 @@ impl SocketHandleItem {
 /// 如果 TCP/UDP 的 socket 绑定了某个端口,它会在对应的表中记录,以检测端口冲突。
 pub struct PortManager {
     // TCP 端口记录表
-    tcp_port_table: SpinLock<HashMap<u16, Pid>>,
+    tcp_port_table: SpinLock<HashMap<u16, RawPid>>,
     // UDP 端口记录表
-    udp_port_table: SpinLock<HashMap<u16, Pid>>,
+    udp_port_table: SpinLock<HashMap<u16, RawPid>>,
 }
 
 impl PortManager {

+ 24 - 7
kernel/src/process/cred.rs

@@ -1,10 +1,15 @@
+use alloc::sync::{Arc, Weak};
 use core::sync::atomic::AtomicUsize;
 
 use alloc::vec::Vec;
 
+use super::namespace::user_namespace::{UserNamespace, INIT_USER_NAMESPACE};
+
 const GLOBAL_ROOT_UID: Kuid = Kuid(0);
 const GLOBAL_ROOT_GID: Kgid = Kgid(0);
-pub static INIT_CRED: Cred = Cred::init();
+lazy_static::lazy_static! {
+    pub static ref INIT_CRED: Arc<Cred> = Cred::init();
+}
 
 int_like!(Kuid, AtomicKuid, usize, AtomicUsize);
 int_like!(Kgid, AtomicKgid, usize, AtomicUsize);
@@ -23,8 +28,9 @@ pub enum CredFsCmp {
 }
 
 /// 凭证集
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone)]
 pub struct Cred {
+    pub self_ref: Weak<Cred>,
     /// 进程实际uid
     pub uid: Kuid,
     /// 进程实际gid
@@ -53,11 +59,13 @@ pub struct Cred {
     pub cap_ambient: CAPFlags,
     /// supplementary groups for euid/fsgid
     pub group_info: Option<GroupInfo>,
+    pub user_ns: Arc<UserNamespace>,
 }
 
 impl Cred {
-    pub const fn init() -> Self {
-        Self {
+    fn init() -> Arc<Self> {
+        Arc::new_cyclic(|weak_self| Self {
+            self_ref: weak_self.clone(),
             uid: GLOBAL_ROOT_UID,
             gid: GLOBAL_ROOT_GID,
             suid: GLOBAL_ROOT_UID,
@@ -72,13 +80,22 @@ impl Cred {
             cap_bset: CAPFlags::CAP_FULL_SET,
             cap_ambient: CAPFlags::CAP_FULL_SET,
             group_info: None,
-        }
+            user_ns: INIT_USER_NAMESPACE.clone(),
+        })
+    }
+
+    pub fn new_arc(cred: Cred) -> Arc<Self> {
+        Arc::new_cyclic(|weak_self| {
+            let mut new_cred = cred;
+            new_cred.self_ref = weak_self.clone();
+            new_cred
+        })
     }
 
     #[allow(dead_code)]
     /// Compare two credentials with respect to filesystem access.
-    pub fn fscmp(&self, other: Cred) -> CredFsCmp {
-        if *self == other {
+    pub fn fscmp(&self, other: Arc<Cred>) -> CredFsCmp {
+        if Arc::ptr_eq(&self.self_ref.upgrade().unwrap(), &other) {
             return CredFsCmp::Equal;
         }
 

+ 46 - 2
kernel/src/process/exec.rs

@@ -1,8 +1,10 @@
-use core::{fmt::Debug, ptr::null};
+use core::{fmt::Debug, ptr::null, sync::atomic::Ordering};
 
 use alloc::{collections::BTreeMap, ffi::CString, string::String, sync::Arc, vec::Vec};
 use system_error::SystemError;
 
+use crate::process::Signal;
+
 use crate::{
     driver::base::block::SeekFrom,
     filesystem::vfs::{
@@ -16,7 +18,9 @@ use crate::{
     },
 };
 
-use super::ProcessManager;
+use super::{
+    namespace::nsproxy::exec_task_namespaces, ProcessControlBlock, ProcessFlags, ProcessManager,
+};
 
 /// 系统支持的所有二进制文件加载器的列表
 const BINARY_LOADERS: [&'static dyn BinaryLoader; 1] = [&ELF_LOADER];
@@ -52,6 +56,7 @@ impl BinaryLoaderResult {
 #[allow(dead_code)]
 #[derive(Debug)]
 pub enum ExecError {
+    SystemError(SystemError),
     /// 二进制文件不可执行
     NotExecutable,
     /// 二进制文件不是当前架构的
@@ -70,9 +75,11 @@ pub enum ExecError {
     BadAddress(Option<VirtAddr>),
     Other(String),
 }
+
 impl From<ExecError> for SystemError {
     fn from(val: ExecError) -> Self {
         match val {
+            ExecError::SystemError(e) => e,
             ExecError::NotExecutable => SystemError::ENOEXEC,
             ExecError::WrongArchitecture => SystemError::ENOEXEC,
             ExecError::PermissionDenied => SystemError::EACCES,
@@ -162,6 +169,43 @@ impl ExecParam {
     pub fn file(self) -> File {
         self.file
     }
+
+    /// Calling this is the point of no return. None of the failures will be
+    /// seen by userspace since either the process is already taking a fatal
+    /// signal.
+    ///
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/fs/exec.c#1246
+    pub fn begin_new_exec(&mut self) -> Result<(), ExecError> {
+        let me = ProcessManager::current_pcb();
+        // todo: 补充linux的逻辑
+        de_thread(&me).map_err(ExecError::SystemError)?;
+
+        me.flags().remove(ProcessFlags::FORKNOEXEC);
+
+        exec_task_namespaces().map_err(ExecError::SystemError)?;
+        Ok(())
+    }
+
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/fs/exec.c?fi=setup_new_exec#1443
+    pub fn setup_new_exec(&mut self) {
+        // todo!("setup_new_exec logic");
+    }
+}
+
+/// https://code.dragonos.org.cn/xref/linux-6.6.21/fs/exec.c#1044
+fn de_thread(pcb: &Arc<ProcessControlBlock>) -> Result<(), SystemError> {
+    ProcessManager::current_pcb()
+        .sig_struct_irqsave()
+        .reset_sighandlers();
+    // todo: 该函数未正确实现
+    let tg_empty = pcb.threads_read_irqsave().thread_group_empty();
+    if tg_empty {
+        pcb.exit_signal.store(Signal::SIGCHLD, Ordering::SeqCst);
+        return Ok(());
+    }
+    log::warn!("de_thread: todo impl thread group logic");
+
+    return Ok(());
 }
 
 /// ## 加载二进制文件

+ 0 - 17
kernel/src/process/execve.rs

@@ -1,7 +1,6 @@
 use crate::arch::CurrentIrqArch;
 use crate::exception::InterruptArch;
 use crate::filesystem::vfs::IndexNode;
-use crate::ipc::signal_types::SignalStruct;
 use crate::process::exec::{load_binary_file, ExecParam, ExecParamFlags};
 use crate::process::ProcessManager;
 use crate::syscall::Syscall;
@@ -114,19 +113,3 @@ fn do_execve_switch_user_vm(new_vm: Arc<AddressSpace>) -> Option<Arc<AddressSpac
 
     old_address_space
 }
-
-/// todo: 该函数未正确实现
-/// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/fs/exec.c?fi=begin_new_exec#1244
-pub fn begin_new_exec(_param: &mut ExecParam) -> Result<(), SystemError> {
-    de_thread()?;
-
-    Ok(())
-}
-
-/// todo: 该函数未正确实现
-/// https://code.dragonos.org.cn/xref/linux-6.1.9/fs/exec.c?fi=begin_new_exec#1042
-fn de_thread() -> Result<(), SystemError> {
-    *ProcessManager::current_pcb().sig_struct_irqsave() = SignalStruct::default();
-
-    Ok(())
-}

+ 73 - 24
kernel/src/process/exit.rs

@@ -1,17 +1,19 @@
-use alloc::sync::Arc;
+use alloc::sync::{Arc, Weak};
 use core::intrinsics::likely;
 use system_error::SystemError;
 
 use crate::{
     arch::ipc::signal::{SigChildCode, Signal},
+    driver::tty::tty_core::TtyCore,
     ipc::syscall::sys_kill::PidConverter,
+    process::pid::PidType,
     sched::{schedule, SchedMode},
     syscall::user_access::UserBufferWriter,
     time::{sleep::nanosleep, Duration},
 };
 
 use super::{
-    abi::WaitOption, resource::RUsage, Pid, ProcessControlBlock, ProcessManager, ProcessState,
+    abi::WaitOption, resource::RUsage, ProcessControlBlock, ProcessManager, ProcessState, RawPid,
 };
 
 /// 内核wait4时的参数
@@ -28,7 +30,7 @@ pub struct KernelWaitOption<'a> {
 #[derive(Debug, Clone)]
 #[allow(dead_code)]
 pub struct WaitIdInfo {
-    pub pid: Pid,
+    pub pid: RawPid,
     pub status: i32,
     pub cause: i32,
 }
@@ -52,7 +54,7 @@ pub fn kernel_wait4(
     options: WaitOption,
     rusage_buf: Option<&mut RUsage>,
 ) -> Result<usize, SystemError> {
-    let converter = PidConverter::from_id(pid);
+    let converter = PidConverter::from_id(pid).ok_or(SystemError::ESRCH)?;
 
     // 构造参数
     let mut kwo = KernelWaitOption::new(converter, options);
@@ -106,9 +108,10 @@ fn do_wait(kwo: &mut KernelWaitOption) -> Result<usize, SystemError> {
 
     'outer: loop {
         kwo.no_task_error = Some(SystemError::ECHILD);
-        match kwo.pid_converter {
+        match &kwo.pid_converter {
             PidConverter::Pid(pid) => {
-                let child_pcb = ProcessManager::find(pid)
+                let child_pcb = pid
+                    .pid_task(PidType::PID)
                     .ok_or(SystemError::ECHILD)
                     .unwrap();
                 // 获取weak引用,以便于在do_waitpid中能正常drop pcb
@@ -134,7 +137,8 @@ fn do_wait(kwo: &mut KernelWaitOption) -> Result<usize, SystemError> {
                         break;
                     }
                     for pid in rd_childen.iter() {
-                        let pcb = ProcessManager::find(*pid).ok_or(SystemError::ECHILD)?;
+                        let pcb =
+                            ProcessManager::find_task_by_vpid(*pid).ok_or(SystemError::ECHILD)?;
                         let sched_guard = pcb.sched_info().inner_lock_read_irqsave();
                         let state = sched_guard.state();
                         if state.is_exited() {
@@ -144,7 +148,7 @@ fn do_wait(kwo: &mut KernelWaitOption) -> Result<usize, SystemError> {
                             // 而是要先break到外层循环,以便释放父进程的children字段的锁,才能drop pcb。
                             // 否则会死锁。
                             tmp_child_pcb = Some(pcb.clone());
-                            unsafe { ProcessManager::release(*pid) };
+                            unsafe { ProcessManager::release(pcb.raw_pid()) };
                             retval = Ok((*pid).into());
                             break 'outer;
                         }
@@ -153,11 +157,9 @@ fn do_wait(kwo: &mut KernelWaitOption) -> Result<usize, SystemError> {
                     nanosleep(Duration::from_millis(100).into())?;
                 }
             }
-            PidConverter::Pgid(pgid) => {
-                let pg = ProcessManager::find_process_group(pgid).ok_or(SystemError::ESRCH)?;
+            PidConverter::Pgid(Some(pgid)) => {
                 loop {
-                    let inner = pg.process_group_inner.lock();
-                    for (_, pcb) in inner.processes.iter() {
+                    for pcb in pgid.tasks_iter(PidType::PGID) {
                         let sched_guard = pcb.sched_info().inner_lock_read_irqsave();
                         let state = sched_guard.state();
                         if state.is_exited() {
@@ -167,16 +169,16 @@ fn do_wait(kwo: &mut KernelWaitOption) -> Result<usize, SystemError> {
                             // 而是要先break到外层循环,以便释放父进程的children字段的锁,才能drop pcb。
                             // 否则会死锁。
                             tmp_child_pcb = Some(pcb.clone());
-                            let pid = pcb.pid();
-                            unsafe { ProcessManager::release(pid) };
-                            retval = Ok((pid).into());
+                            unsafe { ProcessManager::release(pcb.raw_pid()) };
+                            retval = Ok(pcb.task_pid_vnr().into());
                             break 'outer;
                         }
                     }
-                    drop(inner);
                     nanosleep(Duration::from_millis(100).into())?;
                 }
             }
+
+            _ => {}
         }
         notask!('outer);
     }
@@ -210,7 +212,7 @@ fn do_waitpid(
             {
                 if let Some(info) = &mut kwo.ret_info {
                     *info = WaitIdInfo {
-                        pid: child_pcb.pid(),
+                        pid: child_pcb.raw_pid(),
                         status: Signal::SIGCONT as i32,
                         cause: SigChildCode::Continued.into(),
                     };
@@ -242,17 +244,22 @@ fn do_waitpid(
             }
             if let Some(infop) = &mut kwo.ret_info {
                 *infop = WaitIdInfo {
-                    pid: child_pcb.pid(),
+                    pid: child_pcb.raw_pid(),
                     status: exitcode,
                     cause: SigChildCode::Stopped.into(),
                 };
             }
 
-            return Some(Ok(child_pcb.pid().data()));
+            return Some(Ok(child_pcb.raw_pid().data()));
         }
         ProcessState::Exited(status) => {
-            let pid = child_pcb.pid();
-            // debug!("wait4: child exited, pid: {:?}, status: {status}\n", pid);
+            let pid = child_pcb.task_pid_vnr();
+            // log::debug!(
+            //     "wait4: current: {}, child exited, pid: {:?}, status: {status}, \n kwo.opt: {:?}",
+            //     ProcessManager::current_pid().data(),
+            //     child_pcb.raw_pid(),
+            //     kwo.options
+            // );
 
             if likely(!kwo.options.contains(WaitOption::WEXITED)) {
                 return None;
@@ -270,13 +277,55 @@ fn do_waitpid(
 
             kwo.ret_status = status as i32;
 
-            child_pcb.clear_pg_and_session_reference();
-            drop(child_pcb);
             // debug!("wait4: to release {pid:?}");
-            unsafe { ProcessManager::release(pid) };
+            unsafe { ProcessManager::release(child_pcb.raw_pid()) };
+            drop(child_pcb);
             return Some(Ok(pid.into()));
         }
     };
 
     return None;
 }
+
+impl ProcessControlBlock {
+    /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/exit.c#143
+    pub(super) fn __exit_signal(&mut self) {
+        let group_dead = self.is_thread_group_leader();
+        let mut sig_guard = self.sig_info_mut();
+        let mut tty: Option<Arc<TtyCore>> = None;
+        // log::debug!(
+        //     "Process {} is exiting, group_dead: {}, state: {:?}",
+        //     self.raw_pid(),
+        //     group_dead,
+        //     self.sched_info().inner_lock_read_irqsave().state()
+        // );
+        if group_dead {
+            tty = sig_guard.tty();
+            sig_guard.set_tty(None);
+        } else {
+            // todo: 通知那些等待当前线程组退出的进程
+        }
+        self.__unhash_process(group_dead);
+
+        drop(tty);
+    }
+
+    /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/exit.c#123
+    fn __unhash_process(&self, group_dead: bool) {
+        self.detach_pid(PidType::PID);
+        if group_dead {
+            self.detach_pid(PidType::TGID);
+            self.detach_pid(PidType::PGID);
+            self.detach_pid(PidType::SID);
+        }
+
+        // 从线程组中移除
+        let thread_group_leader = self.threads_read_irqsave().group_leader();
+        if let Some(leader) = thread_group_leader {
+            leader
+                .threads_write_irqsave()
+                .group_tasks
+                .retain(|pcb| !Weak::ptr_eq(pcb, &self.self_ref));
+        }
+    }
+}

+ 148 - 65
kernel/src/process/fork.rs

@@ -8,7 +8,7 @@ use system_error::SystemError;
 use crate::{
     arch::{interrupt::TrapFrame, ipc::signal::Signal},
     filesystem::procfs::procfs_register_pid,
-    ipc::signal::flush_signal_handlers,
+    ipc::{signal::flush_signal_handlers, signal_types::SignalFlags},
     libs::rwlock::RwLock,
     mm::VirtAddr,
     process::ProcessFlags,
@@ -18,14 +18,17 @@ use crate::{
 };
 
 use super::{
+    alloc_pid,
     kthread::{KernelThreadPcbPrivate, WorkerPrivate},
-    KernelStack, Pgid, Pid, ProcessControlBlock, ProcessManager, Sid,
+    pid::{Pid, PidType},
+    KernelStack, ProcessControlBlock, ProcessManager, RawPid,
 };
 const MAX_PID_NS_LEVEL: usize = 32;
 
 bitflags! {
     /// 进程克隆标志
     pub struct CloneFlags: u64 {
+        const CLONE_NEWTIME = 0x00000080;
         /// 在进程间共享虚拟内存空间
         const CLONE_VM = 0x00000100;
         /// 在进程间共享文件系统信息
@@ -157,7 +160,7 @@ impl ProcessManager {
     pub fn fork(
         current_trapframe: &TrapFrame,
         clone_flags: CloneFlags,
-    ) -> Result<Pid, SystemError> {
+    ) -> Result<RawPid, SystemError> {
         let current_pcb = ProcessManager::current_pcb();
 
         let new_kstack: KernelStack = KernelStack::new()?;
@@ -172,18 +175,27 @@ impl ProcessManager {
         Self::copy_process(&current_pcb, &pcb, args, current_trapframe).map_err(|e| {
             error!(
                 "fork: Failed to copy process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(),
-                pcb.pid(),
+                current_pcb.raw_pid(),
+                pcb.raw_pid(),
                 e
             );
             e
         })?;
+        // if pcb.raw_pid().data() > 1 {
+        //     log::debug!(
+        //         "fork done, pid: {}, pgid: {:?}, tgid: {:?}, sid: {}",
+        //         pcb.raw_pid(),
+        //         pcb.task_pgrp().map(|x| x.pid_vnr().data()),
+        //         pcb.task_tgid_vnr(),
+        //         pcb.task_session().map_or(0, |s| s.pid_vnr().data())
+        //     );
+        // }
 
         // 向procfs注册进程
-        procfs_register_pid(pcb.pid()).unwrap_or_else(|e| {
+        procfs_register_pid(pcb.raw_pid()).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to register pid to procfs, pid: [{:?}]. Error: {:?}",
-                pcb.pid(),
+                pcb.raw_pid(),
                 e
             )
         });
@@ -193,12 +205,16 @@ impl ProcessManager {
         ProcessManager::wakeup(&pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to wakeup new process, pid: [{:?}]. Error: {:?}",
-                pcb.pid(),
+                pcb.raw_pid(),
                 e
             )
         });
 
-        return Ok(pcb.pid());
+        if ProcessManager::current_pid().data() == 0 {
+            return Ok(pcb.raw_pid());
+        }
+
+        return Ok(pcb.pid().pid_vnr());
     }
 
     fn copy_flags(
@@ -236,7 +252,7 @@ impl ProcessManager {
         let old_address_space = current_pcb.basic().user_vm().unwrap_or_else(|| {
             panic!(
                 "copy_mm: Failed to get address space of current process, current pid: [{:?}]",
-                current_pcb.pid()
+                current_pcb.raw_pid()
             )
         });
 
@@ -247,7 +263,7 @@ impl ProcessManager {
         let new_address_space = old_address_space.write_irqsave().try_clone().unwrap_or_else(|e| {
             panic!(
                 "copy_mm: Failed to clone address space of current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), new_pcb.pid(), e
+                current_pcb.raw_pid(), new_pcb.raw_pid(), e
             )
         });
         unsafe { new_pcb.basic_mut().set_user_vm(Some(new_address_space)) };
@@ -350,7 +366,17 @@ impl ProcessManager {
             return Err(SystemError::EINVAL);
         }
 
-        // TODO: 处理CLONE_PARENT 与 SIGNAL_UNKILLABLE的情况
+        // 当全局init进程或其容器内init进程的兄弟进程退出时,由于它们的父进程(swapper)不会回收它们,
+        // 这些进程会保持僵尸状态。为了避免这种情况以及防止出现多根进程树,
+        // 需要阻止全局init和容器内init进程创建兄弟进程。
+        if clone_flags.contains(CloneFlags::CLONE_PARENT)
+            && current_pcb
+                .sig_info_irqsave()
+                .flags
+                .contains(SignalFlags::UNKILLABLE)
+        {
+            return Err(SystemError::EINVAL);
+        }
 
         // 如果新进程使用不同的 pid 或 namespace,
         // 则不允许它与分叉任务共享线程组。
@@ -396,6 +422,9 @@ impl ProcessManager {
             pcb.thread.write_irqsave().set_child_tid = Some(clone_args.child_tid);
         }
 
+        // 标记当前线程还未被执行exec
+        pcb.flags().insert(ProcessFlags::FORKNOEXEC);
+
         // 将子进程/线程的id存储在用户态传进的地址中
         if clone_flags.contains(CloneFlags::CLONE_PARENT_SETTID) {
             let mut writer = UserBufferWriter::new(
@@ -404,13 +433,13 @@ impl ProcessManager {
                 true,
             )?;
 
-            writer.copy_one_to_user(&(pcb.pid().0 as i32), 0)?;
+            writer.copy_one_to_user(&(pcb.raw_pid().0 as i32), 0)?;
         }
 
         sched_fork(pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to set sched info from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -418,7 +447,7 @@ impl ProcessManager {
         Self::copy_flags(&clone_flags, pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy flags from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -426,7 +455,7 @@ impl ProcessManager {
         Self::copy_mm(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy mm from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -434,7 +463,7 @@ impl ProcessManager {
         Self::copy_files(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy files from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -442,7 +471,15 @@ impl ProcessManager {
         Self::copy_sighand(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy sighand from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
+            )
+        });
+
+        // 拷贝namespace
+        Self::copy_namespaces(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| {
+            panic!(
+                "fork: Failed to copy namespaces from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -450,7 +487,7 @@ impl ProcessManager {
         Self::copy_thread(current_pcb, pcb, &clone_args, current_trapframe).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy thread from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
@@ -485,7 +522,7 @@ impl ProcessManager {
                 if unlikely(leader.is_none()) {
                     panic!(
                         "fork: Failed to get leader of current process, current pid: [{:?}]",
-                        current_pcb.pid()
+                        current_pcb.raw_pid()
                     );
                 }
 
@@ -501,67 +538,97 @@ impl ProcessManager {
                 .store(clone_args.exit_signal, Ordering::SeqCst);
         }
 
-        // todo: 增加线程组相关的逻辑。 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/fork.c#2437
-
-        Self::copy_group(current_pcb, pcb).unwrap_or_else(|e| {
-            panic!(
-                "fork: Failed to set the process group for the new pcb, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
-            )
-        });
-
         Self::copy_fs(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to copy fs from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}",
-                current_pcb.pid(), pcb.pid(), e
+                current_pcb.raw_pid(), pcb.raw_pid(), e
             )
         });
 
-        sched_cgroup_fork(pcb);
+        if pcb.raw_pid() == RawPid::UNASSIGNED {
+            // 分层PID分配:在父进程的子PID namespace中为新任务分配PID
+            let ns = pcb.nsproxy().pid_namespace_for_children().clone();
 
-        Ok(())
-    }
+            let main_pid_arc = alloc_pid(&ns).expect("alloc_pid failed");
 
-    /// 拷贝进程组信息
-    ///
-    /// ## 参数
-    ///
-    /// `parent_pcb` - 父进程
-    /// `child_pcb` - 子进程
-    /// ## 返回值
-    ///
-    /// 无
-    fn copy_group(
-        parent_pcb: &Arc<ProcessControlBlock>,
-        child_pcb: &Arc<ProcessControlBlock>,
-    ) -> Result<(), SystemError> {
-        if parent_pcb.process_group().is_none() && parent_pcb.pid() == Pid(0) {
-            return Ok(());
-        }
-        let pg = parent_pcb.process_group().unwrap();
+            // 根namespace中的PID号作为RawPid
+            let root_pid_nr = main_pid_arc
+                .first_upid()
+                .expect("UPid list empty")
+                .nr
+                .data();
+            // log::debug!("fork: root_pid_nr: {}", root_pid_nr);
 
-        let mut pg_inner = pg.process_group_inner.lock();
+            unsafe {
+                pcb.force_set_raw_pid(RawPid(root_pid_nr));
+            }
+            pcb.init_task_pid(PidType::PID, main_pid_arc);
+        }
 
-        let mut children_writelock = parent_pcb.children.write();
+        // 将当前pcb加入父进程的子进程哈希表中
+        if pcb.raw_pid() > RawPid(1) {
+            if let Some(ppcb_arc) = pcb.parent_pcb.read_irqsave().upgrade() {
+                let mut children = ppcb_arc.children.write_irqsave();
+                children.push(pcb.raw_pid());
+            } else {
+                panic!("parent pcb is None");
+            }
+        }
 
-        children_writelock.push(child_pcb.pid());
+        if pcb.raw_pid() > RawPid(0) {
+            ProcessManager::add_pcb(pcb.clone());
+        }
 
-        pg_inner
-            .processes
-            .insert(child_pcb.pid(), child_pcb.clone());
+        let pid = pcb.pid();
+        if pcb.is_thread_group_leader() {
+            if pcb.raw_pid() == RawPid(1) {
+                pcb.init_task_pid(PidType::TGID, pid.clone());
+                pcb.init_task_pid(PidType::PGID, pid.clone());
+                pcb.init_task_pid(PidType::SID, pid.clone());
+            } else {
+                pcb.init_task_pid(PidType::TGID, pid.clone());
+                pcb.init_task_pid(PidType::PGID, current_pcb.task_pgrp().unwrap());
+                pcb.init_task_pid(PidType::SID, current_pcb.task_session().unwrap());
+            }
 
-        // 检查是否已经存在pgid和sid
-        let pgid = Pgid::new(child_pcb.pid().0);
-        let sid = Sid::new(pgid.into());
+            if pid.is_child_reaper() {
+                pid.ns_of_pid().set_child_reaper(Arc::downgrade(pcb));
+                pcb.sig_info_mut().flags.insert(SignalFlags::UNKILLABLE);
+            }
 
-        if ProcessManager::find_process_group(pgid).is_some() {
-            ProcessManager::remove_process_group(pgid);
-        }
-        if ProcessManager::find_session(sid).is_some() {
-            ProcessManager::remove_session(sid);
+            let parent_siginfo = current_pcb.sig_info_irqsave();
+            let parent_tty = parent_siginfo.tty();
+            let parent_has_child_subreaper = parent_siginfo.has_child_subreaper();
+            let parent_is_child_reaper = parent_siginfo.is_child_subreaper();
+            drop(parent_siginfo);
+            let mut sig_info_guard = pcb.sig_info_mut();
+
+            // log::debug!("set tty: {:?}", parent_tty);
+            sig_info_guard.set_tty(parent_tty);
+
+            /*
+             * Inherit has_child_subreaper flag under the same
+             * tasklist_lock with adding child to the process tree
+             * for propagate_has_child_subreaper optimization.
+             */
+            sig_info_guard
+                .set_has_child_subreaper(parent_has_child_subreaper || parent_is_child_reaper);
+            drop(sig_info_guard);
+            pcb.attach_pid(PidType::TGID);
+            pcb.attach_pid(PidType::PGID);
+            pcb.attach_pid(PidType::SID);
+        } else {
+            pcb.task_join_group_stop();
+            let group_leader = pcb.threads_read_irqsave().group_leader().unwrap();
+            group_leader
+                .threads_write_irqsave()
+                .group_tasks
+                .push(Arc::downgrade(pcb));
         }
 
-        child_pcb.set_process_group(&pg);
+        pcb.attach_pid(PidType::PID);
+
+        sched_cgroup_fork(pcb);
 
         Ok(())
     }
@@ -582,3 +649,19 @@ impl ProcessManager {
         Ok(())
     }
 }
+
+impl ProcessControlBlock {
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/fork.c#1959
+    pub(super) fn init_task_pid(&self, pid_type: PidType, pid: Arc<Pid>) {
+        // log::debug!(
+        //     "init_task_pid: pid_type: {:?}, raw_pid:{}",
+        //     pid_type,
+        //     self.raw_pid().data()
+        // );
+        if pid_type == PidType::PID {
+            self.thread_pid.write().replace(pid);
+        } else {
+            self.sig_struct().pids[pid_type as usize] = Some(pid);
+        }
+    }
+}

+ 8 - 8
kernel/src/process/kthread.rs

@@ -22,7 +22,7 @@ use crate::{
     sched::{schedule, SchedMode},
 };
 
-use super::{fork::CloneFlags, Pid, ProcessControlBlock, ProcessFlags};
+use super::{fork::CloneFlags, ProcessControlBlock, ProcessFlags, RawPid};
 
 /// 内核线程的创建任务列表
 static KTHREAD_CREATE_LIST: SpinLock<LinkedList<Arc<KernelThreadCreateInfo>>> =
@@ -263,7 +263,7 @@ pub struct KernelThreadMechanism;
 
 impl KernelThreadMechanism {
     pub fn init_stage1() {
-        assert!(ProcessManager::current_pcb().pid() == Pid::new(0));
+        assert!(ProcessManager::current_pcb().raw_pid() == RawPid::new(0));
         info!("Initializing kernel thread mechanism stage1...");
 
         // 初始化第一个内核线程
@@ -311,12 +311,12 @@ impl KernelThreadMechanism {
             let info = KernelThreadCreateInfo::new(closure, "kthreadd".to_string());
             info.set_to_mark_sleep(false)
                 .expect("kthreadadd should be run first");
-            let kthreadd_pid: Pid = Self::__inner_create(
+            let kthreadd_pid: RawPid = Self::__inner_create(
                 &info,
                 CloneFlags::CLONE_VM | CloneFlags::CLONE_FS | CloneFlags::CLONE_SIGHAND,
             )
             .expect("Failed to create kthread daemon");
-            let pcb = ProcessManager::find(kthreadd_pid).unwrap();
+            let pcb = ProcessManager::find_task_by_vpid(kthreadd_pid).unwrap();
             ProcessManager::wakeup(&pcb).expect("Failed to wakeup kthread daemon");
             unsafe {
                 KTHREAD_DAEMON_PCB.replace(pcb);
@@ -366,7 +366,7 @@ impl KernelThreadMechanism {
     ) -> Option<Arc<ProcessControlBlock>> {
         let pcb = Self::create(func, name)?;
         ProcessManager::wakeup(&pcb)
-            .unwrap_or_else(|_| panic!("Failed to wakeup kthread: {:?}", pcb.pid()));
+            .unwrap_or_else(|_| panic!("Failed to wakeup kthread: {:?}", pcb.raw_pid()));
         return Some(pcb);
     }
 
@@ -387,7 +387,7 @@ impl KernelThreadMechanism {
         assert!(
             worker_private.is_some(),
             "kthread stop: worker_private is none, pid: {:?}",
-            pcb.pid()
+            pcb.raw_pid()
         );
         worker_private
             .as_mut()
@@ -434,7 +434,7 @@ impl KernelThreadMechanism {
         assert!(
             worker_private.is_some(),
             "kthread should_stop: worker_private is none, pid: {:?}",
-            pcb.pid()
+            pcb.raw_pid()
         );
         return worker_private
             .as_ref()
@@ -464,7 +464,7 @@ impl KernelThreadMechanism {
             while let Some(info) = list.pop_front() {
                 drop(list);
                 // create a new kernel thread
-                let result: Result<Pid, SystemError> = Self::__inner_create(
+                let result: Result<RawPid, SystemError> = Self::__inner_create(
                     &info,
                     CloneFlags::CLONE_VM | CloneFlags::CLONE_FS | CloneFlags::CLONE_SIGHAND,
                 );

+ 251 - 100
kernel/src/process/mod.rs

@@ -16,8 +16,8 @@ use alloc::{
 use cred::INIT_CRED;
 use hashbrown::HashMap;
 use log::{debug, error, info, warn};
-use process_group::{Pgid, ProcessGroup, ALL_PROCESS_GROUP};
-use session::{Session, Sid, ALL_SESSION};
+use pid::{alloc_pid, Pid, PidLink, PidType};
+use process_group::Pgid;
 use system_error::SystemError;
 
 use crate::{
@@ -36,7 +36,7 @@ use crate::{
     },
     ipc::{
         signal::RestartBlock,
-        signal_types::{SigInfo, SigPending, SignalStruct},
+        signal_types::{SigInfo, SigPending, SignalFlags, SignalStruct},
     },
     libs::{
         align::AlignedBox,
@@ -46,8 +46,7 @@ use crate::{
             futex::{Futex, RobustListHead},
         },
         lock_free_flags::LockFreeFlags,
-        mutex::Mutex,
-        rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
+        rwlock::{RwLock, RwLockReadGuard, RwLockUpgradableGuard, RwLockWriteGuard},
         spinlock::{SpinLock, SpinLockGuard},
         wait_queue::WaitQueue,
     },
@@ -72,6 +71,7 @@ use crate::{
 use timer::AlarmTimer;
 
 use self::{cred::Cred, kthread::WorkerPrivate};
+use crate::process::namespace::nsproxy::NsProxy;
 
 pub mod abi;
 pub mod cred;
@@ -82,17 +82,20 @@ pub mod fork;
 pub mod geteuid;
 pub mod idle;
 pub mod kthread;
+pub mod namespace;
 pub mod pid;
 pub mod process_group;
 pub mod resource;
 pub mod session;
+pub mod signal;
 pub mod stdio;
 pub mod syscall;
 pub mod timer;
 pub mod utils;
 
 /// 系统中所有进程的pcb
-static ALL_PROCESS: SpinLock<Option<HashMap<Pid, Arc<ProcessControlBlock>>>> = SpinLock::new(None);
+static ALL_PROCESS: SpinLock<Option<HashMap<RawPid, Arc<ProcessControlBlock>>>> =
+    SpinLock::new(None);
 
 pub static mut PROCESS_SWITCH_RESULT: Option<PerCpuVar<SwitchResult>> = None;
 
@@ -138,8 +141,6 @@ impl ProcessManager {
         };
 
         ALL_PROCESS.lock_irqsave().replace(HashMap::new());
-        ALL_PROCESS_GROUP.lock_irqsave().replace(HashMap::new());
-        ALL_SESSION.lock_irqsave().replace(HashMap::new());
         Self::init_switch_result();
         Self::arch_init();
         debug!("process arch init done.");
@@ -180,12 +181,12 @@ impl ProcessManager {
     /// 获取当前进程的pid
     ///
     /// 如果进程管理器未初始化完成,那么返回0
-    pub fn current_pid() -> Pid {
+    pub fn current_pid() -> RawPid {
         if unlikely(unsafe { !__PROCESS_MANAGEMENT_INIT_DONE }) {
-            return Pid(0);
+            return RawPid(0);
         }
 
-        return ProcessManager::current_pcb().pid();
+        return ProcessManager::current_pcb().raw_pid();
     }
 
     /// 增加当前进程的锁持有计数
@@ -213,7 +214,7 @@ impl ProcessManager {
     /// ## 返回值
     ///
     /// 如果找到了对应的进程,那么返回该进程的pcb,否则返回None
-    pub fn find(pid: Pid) -> Option<Arc<ProcessControlBlock>> {
+    pub fn find(pid: RawPid) -> Option<Arc<ProcessControlBlock>> {
         return ALL_PROCESS.lock_irqsave().as_ref()?.get(&pid).cloned();
     }
 
@@ -231,11 +232,11 @@ impl ProcessManager {
             .lock_irqsave()
             .as_mut()
             .unwrap()
-            .insert(pcb.pid(), pcb.clone());
+            .insert(pcb.raw_pid(), pcb.clone());
     }
 
     /// ### 获取所有进程的pid
-    pub fn get_all_processes() -> Vec<Pid> {
+    pub fn get_all_processes() -> Vec<RawPid> {
         let mut pids = Vec::new();
         for (pid, _) in ALL_PROCESS.lock_irqsave().as_ref().unwrap().iter() {
             pids.push(*pid);
@@ -373,11 +374,13 @@ impl ProcessManager {
         }
         return Err(SystemError::EINTR);
     }
+
     /// 当子进程退出后向父进程发送通知
+    #[inline(never)]
     fn exit_notify() {
         let current = ProcessManager::current_pcb();
         // 让INIT进程收养所有子进程
-        if current.pid() != Pid(1) {
+        if current.raw_pid() != RawPid(1) {
             unsafe {
                 current
                     .adopt_childen()
@@ -388,15 +391,16 @@ impl ProcessManager {
                 return;
             }
             let parent_pcb = r.unwrap();
-            let r = crate::ipc::kill::kill_process(parent_pcb.pid(), Signal::SIGCHLD);
-            if r.is_err() {
+
+            let r = crate::ipc::kill::kill_process_by_pcb(parent_pcb.clone(), Signal::SIGCHLD);
+            if let Err(e) = r {
                 warn!(
-                    "failed to send kill signal to {:?}'s parent pcb {:?}",
-                    current.pid(),
-                    parent_pcb.pid()
+                    "failed to send kill signal to {:?}'s parent pcb {:?}: {:?}",
+                    current.raw_pid(),
+                    parent_pcb.raw_pid(),
+                    e
                 );
             }
-            // todo: 这里需要向父进程发送SIGCHLD信号
             // todo: 这里还需要根据线程组的信息,决定信号的发送
         }
     }
@@ -414,7 +418,7 @@ impl ProcessManager {
     pub fn exit(exit_code: usize) -> ! {
         // 检查是否是init进程尝试退出,如果是则产生panic
         let current_pcb = ProcessManager::current_pcb();
-        if current_pcb.pid() == Pid(1) {
+        if current_pcb.raw_pid() == RawPid(1) {
             log::error!(
                 "Init process (pid=1) attempted to exit with code {}. This should not happen and indicates a serious system error.",
                 exit_code
@@ -428,7 +432,8 @@ impl ProcessManager {
         // 关中断
         let _irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
 
-        let pid: Pid;
+        let pid: Arc<Pid>;
+        let raw_pid = ProcessManager::current_pid();
         {
             let pcb = ProcessManager::current_pcb();
             pid = pcb.pid();
@@ -483,20 +488,24 @@ impl ProcessManager {
                 }
             }
             pcb.sig_info_mut().set_tty(None);
-            pcb.clear_pg_and_session_reference();
             drop(pcb);
             ProcessManager::exit_notify();
         }
 
         __schedule(SchedMode::SM_NONE);
-        error!("pid {pid:?} exited but sched again!");
+        error!("raw_pid {raw_pid:?} exited but sched again!");
         #[allow(clippy::empty_loop)]
         loop {
             spin_loop();
         }
     }
 
-    pub unsafe fn release(pid: Pid) {
+    /// 从全局进程列表中删除一个进程
+    ///
+    /// # 参数
+    ///
+    /// - `pid` : 进程的**全局** pid
+    pub(super) unsafe fn release(pid: RawPid) {
         let pcb = ProcessManager::find(pid);
         if pcb.is_some() {
             // log::debug!("release pid {}", pid);
@@ -557,7 +566,7 @@ impl ProcessManager {
         let cpu_id = pcb.sched_info().on_cpu();
 
         if let Some(cpu_id) = cpu_id {
-            if pcb.pid() == cpu_rq(cpu_id.data() as usize).current().pid() {
+            if pcb.raw_pid() == cpu_rq(cpu_id.data() as usize).current().raw_pid() {
                 kick_cpu(cpu_id).expect("ProcessManager::kick(): Failed to kick cpu");
             }
         }
@@ -578,14 +587,25 @@ pub unsafe fn switch_finish_hook() {
     ProcessManager::switch_finish_hook();
 }
 
-int_like!(Pid, AtomicPid, usize, AtomicUsize);
+int_like!(RawPid, AtomicRawPid, usize, AtomicUsize);
 
-impl fmt::Display for Pid {
+impl fmt::Display for RawPid {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{}", self.0)
     }
 }
 
+impl RawPid {
+    /// 该RawPid暂未分配,待会会初始化它。
+    /// 这个状态只应当出现在进程/线程创建的过程中
+    pub const UNASSIGNED: RawPid = RawPid(usize::MAX - 1);
+    pub const MAX_VALID: RawPid = RawPid(usize::MAX - 32);
+
+    pub fn is_valid(&self) -> bool {
+        self.0 >= Self::MAX_VALID.0
+    }
+}
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
 pub enum ProcessState {
     /// The process is running on a CPU or in a run queue.
@@ -668,6 +688,8 @@ bitflags! {
         const HAS_PENDING_SIGNAL = 1 << 9;
         /// 进程需要恢复之前保存的信号掩码
         const RESTORE_SIG_MASK = 1 << 10;
+        /// Forked but didn't exec
+        const FORKNOEXEC = 1 << 11;
     }
 }
 
@@ -694,9 +716,17 @@ impl ProcessFlags {
 #[derive(Debug)]
 pub struct ProcessControlBlock {
     /// 当前进程的pid
-    pid: Pid,
+    pid: RawPid,
     /// 当前进程的线程组id(这个值在同一个线程组内永远不变)
-    tgid: Pid,
+    tgid: RawPid,
+
+    thread_pid: RwLock<Option<Arc<Pid>>>,
+    /// PID链接数组
+    pid_links: [PidLink; PidType::PIDTYPE_MAX],
+
+    /// namespace代理
+    nsproxy: RwLock<Arc<NsProxy>>,
+
     basic: RwLock<ProcessBasicInfo>,
     /// 当前进程的自旋锁持有计数
     preempt_count: AtomicUsize,
@@ -726,7 +756,7 @@ pub struct ProcessControlBlock {
     real_parent_pcb: RwLock<Weak<ProcessControlBlock>>,
 
     /// 子进程链表
-    children: RwLock<Vec<Pid>>,
+    children: RwLock<Vec<RawPid>>,
 
     /// 等待队列
     wait_queue: WaitQueue,
@@ -744,14 +774,11 @@ pub struct ProcessControlBlock {
     robust_list: RwLock<Option<RobustListHead>>,
 
     /// 进程作为主体的凭证集
-    cred: SpinLock<Cred>,
+    cred: SpinLock<Arc<Cred>>,
     self_ref: Weak<ProcessControlBlock>,
 
     restart_block: SpinLock<Option<RestartBlock>>,
 
-    /// 进程组
-    process_group: Mutex<Weak<ProcessGroup>>,
-
     /// 进程的可执行文件路径
     executable_path: RwLock<String>,
 }
@@ -792,17 +819,41 @@ impl ProcessControlBlock {
 
     #[inline(never)]
     fn do_create_pcb(name: String, kstack: KernelStack, is_idle: bool) -> Arc<Self> {
-        let (pid, ppid, cwd, cred, tty) = if is_idle {
+        // 初始化namespace代理
+        let nsproxy = if is_idle {
+            // idle进程使用root namespace
+            NsProxy::new_root()
+        } else {
+            // 其他进程继承父进程的namespace
+            ProcessManager::current_pcb().nsproxy().clone()
+        };
+
+        let (raw_pid, ppid, cwd, cred, tty): (
+            RawPid,
+            RawPid,
+            String,
+            Arc<Cred>,
+            Option<Arc<TtyCore>>,
+        ) = if is_idle {
             let cred = INIT_CRED.clone();
-            (Pid(0), Pid(0), "/".to_string(), cred, None)
+            (RawPid(0), RawPid(0), "/".to_string(), cred, None)
         } else {
-            let ppid = ProcessManager::current_pcb().pid();
-            let mut cred = ProcessManager::current_pcb().cred();
-            cred.cap_permitted = cred.cap_ambient;
-            cred.cap_effective = cred.cap_ambient;
+            let ppid = ProcessManager::current_pcb().task_pid_vnr();
+            let cred = ProcessManager::current_pcb().cred();
+            if cred.cap_ambient != cred.cap_permitted || cred.cap_ambient != cred.cap_effective {
+                todo!("create a new cred for child.")
+                //     cred.cap_permitted = cred.cap_ambient;
+                // cred.cap_effective = cred.cap_ambient;
+            }
+
             let cwd = ProcessManager::current_pcb().basic().cwd();
             let tty = ProcessManager::current_pcb().sig_info_irqsave().tty();
-            (Self::generate_pid(), ppid, cwd, cred, tty)
+
+            // Here, UNASSIGNED is used to represent an unallocated pid,
+            // which will be allocated later in `copy_process`.
+            let raw_pid = RawPid::UNASSIGNED;
+
+            (raw_pid, ppid, cwd, cred, tty)
         };
 
         let basic_info = ProcessBasicInfo::new(ppid, name.clone(), cwd, None);
@@ -811,16 +862,20 @@ impl ProcessControlBlock {
 
         let sched_info = ProcessSchedulerInfo::new(None);
 
-        let ppcb: Weak<ProcessControlBlock> = ProcessManager::find(ppid)
+        let ppcb: Weak<ProcessControlBlock> = ProcessManager::find_task_by_vpid(ppid)
             .map(|p| Arc::downgrade(&p))
             .unwrap_or_default();
 
         // 使用 Arc::new_cyclic 避免在栈上创建巨大的结构体
         let pcb = Arc::new_cyclic(|weak| {
             let arch_info = SpinLock::new(ArchPCBInfo::new(&kstack));
+
             let pcb = Self {
-                pid,
-                tgid: pid,
+                pid: raw_pid,
+                tgid: raw_pid,
+                thread_pid: RwLock::new(None),
+                pid_links: core::array::from_fn(|_| PidLink::default()),
+                nsproxy: RwLock::new(nsproxy),
                 basic: basic_info,
                 preempt_count,
                 flags,
@@ -843,7 +898,6 @@ impl ProcessControlBlock {
                 cred: SpinLock::new(cred),
                 self_ref: weak.clone(),
                 restart_block: SpinLock::new(None),
-                process_group: Mutex::new(Weak::new()),
                 executable_path: RwLock::new(name),
             };
 
@@ -875,45 +929,9 @@ impl ProcessControlBlock {
                 .unwrap()
         };
 
-        // 将当前pcb加入父进程的子进程哈希表中
-        if pcb.pid() > Pid(1) {
-            if let Some(ppcb_arc) = pcb.parent_pcb.read_irqsave().upgrade() {
-                let mut children = ppcb_arc.children.write_irqsave();
-                children.push(pcb.pid());
-            } else {
-                panic!("parent pcb is None");
-            }
-        }
-
-        if pcb.pid() > Pid(0) && !is_idle {
-            let process_group = ProcessGroup::new(pcb.clone());
-            *pcb.process_group.lock() = Arc::downgrade(&process_group);
-            ProcessManager::add_process_group(process_group.clone());
-
-            let session = Session::new(process_group.clone());
-            process_group.process_group_inner.lock().session = Arc::downgrade(&session);
-            session.session_inner.lock().leader = Some(pcb.clone());
-            ProcessManager::add_session(session);
-
-            ProcessManager::add_pcb(pcb.clone());
-        }
-        // log::debug!(
-        //     "A new process is created, pid: {:?}, pgid: {:?}, sid: {:?}",
-        //     pcb.pid(),
-        //     pcb.process_group().unwrap().pgid(),
-        //     pcb.session().unwrap().sid()
-        // );
-
         return pcb;
     }
 
-    /// 生成一个新的pid
-    #[inline(always)]
-    fn generate_pid() -> Pid {
-        static NEXT_PID: AtomicPid = AtomicPid::new(Pid(1));
-        return NEXT_PID.fetch_add(Pid(1), Ordering::SeqCst);
-    }
-
     /// 返回当前进程的锁持有计数
     #[inline(always)]
     pub fn preempt_count(&self) -> usize {
@@ -938,7 +956,7 @@ impl ProcessControlBlock {
     }
 
     #[inline(always)]
-    pub fn contain_child(&self, pid: &Pid) -> bool {
+    pub fn contain_child(&self, pid: &RawPid) -> bool {
         let children = self.children.read();
         return children.contains(pid);
     }
@@ -1010,15 +1028,10 @@ impl ProcessControlBlock {
     }
 
     #[inline(always)]
-    pub fn pid(&self) -> Pid {
+    pub fn raw_pid(&self) -> RawPid {
         return self.pid;
     }
 
-    #[inline(always)]
-    pub fn tgid(&self) -> Pid {
-        return self.tgid;
-    }
-
     #[inline(always)]
     pub fn fs_struct(&self) -> Arc<FsStruct> {
         self.fs.read().clone()
@@ -1039,7 +1052,7 @@ impl ProcessControlBlock {
     }
 
     #[inline(always)]
-    pub fn cred(&self) -> Cred {
+    pub fn cred(&self) -> Arc<Cred> {
         self.cred.lock().clone()
     }
 
@@ -1051,6 +1064,15 @@ impl ProcessControlBlock {
         self.executable_path.read().clone()
     }
 
+    pub fn real_parent_pcb(&self) -> Option<Arc<ProcessControlBlock>> {
+        return self.real_parent_pcb.read_irqsave().upgrade();
+    }
+
+    /// 判断当前进程是否是全局的init进程
+    pub fn is_global_init(&self) -> bool {
+        self.task_tgid_vnr().unwrap() == RawPid(1)
+    }
+
     /// 根据文件描述符序号,获取socket对象的Arc指针
     ///
     /// ## 参数
@@ -1079,12 +1101,53 @@ impl ProcessControlBlock {
 
     /// 当前进程退出时,让初始进程收养所有子进程
     unsafe fn adopt_childen(&self) -> Result<(), SystemError> {
-        match ProcessManager::find(Pid(1)) {
+        match ProcessManager::find_task_by_vpid(RawPid(1)) {
             Some(init_pcb) => {
                 let childen_guard = self.children.write();
+                if Arc::ptr_eq(&self.self_ref.upgrade().unwrap(), &init_pcb) {
+                    // 当前进程是namespace的init进程,由父进程所在的pidns的init进程去收养子进程
+                    if let Some(parent_pcb) = self.real_parent_pcb() {
+                        assert!(
+                            !Arc::ptr_eq(&parent_pcb, &init_pcb),
+                            "adopt_childen: parent_pcb is init_pcb, pid: {}",
+                            self.raw_pid()
+                        );
+                        let parent_init = ProcessManager::find_task_by_pid_ns(
+                            RawPid(1),
+                            &parent_pcb.active_pid_ns(),
+                        );
+                        if parent_init.is_none() {
+                            log::warn!(
+                                "adopt_childen: parent_init is None, pid: {}",
+                                self.raw_pid()
+                            );
+                            return Ok(());
+                        }
+                        let parent_init = parent_init.unwrap();
+                        let mut parent_children_guard = parent_init.children.write();
+                        childen_guard.iter().for_each(|pid| {
+                            log::debug!(
+                                "adopt_childen: pid {} is adopted by parent init pid {}",
+                                pid,
+                                parent_init.raw_pid()
+                            );
+                            parent_children_guard.push(*pid);
+                        });
+
+                        return Ok(());
+                    } else {
+                        log::warn!("adopt_childen: parent_pcb is None, pid: {}", self.raw_pid());
+                        return Ok(());
+                    }
+                }
                 let mut init_childen_guard = init_pcb.children.write();
 
                 childen_guard.iter().for_each(|pid| {
+                    log::debug!(
+                        "adopt_childen: pid {} is adopted by init pid {}",
+                        pid,
+                        init_pcb.raw_pid()
+                    );
                     init_childen_guard.push(*pid);
                 });
 
@@ -1108,6 +1171,10 @@ impl ProcessControlBlock {
         self.sig_info.read_irqsave()
     }
 
+    pub fn sig_info_upgradable(&self) -> RwLockUpgradableGuard<ProcessSignalInfo> {
+        self.sig_info.upgradeable_read_irqsave()
+    }
+
     pub fn try_siginfo_irqsave(&self, times: u8) -> Option<RwLockReadGuard<ProcessSignalInfo>> {
         for _ in 0..times {
             if let Some(r) = self.sig_info.try_read_irqsave() {
@@ -1205,7 +1272,7 @@ impl ProcessControlBlock {
         drop(old)
     }
 
-    pub fn children_read_irqsave(&self) -> RwLockReadGuard<Vec<Pid>> {
+    pub fn children_read_irqsave(&self) -> RwLockReadGuard<Vec<RawPid>> {
         self.children.read_irqsave()
     }
 
@@ -1213,6 +1280,10 @@ impl ProcessControlBlock {
         self.thread.read_irqsave()
     }
 
+    pub fn threads_write_irqsave(&self) -> RwLockWriteGuard<ThreadInfo> {
+        self.thread.write_irqsave()
+    }
+
     pub fn restart_block(&self) -> SpinLockGuard<Option<RestartBlock>> {
         self.restart_block.lock()
     }
@@ -1235,19 +1306,50 @@ impl ProcessControlBlock {
             .state()
             .is_exited()
     }
+
+    pub fn exit_code(&self) -> Option<usize> {
+        self.sched_info
+            .inner_lock_read_irqsave()
+            .state()
+            .exit_code()
+    }
+
+    /// 获取进程的namespace代理
+    pub fn nsproxy(&self) -> Arc<NsProxy> {
+        self.nsproxy.read().clone()
+    }
+
+    /// 设置进程的namespace代理
+    ///
+    /// ## 参数
+    /// - `nsproxy` : 新的namespace代理
+    ///
+    /// ## 返回值
+    /// 返回旧的namespace代理
+    pub fn set_nsproxy(&self, nsproxy: Arc<NsProxy>) -> Arc<NsProxy> {
+        let mut guard = self.nsproxy.write();
+        let old = guard.clone();
+        *guard = nsproxy;
+        return old;
+    }
+
+    pub fn is_thread_group_leader(&self) -> bool {
+        self.exit_signal.load(Ordering::SeqCst) != Signal::INVALID
+    }
 }
 
 impl Drop for ProcessControlBlock {
     fn drop(&mut self) {
         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
+        // log::debug!("Drop ProcessControlBlock: pid: {}", self.raw_pid(),);
+        self.__exit_signal();
         // 在ProcFS中,解除进程的注册
-        procfs_unregister_pid(self.pid())
-            .unwrap_or_else(|e| panic!("procfs_unregister_pid failed: error: {e:?}"));
-
+        procfs_unregister_pid(self.raw_pid())
+            .unwrap_or_else(|e: SystemError| panic!("procfs_unregister_pid failed: error: {e:?}"));
         if let Some(ppcb) = self.parent_pcb.read_irqsave().upgrade() {
             ppcb.children
                 .write_irqsave()
-                .retain(|pid| *pid != self.pid());
+                .retain(|pid| *pid != self.raw_pid());
         }
 
         // log::debug!("Drop pid: {:?}", self.pid());
@@ -1265,6 +1367,9 @@ pub struct ThreadInfo {
     vfork_done: Option<Arc<Completion>>,
     /// 线程组的组长
     group_leader: Weak<ProcessControlBlock>,
+
+    /// 当前线程为组长时,该字段存储组内所有线程的pcb
+    group_tasks: Vec<Weak<ProcessControlBlock>>,
 }
 
 impl Default for ThreadInfo {
@@ -1280,12 +1385,24 @@ impl ThreadInfo {
             set_child_tid: None,
             vfork_done: None,
             group_leader: Weak::default(),
+            group_tasks: Vec::new(),
         }
     }
 
     pub fn group_leader(&self) -> Option<Arc<ProcessControlBlock>> {
         return self.group_leader.upgrade();
     }
+
+    pub fn thread_group_empty(&self) -> bool {
+        let group_leader = self.group_leader();
+        if let Some(leader) = group_leader {
+            if Arc::ptr_eq(&leader, &ProcessManager::current_pcb()) {
+                return self.group_tasks.is_empty();
+            }
+            return false;
+        }
+        return true;
+    }
 }
 
 /// 进程的基本信息
@@ -1294,7 +1411,7 @@ impl ThreadInfo {
 #[derive(Debug)]
 pub struct ProcessBasicInfo {
     /// 当前进程的父进程的pid
-    ppid: Pid,
+    ppid: RawPid,
     /// 进程的名字
     name: String,
 
@@ -1311,7 +1428,7 @@ pub struct ProcessBasicInfo {
 impl ProcessBasicInfo {
     #[inline(never)]
     pub fn new(
-        ppid: Pid,
+        ppid: RawPid,
         name: String,
         cwd: String,
         user_vm: Option<Arc<AddressSpace>>,
@@ -1326,7 +1443,7 @@ impl ProcessBasicInfo {
         });
     }
 
-    pub fn ppid(&self) -> Pid {
+    pub fn ppid(&self) -> RawPid {
         return self.ppid;
     }
 
@@ -1863,8 +1980,18 @@ pub struct ProcessSignalInfo {
     sig_pending: SigPending,
     // sig_shared_pending 中存储当前线程所属进程要处理的信号
     sig_shared_pending: SigPending,
+    flags: SignalFlags,
     // 当前进程对应的tty
     tty: Option<Arc<TtyCore>>,
+    has_child_subreaper: bool,
+
+    /// 标记当前进程是否是一个“子进程收割者”
+    ///
+    /// todo: 在prctl里面实现设置这个标志位的功能
+    is_child_subreaper: bool,
+
+    /// boolean value for session group leader
+    pub is_session_leader: bool,
 }
 
 impl ProcessSignalInfo {
@@ -1908,6 +2035,10 @@ impl ProcessSignalInfo {
         self.tty = tty;
     }
 
+    pub fn flags(&self) -> SignalFlags {
+        self.flags
+    }
+
     /// 从 pcb 的 siginfo中取出下一个要处理的信号,先处理线程信号,再处理进程信号
     ///
     /// ## 参数
@@ -1929,6 +2060,22 @@ impl ProcessSignalInfo {
             return res;
         }
     }
+
+    pub fn has_child_subreaper(&self) -> bool {
+        self.has_child_subreaper
+    }
+
+    pub fn set_has_child_subreaper(&mut self, has_child_subreaper: bool) {
+        self.has_child_subreaper = has_child_subreaper;
+    }
+
+    pub fn is_child_subreaper(&self) -> bool {
+        self.is_child_subreaper
+    }
+
+    pub fn set_is_child_subreaper(&mut self, is_child_subreaper: bool) {
+        self.is_child_subreaper = is_child_subreaper;
+    }
 }
 
 impl Default for ProcessSignalInfo {
@@ -1939,6 +2086,10 @@ impl Default for ProcessSignalInfo {
             sig_pending: SigPending::default(),
             sig_shared_pending: SigPending::default(),
             tty: None,
+            has_child_subreaper: false,
+            is_child_subreaper: false,
+            is_session_leader: false,
+            flags: SignalFlags::empty(),
         }
     }
 }

+ 37 - 0
kernel/src/process/namespace/mod.rs

@@ -0,0 +1,37 @@
+pub mod nsproxy;
+pub mod pid_namespace;
+pub mod user_namespace;
+
+use nsproxy::NsCommon;
+
+/// Namespace 种类,用于运行时调试与 /proc 导出
+#[repr(u8)]
+#[allow(dead_code)]
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum NamespaceType {
+    Pid,
+    Mount,
+    Net,
+    Ipc,
+    Uts,
+    User,
+    Cgroup,
+    Time,
+}
+
+/// Namespace 通用操作接口
+#[allow(dead_code)]
+pub trait NamespaceOps: Send + Sync {
+    /// 返回公共字段,便于统一处理
+    fn ns_common(&self) -> &NsCommon;
+
+    /// 用于 debug /proc 导出
+    fn ty(&self) -> NamespaceType {
+        self.ns_common().ty()
+    }
+
+    /// 获取层级
+    fn level(&self) -> u32 {
+        self.ns_common().level()
+    }
+}

+ 179 - 0
kernel/src/process/namespace/nsproxy.rs

@@ -0,0 +1,179 @@
+use alloc::sync::Arc;
+use system_error::SystemError;
+
+use crate::process::{fork::CloneFlags, ProcessControlBlock, ProcessManager};
+use core::{fmt::Debug, intrinsics::likely};
+
+use super::{pid_namespace::PidNamespace, user_namespace::UserNamespace, NamespaceType};
+
+/// A structure containing references to all per-process namespaces (filesystem/mount, UTS, network, etc.).
+///
+/// The PID namespace here is specifically for child processes (accessed via `task_active_pid_ns`).
+///
+/// Namespace references are counted by the number of nsproxies pointing to them, not by the number of tasks.
+///
+/// The nsproxy is shared by tasks that share all namespaces. It will be copied when any namespace is cloned or unshared.
+#[derive(Clone)]
+pub struct NsProxy {
+    /// PID namespace(用于子进程)
+    pub pid_ns_for_children: Arc<PidNamespace>,
+    // 其他namespace(为未来扩展预留)
+    // pub mount_ns: Option<Arc<MountNamespace>>,
+    // pub user_ns: Option<Arc<UserNamespace>>,
+    // pub net_ns: Option<Arc<NetNamespace>>,
+    // pub ipc_ns: Option<Arc<IpcNamespace>>,
+    // pub uts_ns: Option<Arc<UtsNamespace>>,
+    // pub cgroup_ns: Option<Arc<CgroupNamespace>>,
+    // pub time_ns: Option<Arc<TimeNamespace>>,
+}
+
+impl Debug for NsProxy {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("NsProxy").finish()
+    }
+}
+
+impl NsProxy {
+    /// 创建root namespace代理
+    pub fn new_root() -> Arc<Self> {
+        let root_pid_ns = super::pid_namespace::INIT_PID_NAMESPACE.clone();
+        Arc::new(Self {
+            pid_ns_for_children: root_pid_ns,
+        })
+    }
+
+    /// 获取子进程的PID namespace
+    pub fn pid_namespace_for_children(&self) -> &Arc<PidNamespace> {
+        &self.pid_ns_for_children
+    }
+}
+
+impl ProcessManager {
+    /// 拷贝namespace
+    ///
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/nsproxy.c?r=&mo=3770&fi=151#151
+    #[inline(never)]
+    pub fn copy_namespaces(
+        clone_flags: &CloneFlags,
+        _parent_pcb: &Arc<ProcessControlBlock>,
+        child_pcb: &Arc<ProcessControlBlock>,
+    ) -> Result<(), SystemError> {
+        // log::debug!(
+        //     "copy_namespaces: clone_flags={:?}, parent pid={}, child pid={}, child name={}",
+        //     clone_flags,
+        //     parent_pcb.raw_pid(),
+        //     child_pcb.raw_pid(),
+        //     child_pcb.basic().name()
+        // );
+        if likely(!clone_flags.intersects(
+            CloneFlags::CLONE_NEWNS
+                | CloneFlags::CLONE_NEWUTS
+                | CloneFlags::CLONE_NEWIPC
+                | CloneFlags::CLONE_NEWPID
+                | CloneFlags::CLONE_NEWNET
+                | CloneFlags::CLONE_NEWCGROUP
+                | CloneFlags::CLONE_NEWTIME,
+        )) && clone_flags.contains(CloneFlags::CLONE_VM)
+        // || likely(parent_nsproxy.time_ns_for_children() == parent_nsproxy.time_ns())
+        {
+            // 由于在创建pcb的时候已经默认继承了parent的nsproxy,所以这里不需要做任何操作
+            return Ok(());
+        }
+
+        // todo: 这里要添加一个对user_namespace的处理
+        // https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/nsproxy.c?r=&mo=3770&fi=151#165
+
+        /*
+         * CLONE_NEWIPC must detach from the undolist: after switching
+         * to a new ipc namespace, the semaphore arrays from the old
+         * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
+         * means share undolist with parent, so we must forbid using
+         * it along with CLONE_NEWIPC.
+         */
+
+        if *clone_flags & (CloneFlags::CLONE_NEWIPC | CloneFlags::CLONE_SYSVSEM)
+            == (CloneFlags::CLONE_NEWIPC | CloneFlags::CLONE_SYSVSEM)
+        {
+            return Err(SystemError::EINVAL);
+        }
+        let user_ns = child_pcb.cred().user_ns.clone();
+        let new_ns = create_new_namespaces(clone_flags, child_pcb, user_ns)?;
+        // 设置新的nsproxy
+
+        child_pcb.set_nsproxy(new_ns);
+
+        Ok(())
+    }
+}
+
+/// 创建新的namespace代理及其所有关联的命名空间。
+///
+/// 返回新创建的nsproxy。调用者需要负责正确的加锁并将其附加到进程上。
+///
+/// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/nsproxy.c?r=&mo=3770&fi=151#67
+fn create_new_namespaces(
+    clone_flags: &CloneFlags,
+    pcb: &Arc<ProcessControlBlock>,
+    user_ns: Arc<UserNamespace>,
+) -> Result<Arc<NsProxy>, SystemError> {
+    let pid_ns_for_children = pcb
+        .nsproxy()
+        .pid_ns_for_children
+        .copy_pid_ns(clone_flags, user_ns)?;
+
+    let result = NsProxy {
+        pid_ns_for_children,
+    };
+
+    let result = Arc::new(result);
+    return Ok(result);
+}
+
+/// https://code.dragonos.org.cn/xref/linux-6.6.21/include/linux/ns_common.h#9
+/// 融合了 NamespaceBase 的公共字段
+#[derive(Debug)]
+pub struct NsCommon {
+    /// 层级(root = 0)
+    pub level: u32,
+    /// 种类
+    ty: NamespaceType,
+}
+
+impl NsCommon {
+    pub fn new(level: u32, ty: NamespaceType) -> Self {
+        Self { level, ty }
+    }
+
+    pub fn level(&self) -> u32 {
+        self.level
+    }
+
+    pub fn ty(&self) -> NamespaceType {
+        self.ty
+    }
+}
+
+impl Default for NsCommon {
+    fn default() -> Self {
+        Self::new(0, NamespaceType::Pid) // 默认值,实际使用时应该明确指定
+    }
+}
+
+/// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/nsproxy.c?fi=exec_task_namespaces#259
+pub fn exec_task_namespaces() -> Result<(), SystemError> {
+    let tsk = ProcessManager::current_pcb();
+    let user_ns = tsk.cred().user_ns.clone();
+    let new_nsproxy = create_new_namespaces(&CloneFlags::empty(), &tsk, user_ns)?;
+    // todo: time_ns的逻辑
+    switch_task_namespaces(&tsk, new_nsproxy)?;
+
+    return Ok(());
+}
+
+pub fn switch_task_namespaces(
+    tsk: &Arc<ProcessControlBlock>,
+    new_nsproxy: Arc<NsProxy>,
+) -> Result<(), SystemError> {
+    tsk.set_nsproxy(new_nsproxy);
+    Ok(())
+}

+ 220 - 0
kernel/src/process/namespace/pid_namespace.rs

@@ -0,0 +1,220 @@
+use alloc::sync::Weak;
+
+use alloc::{sync::Arc, vec::Vec};
+use hashbrown::HashMap;
+use ida::IdAllocator;
+use system_error::SystemError;
+
+use crate::libs::spinlock::SpinLock;
+use crate::libs::spinlock::SpinLockGuard;
+use crate::process::fork::CloneFlags;
+use crate::process::pid::Pid;
+use crate::process::ProcessControlBlock;
+use crate::process::ProcessManager;
+use crate::process::RawPid;
+
+use super::nsproxy::NsCommon;
+use super::user_namespace::UserNamespace;
+use super::{NamespaceOps, NamespaceType};
+
+lazy_static! {
+    pub static ref INIT_PID_NAMESPACE: Arc<PidNamespace> = PidNamespace::new_root();
+}
+
+pub struct PidNamespace {
+    ns_common: NsCommon,
+    self_ref: Weak<PidNamespace>,
+    /// 父namespace的弱引用
+    parent: Option<Weak<PidNamespace>>,
+    user_ns: Arc<UserNamespace>,
+
+    inner: SpinLock<InnerPidNamespace>,
+}
+
+pub struct InnerPidNamespace {
+    dead: bool,
+    ida: IdAllocator,
+    /// PID到进程的映射表
+    pid_map: HashMap<RawPid, Arc<Pid>>,
+    /// init进程引用
+    child_reaper: Option<Weak<ProcessControlBlock>>,
+    children: Vec<Arc<PidNamespace>>,
+}
+
+impl InnerPidNamespace {
+    pub fn dead(&self) -> bool {
+        self.dead
+    }
+
+    pub fn child_reaper(&self) -> &Option<Weak<ProcessControlBlock>> {
+        &self.child_reaper
+    }
+}
+
+impl NamespaceOps for PidNamespace {
+    fn ns_common(&self) -> &NsCommon {
+        &self.ns_common
+    }
+}
+
+impl PidNamespace {
+    /// 最大PID namespace层级
+    pub const MAX_PID_NS_LEVEL: u32 = 32;
+
+    /// 创建root PID namespace
+    fn new_root() -> Arc<Self> {
+        Arc::new_cyclic(|self_ref| Self {
+            self_ref: self_ref.clone(),
+            ns_common: NsCommon::new(0, NamespaceType::Pid),
+            parent: None,
+            user_ns: super::user_namespace::INIT_USER_NAMESPACE.clone(),
+            inner: SpinLock::new(InnerPidNamespace {
+                dead: false,
+                child_reaper: None,
+                ida: IdAllocator::new(1, usize::MAX).unwrap(),
+                pid_map: HashMap::new(),
+                children: Vec::new(),
+            }),
+        })
+    }
+
+    /// 获取层级
+    pub fn level(&self) -> u32 {
+        self.ns_common.level
+    }
+
+    pub fn alloc_pid_in_ns(&self, pid: Arc<Pid>) -> Result<RawPid, SystemError> {
+        let mut inner = self.inner();
+        let raw_pid = inner.do_alloc_pid_in_ns(pid)?;
+        Ok(raw_pid)
+    }
+
+    pub fn pid_allocated(&self) -> usize {
+        let inner = self.inner();
+        inner.do_pid_allocated()
+    }
+
+    pub fn release_pid_in_ns(&self, raw_pid: RawPid) {
+        let mut inner = self.inner();
+        inner.do_release_pid_in_ns(raw_pid);
+    }
+
+    pub fn find_pid_in_ns(&self, raw_pid: RawPid) -> Option<Arc<Pid>> {
+        let inner = self.inner();
+        inner.pid_map.get(&raw_pid).cloned()
+    }
+
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid_namespace.c#145
+    pub(super) fn copy_pid_ns(
+        &self,
+        clone_flags: &CloneFlags,
+        user_ns: Arc<UserNamespace>,
+    ) -> Result<Arc<Self>, SystemError> {
+        if !clone_flags.contains(CloneFlags::CLONE_NEWPID) {
+            return Ok(self.self_ref.upgrade().unwrap());
+        }
+        if !Arc::ptr_eq(
+            &ProcessManager::current_pcb().active_pid_ns(),
+            &self.self_ref.upgrade().unwrap(),
+        ) {
+            return Err(SystemError::EINVAL);
+        }
+
+        return self.create_pid_namespace(user_ns);
+    }
+
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid_namespace.c#72
+    fn create_pid_namespace(&self, user_ns: Arc<UserNamespace>) -> Result<Arc<Self>, SystemError> {
+        let level = self.level() + 1;
+        if !self.user_ns.is_ancestor_of(&user_ns) {
+            return Err(SystemError::EINVAL);
+        }
+
+        if level > Self::MAX_PID_NS_LEVEL {
+            return Err(SystemError::ENOSPC);
+        }
+
+        // todo: 补充ucount相关
+
+        let pidns = Arc::new_cyclic(|self_ref| Self {
+            self_ref: self_ref.clone(),
+            ns_common: NsCommon::new(level, NamespaceType::Pid),
+            parent: Some(self.self_ref.clone()),
+            user_ns,
+            inner: SpinLock::new(InnerPidNamespace {
+                child_reaper: None,
+                dead: false,
+                ida: IdAllocator::new(1, usize::MAX).unwrap(),
+                pid_map: HashMap::new(),
+                children: Vec::new(),
+            }),
+        });
+
+        // todo: procfs相关,申请inode号,赋值operations等
+
+        self.inner().children.push(pidns.clone());
+        return Ok(pidns);
+    }
+
+    pub fn inner(&self) -> SpinLockGuard<InnerPidNamespace> {
+        self.inner.lock()
+    }
+
+    pub fn child_reaper(&self) -> Option<Weak<ProcessControlBlock>> {
+        self.inner().child_reaper.clone()
+    }
+
+    pub fn set_child_reaper(&self, child_reaper: Weak<ProcessControlBlock>) {
+        self.inner().child_reaper = Some(child_reaper);
+    }
+
+    pub fn parent(&self) -> Option<Arc<PidNamespace>> {
+        self.parent.as_ref().and_then(|p| p.upgrade())
+    }
+
+    /// 从父namespace中删除当前PID namespace
+    pub fn delete_current_pidns_in_parent(&self) {
+        let current = self.self_ref.upgrade().unwrap();
+        if let Some(p) = self.parent() {
+            p.inner().children.retain(|c| !Arc::ptr_eq(c, &current));
+        }
+    }
+}
+
+impl InnerPidNamespace {
+    pub fn do_alloc_pid_in_ns(&mut self, pid: Arc<Pid>) -> Result<RawPid, SystemError> {
+        if self.dead {
+            return Err(SystemError::ESRCH);
+        }
+        let raw_pid = self.ida.alloc().ok_or(SystemError::ENOMEM)?;
+        let raw_pid = RawPid(raw_pid);
+        self.pid_map.insert(raw_pid, pid);
+        Ok(raw_pid)
+    }
+
+    pub fn do_release_pid_in_ns(&mut self, raw_pid: RawPid) {
+        // log::debug!("do_release_pid_in_ns: raw_pid={}", raw_pid);
+        self.pid_map.remove(&raw_pid);
+        self.ida.free(raw_pid.data());
+        if self.pid_map.is_empty() {
+            // 如果当前namespace中没有任何PID了,则标记为dead
+            self.dead = true;
+        }
+    }
+
+    pub fn do_pid_allocated(&self) -> usize {
+        self.pid_map.len()
+    }
+}
+
+impl Drop for PidNamespace {
+    fn drop(&mut self) {
+        // log::debug!("Dropping PidNamespace at level {}", self.level);
+    }
+}
+
+impl ProcessControlBlock {
+    pub fn active_pid_ns(&self) -> Arc<PidNamespace> {
+        self.pid().ns_of_pid()
+    }
+}

+ 88 - 0
kernel/src/process/namespace/user_namespace.rs

@@ -0,0 +1,88 @@
+use alloc::sync::{Arc, Weak};
+use core::cmp::Ordering;
+use core::fmt::Debug;
+
+use crate::libs::spinlock::SpinLock;
+
+use super::nsproxy::NsCommon;
+use super::{NamespaceOps, NamespaceType};
+use alloc::vec::Vec;
+
+lazy_static! {
+    pub static ref INIT_USER_NAMESPACE: Arc<UserNamespace> = UserNamespace::new_root();
+}
+
+pub struct UserNamespace {
+    parent: Option<Weak<UserNamespace>>,
+    nscommon: NsCommon,
+    self_ref: Weak<UserNamespace>,
+    _inner: SpinLock<InnerUserNamespace>,
+}
+
+pub struct InnerUserNamespace {
+    _children: Vec<Arc<UserNamespace>>,
+}
+
+impl NamespaceOps for UserNamespace {
+    fn ns_common(&self) -> &NsCommon {
+        &self.nscommon
+    }
+}
+
+impl UserNamespace {
+    /// 创建root user namespace
+    fn new_root() -> Arc<Self> {
+        Arc::new_cyclic(|self_ref| Self {
+            self_ref: self_ref.clone(),
+            nscommon: NsCommon::new(0, NamespaceType::User),
+            parent: None,
+            _inner: SpinLock::new(InnerUserNamespace {
+                _children: Vec::new(),
+            }),
+        })
+    }
+
+    /// 获取层级
+    pub fn level(&self) -> u32 {
+        self.nscommon.level
+    }
+
+    /// 检查当前用户命名空间是否是另一个用户命名空间的祖先
+    ///
+    /// # 参数
+    /// * `other` - 要检查的目标用户命名空间
+    ///
+    /// # 返回值
+    /// * `true` - 如果当前命名空间是 `other` 的祖先
+    /// * `false` - 如果当前命名空间不是 `other` 的祖先
+    ///
+    /// # 说明
+    /// 该方法通过遍历 `other` 的父命名空间链来判断当前命名空间是否为其祖先。
+    /// 如果两个命名空间处于同一层级且指向同一个对象,则认为是祖先关系。
+    /// 如果当前命名空间的层级大于目标命名空间,则不可能是祖先关系。
+    pub fn is_ancestor_of(&self, other: &Arc<Self>) -> bool {
+        let mut current = other.clone();
+        let self_level = self.level();
+        loop {
+            let current_level = current.level();
+            match current_level.cmp(&self_level) {
+                Ordering::Greater => {
+                    if let Some(parent) = current.parent.as_ref().and_then(|p| p.upgrade()) {
+                        current = parent;
+                        continue;
+                    } else {
+                        return false;
+                    }
+                }
+                Ordering::Equal => return Arc::ptr_eq(&self.self_ref.upgrade().unwrap(), &current),
+                Ordering::Less => return false,
+            }
+        }
+    }
+}
+
+impl Debug for UserNamespace {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("UserNamespace").finish()
+    }
+}

+ 515 - 5
kernel/src/process/pid.rs

@@ -1,5 +1,18 @@
+use core::fmt::Debug;
+use core::sync::atomic::AtomicBool;
+
+use crate::libs::rwlock::RwLock;
+use crate::libs::spinlock::{SpinLock, SpinLockGuard};
+use crate::process::ProcessManager;
+use alloc::sync::{Arc, Weak};
+use alloc::vec::Vec;
+use system_error::SystemError;
+
+use super::namespace::pid_namespace::PidNamespace;
+use super::{ProcessControlBlock, RawPid};
+
 #[allow(dead_code)]
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 #[repr(u8)]
 pub enum PidType {
     /// pid类型是进程id
@@ -10,9 +23,506 @@ pub enum PidType {
     MAX = 5,
 }
 
-/// 为PidType实现判断相等的trait
-impl PartialEq for PidType {
-    fn eq(&self, other: &PidType) -> bool {
-        *self as u8 == *other as u8
+impl PidType {
+    pub const ALL: [PidType; Self::PIDTYPE_MAX - 1] =
+        [PidType::PID, PidType::TGID, PidType::PGID, PidType::SID];
+    pub const PIDTYPE_MAX: usize = PidType::MAX as usize;
+}
+
+pub struct Pid {
+    self_ref: Weak<Pid>,
+    pub level: u32,
+    dead: AtomicBool,
+    /// 使用此PID的任务列表,按PID类型分组
+    /// tasks[PidType::PID as usize] = 使用该PID作为进程ID的任务
+    /// tasks[PidType::TGID as usize] = 使用该PID作为线程组ID的任务
+    tasks: [SpinLock<Vec<Weak<ProcessControlBlock>>>; PidType::PIDTYPE_MAX],
+    /// 在各个namespace中的PID值
+    numbers: SpinLock<Vec<Option<UPid>>>,
+}
+
+impl Debug for Pid {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("Pid").finish()
+    }
+}
+
+impl Pid {
+    fn new(level: u32) -> Arc<Self> {
+        let pid = Arc::new_cyclic(|weak_self| Self {
+            self_ref: weak_self.clone(),
+            dead: AtomicBool::new(false),
+            level,
+            tasks: core::array::from_fn(|_| SpinLock::new(Vec::new())),
+            numbers: SpinLock::new(vec![None; level as usize + 1]),
+        });
+
+        pid
+    }
+
+    pub fn dead(&self) -> bool {
+        self.dead.load(core::sync::atomic::Ordering::Relaxed)
+    }
+
+    pub fn set_dead(&self) {
+        self.dead.store(true, core::sync::atomic::Ordering::Relaxed);
+    }
+
+    /// 获取指定PID所属的命名空间
+    ///
+    /// 返回该PID被分配时所在的PID命名空间的引用(Arc封装)
+    pub fn ns_of_pid(&self) -> Arc<PidNamespace> {
+        self.numbers
+            .lock()
+            .get(self.level as usize)
+            .map(|upid| upid.as_ref().unwrap().ns.clone())
+            .unwrap()
+    }
+
+    pub fn first_upid(&self) -> Option<UPid> {
+        self.numbers.lock().first().cloned().unwrap()
+    }
+
+    pub fn tasks_iter(&self, pid_type: PidType) -> PidTaskIterator {
+        let guard = self.tasks[pid_type as usize].lock();
+        PidTaskIterator { guard, index: 0 }
+    }
+
+    /// 判断当前pid是否是当前命名空间的init进程(即child reaper)
+    ///
+    /// 由于在copy_process中可能在pid_ns->child_reaper被赋值前就需要检查,
+    /// 因此这里通过pid号来检查。
+    /// 如果当前pid在当前命名空间中的pid号为1,则返回true,否则返回false。
+    pub fn is_child_reaper(&self) -> bool {
+        self.numbers.lock()[self.level as usize]
+            .as_ref()
+            .unwrap()
+            .nr
+            .data()
+            == 1
+    }
+
+    pub fn has_task(&self, pid_type: PidType) -> bool {
+        let tasks = self.tasks[pid_type as usize].lock();
+        !tasks.is_empty()
+    }
+
+    pub fn pid_task(&self, pid_type: PidType) -> Option<Arc<ProcessControlBlock>> {
+        let tasks = self.tasks[pid_type as usize].lock();
+        if tasks.is_empty() {
+            None
+        } else {
+            // 返回第一个进程
+            tasks.first().and_then(|task| task.upgrade())
+        }
+    }
+
+    pub fn pid_vnr(&self) -> RawPid {
+        let active_pid_ns = ProcessManager::current_pcb().active_pid_ns();
+        self.pid_nr_ns(&active_pid_ns)
+    }
+
+    /// 获取在指定namespace中的PID号
+    ///
+    /// 如果当前PID在指定namespace中不存在,则返回0。
+    ///
+    /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid.c#475
+    pub fn pid_nr_ns(&self, ns: &Arc<PidNamespace>) -> RawPid {
+        if ns.level() <= self.level {
+            let numbers = self.numbers.lock();
+            let upid = numbers[ns.level() as usize]
+                .as_ref()
+                .cloned()
+                .unwrap_or_else(|| panic!("pid numbers should not be empty: ns.level={}, self.level={}, numbers.len={}", ns.level(), self.level, numbers.len()));
+            if Arc::ptr_eq(&upid.ns, ns) {
+                return upid.nr;
+            }
+        }
+
+        // 如果没有找到对应的UPid,返回0
+        RawPid::new(0)
+    }
+}
+
+impl PartialEq for Pid {
+    fn eq(&self, other: &Self) -> bool {
+        // 比较 `self_ref` 的指针地址(Weak 比较需要先升级为 Arc)
+        if let (Some(self_arc), Some(other_arc)) =
+            (self.self_ref.upgrade(), other.self_ref.upgrade())
+        {
+            Arc::ptr_eq(&self_arc, &other_arc)
+        } else {
+            false
+        }
+    }
+}
+
+impl Eq for Pid {}
+
+impl Drop for Pid {
+    fn drop(&mut self) {
+        // 清理numbers中的UPid引用
+        let numbers_guard = self.numbers.lock();
+        for upid in numbers_guard.iter().flatten() {
+            upid.ns.release_pid_in_ns(upid.nr);
+        }
+    }
+}
+
+pub struct PidTaskIterator<'a> {
+    guard: SpinLockGuard<'a, Vec<Weak<ProcessControlBlock>>>,
+    index: usize,
+}
+
+impl Iterator for PidTaskIterator<'_> {
+    type Item = Arc<ProcessControlBlock>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.index < self.guard.len() {
+            if let Some(task) = self.guard[self.index].upgrade() {
+                self.index += 1;
+                return Some(task);
+            }
+            self.index += 1;
+        }
+        None
+    }
+}
+
+/// 在特定namespace中的PID信息
+#[derive(Clone)]
+pub struct UPid {
+    /// 在该namespace中的PID值
+    pub nr: RawPid,
+    /// 所属的namespace
+    pub ns: Arc<PidNamespace>,
+}
+
+impl UPid {
+    /// 创建新的UPid
+    pub fn new(nr: RawPid, ns: Arc<PidNamespace>) -> Self {
+        Self { nr, ns }
+    }
+}
+
+impl Debug for UPid {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("UPid").field("nr", &self.nr).finish()
+    }
+}
+
+impl ProcessControlBlock {
+    pub fn pid(&self) -> Arc<Pid> {
+        self.thread_pid.read().clone().unwrap()
+    }
+
+    /// 强制设置当前进程的raw_pid
+    /// 注意:这个函数应该在创建进程时调用,不能在运行时随意调用
+    pub(super) unsafe fn force_set_raw_pid(&self, pid: RawPid) {
+        let self_mut = self as *const Self as *mut Self;
+        (*self_mut).pid = pid;
+    }
+}
+
+/// 连接任务和PID的桥梁结构体
+#[derive(Debug)]
+pub struct PidLink {
+    /// 指向对应的Pid结构体
+    pub pid: RwLock<Option<Arc<Pid>>>,
+}
+
+impl PidLink {
+    /// 创建新的PidLink
+    pub fn new() -> Self {
+        Self {
+            pid: RwLock::new(None),
+        }
+    }
+
+    /// 链接到指定的PID
+    pub(super) fn link_pid(&self, pid: Arc<Pid>) {
+        self.pid.write().replace(pid);
+    }
+
+    /// 取消PID链接
+    pub(super) fn unlink_pid(&self) {
+        self.pid.write().take();
+    }
+
+    /// 获取链接的PID
+    pub fn get_pid(&self) -> Option<Arc<Pid>> {
+        self.pid.read().clone()
+    }
+
+    /// 检查是否已链接PID
+    #[allow(dead_code)]
+    pub fn is_linked(&self) -> bool {
+        self.pid.read().is_some()
+    }
+}
+
+impl Default for PidLink {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl Clone for PidLink {
+    fn clone(&self) -> Self {
+        Self {
+            pid: RwLock::new(self.get_pid()),
+        }
+    }
+}
+
+/// 分配一个新的PID
+///
+/// 参考:https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid.c?fi=alloc_pid#162
+pub(super) fn alloc_pid(ns: &Arc<PidNamespace>) -> Result<Arc<Pid>, SystemError> {
+    let pid = Pid::new(ns.level());
+
+    // 用于记录已分配的PID,以便失败时清理
+    let mut allocated_upids: Vec<(isize, UPid)> = Vec::new();
+
+    // 获取当前namespace的引用链
+    let mut current_ns = Some(ns.clone());
+    let mut level = ns.level() as isize;
+
+    // 从最深层级开始向上分配PID
+    while level >= 0 {
+        if let Some(ref curr_ns) = current_ns {
+            // warn: 这里会造成Arc的循环引用,不过暂时没想到什么好办法
+            // 因此需要在进程退出的时候需要手动清理pid。
+            // 循环引用的路径: current_ns -> pid_map -> pid -> numbers -> upid -> ns(curr_ns)
+            match curr_ns.alloc_pid_in_ns(pid.clone()) {
+                Ok(nr) => {
+                    let upid = UPid::new(nr, curr_ns.clone());
+                    allocated_upids.push((level, upid.clone()));
+                    pid.numbers.lock()[level as usize] = Some(upid);
+                    current_ns = curr_ns.parent();
+                }
+                Err(e) => {
+                    // 分配失败,需要清理已分配的PID
+                    cleanup_allocated_pids(pid, allocated_upids);
+                    return Err(e);
+                }
+            }
+        }
+        level -= 1;
+    }
+
+    // 如果分配成功,返回新创建的PID
+    Ok(pid)
+}
+
+/// 清理已分配的PID
+fn cleanup_allocated_pids(pid: Arc<Pid>, mut allocated_upids: Vec<(isize, UPid)>) {
+    // 反转已分配的UPid列表,以便从最深层级开始清理
+    allocated_upids.reverse();
+    for (level, upid) in allocated_upids {
+        let curr_ns = upid.ns;
+        // 在当前namespace中释放UPid
+        curr_ns.release_pid_in_ns(upid.nr);
+        pid.numbers.lock()[level as usize] = None;
+    }
+}
+
+/// 释放pid
+///
+/// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid.c#129
+pub(super) fn free_pid(pid: Arc<Pid>) {
+    // 释放PID
+    pid.set_dead();
+    // let raw_pid = pid.pid_vnr().data();
+    let mut level = 0;
+    while level <= pid.level {
+        let upid = pid.numbers.lock()[level as usize]
+            .take()
+            .expect("pid numbers should not be empty");
+        // log::debug!(
+        //     "Freeing pid: raw:{}, upid.nr:{}, level: {}",
+        //     raw_pid,
+        //     upid.nr.data(),
+        //     level
+        // );
+        let mut ns_guard = upid.ns.inner();
+        let pid_allocated_after_free = ns_guard.do_pid_allocated() - 1;
+        if pid_allocated_after_free == 1 || pid_allocated_after_free == 2 {
+            if let Some(child_reaper) = ns_guard.child_reaper().as_ref().and_then(|x| x.upgrade()) {
+                ProcessManager::wakeup(&child_reaper).ok();
+            }
+        }
+        ns_guard.do_release_pid_in_ns(upid.nr);
+        if ns_guard.dead() {
+            // log::debug!("Releasing pid namespace with level {}", level);
+            upid.ns.delete_current_pidns_in_parent();
+        }
+        level += 1;
+    }
+}
+
+impl ProcessControlBlock {
+    pub(super) fn attach_pid(&self, pid_type: PidType) {
+        let pid = self.task_pid_ptr(pid_type);
+        if let Some(pid) = pid {
+            self.pid_links[pid_type as usize].link_pid(pid.clone());
+            pid.tasks[pid_type as usize]
+                .lock()
+                .push(self.self_ref.clone());
+        }
+    }
+
+    pub fn task_pid_ptr(&self, pid_type: PidType) -> Option<Arc<Pid>> {
+        if pid_type == PidType::PID {
+            return self.thread_pid.read().clone();
+        }
+
+        self.sig_struct().pids[pid_type as usize].clone()
+    }
+
+    pub fn task_pid_vnr(&self) -> RawPid {
+        self.__task_pid_nr_ns(PidType::PID, None).unwrap()
+    }
+
+    /// 获取进程在指定PID命名空间中的PID号
+    ///
+    /// 根据指定的PID类型和命名空间,返回进程对应的PID值。
+    /// 如果未指定命名空间,则使用当前进程的活跃PID命名空间。
+    ///
+    /// # 参数
+    /// * `pid_type` - PID类型(进程ID、线程组ID、进程组ID或会话ID)
+    /// * `ns` - 可选的PID命名空间引用,如果为None则使用当前命名空间
+    ///
+    /// # 返回值
+    /// 返回指定命名空间中的PID值,如果进程没有对应的PID则返回None
+    ///
+    /// # 特殊情况
+    /// * 如果进程的raw_pid为0(通常是空闲进程),则直接返回raw_pid
+    #[allow(dead_code)]
+    pub(super) fn task_pid_nr_ns(
+        &self,
+        pid_type: PidType,
+        ns: Option<Arc<PidNamespace>>,
+    ) -> Option<RawPid> {
+        self.__task_pid_nr_ns(pid_type, ns)
+    }
+
+    fn __task_pid_nr_ns(
+        &self,
+        pid_type: PidType,
+        mut ns: Option<Arc<PidNamespace>>,
+    ) -> Option<RawPid> {
+        if self.raw_pid().data() == 0 {
+            return Some(self.raw_pid());
+        }
+        if ns.is_none() {
+            ns = Some(ProcessManager::current_pcb().active_pid_ns());
+        }
+        let mut retval = None;
+        let ns = ns.unwrap();
+        let pid = self.task_pid_ptr(pid_type);
+        if let Some(pid) = pid {
+            retval = Some(pid.pid_nr_ns(&ns));
+        }
+
+        return retval;
+    }
+
+    /// 获取当前任务的线程组ID (在当前PID命名空间中的TGID)
+    pub fn task_tgid_vnr(&self) -> Option<RawPid> {
+        self.__task_pid_nr_ns(PidType::TGID, None)
+    }
+
+    pub(super) fn detach_pid(&self, pid_type: PidType) {
+        self.__change_pid(pid_type, None);
+    }
+
+    pub(super) fn change_pid(&self, pid_type: PidType, new_pid: Arc<Pid>) {
+        self.__change_pid(pid_type, Some(new_pid));
+        self.attach_pid(pid_type);
+    }
+
+    fn __change_pid(&self, pid_type: PidType, new_pid: Option<Arc<Pid>>) {
+        // log::debug!(
+        //     "Changing PID type={:?}, current_pid={:?}, new_pid={:?}",
+        //     pid_type,
+        //     self.task_pid_ptr(pid_type)
+        //         .as_ref()
+        //         .map_or("None".to_string(), |p| p.pid_vnr().data().to_string()),
+        //     new_pid
+        // );
+        // log::debug!("current name: {}", self.basic().name());
+
+        let pid = self.task_pid_ptr(pid_type);
+        self.pid_links[pid_type as usize].unlink_pid();
+        // log::debug!(
+        //     "Unlinked PID type={:?}, pid={}",
+        //     pid_type,
+        //     pid.as_ref()
+        //         .map_or("None".to_string(), |p| p.pid_vnr().data().to_string())
+        // );
+
+        if let Some(new_pid) = new_pid {
+            self.init_task_pid(pid_type, new_pid.clone());
+            // log::debug!(
+            //     "Set new PID type={:?}, pid={:?}",
+            //     pid_type,
+            //     new_pid.pid_vnr().data()
+            // );
+        }
+
+        if let Some(pid) = pid {
+            pid.tasks[pid_type as usize]
+                .lock()
+                .retain(|task| !Weak::ptr_eq(task, &self.self_ref));
+            for x in PidType::ALL.iter().rev() {
+                if pid.has_task(*x) {
+                    // log::debug!(
+                    //     "PID type={:?}, raw={} still has tasks, not freeing",
+                    //     pid_type,
+                    //     pid.pid_vnr().data()
+                    // );
+                    return;
+                }
+            }
+            // log::debug!(
+            //     "Freeing PID type={:?}, pid={:?}",
+            //     pid_type,
+            //     pid.pid_vnr().data()
+            // );
+            free_pid(pid);
+        }
+    }
+}
+
+impl ProcessManager {
+    pub fn find_task_by_vpid(vnr: RawPid) -> Option<Arc<ProcessControlBlock>> {
+        // 如果进程管理器未初始化,用旧的方法
+        if !ProcessManager::initialized() {
+            return Self::find(vnr);
+        }
+
+        // 如果当前进程是真实的PID 0,则用旧的方法
+        let current_pcb = ProcessManager::current_pcb();
+        if current_pcb.raw_pid().data() == 0 {
+            return Self::find(vnr);
+        }
+
+        let active_pid_ns = current_pcb.active_pid_ns();
+        return Self::find_task_by_pid_ns(vnr, &active_pid_ns);
+    }
+
+    pub fn find_task_by_pid_ns(
+        nr: RawPid,
+        ns: &Arc<PidNamespace>,
+    ) -> Option<Arc<ProcessControlBlock>> {
+        let pid: Arc<Pid> = ns.find_pid_in_ns(nr)?;
+        return pid.pid_task(PidType::PID);
+    }
+
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/pid.c?fi=find_vpid#318
+    pub fn find_vpid(nr: RawPid) -> Option<Arc<Pid>> {
+        let active_pid_ns = ProcessManager::current_pcb().active_pid_ns();
+        active_pid_ns.find_pid_in_ns(nr)
     }
 }

+ 36 - 345
kernel/src/process/process_group.rs

@@ -1,368 +1,59 @@
-use super::{
-    session::{Session, Sid},
-    Pid, ProcessControlBlock, ProcessManager,
-};
-use crate::libs::spinlock::SpinLock;
-use alloc::{
-    collections::BTreeMap,
-    sync::{Arc, Weak},
-};
-use hashbrown::HashMap;
-use system_error::SystemError;
+use super::{ProcessControlBlock, ProcessManager, RawPid};
+use crate::process::pid::{Pid, PidType};
+use alloc::sync::Arc;
 
 /// 进程组ID
-pub type Pgid = Pid;
-
-/// 系统中所有进程组
-pub static ALL_PROCESS_GROUP: SpinLock<Option<HashMap<Pgid, Arc<ProcessGroup>>>> =
-    SpinLock::new(None);
-
-pub struct ProcessGroup {
-    /// 进程组pgid
-    pub pgid: Pgid,
-    pub process_group_inner: SpinLock<PGInner>,
-}
-
-pub struct PGInner {
-    pub processes: BTreeMap<Pid, Arc<ProcessControlBlock>>,
-    pub leader: Option<Arc<ProcessControlBlock>>,
-    pub session: Weak<Session>,
-}
-
-impl PGInner {
-    pub fn remove_process(&mut self, pid: &Pid) {
-        if let Some(process) = self.processes.remove(pid) {
-            if let Some(leader) = &self.leader {
-                if Arc::ptr_eq(leader, &process) {
-                    self.leader = None;
-                }
-            }
-        }
-    }
-    pub fn is_empty(&self) -> bool {
-        self.processes.is_empty()
-    }
-}
-
-impl ProcessGroup {
-    pub fn new(pcb: Arc<ProcessControlBlock>) -> Arc<Self> {
-        let pid = pcb.pid();
-        let mut processes = BTreeMap::new();
-        processes.insert(pid, pcb.clone());
-        let inner = PGInner {
-            processes,
-            leader: Some(pcb),
-            session: Weak::new(),
-        };
-        // log::debug!("New ProcessGroup {:?}", pid);
-
-        Arc::new(Self {
-            pgid: pid,
-            process_group_inner: SpinLock::new(inner),
-        })
-    }
-
-    pub fn contains(&self, pid: Pid) -> bool {
-        self.process_group_inner.lock().processes.contains_key(&pid)
-    }
-
-    pub fn pgid(&self) -> Pgid {
-        self.pgid
-    }
-
-    pub fn leader(&self) -> Option<Arc<ProcessControlBlock>> {
-        self.process_group_inner.lock().leader.clone()
-    }
-
-    pub fn session(&self) -> Option<Arc<Session>> {
-        // log::debug!("Before lock");
-        let guard = self.process_group_inner.lock();
-        // log::debug!("Locking");
-        let session = guard.session.upgrade();
-        drop(guard);
-        // log::debug!("After lock");
-        return session;
-    }
-
-    pub fn broadcast(&self) {
-        unimplemented!("broadcast not supported yet");
-    }
-
-    pub fn sid(&self) -> Sid {
-        if let Some(session) = self.session() {
-            return session.sid();
-        }
-        Sid::from(0)
-    }
-}
-
-impl Drop for ProcessGroup {
-    fn drop(&mut self) {
-        let mut inner = self.process_group_inner.lock();
-
-        if let Some(leader) = inner.leader.take() {
-            // 组长进程仍然在进程列表中,不应该直接销毁
-            if inner.processes.contains_key(&leader.pid()) {
-                inner.leader = Some(leader);
-            }
-        }
-
-        inner.processes.clear();
-
-        if let Some(session) = inner.session.upgrade() {
-            let mut session_inner = session.session_inner.lock();
-            session_inner.process_groups.remove(&self.pgid);
-
-            if session_inner.should_destory() {
-                ProcessManager::remove_session(session.sid());
-            }
-        }
-        // log::debug!("Dropping pg {:?}", self.pgid.clone());
-    }
-}
+pub type Pgid = RawPid;
 
 impl ProcessManager {
-    /// 根据pgid获取进程组
-    ///
-    /// ## 参数
-    ///
-    /// - `pgid` : 进程组的pgid
-    ///
-    /// ## 返回值
-    ///
-    /// 如果找到了对应的进程组,那么返回该进程组,否则返回None
-    pub fn find_process_group(pgid: Pgid) -> Option<Arc<ProcessGroup>> {
-        return ALL_PROCESS_GROUP
-            .lock_irqsave()
-            .as_ref()?
-            .get(&pgid)
-            .cloned();
-    }
-
-    /// 向系统中添加一个进程组
-    ///
-    /// ## 参数
-    ///
-    /// - `pg` : Arc<ProcessGroup>
-    ///
-    /// ## 返回值
-    ///
-    /// 无
-    pub fn add_process_group(pg: Arc<ProcessGroup>) {
-        ALL_PROCESS_GROUP
-            .lock_irqsave()
-            .as_mut()
-            .unwrap()
-            .insert(pg.pgid(), pg.clone());
-        // log::debug!("New ProcessGroup added, pgid: {:?}", pg.pgid());
-    }
-
-    /// 删除一个进程组
-    pub fn remove_process_group(pgid: Pgid) {
-        // log::debug!("Removing pg {:?}", pgid.clone());
-        let mut all_groups = ALL_PROCESS_GROUP.lock_irqsave();
-        if let Some(pg) = all_groups.as_mut().unwrap().remove(&pgid) {
-            // log::debug!("count: {:?}", Arc::strong_count(&pg));
-            if Arc::strong_count(&pg) <= 2 {
-                // 这里 Arc 计数小于等于 2,意味着它只有在 all_groups 里有一个引用,移除后会自动释放
-                drop(pg);
-            }
-        }
-    }
-
     // 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/exit.c#345
     pub fn is_current_pgrp_orphaned() -> bool {
         let current_pcb = ProcessManager::current_pcb();
-        let sid = current_pcb.sid();
-        let process_group = current_pcb.process_group();
-        if let Some(pg) = process_group {
-            for process in pg.process_group_inner.lock().processes.values() {
-                if let Some(real_parent) = process.real_parent_pcb.read().clone().upgrade() {
-                    //todo 添加判断: 1.是否被忽略 2.是否已经退出(线程组是否为空)
-                    if real_parent.pid == Pid(1) || process.is_exited() {
-                        log::debug!("is_current_pgrp_orphaned: real_parent is init or exited");
-                        continue;
-                    }
-                    let real_parent_pg = real_parent.process_group().unwrap();
-                    if real_parent_pg.pgid() != pg.pgid() && real_parent_pg.sid() == sid {
-                        return false;
-                    }
-                }
-            }
-        }
-        true
-    }
-}
-
-impl ProcessControlBlock {
-    #[inline(always)]
-    pub fn pgid(&self) -> Pgid {
-        if let Some(process_group) = self.process_group.lock().upgrade() {
-            process_group.pgid()
-        } else {
-            Pgid::from(0)
-        }
-    }
-
-    #[inline(always)]
-    pub fn process_group(&self) -> Option<Arc<ProcessGroup>> {
-        self.process_group.lock().upgrade()
-    }
-
-    pub fn set_process_group(&self, pg: &Arc<ProcessGroup>) {
-        if let Some(pcb) = self.self_ref.upgrade() {
-            *pcb.process_group.lock() = Arc::downgrade(pg);
-            // log::debug!("pid: {:?} set pgid: {:?}", self.pid(), pg.pgid());
-        }
+        let pgrp = current_pcb.task_pgrp().unwrap();
+        Self::will_become_orphaned_pgrp(&pgrp, None)
     }
 
-    pub fn is_process_group_leader(&self) -> bool {
-        if let Some(pcb) = self.self_ref.upgrade() {
-            let pg = self.process_group().unwrap();
-            if let Some(leader) = pg.leader() {
-                return Arc::ptr_eq(&pcb, &leader);
-            }
-        }
-
-        return false;
-    }
-
-    /// 将进程加入到指定pgid的进程组中(无论该进程组是否已经存在)
-    ///
-    /// 如果进程组已经存在,则将进程加入到该进程组中
-    /// 如果进程组不存在,则创建一个新的进程组,并将进程加入到该进程组中
+    /// 检查一个进程组是否为孤儿进程组
     ///
-    /// ## 参数
-    /// `pgid` : 目标进程组的pgid
-    ///
-    /// ## 返回值
-    /// 无
-    pub fn join_other_group(&self, pgid: Pgid) -> Result<(), SystemError> {
-        // if let Some(pcb) = self.self_ref.upgrade() {
-        if self.pgid() == pgid {
-            return Ok(());
-        }
-        if self.is_session_leader() {
-            // 会话领导者不能加入其他进程组
-            return Err(SystemError::EPERM);
-        }
-        if let Some(pg) = ProcessManager::find_process_group(pgid) {
-            let session = self.session().unwrap();
-            if !session.contains_process_group(&pg) {
-                // 进程组和进程应该属于同一个会话
-                return Err(SystemError::EPERM);
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/exit.c#326
+    #[inline(never)]
+    fn will_become_orphaned_pgrp(
+        pgrp: &Arc<Pid>,
+        ignored_pcb: Option<&Arc<ProcessControlBlock>>,
+    ) -> bool {
+        for pcb in pgrp.tasks_iter(PidType::PGID) {
+            let real_parent = pcb.real_parent_pcb().unwrap();
+            if ignored_pcb.is_some() && Arc::ptr_eq(&pcb, ignored_pcb.unwrap()) {
+                continue;
             }
-            self.join_specified_group(&pg)?;
-        } else {
-            if pgid != self.pid() {
-                // 进程组不存在,只能加入自己的进程组
-                return Err(SystemError::EPERM);
+            if (pcb.is_exited() && pcb.threads_read_irqsave().thread_group_empty())
+                || real_parent.is_global_init()
+            {
+                continue;
             }
-            self.join_new_group()?;
-        }
-        // }
-
-        Ok(())
-    }
 
-    /// 将进程加入到新创建的进程组中
-    fn join_new_group(&self) -> Result<(), SystemError> {
-        let session = self.session().unwrap();
-        let mut self_pg_mut = self.process_group.lock();
-
-        if let Some(old_pg) = self_pg_mut.upgrade() {
-            let mut old_pg_inner = old_pg.process_group_inner.lock();
-            let mut session_inner = session.session_inner.lock();
-            old_pg_inner.remove_process(&self.pid);
-            *self_pg_mut = Weak::new();
-
-            if old_pg_inner.is_empty() {
-                ProcessManager::remove_process_group(old_pg.pgid());
-                assert!(session_inner.process_groups.contains_key(&old_pg.pgid()));
-                session_inner.process_groups.remove(&old_pg.pgid());
+            if real_parent.task_pgrp() != Some(pgrp.clone())
+                && real_parent.task_session() == pcb.task_session()
+            {
+                return false;
             }
         }
 
-        let pcb = self.self_ref.upgrade().unwrap();
-        let new_pg = ProcessGroup::new(pcb);
-        let mut new_pg_inner = new_pg.process_group_inner.lock();
-        let mut session_inner = session.session_inner.lock();
-
-        *self_pg_mut = Arc::downgrade(&new_pg);
-        ProcessManager::add_process_group(new_pg.clone());
-
-        new_pg_inner.session = Arc::downgrade(&session);
-        session_inner
-            .process_groups
-            .insert(new_pg.pgid, new_pg.clone());
-
-        Ok(())
+        return true;
     }
+}
 
-    /// 将进程加入到指定的进程组中
-    fn join_specified_group(&self, group: &Arc<ProcessGroup>) -> Result<(), SystemError> {
-        let mut self_group = self.process_group.lock();
-
-        let mut group_inner = if let Some(old_pg) = self_group.upgrade() {
-            let (mut old_pg_inner, group_inner) = match old_pg.pgid().cmp(&group.pgid()) {
-                core::cmp::Ordering::Equal => return Ok(()),
-                core::cmp::Ordering::Less => (
-                    old_pg.process_group_inner.lock(),
-                    group.process_group_inner.lock(),
-                ),
-                core::cmp::Ordering::Greater => {
-                    let group_inner = group.process_group_inner.lock();
-                    let old_pg_inner = old_pg.process_group_inner.lock();
-                    (old_pg_inner, group_inner)
-                }
-            };
-            old_pg_inner.remove_process(&self.pid);
-            *self_group = Weak::new();
-
-            if old_pg_inner.is_empty() {
-                ProcessManager::remove_process_group(old_pg.pgid());
-            }
-            group_inner
-        } else {
-            group.process_group_inner.lock()
-        };
-
-        let pcb = self.self_ref.upgrade().unwrap();
-        group_inner.processes.insert(self.pid, pcb);
-        *self_group = Arc::downgrade(group);
-        Ok(())
+impl ProcessControlBlock {
+    pub fn task_pgrp(&self) -> Option<Arc<Pid>> {
+        self.sig_struct().pids[PidType::PGID as usize].clone()
     }
 
-    /// ### 清除自身的进程组以及会话引用(如果有的话),这个方法只能在进程退出时调用
-    pub fn clear_pg_and_session_reference(&self) {
-        if let Some(pg) = self.process_group() {
-            let mut pg_inner = pg.process_group_inner.lock();
-            pg_inner.remove_process(&self.pid());
-
-            if pg_inner.is_empty() {
-                // 如果进程组没有任何进程了,就删除该进程组
-                ProcessManager::remove_process_group(pg.pgid());
-                // log::debug!("clear_pg_reference: {:?}", pg.pgid());
-
-                if let Some(session) = pg_inner.session.upgrade() {
-                    let mut session_inner = session.session_inner.lock();
-                    session_inner.remove_process_group(&pg.pgid());
-                    if session_inner.is_empty() {
-                        // 如果会话没有任何进程组了,就删除该会话
-                        ProcessManager::remove_session(session.sid());
-                        // log::debug!("clear_pg_reference: {:?}", session.sid());
-                    }
-                }
-            }
-        }
+    pub fn task_session(&self) -> Option<Arc<Pid>> {
+        self.sig_struct().pids[PidType::SID as usize].clone()
+    }
 
-        if let Some(session) = self.session() {
-            let mut session_inner = session.session_inner.lock();
-            if let Some(leader) = &session_inner.leader {
-                if Arc::ptr_eq(leader, &self.self_ref.upgrade().unwrap()) {
-                    session_inner.leader = None;
-                }
-            }
-        }
+    /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/signal.c?fi=task_join_group_stop#393
+    pub(super) fn task_join_group_stop(&self) {
+        // todo: 实现  https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/signal.c?fi=task_join_group_stop#393
     }
 }

+ 68 - 213
kernel/src/process/session.rs

@@ -1,218 +1,73 @@
-use super::{
-    process_group::{Pgid, ProcessGroup},
-    Pid, ProcessControlBlock, ProcessManager,
-};
-use crate::libs::spinlock::SpinLock;
-use alloc::{
-    collections::BTreeMap,
-    sync::{Arc, Weak},
-};
-use hashbrown::HashMap;
+use super::{pid::Pid, ProcessControlBlock, ProcessManager, RawPid};
+use crate::{driver::tty::tty_job_control::TtyJobCtrlManager, process::pid::PidType};
+use alloc::sync::Arc;
 use system_error::SystemError;
 
-/// 会话SID
-pub type Sid = Pid;
-
-/// 系统中所有会话
-pub static ALL_SESSION: SpinLock<Option<HashMap<Sid, Arc<Session>>>> = SpinLock::new(None);
-
-pub struct Session {
-    pub sid: Sid,
-    pub session_inner: SpinLock<SessionInner>,
-}
-
-pub struct SessionInner {
-    pub process_groups: BTreeMap<Pgid, Arc<ProcessGroup>>,
-    pub leader: Option<Arc<ProcessControlBlock>>,
-}
-
-impl SessionInner {
-    pub fn is_empty(&self) -> bool {
-        self.process_groups.is_empty()
-    }
-    pub fn remove_process_group(&mut self, pgid: &Pgid) {
-        self.process_groups.remove(pgid);
-    }
-    pub fn remove_process(&mut self, pcb: &Arc<ProcessControlBlock>) {
-        if let Some(leader) = &self.leader {
-            if Arc::ptr_eq(leader, pcb) {
-                self.leader = None;
-            }
-        }
-    }
-    pub fn should_destory(&self) -> bool {
-        self.process_groups.is_empty()
-    }
-}
-
-impl Session {
-    pub fn new(group: Arc<ProcessGroup>) -> Arc<Self> {
-        let sid = group.pgid;
-        let mut process_groups = BTreeMap::new();
-        process_groups.insert(group.pgid, group.clone());
-        let inner = SessionInner {
-            process_groups,
-            leader: None,
-        };
-        // log::debug!("New Session {:?}", sid);
-        Arc::new(Self {
-            sid,
-            session_inner: SpinLock::new(inner),
-        })
-    }
-
-    pub fn sid(&self) -> Sid {
-        self.sid
-    }
-
-    pub fn leader(&self) -> Option<Arc<ProcessControlBlock>> {
-        self.session_inner.lock().leader.clone()
-    }
-
-    // pub fn contains_process_group(&self, pgid: Pgid) -> bool {
-    //     self.session_inner.lock().process_groups.contains_key(&pgid)
-    // }
-
-    pub fn contains_process_group(&self, process_group: &Arc<ProcessGroup>) -> bool {
-        self.session_inner
-            .lock()
-            .process_groups
-            .contains_key(&process_group.pgid)
-    }
-}
-
-impl Drop for Session {
-    fn drop(&mut self) {
-        let mut session_inner = self.session_inner.lock();
-        session_inner.process_groups.clear();
-        session_inner.leader = None;
-        // log::debug!("Dropping session: {:?}", self.sid());
-    }
+/// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/sys.c#1225
+pub(super) fn ksys_setsid() -> Result<RawPid, SystemError> {
+    let pcb = ProcessManager::current_pcb();
+    let group_leader = pcb
+        .threads_read_irqsave()
+        .group_leader()
+        .ok_or(SystemError::ESRCH)?;
+    let sid = group_leader.pid();
+    let session = sid.pid_vnr();
+    // log::debug!(
+    //     "ksys_setsid: group_leader: {}",
+    //     group_leader.raw_pid().data()
+    // );
+    let siginfo_lock = group_leader.sig_info_upgradable();
+    // Fail if pcb already a session leader
+    if siginfo_lock.is_session_leader {
+        return Err(SystemError::EPERM);
+    }
+
+    // Fail if a process group id already exists that equals the
+    // proposed session id.
+    if sid.pid_task(PidType::PGID).is_some() {
+        return Err(SystemError::EPERM);
+    }
+
+    let mut siginfo_guard = siginfo_lock.upgrade();
+    siginfo_guard.is_session_leader = true;
+    set_special_pids(&group_leader, &sid);
+
+    TtyJobCtrlManager::__proc_clear_tty(&mut siginfo_guard);
+    return Ok(session);
 }
 
-impl ProcessManager {
-    /// 根据sid获取会话
-    ///
-    /// ## 参数
-    ///
-    /// - `sid` : 会话的sid
-    ///
-    /// ## 返回值
-    ///
-    /// 如果找到了对应的会话,那么返回该会话,否则返回None
-    pub fn find_session(sid: Sid) -> Option<Arc<Session>> {
-        return ALL_SESSION.lock_irqsave().as_ref()?.get(&sid).cloned();
-    }
-
-    /// 向系统中添加一个会话
-    ///
-    /// ## 参数
-    ///
-    /// - `session` : Arc<Session>
-    ///
-    /// ## 返回值
-    ///
-    /// 无
-    pub fn add_session(session: Arc<Session>) {
-        ALL_SESSION
-            .lock_irqsave()
-            .as_mut()
-            .unwrap()
-            .insert(session.sid(), session.clone());
-        // log::debug!("New Session added, sid: {:?}", session.sid());
-    }
-
-    pub fn remove_session(sid: Sid) {
-        // log::debug!("Removing session: {:?}", sid.clone());
-        let mut all_sessions = ALL_SESSION.lock_irqsave();
-        if let Some(session) = all_sessions.as_mut().unwrap().remove(&sid) {
-            if Arc::strong_count(&session) <= 2 {
-                // 这里 Arc 计数为 1,意味着它只有在 all_groups 里有一个引用,移除后会自动释放
-                drop(session);
-            }
-        }
-    }
-}
-
-impl ProcessControlBlock {
-    pub fn session(&self) -> Option<Arc<Session>> {
-        let pg = self.process_group()?;
-        pg.session()
-    }
-
-    pub fn is_session_leader(&self) -> bool {
-        if let Some(pcb) = self.self_ref.upgrade() {
-            let session = pcb.session().unwrap();
-            if let Some(leader) = session.leader() {
-                return Arc::ptr_eq(&pcb, &leader);
-            }
-        }
-
-        return false;
-    }
-
-    /// 将进程移动到新会话中
-    /// 如果进程已经是会话领导者,则返回当前会话
-    /// 如果不是,则主动创建一个新会话,并将进程移动到新会话中,返回新会话
-    ///
-    /// ## 返回值
-    ///
-    /// 新会话
-    pub fn go_to_new_session(&self) -> Result<Arc<Session>, SystemError> {
-        if self.is_session_leader() {
-            return Ok(self.session().unwrap());
-        }
-
-        if self.is_process_group_leader() {
-            return Err(SystemError::EPERM);
-        }
-
-        let session = self.session().unwrap();
-
-        let mut self_group = self.process_group.lock();
-        if ProcessManager::find_session(self.pid()).is_some() {
-            return Err(SystemError::EPERM);
-        }
-        if ProcessManager::find_process_group(self.pid).is_some() {
-            return Err(SystemError::EPERM);
-        }
-        if let Some(old_pg) = self_group.upgrade() {
-            let mut old_pg_inner = old_pg.process_group_inner.lock();
-            let mut session_inner = session.session_inner.lock();
-            old_pg_inner.remove_process(&self.pid);
-            *self_group = Weak::new();
-
-            if old_pg_inner.is_empty() {
-                ProcessManager::remove_process_group(old_pg.pgid());
-                assert!(session_inner.process_groups.contains_key(&old_pg.pgid()));
-                session_inner.process_groups.remove(&old_pg.pgid());
-                if session_inner.is_empty() {
-                    ProcessManager::remove_session(session.sid());
-                }
-            }
-        }
-
-        let pcb = self.self_ref.upgrade().unwrap();
-        let new_pg = ProcessGroup::new(pcb.clone());
-        *self_group = Arc::downgrade(&new_pg);
-        ProcessManager::add_process_group(new_pg.clone());
-
-        let new_session = Session::new(new_pg.clone());
-        let mut new_pg_inner = new_pg.process_group_inner.lock();
-        new_pg_inner.session = Arc::downgrade(&new_session);
-        new_session.session_inner.lock().leader = Some(pcb.clone());
-        ProcessManager::add_session(new_session.clone());
-
-        let mut session_inner = session.session_inner.lock();
-        session_inner.remove_process(&pcb);
-
-        Ok(new_session)
-    }
-
-    pub fn sid(&self) -> Sid {
-        if let Some(session) = self.session() {
-            return session.sid();
-        }
-        return Sid::new(1);
-    }
+fn set_special_pids(current_session_group_leader: &Arc<ProcessControlBlock>, sid: &Arc<Pid>) {
+    let session = current_session_group_leader.task_session();
+    let change_sid = match session {
+        Some(s) => !Arc::ptr_eq(&s, sid),
+        None => true,
+    };
+
+    let pgrp = current_session_group_leader.task_pgrp();
+    let change_pgrp = match pgrp {
+        Some(pg) => !Arc::ptr_eq(&pg, sid),
+        None => true,
+    };
+    // log::debug!(
+    //     "leader: {}, change sid: {}, pgrp: {}, sid_raw: {}",
+    //     current_session_group_leader.raw_pid().data(),
+    //     change_sid,
+    //     change_pgrp,
+    //     sid.pid_vnr().data()
+    // );
+    if change_sid {
+        current_session_group_leader.change_pid(PidType::SID, sid.clone());
+    }
+    if change_pgrp {
+        current_session_group_leader.change_pid(PidType::PGID, sid.clone());
+    }
+
+    // log::debug!(
+    //     "after change, pgrp: {}",
+    //     current_session_group_leader
+    //         .task_pgrp()
+    //         .unwrap()
+    //         .pid_vnr()
+    //         .data()
+    // );
 }

+ 16 - 0
kernel/src/process/signal.rs

@@ -0,0 +1,16 @@
+use crate::process::ProcessControlBlock;
+use crate::process::ProcessManager;
+use alloc::sync::{Arc, Weak};
+
+impl ProcessManager {
+    pub fn same_thread_group(
+        group_leader: &Arc<ProcessControlBlock>,
+        real_parent: &Weak<ProcessControlBlock>,
+    ) -> bool {
+        group_leader
+            .threads_read_irqsave()
+            .group_tasks
+            .iter()
+            .any(|x| x.ptr_eq(real_parent))
+    }
+}

+ 2 - 2
kernel/src/process/stdio.rs

@@ -6,12 +6,12 @@ use crate::{
         file::{File, FileMode},
         ROOT_INODE,
     },
-    process::{Pid, ProcessManager},
+    process::{ProcessManager, RawPid},
 };
 
 /// @brief 初始化pid=1的进程的stdio
 pub fn stdio_init() -> Result<(), SystemError> {
-    if ProcessManager::current_pcb().pid() != Pid(1) {
+    if ProcessManager::current_pcb().raw_pid() != RawPid(1) {
         return Err(SystemError::EPERM);
     }
     let tty_path = format!(

+ 3 - 0
kernel/src/process/syscall/mod.rs

@@ -28,6 +28,9 @@ mod sys_wait4;
 
 #[cfg(target_arch = "x86_64")]
 mod sys_fork;
+
+#[cfg(target_arch = "x86_64")]
+mod sys_getpgrp;
 #[cfg(target_arch = "x86_64")]
 mod sys_getrlimit;
 #[cfg(target_arch = "x86_64")]

+ 5 - 5
kernel/src/process/syscall/sys_clone.rs

@@ -76,10 +76,10 @@ impl Syscall for SysClone {
         ProcessManager::copy_process(&current_pcb, &pcb, clone_args, frame)?;
 
         // 向procfs注册进程
-        procfs_register_pid(pcb.pid()).unwrap_or_else(|e| {
+        procfs_register_pid(pcb.raw_pid()).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to register pid to procfs, pid: [{:?}]. Error: {:?}",
-                pcb.pid(),
+                pcb.raw_pid(),
                 e
             )
         });
@@ -92,13 +92,13 @@ impl Syscall for SysClone {
             let addr = pcb.thread.read_irqsave().set_child_tid.unwrap();
             let mut writer =
                 UserBufferWriter::new(addr.as_ptr::<i32>(), core::mem::size_of::<i32>(), true)?;
-            writer.copy_one_to_user(&(pcb.pid().data() as i32), 0)?;
+            writer.copy_one_to_user(&(pcb.raw_pid().data() as i32), 0)?;
         }
 
         ProcessManager::wakeup(&pcb).unwrap_or_else(|e| {
             panic!(
                 "fork: Failed to wakeup new process, pid: [{:?}]. Error: {:?}",
-                pcb.pid(),
+                pcb.raw_pid(),
                 e
             )
         });
@@ -108,7 +108,7 @@ impl Syscall for SysClone {
             vfork.wait_for_completion_interruptible()?;
         }
 
-        return Ok(pcb.pid().0);
+        return Ok(pcb.raw_pid().0);
     }
 
     fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 23 - 9
kernel/src/process/syscall/sys_getpgid.rs

@@ -1,16 +1,19 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_GETPGID;
-use crate::process::Pid;
+use crate::process::pid::Pid;
 use crate::process::ProcessManager;
+use crate::process::RawPid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
+use alloc::sync::Arc;
 use alloc::vec::Vec;
 use system_error::SystemError;
+
 pub struct SysGetPgid;
 
 impl SysGetPgid {
-    fn pid(args: &[usize]) -> Pid {
-        Pid::new(args[0])
+    fn pid(args: &[usize]) -> RawPid {
+        RawPid::new(args[0])
     }
 }
 
@@ -30,12 +33,7 @@ impl Syscall for SysGetPgid {
     /// - 错误,不存在该进程
     fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let pid = Self::pid(args);
-        if pid == Pid(0) {
-            let current_pcb = ProcessManager::current_pcb();
-            return Ok(current_pcb.pgid().into());
-        }
-        let target_proc = ProcessManager::find(pid).ok_or(SystemError::ESRCH)?;
-        return Ok(target_proc.pgid().into());
+        return do_getpgid(pid);
     }
 
     fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
@@ -47,3 +45,19 @@ impl Syscall for SysGetPgid {
 }
 
 syscall_table_macros::declare_syscall!(SYS_GETPGID, SysGetPgid);
+
+/// 获取进程组ID
+///
+/// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/sys.c#1144
+pub(super) fn do_getpgid(pid: RawPid) -> Result<usize, SystemError> {
+    let grp: Arc<Pid> = if pid == RawPid(0) {
+        let current_pcb = ProcessManager::current_pcb();
+        current_pcb.task_pgrp().ok_or(SystemError::ESRCH)?
+    } else {
+        let p = ProcessManager::find_task_by_vpid(pid).ok_or(SystemError::ESRCH)?;
+        p.task_pgrp().ok_or(SystemError::ESRCH)?
+    };
+
+    let retval = grp.pid_vnr();
+    Ok(retval.into())
+}

+ 41 - 0
kernel/src/process/syscall/sys_getpgrp.rs

@@ -0,0 +1,41 @@
+use crate::arch::interrupt::TrapFrame;
+use crate::arch::syscall::nr::SYS_GETPGRP;
+use crate::process::RawPid;
+use crate::syscall::table::FormattedSyscallParam;
+use crate::syscall::table::Syscall;
+use alloc::vec::Vec;
+use system_error::SystemError;
+
+use super::sys_getpgid::do_getpgid;
+
+pub struct SysGetPgrp;
+
+impl Syscall for SysGetPgrp {
+    fn num_args(&self) -> usize {
+        1
+    }
+
+    /// # SYS_GETPGRP
+    ///
+    /// 实现 `getpgrp` 系统调用,用于获取当前进程的进程组 ID (PGID)。
+    ///
+    /// 根据 POSIX 标准,`getpgrp` 不需要任何参数,直接返回调用者的进程组 ID。
+    /// 在实现中,我们通过调用 `do_getpgid` 函数,并传入 `RawPid(0)` 来获取当前进程的 PGID。
+    ///
+    /// ## 参数
+    /// - `_args`: 系统调用的参数列表。对于 `getpgrp`,此参数未被使用,因为该系统调用不需要额外参数。
+    /// - `_frame`: 当前中断帧,包含系统调用的上下文信息。在此函数中未被直接使用。
+    ///
+    /// ## 返回值
+    /// - 成功时,返回当前进程的进程组 ID (PGID),类型为 `usize`。
+    /// - 如果发生错误(例如无法获取 PGID),返回 `SystemError` 错误码。
+    fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
+        return do_getpgid(RawPid(0));
+    }
+
+    fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {
+        vec![]
+    }
+}
+
+syscall_table_macros::declare_syscall!(SYS_GETPGRP, SysGetPgrp);

+ 2 - 7
kernel/src/process/syscall/sys_getpid.rs

@@ -16,13 +16,8 @@ impl Syscall for SysGetPid {
     /// 获取当前进程的pid
     fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let current_pcb = ProcessManager::current_pcb();
-        // if let Some(pid_ns) = &current_pcb.get_nsproxy().read().pid_namespace {
-        //     // 获取该进程在命名空间中的 PID
-        //     return Ok(current_pcb.pid_strcut().read().numbers[pid_ns.level].nr);
-        //     // 返回命名空间中的 PID
-        // }
-        // 默认返回 tgid
-        return Ok(current_pcb.tgid().into());
+
+        Ok(current_pcb.task_pid_vnr().into())
     }
 
     fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 6 - 1
kernel/src/process/syscall/sys_getppid.rs

@@ -1,6 +1,7 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_GETPPID;
 use crate::process::ProcessManager;
+use crate::process::RawPid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
@@ -16,7 +17,11 @@ impl Syscall for SysGetPpid {
     /// 获取当前进程的父进程id
     fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let current_pcb = ProcessManager::current_pcb();
-        return Ok(current_pcb.basic().ppid().into());
+        let parent_pcb = current_pcb.real_parent_pcb.read_irqsave().clone();
+        let parent_pcb = parent_pcb.upgrade().ok_or(SystemError::ESRCH)?;
+
+        let r = parent_pcb.task_tgid_vnr().unwrap_or(RawPid::new(0));
+        return Ok(r.into());
     }
 
     fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 1 - 1
kernel/src/process/syscall/sys_getrlimit.rs

@@ -32,7 +32,7 @@ impl Syscall for SysGetRlimit {
         let rlimit = Self::rlimit(args);
 
         do_prlimit64(
-            ProcessManager::current_pcb().pid(),
+            ProcessManager::current_pcb().raw_pid(),
             resource,
             core::ptr::null::<RLimit64>(),
             rlimit,

+ 12 - 14
kernel/src/process/syscall/sys_getsid.rs

@@ -1,17 +1,16 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_GETSID;
-use crate::process::Pid;
 use crate::process::ProcessManager;
+use crate::process::RawPid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
-use alloc::sync::Arc;
 use alloc::vec::Vec;
 use system_error::SystemError;
 pub struct SysGetsid;
 
 impl SysGetsid {
-    fn pid(args: &[usize]) -> Pid {
-        Pid::new(args[0])
+    fn pid(args: &[usize]) -> RawPid {
+        RawPid::new(args[0])
     }
 }
 
@@ -31,16 +30,15 @@ impl Syscall for SysGetsid {
     /// - pid: 指定一个进程号
     fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let pid = Self::pid(args);
-        let session = ProcessManager::current_pcb().session().unwrap();
-        let sid = session.sid().into();
-        if pid == Pid(0) {
-            return Ok(sid);
-        }
-        let pcb = ProcessManager::find(pid).ok_or(SystemError::ESRCH)?;
-        if !Arc::ptr_eq(&session, &pcb.session().unwrap()) {
-            return Err(SystemError::EPERM);
-        }
-        return Ok(sid);
+        let sid = if pid.data() == 0 {
+            ProcessManager::current_pcb()
+                .task_session()
+                .ok_or(SystemError::ESRCH)?
+        } else {
+            let p = ProcessManager::find_task_by_vpid(pid).ok_or(SystemError::ESRCH)?;
+            p.task_session().ok_or(SystemError::ESRCH)?
+        };
+        return Ok(sid.pid_vnr().into());
     }
 
     fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 1 - 1
kernel/src/process/syscall/sys_gettid.rs

@@ -14,7 +14,7 @@ impl Syscall for SysGetTid {
 
     fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let pcb = ProcessManager::current_pcb();
-        return Ok(pcb.pid.into());
+        return Ok(pcb.task_pid_vnr().into());
     }
 
     fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 4 - 4
kernel/src/process/syscall/sys_prlimit64.rs

@@ -7,7 +7,7 @@ use crate::{
     mm::{ucontext::UserStack, MemoryManagementArch},
     process::{
         resource::{RLimit64, RLimitID},
-        Pid,
+        RawPid,
     },
     syscall::user_access::UserBufferWriter,
 };
@@ -18,8 +18,8 @@ use crate::arch::interrupt::TrapFrame;
 pub struct SysPrlimit64;
 
 impl SysPrlimit64 {
-    fn pid(args: &[usize]) -> Pid {
-        Pid::new(args[0])
+    fn pid(args: &[usize]) -> RawPid {
+        RawPid::new(args[0])
     }
 
     fn resource(args: &[usize]) -> usize {
@@ -84,7 +84,7 @@ syscall_table_macros::declare_syscall!(SYS_PRLIMIT64, SysPrlimit64);
 /// - 如果old_limit不为NULL,则返回旧的资源限制到old_limit
 ///
 pub(super) fn do_prlimit64(
-    _pid: Pid,
+    _pid: RawPid,
     resource: usize,
     _new_limit: *const RLimit64,
     old_limit: *mut RLimit64,

+ 0 - 3
kernel/src/process/syscall/sys_set_tid_address.rs

@@ -1,6 +1,5 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SET_TID_ADDRESS;
-use crate::mm::verify_area;
 use crate::mm::VirtAddr;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
@@ -24,8 +23,6 @@ impl Syscall for SysSetTidAddress {
     /// 设置线程地址
     fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
         let ptr = Self::ptr(args);
-        verify_area(VirtAddr::new(ptr), core::mem::size_of::<i32>())
-            .map_err(|_| SystemError::EFAULT)?;
 
         let pcb = ProcessManager::current_pcb();
         pcb.thread.write_irqsave().clear_child_tid = Some(VirtAddr::new(ptr));

+ 7 - 2
kernel/src/process/syscall/sys_setfsgid.rs

@@ -1,5 +1,8 @@
+use alloc::sync::Arc;
+
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETFSGID;
+use crate::process::cred::Cred;
 use crate::process::cred::Kgid;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
@@ -25,11 +28,13 @@ impl Syscall for SysSetFsgid {
         let fsgid = Kgid::new(fsgid);
 
         let pcb = ProcessManager::current_pcb();
-        let mut guard = pcb.cred.lock();
+        let mut guard: crate::libs::spinlock::SpinLockGuard<'_, Arc<Cred>> = pcb.cred.lock();
         let old_fsgid = guard.fsgid;
 
         if fsgid == guard.gid || fsgid == guard.egid || fsgid == guard.sgid {
-            guard.setfsgid(fsgid.data());
+            let mut new_cred: Cred = (**guard).clone();
+            new_cred.setfsgid(fsgid.data());
+            *guard = Cred::new_arc(new_cred);
         }
 
         Ok(old_fsgid.data())

+ 5 - 1
kernel/src/process/syscall/sys_setfsuid.rs

@@ -1,5 +1,6 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETFSUID;
+use crate::process::cred::Cred;
 use crate::process::cred::Kuid;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
@@ -25,10 +26,13 @@ impl Syscall for SysSetFsuid {
 
         let pcb = ProcessManager::current_pcb();
         let mut guard = pcb.cred.lock();
+
         let old_fsuid = guard.fsuid;
 
         if fsuid == guard.uid || fsuid == guard.euid || fsuid == guard.suid {
-            guard.setfsuid(fsuid.data());
+            let mut new_cred: Cred = (**guard).clone();
+            new_cred.setfsuid(fsuid.data());
+            *guard = Cred::new_arc(new_cred);
         }
 
         Ok(old_fsuid.data())

+ 10 - 6
kernel/src/process/syscall/sys_setgid.rs

@@ -1,5 +1,6 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETGID;
+use crate::process::cred::Cred;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
@@ -23,19 +24,22 @@ impl Syscall for SysSetGid {
         let gid = Self::gid(args);
         let pcb = ProcessManager::current_pcb();
         let mut guard = pcb.cred.lock();
+        let mut new_cred: Cred = (**guard).clone();
 
         if guard.egid.data() == 0 {
-            guard.setgid(gid);
-            guard.setegid(gid);
-            guard.setsgid(gid);
-            guard.setfsgid(gid);
+            new_cred.setgid(gid);
+            new_cred.setegid(gid);
+            new_cred.setsgid(gid);
+            new_cred.setfsgid(gid);
         } else if guard.gid.data() == gid || guard.sgid.data() == gid {
-            guard.setegid(gid);
-            guard.setfsgid(gid);
+            new_cred.setegid(gid);
+            new_cred.setfsgid(gid);
         } else {
             return Err(SystemError::EPERM);
         }
 
+        *guard = Cred::new_arc(new_cred);
+
         return Ok(0);
     }
 

+ 72 - 22
kernel/src/process/syscall/sys_setpgid.rs

@@ -1,8 +1,12 @@
+use alloc::sync::Arc;
+
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETPGID;
+use crate::process::pid::PidType;
 use crate::process::Pgid;
-use crate::process::Pid;
+use crate::process::ProcessFlags;
 use crate::process::ProcessManager;
+use crate::process::RawPid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
@@ -10,8 +14,8 @@ use system_error::SystemError;
 pub struct SysSetPgid;
 
 impl SysSetPgid {
-    fn pid(args: &[usize]) -> Pid {
-        Pid::new(args[0])
+    fn pid(args: &[usize]) -> RawPid {
+        RawPid::new(args[0])
     }
 
     fn pgid(args: &[usize]) -> Pgid {
@@ -24,36 +28,82 @@ impl Syscall for SysSetPgid {
         2
     }
 
-    /// # 函数的功能
+    /// # sys_setpgid
     /// 设置指定进程的pgid
     ///
     /// ## 参数
     /// - pid: 指定进程号
     /// - pgid: 新的进程组号
+    ///
+    /// https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/sys.c#1073
     fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
-        let pid = Self::pid(args);
-        let pgid = Self::pgid(args);
-
-        let current_pcb = ProcessManager::current_pcb();
-        let pid = if pid == Pid(0) {
-            current_pcb.pid()
-        } else {
-            pid
-        };
-        let pgid = if pgid == Pgid::from(0) {
-            Pgid::from(pid.into())
-        } else {
-            pgid
-        };
-        if pid != current_pcb.pid() && !current_pcb.contain_child(&pid) {
+        let mut pid = Self::pid(args);
+        let mut pgid = Self::pgid(args);
+        let group_leader = ProcessManager::current_pcb()
+            .threads_read_irqsave()
+            .group_leader
+            .clone();
+
+        let group_leader = group_leader.upgrade().ok_or(SystemError::ESRCH)?;
+
+        if pid.data() == 0 {
+            pid = group_leader.task_pid_vnr();
+        }
+
+        if pgid.data() == 0 {
+            pgid = pid;
+        }
+
+        let p = ProcessManager::find_task_by_vpid(pid).ok_or(SystemError::ESRCH)?;
+        if !p.is_thread_group_leader() {
+            return Err(SystemError::EINVAL);
+        }
+        let real_parent = p.real_parent_pcb.read().clone();
+        if ProcessManager::same_thread_group(&group_leader, &real_parent) {
+            if !Arc::ptr_eq(
+                &p.task_session().unwrap(),
+                &group_leader.task_session().unwrap(),
+            ) {
+                return Err(SystemError::EPERM);
+            }
+
+            if !p.flags().contains(ProcessFlags::FORKNOEXEC) {
+                return Err(SystemError::EACCES);
+            }
+        } else if !Arc::ptr_eq(&p, &group_leader) {
             return Err(SystemError::ESRCH);
         }
 
-        if pgid.into() != pid.into() && ProcessManager::find_process_group(pgid).is_none() {
+        if p.sig_info_irqsave().is_session_leader {
             return Err(SystemError::EPERM);
         }
-        let pcb = ProcessManager::find(pid).ok_or(SystemError::ESRCH)?;
-        pcb.join_other_group(pgid)?;
+        let mut pgrp = p.pid();
+        if pgid != pid {
+            pgrp = ProcessManager::find_vpid(pgid).ok_or(SystemError::EPERM)?;
+            let g = pgrp.pid_task(PidType::PGID).ok_or(SystemError::EPERM)?;
+            let s1 = g.task_session();
+            let s2 = group_leader.task_session();
+            // 模拟C的 task_session(g) != task_session(group_leader) 判断
+            // 1. 如果两个会话都是None,视为相等
+            // 2. 如果只有一个为None,视为不等
+            // 3. 如果两个都有值,比较内部Pid
+            match (s1, s2) {
+                (None, None) => (), // 都为空,允许
+                (Some(_), None) | (None, Some(_)) => {
+                    return Err(SystemError::EPERM);
+                }
+                (Some(session1), Some(session2)) if !Arc::ptr_eq(&session1, &session2) => {
+                    return Err(SystemError::EPERM);
+                }
+                _ => (), // 会话相同,继续
+            }
+        }
+
+        let pp = p.task_pgrp().unwrap();
+
+        if !Arc::ptr_eq(&pp, &pgrp) {
+            p.change_pid(PidType::PGID, pgrp);
+        }
 
         return Ok(0);
     }

+ 8 - 2
kernel/src/process/syscall/sys_setresgid.rs

@@ -1,10 +1,12 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETRESGID;
+use crate::process::cred::Cred;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
 use system_error::SystemError;
+
 pub struct SysSetResGid;
 
 impl SysSetResGid {
@@ -27,12 +29,16 @@ impl Syscall for SysSetResGid {
             return Ok(0);
         }
 
+        let mut new_cred = (**guard).clone();
+
         if egid != usize::MAX {
-            guard.setegid(egid);
+            new_cred.setegid(egid);
         }
 
         let egid = guard.egid.data();
-        guard.setfsgid(egid);
+        new_cred.setfsgid(egid);
+
+        *guard = Cred::new_arc(new_cred);
 
         return Ok(0);
     }

+ 7 - 2
kernel/src/process/syscall/sys_setresuid.rs

@@ -1,10 +1,12 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETRESUID;
+use crate::process::cred::Cred;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
 use system_error::SystemError;
+
 pub struct SysSetResUid;
 
 impl SysSetResUid {
@@ -26,13 +28,16 @@ impl Syscall for SysSetResUid {
         if euid == usize::MAX || (euid == guard.euid.data() && euid == guard.fsuid.data()) {
             return Ok(0);
         }
+        let mut new_cred = (**guard).clone();
 
         if euid != usize::MAX {
-            guard.seteuid(euid);
+            new_cred.seteuid(euid);
         }
 
         let euid = guard.euid.data();
-        guard.setfsuid(euid);
+        new_cred.setfsuid(euid);
+
+        *guard = Cred::new_arc(new_cred);
 
         return Ok(0);
     }

+ 5 - 6
kernel/src/process/syscall/sys_setsid.rs

@@ -1,6 +1,6 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETSID;
-use crate::process::ProcessManager;
+use crate::process::session::ksys_setsid;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
@@ -15,11 +15,10 @@ impl Syscall for SysSetsid {
     /// # 函数的功能
     /// 创建新的会话
     fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
-        let pcb = ProcessManager::current_pcb();
-        let session = pcb.go_to_new_session()?;
-        let mut guard = pcb.sig_info_mut();
-        guard.set_tty(None);
-        Ok(session.sid().into())
+        ksys_setsid().map(|sid| {
+            // 返回会话ID
+            sid.data()
+        })
     }
 
     fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {

+ 9 - 4
kernel/src/process/syscall/sys_setuid.rs

@@ -1,10 +1,12 @@
 use crate::arch::interrupt::TrapFrame;
 use crate::arch::syscall::nr::SYS_SETUID;
+use crate::process::cred::Cred;
 use crate::process::ProcessManager;
 use crate::syscall::table::FormattedSyscallParam;
 use crate::syscall::table::Syscall;
 use alloc::vec::Vec;
 use system_error::SystemError;
+
 pub struct SysSetUid;
 
 impl SysSetUid {
@@ -23,15 +25,18 @@ impl Syscall for SysSetUid {
         let pcb = ProcessManager::current_pcb();
         let mut guard = pcb.cred.lock();
 
+        let mut new_cred = (**guard).clone();
+
         if guard.uid.data() == 0 {
-            guard.setuid(uid);
-            guard.seteuid(uid);
-            guard.setsuid(uid);
+            new_cred.setuid(uid);
+            new_cred.seteuid(uid);
+            new_cred.setsuid(uid);
         } else if uid == guard.uid.data() || uid == guard.suid.data() {
-            guard.seteuid(uid);
+            new_cred.seteuid(uid);
         } else {
             return Err(SystemError::EPERM);
         }
+        *guard = Cred::new_arc(new_cred);
 
         return Ok(0);
     }

+ 4 - 4
kernel/src/process/timer.rs

@@ -2,7 +2,7 @@ use crate::arch::ipc::signal::{SigCode, Signal};
 use crate::exception::InterruptArch;
 use crate::ipc::signal_types::SigType;
 use crate::process::CurrentIrqArch;
-use crate::process::Pid;
+use crate::process::RawPid;
 use crate::process::SigInfo;
 use crate::time::timer::{clock, Jiffies, Timer, TimerFunction};
 use alloc::{boxed::Box, sync::Arc};
@@ -61,7 +61,7 @@ impl AlarmTimer {
     /// ### 函数返回值
     ///
     /// AlarmTimer结构体
-    pub fn alarm_timer_init(pid: Pid, second: u64) -> AlarmTimer {
+    pub fn alarm_timer_init(pid: RawPid, second: u64) -> AlarmTimer {
         //初始化Timerfunc
         let timerfunc = AlarmTimerFunc::new(pid);
         let alarmtimer = AlarmTimer::new(timerfunc, second);
@@ -106,11 +106,11 @@ impl AlarmTimer {
 /// pid:发送消息的目标进程的pid
 #[derive(Debug)]
 pub struct AlarmTimerFunc {
-    pid: Pid,
+    pid: RawPid,
 }
 
 impl AlarmTimerFunc {
-    pub fn new(pid: Pid) -> Box<AlarmTimerFunc> {
+    pub fn new(pid: RawPid) -> Box<AlarmTimerFunc> {
         return Box::new(AlarmTimerFunc { pid });
     }
 }

+ 18 - 17
kernel/src/syscall/mod.rs

@@ -103,25 +103,26 @@ impl Syscall {
         if let Some(handler) = syscall_table().get(syscall_num) {
             // 使用以下代码可以打印系统调用号和参数,方便调试
 
-            // let show = false;
-            // if show {
-            //     log::debug!(
-            //         "pid: {} Syscall {} called with args {}",
-            //         ProcessManager::current_pid().data(),
-            //         handler.name,
-            //         handler.args_string(args)
-            //     );
-            // }
+            // let show = ProcessManager::current_pid().data() >= 8;
+            let show = false;
+            if show {
+                log::debug!(
+                    "pid: {} Syscall {} called with args {}",
+                    ProcessManager::current_pid().data(),
+                    handler.name,
+                    handler.args_string(args)
+                );
+            }
 
             let r = handler.inner_handle.handle(args, frame);
-            // if show {
-            //     log::debug!(
-            //         "pid: {} Syscall {} returned {:?}",
-            //         ProcessManager::current_pid().data(),
-            //         handler.name,
-            //         r
-            //     );
-            // }
+            if show {
+                log::debug!(
+                    "pid: {} Syscall {} returned {:?}",
+                    ProcessManager::current_pid().data(),
+                    handler.name,
+                    r
+                );
+            }
             return r;
         }
 

+ 1 - 1
kernel/src/tracepoint/basic_macro.rs

@@ -110,7 +110,7 @@ macro_rules! define_event_trace{
                 };
 
                 let process = $crate::process::ProcessManager::current_pcb();
-                let pid = process.pid().data() as _;
+                let pid = process.raw_pid().data() as _;
 
                 let common = $crate::tracepoint::TraceEntry {
                     type_: [<__ $name>].id() as _,

+ 2 - 2
kernel/src/virt/vm/kvm_host/vcpu.rs

@@ -13,7 +13,7 @@ use crate::{
         VirtCpuArch, VirtCpuStat,
     },
     libs::spinlock::{SpinLock, SpinLockGuard},
-    process::Pid,
+    process::RawPid,
     smp::cpu::ProcessorId,
     virt::vm::user_api::UapiKvmRun,
 };
@@ -57,7 +57,7 @@ pub struct VirtCpu {
     pub vcpu_id: usize,
     /// id alloctor获取
     pub _vcpu_idx: usize,
-    pub pid: Option<Pid>,
+    pub pid: Option<RawPid>,
     pub _preempted: bool,
     pub _ready: bool,
     pub _last_used_slot: Option<Arc<KvmMemSlot>>,