OperServ: add SHUTDOWN and RESTART
All checks were successful
CI / check (push) Successful in 3m31s

This commit is contained in:
Jean Chevronnet 2026-07-16 01:02:10 +00:00
parent dd43b9a331
commit b2442f85e6
No known key found for this signature in database
6 changed files with 96 additions and 1 deletions

View file

@ -1155,6 +1155,45 @@
assert!(notice(&e.handle(NetEvent::Privmsg { from: "000AAAAAD".into(), to: "42SAAAAAH".into(), text: "FORBID ADD NICK x y".into() }), "Access denied"), "non-oper refused");
}
// OperServ SHUTDOWN/RESTART (admin) emit a Shutdown action (which the link
// layer turns into a process exit) and are refused to non-operators.
#[test]
fn operserv_shutdown_and_restart() {
use echo_nickserv::NickServ;
use echo_operserv::OperServ;
let path = std::env::temp_dir().join("echo-osshutdown.jsonl");
let _ = std::fs::remove_file(&path);
let mut db = Db::open(&path, "42S");
db.scram_iterations = 4096;
db.register("boss", "pw", None).unwrap();
let mut e = Engine::new(
vec![
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
Box::new(OperServ { uid: "42SAAAAAH".into() }),
],
db,
);
let mut opers = std::collections::HashMap::new();
opers.insert("boss".to_string(), Privs::default().with(echo_api::Priv::Admin));
e.set_opers(opers);
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "h".into(), ip: "0.0.0.0".into() });
e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: "IDENTIFY pw".into() });
let os = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAH".into(), text: t.into() });
// A non-oper is refused and no Shutdown action is produced.
e.handle(NetEvent::UserConnect { uid: "000AAAAAD".into(), nick: "rando".into(), host: "h".into(), ip: "0.0.0.0".into() });
let out = os(&mut e, "000AAAAAD", "SHUTDOWN now");
assert!(out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("Access denied"))), "non-oper refused");
assert!(!out.iter().any(|a| matches!(a, NetAction::Shutdown { .. })), "no shutdown for non-oper");
// The admin's SHUTDOWN emits a Shutdown (restart=false) with the reason.
let out = os(&mut e, "000AAAAAB", "SHUTDOWN maintenance");
assert!(out.iter().any(|a| matches!(a, NetAction::Shutdown { restart, reason } if !*restart && reason.contains("maintenance"))), "shutdown action: {out:?}");
// RESTART sets restart=true.
let out = os(&mut e, "000AAAAAB", "RESTART");
assert!(out.iter().any(|a| matches!(a, NetAction::Shutdown { restart, .. } if *restart)), "restart action: {out:?}");
}
// OperServ SVSPART (admin) forces a user out of a channel.
#[test]
fn operserv_svspart() {

View file

@ -70,6 +70,10 @@ pub async fn run(mut proto: Box<dyn Protocol>, engine: Arc<Mutex<Engine>>, addr:
dispatch_email(&email, to, subject, text, html);
continue;
}
if let NetAction::Shutdown { restart, reason } = act {
let _ = write.flush().await;
shutdown(restart, &reason);
}
for out in proto.serialize(&act) {
send(&mut write, &out).await?;
}
@ -82,6 +86,9 @@ pub async fn run(mut proto: Box<dyn Protocol>, engine: Arc<Mutex<Engine>>, addr:
Some(action) = irc_rx.recv() => {
if let NetAction::SendEmail { to, subject, text, html } = action {
dispatch_email(&email, to, subject, text, html);
} else if let NetAction::Shutdown { restart, reason } = action {
let _ = write.flush().await;
shutdown(restart, &reason);
} else {
for out in proto.serialize(&action) {
send(&mut write, &out).await?;
@ -134,6 +141,19 @@ fn dispatch_email(email: &Option<crate::config::Email>, to: String, subject: Str
});
}
// Stop the process on an operator's SHUTDOWN/RESTART. Every committed change is
// already fsync'd, so exiting loses nothing. A restart exits non-zero so a
// supervisor configured to restart-on-failure (systemd Restart=on-failure)
// brings us straight back; a plain shutdown exits cleanly and stays down.
fn shutdown(restart: bool, reason: &str) -> ! {
if restart {
tracing::info!(%reason, "operator requested restart, exiting for supervisor to respawn");
std::process::exit(2);
}
tracing::info!(%reason, "operator requested shutdown, exiting");
std::process::exit(0);
}
async fn send(write: &mut (impl AsyncWriteExt + Unpin), line: &str) -> Result<()> {
tracing::debug!(dir = ">>", %line);
write.write_all(line.as_bytes()).await?;