taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

lib.rs (3452B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024, 2025, 2026 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 crypto;
     28 pub mod db;
     29 pub mod error;
     30 pub mod json;
     31 pub mod notification;
     32 pub mod subject;
     33 
     34 pub enum Serve {
     35     Tcp(SocketAddr),
     36     Unix {
     37         path: String,
     38         permission: Permissions,
     39     },
     40     Systemd,
     41 }
     42 
     43 impl Serve {
     44     /// Resolve listener from a config and environment
     45     fn resolve(&self) -> Result<Listener, std::io::Error> {
     46         match self {
     47             Serve::Tcp(socket_addr) => {
     48                 info!(target: "api", "Server listening on {socket_addr}");
     49                 let listener = std::net::TcpListener::bind(socket_addr)?;
     50                 listener.set_nonblocking(true)?;
     51                 Ok(Listener::Tcp(TcpListener::from_std(listener)?))
     52             }
     53             Serve::Unix { path, permission } => {
     54                 info!(target: "api",
     55                     "Server listening on unxis domain socket {path} {:o}",
     56                     permission.mode()
     57                 );
     58                 if let Err(e) = std::fs::remove_file(path) {
     59                     let kind = e.kind();
     60                     if kind != ErrorKind::NotFound {
     61                         return Err(e);
     62                     }
     63                 }
     64                 let listener = std::os::unix::net::UnixListener::bind(path)?;
     65                 std::fs::set_permissions(path, permission.clone())?;
     66                 listener.set_nonblocking(true)?;
     67                 Ok(Listener::Unix(UnixListener::from_std(listener)?))
     68             }
     69             Serve::Systemd => {
     70                 let mut listenfd = ListenFd::from_env();
     71                 if let Ok(Some(unix)) = listenfd.take_unix_listener(0) {
     72                     info!(target: "api",
     73                         "Server listening on activated unix socket {:?}",
     74                         unix.local_addr()?
     75                     );
     76                     unix.set_nonblocking(true)?;
     77                     Ok(Listener::Unix(UnixListener::from_std(unix)?))
     78                 } else if let Ok(Some(tcp)) = listenfd.take_tcp_listener(0) {
     79                     info!(target: "api",
     80                         "Server listening on activated TCP socket {:?}",
     81                         tcp.local_addr()?
     82                     );
     83                     tcp.set_nonblocking(true)?;
     84                     Ok(Listener::Tcp(TcpListener::from_std(tcp)?))
     85                 } else {
     86                     Err(std::io::Error::other("Missing systemd activated socket"))
     87                 }
     88             }
     89         }
     90     }
     91 }
     92 
     93 enum Listener {
     94     Tcp(TcpListener),
     95     Unix(UnixListener),
     96 }