Refactor and test set{pgid,sid}

This commit is contained in:
Ruihan Li
2025-04-21 14:17:02 +08:00
committed by Jianfeng Jiang
parent 4f2ce276a0
commit 7e4509df9c
13 changed files with 562 additions and 489 deletions

View File

@ -3,35 +3,24 @@
use super::SyscallReturn;
use crate::{
prelude::*,
process::{process_table, Pgid, Pid},
process::{Pgid, Pid},
};
pub fn sys_setpgid(pid: Pid, pgid: Pgid, ctx: &Context) -> Result<SyscallReturn> {
let current = ctx.process;
// if pid is 0, pid should be the pid of current process
// The documentation quoted below is from
// <https://www.man7.org/linux/man-pages/man2/setpgid.2.html>.
// "If `pid` is zero, then the process ID of the calling process is used."
let pid = if pid == 0 { current.pid() } else { pid };
// if pgid is 0, pgid should be pid
// "If `pgid` is zero, then the PGID of the process specified by `pid` is made the same as its
// process ID."
let pgid = if pgid == 0 { pid } else { pgid };
debug!("pid = {}, pgid = {}", pid, pgid);
if pid != current.pid() && !current.has_child(&pid) {
return_errno_with_message!(
Errno::ESRCH,
"cannot set pgid for process other than current or children of current"
);
}
// FIXME: If pid is child process of current and already calls execve, should return error.
// How can we determine a child process has called execve?
// only can move process to an existing group or self
if pgid != pid && !process_table::contain_process_group(&pgid) {
return_errno_with_message!(Errno::EPERM, "process group must exist");
}
let process = process_table::get_process(pid)
.ok_or(Error::with_message(Errno::ESRCH, "process does not exist"))?;
process.to_other_group(pgid)?;
current.move_process_to_group(pid, pgid)?;
Ok(SyscallReturn::Return(0))
}

View File

@ -4,8 +4,7 @@ use super::SyscallReturn;
use crate::prelude::*;
pub fn sys_setsid(_ctx: &Context) -> Result<SyscallReturn> {
let current = current!();
let session = current.to_new_session()?;
let sid = current!().to_new_session()?;
Ok(SyscallReturn::Return(session.sid() as _))
Ok(SyscallReturn::Return(sid as _))
}