From 29170ccbc762756628fe0a2458f841854189ffcc Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 18 Jul 2024 19:38:47 +0200 Subject: [PATCH] Fix sigchld test. --- src/scheme/pipe.rs | 9 ++++++--- src/syscall/process.rs | 28 ++++++++++++++++------------ src/syscall/time.rs | 2 ++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/scheme/pipe.rs b/src/scheme/pipe.rs index aa929845c0..e3d2a0ae35 100644 --- a/src/scheme/pipe.rs +++ b/src/scheme/pipe.rs @@ -213,6 +213,11 @@ impl KernelScheme for PipeScheme { loop { let mut vec = pipe.queue.lock(); + if !pipe.reader_is_alive.load(Ordering::Relaxed) { + return Err(Error::new(EPIPE)); + } + + let bytes_left = MAX_QUEUE_SIZE.saturating_sub(vec.len()); let bytes_to_write = core::cmp::min(bytes_left, user_buf.len()); let src_buf = user_buf @@ -244,9 +249,7 @@ impl KernelScheme for PipeScheme { return Ok(0); } - if !pipe.reader_is_alive.load(Ordering::SeqCst) { - return Err(Error::new(EPIPE)); - } else if fcntl_flags & O_NONBLOCK as u32 != 0 { + if fcntl_flags & O_NONBLOCK as u32 != 0 { return Err(Error::new(EAGAIN)); } else if !pipe.write_condition.wait(vec, "PipeWrite::write") { return Err(Error::new(EINTR)); diff --git a/src/syscall/process.rs b/src/syscall/process.rs index aa9837526b..66cf32facb 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -91,7 +91,9 @@ pub fn exit(status: usize) -> ! { let process = current_process.read(); (process.pgid, process.ppid) }; - let _ = kill(ppid, SIGCHLD); + if let Some(parent) = process::PROCESSES.read().get(&ppid).map(Arc::clone) { + let _ = send_signal(KillTarget::Process(parent), SIGCHLD, true, &mut false); + } // Transfer child processes to parent (TODO: to init) { @@ -365,19 +367,21 @@ pub fn send_signal( send_signal(KillTarget::Process(parent), SIGCHLD, true, killed_self)?; } Sent::SucceededSigcont { ppid, pgid } => { - process::PROCESSES + let parent = process::PROCESSES .read() .get(&ppid) - .ok_or(Error::new(ESRCH))? - .read() - .waitpid - .send( - WaitpidKey { - pid: Some(proc_info.pid), - pgid: Some(pgid), - }, - (proc_info.pid, 0xffff), - ); + .map(Arc::clone) + .ok_or(Error::new(ESRCH))?; + let waitpid = Arc::clone(&parent.read().waitpid); + waitpid.send( + WaitpidKey { + pid: Some(proc_info.pid), + pgid: Some(pgid), + }, + (proc_info.pid, 0xffff), + ); + // POSIX XSI allows but does not require SIGCONT to send signals to the parent. + //send_signal(KillTarget::Process(parent), SIGCHLD, true, killed_self)?; } } diff --git a/src/syscall/time.rs b/src/syscall/time.rs index ac81cab211..3cb871d2b5 100644 --- a/src/syscall/time.rs +++ b/src/syscall/time.rs @@ -74,5 +74,7 @@ pub fn nanosleep(req_buf: UserSliceRo, rem_buf_opt: Option) -> Resu pub fn sched_yield() -> Result<()> { context::switch(); + // TODO: Do this check in userspace + context::signal::signal_handler(); Ok(()) }