From 6d1144bb697f42cd513dffff49cb913a795ac183 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 31 May 2024 00:43:30 +0000 Subject: [PATCH] move unix socket unlink from services to router Signed-off-by: Jason Volk --- src/router/serve/unix.rs | 29 ++++++++++++++++++++++------- src/service/globals/mod.rs | 2 -- src/service/services.rs | 14 ++------------ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/router/serve/unix.rs b/src/router/serve/unix.rs index 2f0cf9cf..ae768d24 100644 --- a/src/router/serve/unix.rs +++ b/src/router/serve/unix.rs @@ -11,6 +11,7 @@ use hyper_util::{ }; use tokio::{ fs, + net::{unix::SocketAddr, UnixListener, UnixStream}, sync::broadcast::{self}, task::JoinSet, }; @@ -37,16 +38,14 @@ pub(super) async fn serve( } } - drop(listener); - tasks.shutdown().await; + fini(listener, tasks).await; Ok(()) } async fn accept( - server: &Arc, listener: &tokio::net::UnixListener, tasks: &mut JoinSet<()>, - mut app: IntoMakeService, builder: server::conn::auto::Builder, - conn: (tokio::net::UnixStream, tokio::net::unix::SocketAddr), + server: &Arc, listener: &UnixListener, tasks: &mut JoinSet<()>, mut app: IntoMakeService, + builder: server::conn::auto::Builder, conn: (UnixStream, SocketAddr), ) { let (socket, remote) = conn; let socket = TokioIo::new(socket); @@ -67,7 +66,7 @@ async fn accept( while tasks.try_join_next().is_some() {} } -async fn init(server: &Arc) -> Result { +async fn init(server: &Arc) -> Result { use std::os::unix::fs::PermissionsExt; let config = &server.config; @@ -89,7 +88,7 @@ async fn init(server: &Arc) -> Result { return Err(Error::Err(format!("Failed to create {dir:?} for socket {path:?}: {e}"))); } - let listener = tokio::net::UnixListener::bind(path); + let listener = UnixListener::bind(path); if let Err(e) = listener { return Err(Error::Err(format!("Failed to bind listener {path:?}: {e}"))); } @@ -105,3 +104,19 @@ async fn init(server: &Arc) -> Result { Ok(listener.unwrap()) } + +async fn fini(listener: UnixListener, mut tasks: JoinSet<()>) { + let local = listener.local_addr(); + + drop(listener); + tasks.shutdown().await; + + if let Ok(local) = local { + if let Some(path) = local.as_pathname() { + debug!(?path, "Removing unix socket file."); + if let Err(e) = fs::remove_file(path).await { + warn!(?path, "Failed to remove UNIX socket file: {e}"); + } + } + } +} diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 8f035fcd..723979da 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -395,8 +395,6 @@ impl Service { pub fn well_known_server(&self) -> &Option { &self.config.well_known.server } - pub fn unix_socket_path(&self) -> &Option { &self.config.unix_socket_path } - pub fn valid_cidr_range(&self, ip: &IPAddress) -> bool { for cidr in &self.cidr_range_denylist { if cidr.includes(ip) { diff --git a/src/service/services.rs b/src/service/services.rs index a2a9ca0b..a9356562 100644 --- a/src/service/services.rs +++ b/src/service/services.rs @@ -6,11 +6,8 @@ use std::{ use conduit::{debug_info, Result, Server}; use database::KeyValueDatabase; use lru_cache::LruCache; -use tokio::{ - fs, - sync::{broadcast, Mutex, RwLock}, -}; -use tracing::{debug, info, trace, warn}; +use tokio::sync::{broadcast, Mutex, RwLock}; +use tracing::{debug, info, trace}; use crate::{ account_data, admin, appservice, globals, key_backups, media, presence, pusher, rooms, sending, transaction_ids, @@ -321,13 +318,6 @@ bad_signature_ratelimiter: {bad_signature_ratelimiter} info!("Shutting down services"); self.interrupt().await; - debug!("Removing unix socket file."); - if let Some(path) = self.globals.unix_socket_path().as_ref() { - if let Err(e) = fs::remove_file(path).await { - warn!("Failed to remove UNIX socket file: {e}"); - } - } - debug!("Waiting for update worker..."); if let Some(updates_handle) = self.globals.updates_handle.lock().await.take() { updates_handle.abort();