lib.rs (3429B)
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 Systemd, 40 } 41 42 impl Serve { 43 /// Resolve listener from a config and environment 44 fn resolve(&self) -> Result<Listener, std::io::Error> { 45 match self { 46 Serve::Tcp(socket_addr) => { 47 info!(target: "api", "Server listening on {socket_addr}"); 48 let listener = std::net::TcpListener::bind(socket_addr)?; 49 listener.set_nonblocking(true)?; 50 Ok(Listener::Tcp(TcpListener::from_std(listener)?)) 51 } 52 Serve::Unix { path, permission } => { 53 info!(target: "api", 54 "Server listening on unxis domain socket {path} {:o}", 55 permission.mode() 56 ); 57 if let Err(e) = std::fs::remove_file(path) { 58 let kind = e.kind(); 59 if kind != ErrorKind::NotFound { 60 return Err(e); 61 } 62 } 63 let listener = std::os::unix::net::UnixListener::bind(path)?; 64 std::fs::set_permissions(path, permission.clone())?; 65 listener.set_nonblocking(true)?; 66 Ok(Listener::Unix(UnixListener::from_std(listener)?)) 67 } 68 Serve::Systemd => { 69 let mut listenfd = ListenFd::from_env(); 70 if let Ok(Some(unix)) = listenfd.take_unix_listener(0) { 71 info!(target: "api", 72 "Server listening on activated unix socket {:?}", 73 unix.local_addr()? 74 ); 75 unix.set_nonblocking(true)?; 76 Ok(Listener::Unix(UnixListener::from_std(unix)?)) 77 } else if let Ok(Some(tcp)) = listenfd.take_tcp_listener(0) { 78 info!(target: "api", 79 "Server listening on activated TCP socket {:?}", 80 tcp.local_addr()? 81 ); 82 tcp.set_nonblocking(true)?; 83 Ok(Listener::Tcp(TcpListener::from_std(tcp)?)) 84 } else { 85 Err(std::io::Error::other("Missing systemd activated socket")) 86 } 87 } 88 } 89 } 90 } 91 92 enum Listener { 93 Tcp(TcpListener), 94 Unix(UnixListener), 95 }