From b63d1204be8b9e264a645352c594e136f248ea3d Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 15 Jul 2023 22:51:47 +0300 Subject: WIP: bind to UNIX sockets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently this requires a helper crate to make Hyper aware of UNIX sockets. That's fine. That's not a priority for now — but the code is practically there. --- kittybox-rs/src/main.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'kittybox-rs/src/main.rs') diff --git a/kittybox-rs/src/main.rs b/kittybox-rs/src/main.rs index c588ad8..cc11f3a 100644 --- a/kittybox-rs/src/main.rs +++ b/kittybox-rs/src/main.rs @@ -264,11 +264,43 @@ async fn main() { match listenfd.take_tcp_listener(i) { Ok(Some(tcp)) => servers.push(build_hyper(tcp)), Ok(None) => {}, - Err(err) => { - tracing::error!("Error binding to socket in fd {}: {}", i, err); - } + Err(err) => tracing::error!("Error binding to socket in fd {}: {}", i, err) } } + // TODO this requires the `hyperlocal` crate + //#[rustfmt::skip] + /*#[cfg(unix)] { + let build_hyper_unix = |unix: std::os::unix::net::UnixListener| { + { + use std::os::linux::net::SocketAddrExt; + + let local_addr = unix.local_addr().unwrap(); + if let Some(pathname) = local_addr.as_pathname() { + tracing::info!("Listening on unix:{}", pathname.display()); + } else if let Some(name) = { + #[cfg(linux)] + local_addr.as_abstract_name(); + #[cfg(not(linux))] + None::<&[u8]> + } { + tracing::info!("Listening on unix:@{}", String::from_utf8_lossy(name)); + } else { + tracing::info!("Listening on unnamed unix socket"); + } + } + unix.set_nonblocking(true).unwrap(); + + hyper::server::Server::builder(unix) + .serve(router.clone().into_make_service()) + }; + for i in 0..(listenfd.len()) { + match listenfd.take_unix_listener(i) { + Ok(Some(unix)) => servers.push(build_hyper_unix(unix)), + Ok(None) => {}, + Err(err) => tracing::error!("Error binding to socket in fd {}: {}", i, err) + } + } + }*/ if servers.is_empty() { servers.push(build_hyper({ let listen_addr = env::var("SERVE_AT") @@ -292,16 +324,7 @@ async fn main() { ), #[cfg(tokio_unstable)] |server| { tokio::task::Builder::new() - // We leak the String here. It is acceptable, as the string - // is reasonably small and needs to live forever. - .name({ - let name = format!("Kittybox HTTP acceptor: {}", server.local_addr()); - - // Polyfill for unstablized [`String::leak`] - // - // SAFETY: the bytes come from a [`String`], which is valid UTF-8. - unsafe { std::str::from_utf8_unchecked(name.into_bytes().leak()) } - }) + .name(format!("Kittybox HTTP acceptor: {}", server.local_addr()).as_str()) .spawn( server.with_graceful_shutdown( cancellation_token.clone().cancelled_owned() -- cgit 1.4.1