move unix socket unlink from services to router

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-05-31 00:43:30 +00:00 committed by June 🍓🦴
parent 2e45cb281a
commit 6d1144bb69
3 changed files with 24 additions and 21 deletions

View file

@ -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<Server>, listener: &tokio::net::UnixListener, tasks: &mut JoinSet<()>,
mut app: IntoMakeService<Router>, builder: server::conn::auto::Builder<TokioExecutor>,
conn: (tokio::net::UnixStream, tokio::net::unix::SocketAddr),
server: &Arc<Server>, listener: &UnixListener, tasks: &mut JoinSet<()>, mut app: IntoMakeService<Router>,
builder: server::conn::auto::Builder<TokioExecutor>, 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<Server>) -> Result<tokio::net::UnixListener> {
async fn init(server: &Arc<Server>) -> Result<UnixListener> {
use std::os::unix::fs::PermissionsExt;
let config = &server.config;
@ -89,7 +88,7 @@ async fn init(server: &Arc<Server>) -> Result<tokio::net::UnixListener> {
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<Server>) -> Result<tokio::net::UnixListener> {
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}");
}
}
}
}

View file

@ -395,8 +395,6 @@ impl Service {
pub fn well_known_server(&self) -> &Option<OwnedServerName> { &self.config.well_known.server }
pub fn unix_socket_path(&self) -> &Option<PathBuf> { &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) {

View file

@ -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();