From 8286249ff7ba6104495a9c50149d0bcddcac4c5d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 30 Mar 2025 17:51:07 +0200 Subject: [PATCH 1/2] Remove unreachable dup impls Dup calls with an empty path never reach scheme implementations --- logd/src/scheme.rs | 24 ------------------------ ptyd/src/controlterm.rs | 6 ------ ptyd/src/pgrp.rs | 5 ----- ptyd/src/resource.rs | 1 - ptyd/src/scheme.rs | 4 +--- ptyd/src/subterm.rs | 6 ------ ptyd/src/termios.rs | 5 ----- ptyd/src/winsize.rs | 5 ----- ramfs/src/scheme.rs | 6 ------ zerod/src/scheme.rs | 8 -------- 10 files changed, 1 insertion(+), 69 deletions(-) diff --git a/logd/src/scheme.rs b/logd/src/scheme.rs index 5a605c2d3f..921fe08c5f 100644 --- a/logd/src/scheme.rs +++ b/logd/src/scheme.rs @@ -57,30 +57,6 @@ impl SchemeMut for LogScheme { Ok(id) } - fn dup(&mut self, old_id: usize, buf: &[u8]) -> Result { - if !buf.is_empty() { - return Err(Error::new(EINVAL)); - } - - let context = { - let handle = self.handles.get(&old_id).ok_or(Error::new(EBADF))?; - handle.context.clone() - }; - - let id = self.next_id; - self.next_id += 1; - - self.handles.insert( - id, - LogHandle { - context, - bufs: BTreeMap::new(), - }, - ); - - Ok(id) - } - fn read(&mut self, id: usize, _buf: &mut [u8], _offset: u64, _flags: u32) -> Result { let _handle = self.handles.get(&id).ok_or(Error::new(EBADF))?; diff --git a/ptyd/src/controlterm.rs b/ptyd/src/controlterm.rs index 2ef8290525..6dbe3f9ac0 100644 --- a/ptyd/src/controlterm.rs +++ b/ptyd/src/controlterm.rs @@ -8,7 +8,6 @@ use crate::pty::Pty; use crate::resource::Resource; /// Read side of a pipe -#[derive(Clone)] pub struct PtyControlTerm { pty: Rc>, flags: usize, @@ -28,10 +27,6 @@ impl PtyControlTerm { } impl Resource for PtyControlTerm { - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - fn pty(&self) -> Weak> { Rc::downgrade(&self.pty) } @@ -45,7 +40,6 @@ impl Resource for PtyControlTerm { } fn read(&mut self, buf: &mut [u8]) -> Result> { - self.notified_read = false; let mut pty = self.pty.borrow_mut(); diff --git a/ptyd/src/pgrp.rs b/ptyd/src/pgrp.rs index a41f6c5272..353dd33b2e 100644 --- a/ptyd/src/pgrp.rs +++ b/ptyd/src/pgrp.rs @@ -9,7 +9,6 @@ use crate::pty::Pty; use crate::resource::Resource; /// Read side of a pipe -#[derive(Clone)] pub struct PtyPgrp { pty: Weak>, flags: usize, @@ -25,10 +24,6 @@ impl PtyPgrp { } impl Resource for PtyPgrp { - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - fn pty(&self) -> Weak> { self.pty.clone() } diff --git a/ptyd/src/resource.rs b/ptyd/src/resource.rs index 7ef300dbf8..ed0c499c03 100644 --- a/ptyd/src/resource.rs +++ b/ptyd/src/resource.rs @@ -7,7 +7,6 @@ use syscall::flag::EventFlags; use crate::pty::Pty; pub trait Resource { - fn boxed_clone(&self) -> Box; fn pty(&self) -> Weak>; fn flags(&self) -> usize; diff --git a/ptyd/src/scheme.rs b/ptyd/src/scheme.rs index e9e9aff26a..73c2626a58 100644 --- a/ptyd/src/scheme.rs +++ b/ptyd/src/scheme.rs @@ -64,9 +64,7 @@ impl SchemeBlock for PtyScheme { let handle: Box = { let old_handle = self.handles.get(&old_id).ok_or(Error::new(EBADF))?; - if buf.is_empty() { - old_handle.boxed_clone() - } else if buf == b"pgrp" { + if buf == b"pgrp" { Box::new(PtyPgrp::new(old_handle.pty(), old_handle.flags())) } else if buf == b"termios" { Box::new(PtyTermios::new(old_handle.pty(), old_handle.flags())) diff --git a/ptyd/src/subterm.rs b/ptyd/src/subterm.rs index d6202c8fb0..96a7357765 100644 --- a/ptyd/src/subterm.rs +++ b/ptyd/src/subterm.rs @@ -8,7 +8,6 @@ use crate::pty::Pty; use crate::resource::Resource; /// Read side of a pipe -#[derive(Clone)] pub struct PtySubTerm { pty: Weak>, flags: usize, @@ -28,10 +27,6 @@ impl PtySubTerm { } impl Resource for PtySubTerm { - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - fn pty(&self) -> Weak> { self.pty.clone() } @@ -49,7 +44,6 @@ impl Resource for PtySubTerm { } fn read(&mut self, buf: &mut [u8]) -> Result> { - self.notified_read = false; if let Some(pty_lock) = self.pty.upgrade() { diff --git a/ptyd/src/termios.rs b/ptyd/src/termios.rs index e71337f65d..4918612350 100644 --- a/ptyd/src/termios.rs +++ b/ptyd/src/termios.rs @@ -9,7 +9,6 @@ use crate::pty::Pty; use crate::resource::Resource; /// Read side of a pipe -#[derive(Clone)] pub struct PtyTermios { pty: Weak>, flags: usize, @@ -25,10 +24,6 @@ impl PtyTermios { } impl Resource for PtyTermios { - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - fn pty(&self) -> Weak> { self.pty.clone() } diff --git a/ptyd/src/winsize.rs b/ptyd/src/winsize.rs index 33942ebb82..a03edc48aa 100644 --- a/ptyd/src/winsize.rs +++ b/ptyd/src/winsize.rs @@ -9,7 +9,6 @@ use crate::pty::Pty; use crate::resource::Resource; /// Read side of a pipe -#[derive(Clone)] pub struct PtyWinsize { pty: Weak>, flags: usize, @@ -25,10 +24,6 @@ impl PtyWinsize { } impl Resource for PtyWinsize { - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - fn pty(&self) -> Weak> { self.pty.clone() } diff --git a/ramfs/src/scheme.rs b/ramfs/src/scheme.rs index c076513fe5..0e3c9e17e7 100644 --- a/ramfs/src/scheme.rs +++ b/ramfs/src/scheme.rs @@ -247,12 +247,6 @@ impl SchemeSync for Scheme { fn unlink(&mut self, path: &str, ctx: &CallerCtx) -> Result<()> { self.remove_dentry(path, ctx.uid, ctx.gid, false) } - fn dup(&mut self, old_inode: usize, _buf: &[u8], _ctx: &CallerCtx) -> Result { - Ok(OpenResult::ThisScheme { - number: old_inode, - flags: NewFdFlags::POSITIONED, - }) - } fn read( &mut self, inode: usize, diff --git a/zerod/src/scheme.rs b/zerod/src/scheme.rs index 4496477e3a..2071d816bf 100644 --- a/zerod/src/scheme.rs +++ b/zerod/src/scheme.rs @@ -10,14 +10,6 @@ impl SchemeMut for ZeroScheme { Ok(0) } - fn dup(&mut self, _file: usize, buf: &[u8]) -> Result { - if !buf.is_empty() { - return Err(Error::new(EINVAL)); - } - - Ok(0) - } - fn read(&mut self, _file: usize, buf: &mut [u8], _offset: u64, _flags: u32) -> Result { match self.0 { Ty::Null => Ok(0), From 72d04da8a9e9d369b0c24360bbd45b66f0f025d7 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 31 Mar 2025 20:10:19 +0200 Subject: [PATCH 2/2] init: Improve command spawning The stdout and stderr of the spawned processes are not captured by init, so attempting to write captured input is useless. Also show the exit code in case the child process returned an non-zero exit code. --- init/src/main.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/init/src/main.rs b/init/src/main.rs index 8b8eef158f..39186a44ec 100644 --- a/init/src/main.rs +++ b/init/src/main.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use std::env; use std::ffi::CString; use std::fs::{read_dir, File}; -use std::io::{BufRead, BufReader, Result, Write}; +use std::io::{BufRead, BufReader, Result}; use std::path::Path; use std::process::Command; @@ -186,15 +186,13 @@ pub fn run(file: &Path) -> Result<()> { } match command.spawn() { - Ok(child) => match child.wait_with_output() { - Ok(output) => { - std::io::stdout() - .write_all(output.stdout.as_slice()) - .unwrap(); - std::io::stderr() - .write_all(output.stderr.as_slice()) - .unwrap(); - println!("{cmd} done."); + Ok(mut child) => match child.wait() { + Ok(exit_status) => { + if !exit_status.success() { + println!("{cmd} failed with {exit_status}"); + } else { + println!("{cmd} done."); + } } Err(err) => { println!("init: failed to wait for '{}': {}", line, err)