From d0719efcb4986151cd1e6f87c69a37f9a335fff8 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Mon, 21 Apr 2025 23:51:16 +0800 Subject: [PATCH] Clarify some API usages --- kernel/src/fs/procfs/pid/stat.rs | 8 ++------ kernel/src/process/kill.rs | 2 +- kernel/src/process/posix_thread/mod.rs | 2 +- kernel/src/process/process/mod.rs | 25 +++++++++++++++++++------ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/kernel/src/fs/procfs/pid/stat.rs b/kernel/src/fs/procfs/pid/stat.rs index 99092379..ce483c5c 100644 --- a/kernel/src/fs/procfs/pid/stat.rs +++ b/kernel/src/fs/procfs/pid/stat.rs @@ -85,17 +85,13 @@ impl FileOps for StatFileOps { let pid = process.pid(); let comm = process.executable_path(); - let ppid = process.parent().pid(); let state = if process.status().is_zombie() { 'Z' } else { 'R' }; - let pgrp = if let Some(pgrp) = process.process_group() { - pgrp.pgid() - } else { - 0 - }; + let ppid = process.parent().pid(); + let pgrp = process.pgid(); let mut stat_output = String::new(); writeln!( diff --git a/kernel/src/process/kill.rs b/kernel/src/process/kill.rs index afa3c0f5..c1c1ce2d 100644 --- a/kernel/src/process/kill.rs +++ b/kernel/src/process/kill.rs @@ -166,7 +166,7 @@ fn current_thread_sender_ids(signum: Option<&SigNum>, ctx: &Context) -> SignalSe let euid = credentials.euid(); let sid = signum.and_then(|signum| { if *signum == SIGCONT { - Some(ctx.process.session().unwrap().sid()) + Some(ctx.process.sid()) } else { None } diff --git a/kernel/src/process/posix_thread/mod.rs b/kernel/src/process/posix_thread/mod.rs index 1647c8ca..445f9870 100644 --- a/kernel/src/process/posix_thread/mod.rs +++ b/kernel/src/process/posix_thread/mod.rs @@ -149,7 +149,7 @@ impl PosixThread { if let Some(signum) = signum && *signum == SIGCONT { - let receiver_sid = self.process().session().unwrap().sid(); + let receiver_sid = self.process().sid(); if receiver_sid == sender.sid().unwrap() { return Ok(()); } diff --git a/kernel/src/process/process/mod.rs b/kernel/src/process/process/mod.rs index 313c8b6f..15b7b54b 100644 --- a/kernel/src/process/process/mod.rs +++ b/kernel/src/process/process/mod.rs @@ -344,11 +344,6 @@ impl Process { // *********** Process group & Session *********** - /// Returns the process group to which the process belongs. - pub fn process_group(&self) -> Option> { - self.process_group.lock().upgrade() - } - /// Returns the process group ID of the process. // // FIXME: If we call this method without holding the process table lock, it may return zero if @@ -365,9 +360,27 @@ impl Process { self.session().map_or(0, |session| session.sid()) } + /// Returns the process group to which the process belongs. + /// + /// **Deprecated:** This is a very poorly designed API. Almost every use of this API is wrong + /// because some race condition is not handled correctly. Such usages are being refactored and + /// this API will be removed soon. So DO NOT ATTEMPT TO USE THIS API IN NEW CODE unless you + /// know exactly what you're doing. + pub fn process_group(&self) -> Option> { + self.process_group.lock().upgrade() + } + /// Returns the session to which the process belongs. + /// + /// **Deprecated:** This is a very poorly designed API. Almost every use of this API is wrong + /// because some race condition is not handled correctly. Such usages are being refactored and + /// this API will be removed soon. So DO NOT ATTEMPT TO USE THIS API IN NEW CODE unless you + /// know exactly what you're doing. pub fn session(&self) -> Option> { - self.process_group()?.session() + self.process_group + .lock() + .upgrade() + .and_then(|group| group.session()) } /// Returns whether the process is session leader.