Skip to main content

net_io/
inherit.rs

1//! Inherited listeners (systemd socket activation, FastCGI descriptor 0).
2//!
3//! The functions arrive in `0.4.0`; the error type exists in every build.
4
5use std::fmt;
6use std::io;
7
8/// Why inherited descriptors were refused.
9///
10/// ```
11/// assert_eq!(net_io::InheritError::BadCount.to_string(), "LISTEN_FDS is not a valid count");
12/// ```
13#[non_exhaustive]
14#[derive(Debug)]
15pub enum InheritError {
16    /// `LISTEN_FDS` is not a non-negative number.
17    BadCount,
18    /// More descriptors than allowed.
19    TooMany {
20        /// The maximum.
21        max: u32,
22    },
23    /// The descriptor is not a socket.
24    NotASocket {
25        /// The descriptor.
26        fd: i32,
27    },
28    /// The socket is not listening.
29    NotListening {
30        /// The descriptor.
31        fd: i32,
32    },
33    /// The socket's family or type is not one net-io serves.
34    WrongKind {
35        /// The descriptor.
36        fd: i32,
37    },
38    /// `LISTEN_FDNAMES` does not match `LISTEN_FDS`.
39    NamesMismatch,
40    /// `LISTEN_PIDFDID` does not match this process.
41    PidFdMismatch,
42    /// An OS call failed.
43    Os(io::Error),
44}
45
46impl fmt::Display for InheritError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            InheritError::BadCount => f.write_str("LISTEN_FDS is not a valid count"),
50            InheritError::TooMany { max } => write!(f, "more than {max} inherited descriptors"),
51            InheritError::NotASocket { fd } => write!(f, "descriptor {fd} is not a socket"),
52            InheritError::NotListening { fd } => {
53                write!(f, "descriptor {fd} is not a listening socket")
54            }
55            InheritError::WrongKind { fd } => {
56                write!(f, "descriptor {fd} is not a TCP or Unix stream socket")
57            }
58            InheritError::NamesMismatch => f.write_str("LISTEN_FDNAMES does not match LISTEN_FDS"),
59            InheritError::PidFdMismatch => {
60                f.write_str("LISTEN_PIDFDID does not match this process")
61            }
62            InheritError::Os(e) => write!(f, "{e}"),
63        }
64    }
65}
66
67impl std::error::Error for InheritError {
68    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69        match self {
70            InheritError::Os(e) => Some(e),
71            _ => None,
72        }
73    }
74}