mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-08 14:16:47 +00:00
* feat(ipc): 完成对ipc的系统调用的重构 * refactor(ipc): 优化结构 Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com> * feat: fmt Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com> --------- Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com> Co-authored-by: Vitus <zhzvitus@gmail.com> Co-authored-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
45 lines
1.5 KiB
Rust
45 lines
1.5 KiB
Rust
use crate::arch::ipc::signal::{SigCode, Signal};
|
||
use crate::ipc::signal_types::{SigInfo, SigType};
|
||
use crate::process::{process_group::Pgid, Pid, ProcessManager};
|
||
use core::sync::atomic::compiler_fence;
|
||
use system_error::SystemError;
|
||
|
||
/// ### 杀死一个进程
|
||
pub fn kill_process(pid: Pid, 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);
|
||
|
||
let ret = sig
|
||
.send_signal_info(Some(&mut info), pid)
|
||
.map(|x| x as usize);
|
||
|
||
compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||
ret
|
||
}
|
||
|
||
/// ### 杀死一个进程组
|
||
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
|
||
}
|
||
Ok(0)
|
||
}
|
||
|
||
/// ### 杀死所有进程
|
||
/// - 该函数会杀死所有进程,除了当前进程和init进程
|
||
pub fn kill_all(sig: Signal) -> Result<usize, SystemError> {
|
||
let current_pid = ProcessManager::current_pcb().pid();
|
||
let all_processes = ProcessManager::get_all_processes();
|
||
|
||
for pid_val in all_processes {
|
||
if pid_val == current_pid || pid_val.data() == 1 {
|
||
continue;
|
||
}
|
||
kill_process(pid_val, sig)?; // Call the new common function
|
||
}
|
||
Ok(0)
|
||
}
|