Add syscall getsid, setsid and refactor other syscalls

This commit is contained in:
Jianfeng Jiang 2023-11-16 17:24:12 +08:00 committed by Tate, Hongliang Tian
parent 9040fb54ea
commit 43fd1a52fa
5 changed files with 60 additions and 24 deletions

View File

@ -0,0 +1,30 @@
use crate::log_syscall_entry;
use crate::prelude::*;
use crate::process::{process_table, Pid};
use super::{SyscallReturn, SYS_GETSID};
pub fn sys_getsid(pid: Pid) -> Result<SyscallReturn> {
log_syscall_entry!(SYS_GETSID);
debug!("pid = {}", pid);
let session = current!().session().unwrap();
let sid = session.sid();
if pid == 0 {
return Ok(SyscallReturn::Return(sid as _));
}
let Some(process) = process_table::get_process(&pid) else {
return_errno_with_message!(Errno::ESRCH, "the process does not exist")
};
if !Arc::ptr_eq(&session, &process.session().unwrap()) {
return_errno_with_message!(
Errno::EPERM,
"the process and current process does not belong to the same session"
);
}
Ok(SyscallReturn::Return(sid as _))
}

View File

@ -34,14 +34,14 @@ pub fn do_sys_kill(filter: ProcessFilter, sig_num: SigNum) -> Result<()> {
}
}
ProcessFilter::WithPid(pid) => {
if let Some(process) = process_table::pid_to_process(pid) {
if let Some(process) = process_table::get_process(&pid) {
process.enqueue_signal(Box::new(signal));
} else {
return_errno_with_message!(Errno::ESRCH, "No such process in process table");
}
}
ProcessFilter::WithPgid(pgid) => {
if let Some(process_group) = process_table::pgid_to_process_group(pgid) {
if let Some(process_group) = process_table::get_process_group(&pgid) {
process_group.user_signal(signal);
} else {
return_errno_with_message!(Errno::ESRCH, "No such process group in process table");

View File

@ -77,12 +77,14 @@ use self::connect::sys_connect;
use self::execve::sys_execveat;
use self::getpeername::sys_getpeername;
use self::getrandom::sys_getrandom;
use self::getsid::sys_getsid;
use self::getsockname::sys_getsockname;
use self::getsockopt::sys_getsockopt;
use self::listen::sys_listen;
use self::pread64::sys_pread64;
use self::recvfrom::sys_recvfrom;
use self::sendto::sys_sendto;
use self::setsid::sys_setsid;
use self::setsockopt::sys_setsockopt;
use self::shutdown::sys_shutdown;
use self::socket::sys_socket;
@ -119,6 +121,7 @@ mod getpgrp;
mod getpid;
mod getppid;
mod getrandom;
mod getsid;
mod getsockname;
mod getsockopt;
mod gettid;
@ -155,6 +158,7 @@ mod sendto;
mod set_robust_list;
mod set_tid_address;
mod setpgid;
mod setsid;
mod setsockopt;
mod shutdown;
mod socket;
@ -274,6 +278,8 @@ define_syscall_nums!(
SYS_SETPGID = 109,
SYS_GETPPID = 110,
SYS_GETPGRP = 111,
SYS_SETSID = 112,
SYS_GETSID = 124,
SYS_STATFS = 137,
SYS_FSTATFS = 138,
SYS_PRCTL = 157,
@ -435,6 +441,8 @@ pub fn syscall_dispatch(
SYS_SETPGID => syscall_handler!(2, sys_setpgid, args),
SYS_GETPPID => syscall_handler!(0, sys_getppid),
SYS_GETPGRP => syscall_handler!(0, sys_getpgrp),
SYS_SETSID => syscall_handler!(0, sys_setsid),
SYS_GETSID => syscall_handler!(1, sys_getsid, args),
SYS_STATFS => syscall_handler!(2, sys_statfs, args),
SYS_FSTATFS => syscall_handler!(2, sys_fstatfs, args),
SYS_PRCTL => syscall_handler!(5, sys_prctl, args),

View File

@ -1,8 +1,6 @@
use crate::{
log_syscall_entry,
prelude::*,
process::{process_table, Pgid, Pid, ProcessGroup},
};
use crate::log_syscall_entry;
use crate::prelude::*;
use crate::process::{process_table, Pgid, Pid};
use super::{SyscallReturn, SYS_SETPGID};
@ -15,7 +13,7 @@ pub fn sys_setpgid(pid: Pid, pgid: Pgid) -> Result<SyscallReturn> {
let pgid = if pgid == 0 { pid } else { pgid };
debug!("pid = {}, pgid = {}", pid, pgid);
if pid != current.pid() && !current.children().lock().contains_key(&pid) {
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"
@ -25,27 +23,14 @@ pub fn sys_setpgid(pid: Pid, pgid: Pgid) -> Result<SyscallReturn> {
// 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::pgid_to_process_group(pgid).is_none() {
if pgid != pid && !process_table::contain_process_group(&pgid) {
return_errno_with_message!(Errno::EPERM, "process group must exist");
}
let process = process_table::pid_to_process(pid)
let process = process_table::get_process(&pid)
.ok_or(Error::with_message(Errno::ESRCH, "process does not exist"))?;
// if the process already belongs to the process group
if process.pgid() == pgid {
return Ok(SyscallReturn::Return(0));
}
if let Some(process_group) = process_table::pgid_to_process_group(pgid) {
process_group.add_process(process.clone());
process.set_process_group(Arc::downgrade(&process_group));
} else {
let new_process_group = Arc::new(ProcessGroup::new(process.clone()));
// new_process_group.add_process(process.clone());
process.set_process_group(Arc::downgrade(&new_process_group));
process_table::add_process_group(new_process_group);
}
process.to_other_group(pgid)?;
Ok(SyscallReturn::Return(0))
}

View File

@ -0,0 +1,13 @@
use crate::log_syscall_entry;
use crate::prelude::*;
use super::{SyscallReturn, SYS_SETSID};
pub fn sys_setsid() -> Result<SyscallReturn> {
log_syscall_entry!(SYS_SETSID);
let current = current!();
let session = current.to_new_session()?;
Ok(SyscallReturn::Return(session.sid() as _))
}