Skip to main content

edit

Function edit 

Source
pub async fn edit<F>(running_as_root: bool, f: F) -> Result<()>
where F: FnOnce(&mut ConfigAst) -> Result<()>,
Expand description

Load, mutate, and save /etc/ssh/sshd_config in one call.

Mirrors ConfigService::edit: the closure mutates the AST in place; on success the result is validated and installed.

§Concurrency

The entire load → mutate → save critical section is serialized across processes by an advisory lock (flock) acquired on edit_lock_path before the read and held until the install completes. This prevents two concurrent toride instances (or toride + another editor) from each loading the same original config, applying their edit, and the second install clobbering the first.

Implementation note: the lock guard is held across the awaited load/save so the critical section stays non-blocking from the runtime’s perspective at the .await points (the file I/O and the privileged sshd -t/install run on blocking threads inside load/save). This avoids tokio::task::block_in_place + a nested Handle::block_on, which would panic with “can call blocking only when running on the multi-threaded runtime” when edit() is driven from a current-thread runtime (the default for #[tokio::test] and many embedded runtimes). The caller’s closure f stays FnOnce (no Send/'static bound) because the guard, lock, and backing file are all plain locals dropped in declaration order at scope end.

§Errors

Propagates any error from locking, loading, the closure, or saving.