Debian Codegraph @

src/manager.rs

raw symbols SCIP
1 use std::io;
2 use std::net::{TcpListener, UdpSocket};
3
4 #[cfg(not(windows))]
5 use std::os::unix::net::{UnixDatagram, UnixListener};
6
7 #[cfg(not(windows))]
8 use crate::unix as imp;
9
10 #[cfg(windows)]
11 use crate::windows as imp;
12
13 /// A helper object that gives access to raw file descriptors.
14 pub struct ListenFd {
15 fds: Vec<Option<imp::FdType>>,
16 }
17
18 impl ListenFd {
19 /// Creates the listenfd manager object from the environment.
20 pub fn from_env() -> ListenFd {
21 match imp::get_fds() {
22 Some(fds) => ListenFd {
23 fds: fds.into_iter().map(Some).collect(),
24 },
25 None => ListenFd::empty(),
26 }
27 }
28
29 /// Creates an empty listenfd object.
30 ///
31 /// This is helpful when the ability to work with external file
32 /// descriptors should be disabled in certain code paths. This
33 /// way the functions on the object will just never return
34 /// sockets.
35 pub fn empty() -> ListenFd {
36 ListenFd { fds: vec![] }
37 }
38
39 /// Returns the number of fds in the manager object.
40 ///
41 /// Note that even if fds are taken out of the manager this count
42 /// does not change.
43 #[allow(clippy::len_without_is_empty)]
44 pub fn len(&self) -> usize {
45 self.fds.len()
46 }
47
48 fn with_fd<R, F: FnOnce(imp::FdType) -> io::Result<R>>(
49 &mut self,
50 idx: usize,
51 f: F,
52 ) -> io::Result<Option<R>> {
53 let bucket = match self.fds.get_mut(idx) {
54 Some(None) | None => return Ok(None),
55 Some(bucket) => bucket,
56 };
57 f(*bucket.as_ref().unwrap()).map(|rv| {
58 bucket.take();
59 Some(rv)
60 })
61 }
62
63 /// Takes the TCP listener at an index.
64 ///
65 /// If the given index has been used before `Ok(None)` is returned,
66 /// otherwise the fd at that index is returned as `TcpListener`. If
67 /// the fd at that position is not a TCP socket then an error is
68 /// returned and the fd is left at its place.
69 pub fn take_tcp_listener(&mut self, idx: usize) -> io::Result<Option<TcpListener>> {
70 self.with_fd(idx, imp::make_tcp_listener)
71 }
72
73 /// Takes the UNIX stream listener at an index.
74 ///
75 /// If the given index has been used before `Ok(None)` is returned,
76 /// otherwise the fd at that index is returned as `UnixListener`. If
77 /// the fd at that position is not a UNIX stream socket then an error is
78 /// returned and the fd is left at its place.
79 ///
80 /// This function is only available on unix platforms.
81 #[cfg(not(windows))]
82 pub fn take_unix_listener(&mut self, idx: usize) -> io::Result<Option<UnixListener>> {
83 self.with_fd(idx, imp::make_unix_listener)
84 }
85
86 /// Takes the UDP socket at an index.
87 ///
88 /// If the given index has been used before `Ok(None)` is returned,
89 /// otherwise the fd at that index is returned as `UdpSocket`. If
90 /// the fd at that position is not a UDP socket then an error is
91 /// returned and the fd is left at its place.
92 pub fn take_udp_socket(&mut self, idx: usize) -> io::Result<Option<UdpSocket>> {
93 let _idx = idx;
94 self.with_fd(idx, imp::make_udp_socket)
95 }
96
97 /// Takes the UNIX datagram socket at an index.
98 ///
99 /// If the given index has been used before `Ok(None)` is returned,
100 /// otherwise the fd at that index is returned as `UnixDatagram`. If
101 /// the fd at that position is not a UNIX datagram socket then an error is
102 /// returned and the fd is left at its place.
103 ///
104 /// This function is only available on unix platforms.
105 #[cfg(not(windows))]
106 pub fn take_unix_datagram(&mut self, idx: usize) -> io::Result<Option<UnixDatagram>> {
107 self.with_fd(idx, imp::make_unix_datagram)
108 }
109
110 /// Takes a custom socket on unix platforms.
111 ///
112 /// You have to provide a socket family, socket type, and a hint
113 /// for the validation error, e.g.
114 /// `libc::AF_UNIX, libc::SOCK_SEQPACKET, "unix seqpacket socket"`.
115 ///
116 /// The file descriptor will be validated to actually be a socket
117 /// with the appropriate options, set as CLOEXEC, and converted to
118 /// the given Rust type using `FromRawFd`.
119 ///
120 /// This function is only available on unix platforms.
121 #[cfg(not(windows))]
122 pub fn take_custom<T: std::os::unix::prelude::FromRawFd>(
123 &mut self,
124 idx: usize,
125 sock_fam: libc::c_int,
126 sock_type: libc::c_int,
127 hint: &str,
128 ) -> io::Result<Option<T>> {
129 self.with_fd(idx, |fd| imp::make_custom(fd, sock_fam, sock_type, hint))
130 }
131
132 /// Takes the `RawFd` on unix platforms.
133 #[cfg(not(windows))]
134 pub fn take_raw_fd(&mut self, idx: usize) -> io::Result<Option<imp::FdType>> {
135 let _idx = idx;
136 self.with_fd(idx, Ok)
137 }
138
139 /// Takes the `RawSocket` on windows platforms.
140 ///
141 /// This will error if the fd at this position is not a socket (but a handle).
142 #[cfg(windows)]
143 pub fn take_raw_socket(&mut self, idx: usize) -> io::Result<Option<imp::RawSocket>> {
144 let _idx = idx;
145 Ok(None)
146 }
147 }