lib.rs (3295B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024-2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::{fs::Permissions, io::ErrorKind, net::SocketAddr, os::unix::fs::PermissionsExt as _}; 18 19 use listenfd::ListenFd; 20 use tokio::net::{TcpListener, UnixListener}; 21 use tracing::info; 22 23 pub mod api; 24 pub mod auth; 25 pub mod config; 26 pub mod constants; 27 pub mod db; 28 pub mod error; 29 pub mod json; 30 pub mod notification; 31 pub mod subject; 32 33 pub enum Serve { 34 Tcp(SocketAddr), 35 Unix { 36 path: String, 37 permission: Permissions, 38 }, 39 } 40 41 impl Serve { 42 /// Resolve listener from a config and environment 43 fn resolve(&self) -> Result<Listener, std::io::Error> { 44 // Check if systemd is passing a socket 45 let mut listenfd = ListenFd::from_env(); 46 if let Ok(Some(unix)) = listenfd.take_unix_listener(0) { 47 info!(target: "api", 48 "Server listening on activated unix socket {:?}", 49 unix.local_addr()? 50 ); 51 unix.set_nonblocking(true)?; 52 Ok(Listener::Unix(UnixListener::from_std(unix)?)) 53 } else if let Ok(Some(tcp)) = listenfd.take_tcp_listener(0) { 54 info!(target: "api", 55 "Server listening on activated TCP socket {:?}", 56 tcp.local_addr()? 57 ); 58 tcp.set_nonblocking(true)?; 59 Ok(Listener::Tcp(TcpListener::from_std(tcp)?)) 60 } else { 61 match self { 62 Serve::Tcp(socket_addr) => { 63 info!(target: "api", "Server listening on {socket_addr}"); 64 let listener = std::net::TcpListener::bind(socket_addr)?; 65 listener.set_nonblocking(true)?; 66 Ok(Listener::Tcp(TcpListener::from_std(listener)?)) 67 } 68 Serve::Unix { path, permission } => { 69 info!(target: "api", 70 "Server listening on unxis domain socket {path} {:o}", 71 permission.mode() 72 ); 73 if let Err(e) = std::fs::remove_file(path) { 74 let kind = e.kind(); 75 if kind != ErrorKind::NotFound { 76 return Err(e); 77 } 78 } 79 let listener = std::os::unix::net::UnixListener::bind(path)?; 80 std::fs::set_permissions(path, permission.clone())?; 81 listener.set_nonblocking(true)?; 82 Ok(Listener::Unix(UnixListener::from_std(listener)?)) 83 } 84 } 85 } 86 } 87 } 88 89 enum Listener { 90 Tcp(TcpListener), 91 Unix(UnixListener), 92 }