Skip to content

Commit

Permalink
sessions: Remove process from session/group only when zombie is colle…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
rafalmiel committed Oct 21, 2023
1 parent f456894 commit ae195ae
Showing 9 changed files with 85 additions and 16 deletions.
13 changes: 13 additions & 0 deletions cykusz-rs/src/arch/x86_64/output/mod.rs
Original file line number Diff line number Diff line change
@@ -257,3 +257,16 @@ macro_rules! log3 {
//$crate::arch::output::log_fmt(format_args!($($arg)*)).unwrap();
}};
}

#[macro_export]
macro_rules! logln4 {
($fmt:expr) => (log4!(concat!("[log4]: ", $fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (log4!(concat!("[log4]: ", $fmt, "\n"), $($arg)*));
}

#[macro_export]
macro_rules! log4 {
($($arg:tt)*) => {{
//$crate::arch::output::log_fmt(format_args!($($arg)*)).unwrap();
}};
}
19 changes: 19 additions & 0 deletions cykusz-rs/src/kernel/fs/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::sync::Arc;
use core::sync::atomic::{AtomicUsize, Ordering};

use syscall_defs::stat::Stat;
use syscall_defs::OpenFlags;

use crate::kernel::fs::inode::INode;
@@ -16,6 +17,7 @@ pub struct Pipe {

impl Pipe {
pub fn new() -> Arc<Pipe> {
logln4!("Created PIPE");
Arc::new(Pipe {
buf: BufferQueue::new(4096 * 4),

@@ -41,7 +43,24 @@ impl Pipe {
}
}

impl Drop for Pipe {
fn drop(&mut self) {
logln4!("Dopped PIPE");
}
}

impl INode for Pipe {
fn stat(&self) -> Result<Stat> {
let mut stat = Stat::default();

stat.st_mode.insert(syscall_defs::stat::Mode::IFIFO);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXU);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXG);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXO);

Ok(stat)
}

fn read_at(&self, _offset: usize, buf: &mut [u8]) -> Result<usize> {
Ok(self.buf.read_data(buf)?)
}
2 changes: 1 addition & 1 deletion cykusz-rs/src/kernel/fs/ramfs.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@ use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering;

use syscall_defs::poll::PollEventFlags;
use syscall_defs::{FileType, OpenFlags};
use syscall_defs::stat::Stat;
use syscall_defs::{FileType, OpenFlags};

use crate::kernel::device::Device;
use crate::kernel::fs::devnode::DevNode;
6 changes: 2 additions & 4 deletions cykusz-rs/src/kernel/sched/mod.rs
Original file line number Diff line number Diff line change
@@ -200,6 +200,8 @@ impl Scheduler {

let forked = current.fork();

logln4!("fork {} -> {}", current.tid(), forked.tid());

self.tasks.register_task(forked.clone());

sessions().register_process(forked.clone());
@@ -266,10 +268,6 @@ impl Scheduler {

self.tasks.remove_task(current.tid());

if let Err(e) = sessions().remove_process(current) {
panic!("Failed to remove process from a session {:?}", e);
}

current.migrate_children_to_init();

self.sched.exit(status)
31 changes: 29 additions & 2 deletions cykusz-rs/src/kernel/session.rs
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ impl Group {

leader.set_gid(group.id);

logln4!("New group {}, sid: {}", leader.pid(), leader.sid());

group.processes.lock().insert(leader.pid(), leader);

Arc::new(group)
@@ -93,6 +95,8 @@ impl Session {
groups: Spin::new(hashbrown::HashMap::new()),
};

logln4!("New session {}", leader.pid());

leader.set_sid(session.id);

session
@@ -109,6 +113,7 @@ impl Session {
if let Some(group) = groups.get(&process.gid()) {
if let Ok(num) = group.remove_process(process.pid()) {
if num == 0 {
logln4!("Remove group {}", process.gid());
groups.remove(&process.gid());
}

@@ -126,12 +131,28 @@ impl Session {

let to = if group != 0 { groups.get(&group) } else { None };

if group != 0 && to.is_none() {
for (_id, _g) in groups.iter() {
logln4!("Group {} not found, existing: {}", group, id);
}
}

logln4!(
"Move from {} to {}",
from.id(),
if to.is_some() { to.unwrap().id() } else { 0 }
);

if group == 0 || (to.is_none() && group == process.pid()) {
from.remove_process(process.pid())?;

if groups.insert(process.pid(), Group::new(process)).is_some() {
if groups
.insert(process.pid(), Group::new(process.clone()))
.is_some()
{
Err(SyscallError::EPERM)
} else {
logln4!("Group {} inserted", process.gid());
Ok(0)
}
} else if let Some(to) = to {
@@ -168,6 +189,10 @@ impl Session {
group.for_each(&f);
}
}

pub fn id(&self) -> usize {
self.id
}
}

pub struct Sessions {
@@ -229,6 +254,8 @@ impl Sessions {
if let Some(session) = sessions.get(&process.sid()) {
assert!(!process.is_session_leader());

logln4!("Add {} to session {}", process.pid(), session.id());

session.register_task(process);
} else {
assert!(process.is_session_leader());
@@ -269,7 +296,7 @@ impl Sessions {
pub fn set_pgid(&self, pid: usize, gid: usize) -> SyscallResult {
let caller = current_task().process_leader();

logln3!("set_pgid {} {}", pid, gid);
logln4!("set_pgid {} {}", pid, gid);

let process = if pid == 0 || pid == caller.pid() {
caller.clone()
2 changes: 1 addition & 1 deletion cykusz-rs/src/kernel/syscall/mod.rs
Original file line number Diff line number Diff line change
@@ -94,4 +94,4 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64)
};

res
}
}
12 changes: 6 additions & 6 deletions cykusz-rs/src/kernel/syscall/sys.rs
Original file line number Diff line number Diff line change
@@ -1098,12 +1098,12 @@ pub fn sys_stat(fd: u64, path: u64, path_len: u64, stat: u64) -> SyscallResult {
let task = current_task_ref();

let file_dir = match fd {
OpenFD::Fd(fd) => {
task.get_handle(fd).ok_or(SyscallError::EBADFD)?.inode.clone()
}
OpenFD::Cwd => {
task.get_dent().ok_or(SyscallError::EBADFD)?.clone()
}
OpenFD::Fd(fd) => task
.get_handle(fd)
.ok_or(SyscallError::EBADFD)?
.inode
.clone(),
OpenFD::Cwd => task.get_dent().ok_or(SyscallError::EBADFD)?.clone(),
OpenFD::None => {
return Err(SyscallError::EINVAL);
}
7 changes: 7 additions & 0 deletions cykusz-rs/src/kernel/task/children_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::sync::Arc;

use crate::kernel::sched::current_task_ref;
use crate::kernel::session::sessions;
use intrusive_collections::LinkedList;
use syscall_defs::waitpid::WaitPidFlags;
use syscall_defs::{SyscallError, SyscallResult};
@@ -85,6 +86,12 @@ impl WaitPidEvents {

if status.is_exited() || status.is_signaled() {
t.remove_from_parent();

if t.is_process_leader() {
if let Err(e) = sessions().remove_process(&t.me()) {
panic!("Failed to remove process from a session {:?}", e);
}
}
}

cur.remove();
9 changes: 7 additions & 2 deletions cykusz-rs/src/kernel/task/mod.rs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ use crate::arch::task::Task as ArchTask;
use crate::kernel::fs::dirent::DirEntryItem;
use crate::kernel::fs::root_dentry;
use crate::kernel::sched::{new_task_tid, SleepFlags};

use crate::kernel::signal::{SignalResult, Signals, KSIGSTOPTHR};
use crate::kernel::sync::{RwSpin, Spin, SpinGuard};
use crate::kernel::task::children_events::WaitPidEvents;
@@ -690,7 +691,11 @@ impl Task {

pub fn signal(&self, sig: usize) -> bool {
logln2!("signal {} sig: {}", self.pid(), sig);
self.do_signal(sig, false)
if self.state() != TaskState::Unused {
self.do_signal(sig, false)
} else {
false
}
}

pub fn signal_thread(&self, sig: usize) -> bool {
@@ -761,6 +766,6 @@ impl Task {

impl Drop for Task {
fn drop(&mut self) {
logln!("drop task {}", self.tid());
logln4!("drop task {}", self.tid());
}
}

0 comments on commit ae195ae

Please sign in to comment.