Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couple of minor cleanups #970

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/exec/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{cutils::cerr, log::dev_debug};

pub(super) trait Process: Sized {
/// IO Events that this process should handle.
type Event: Copy + Eq + Debug;
type Event: Copy + Debug;
/// Reason why the event loop should break.
///
/// See [`EventRegistry::set_break`] for more information.
Expand Down Expand Up @@ -208,8 +208,7 @@ impl<T: Process> EventRegistry<T> {
self.status = Status::Stop(StopReason::Exit(reason));
}

/// Return whether a break reason has been set already. This function will return `false` after
/// [`EventRegistry::event_loop`] has been called.
/// Return whether a break reason has been set already.
pub(super) fn got_break(&self) -> bool {
self.status.is_break()
}
Expand All @@ -219,7 +218,7 @@ impl<T: Process> EventRegistry<T> {
/// The event loop will continue indefinitely unless you call [`EventRegistry::set_break`] or
/// [`EventRegistry::set_exit`].
#[track_caller]
pub(super) fn event_loop(&mut self, process: &mut T) -> StopReason<T> {
pub(super) fn event_loop(mut self, process: &mut T) -> StopReason<T> {
let mut event_queue = Vec::with_capacity(self.poll_fds.len());

loop {
Expand All @@ -232,7 +231,7 @@ impl<T: Process> EventRegistry<T> {
}

for event in event_queue.drain(..) {
process.on_event(event, self);
process.on_event(event, &mut self);

if let Some(reason) = self.status.take_exit() {
return StopReason::Exit(reason);
Expand Down
4 changes: 2 additions & 2 deletions src/exec/use_pty/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub(in crate::exec) fn exec_pty(
}
}

let exit_reason = closure.run(&mut registry);
let exit_reason = closure.run(registry);
// FIXME (ogsudo): Retry if `/dev/tty` is revoked.

// Flush the terminal
Expand Down Expand Up @@ -350,7 +350,7 @@ impl ParentClosure {
})
}

fn run(&mut self, registry: &mut EventRegistry<Self>) -> io::Result<ExitReason> {
fn run(&mut self, registry: EventRegistry<Self>) -> io::Result<ExitReason> {
match registry.event_loop(self) {
StopReason::Break(err) | StopReason::Exit(ParentExit::Backchannel(err)) => Err(err),
StopReason::Exit(ParentExit::Command(exit_reason)) => Ok(exit_reason),
Expand Down
54 changes: 16 additions & 38 deletions src/su/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, mem, path::PathBuf};
use std::{mem, path::PathBuf};

use crate::common::SudoString;

Expand Down Expand Up @@ -138,20 +138,20 @@ impl TryFrom<SuOptions> for SuRunOptions {
}

fn reject_all(context: &str, opts: SuOptions) -> Result<(), String> {
macro_rules! tuple {
($expr:expr) => {
(&$expr as &dyn IsAbsent, {
let name = concat!("--", stringify!($expr));
if name.contains('_') {
Cow::Owned(name.replace('_', "-"))
} else {
Cow::Borrowed(name)
}
})
macro_rules! ensure_options_absent {
($($opt:ident,)*) => {
let SuOptions {
$($opt),*
} = opts;

$(if !$opt.is_absent() {
let name = concat!("--", stringify!($opt)).replace('_', "-");
return Err(format!("{context} conflicts with {name}"));
})*
};
}

let SuOptions {
ensure_options_absent! {
command,
group,
help,
Expand All @@ -163,37 +163,15 @@ fn reject_all(context: &str, opts: SuOptions) -> Result<(), String> {
version,
whitelist_environment,
positional_args,
} = opts;

let flags = [
tuple!(command),
tuple!(group),
tuple!(help),
tuple!(login),
tuple!(preserve_environment),
tuple!(pty),
tuple!(shell),
tuple!(supp_group),
tuple!(version),
tuple!(whitelist_environment),
];
for (value, name) in flags {
ensure_is_absent(context, value, &name)?;
}
};

ensure_is_absent(context, &positional_args, "positional argument")?;
if !positional_args.is_absent() {
return Err(format!("{context} conflicts with positional argument"));
}

Ok(())
}

fn ensure_is_absent(context: &str, thing: &dyn IsAbsent, name: &str) -> Result<(), String> {
if thing.is_absent() {
Ok(())
} else {
Err(format!("{context} conflicts with {name}"))
}
}

trait IsAbsent {
fn is_absent(&self) -> bool;
}
Expand Down
Loading