annotate src/lib.rs @ 24:1941e9d9819c

Fix unsound manipulation of env vars Modifying env vars in multi-threaded process is unsound but this crate was neither checking the number of threads nor mark its functions as `unsafe`. This change fixes it by both adding a check and adding an `unsafe` function that can bypass that check if needed.
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Fri, 28 Feb 2025 13:52:31 +0100
parents f33e2c048104
children 8e20daee41ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
1 //! A convenience crate for optionally supporting systemd socket activation.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
2 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
3 //! ## About
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
4 //!
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
5 //! **Important:** because of various reasons it is recommended to call the [`init`] function at
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
6 //! the start of your program!
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
7 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
8 //! The goal of this crate is to make socket activation with systemd in your project trivial.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
9 //! It provides a replacement for `std::net::SocketAddr` that allows parsing the bind address from string just like the one from `std`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
10 //! but on top of that also allows `systemd://socket_name` format that tells it to use systemd activation with given socket name.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
11 //! Then it provides a method to bind the address which will return the socket from systemd if available.
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
12 //!
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
13 //! The provided type supports conversions from various types of strings and also `serde` and `parse_arg` via feature flag.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
14 //! Thanks to this the change to your code should be minimal - parsing will continue to work, it'll just allow a new format.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
15 //! You only need to change the code to use `SocketAddr::bind()` instead of `TcpListener::bind()` for binding.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
16 //!
6
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
17 //! You also don't need to worry about conditional compilation to ensure OS compatibility.
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
18 //! This crate handles that for you by disabling systemd on non-linux systems.
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
19 //!
21
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
20 //! Further, the crate also provides methods for binding `tokio` 1.0, 0.2, 0.3, and `async_std` sockets if the appropriate features are
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
21 //! activated.
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
22 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
23 //! ## Example
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
24 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
25 //! ```no_run
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
26 //! use systemd_socket::SocketAddr;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
27 //! use std::convert::TryFrom;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
28 //! use std::io::Write;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
29 //!
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
30 //! systemd_socket::init().expect("Failed to initialize systemd sockets");
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
31 //! let mut args = std::env::args_os();
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
32 //! let program_name = args.next().expect("unknown program name");
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
33 //! let socket_addr = args.next().expect("missing socket address");
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
34 //! let socket_addr = SocketAddr::try_from(socket_addr).expect("failed to parse socket address");
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
35 //! let socket = socket_addr.bind().expect("failed to bind socket");
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
36 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
37 //! loop {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
38 //! let _ = socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
39 //! .accept()
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
40 //! .expect("failed to accept connection")
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
41 //! .0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
42 //! .write_all(b"Hello world!")
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
43 //! .map_err(|err| eprintln!("Failed to send {}", err));
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
44 //! }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
45 //! ```
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
46 //!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
47 //! ## Features
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
48 //!
13
f740dadd2948 Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 11
diff changeset
49 //! * `enable_systemd` - on by default, the existence of this feature can allow your users to turn
f740dadd2948 Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 11
diff changeset
50 //! off systemd support if they don't need it. Note that it's already disabled on non-linux
f740dadd2948 Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 11
diff changeset
51 //! systems, so you don't need to care about that.
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
52 //! * `serde` - implements `serde::Deserialize` for `SocketAddr`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
53 //! * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr`
21
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
54 //! * `tokio` - adds `bind_tokio` method to `SocketAddr` (tokio 1.0)
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
55 //! * `tokio_0_2` - adds `bind_tokio_0_2` method to `SocketAddr`
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
56 //! * `tokio_0_3` - adds `bind_tokio_0_3` method to `SocketAddr`
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
57 //! * `async_std` - adds `bind_async_std` method to `SocketAddr`
3
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
58 //!
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
59 //! ## Soundness
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
60 //!
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
61 //! The systemd file descriptors are transferred using environment variables and since they are
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
62 //! file descriptors, they should have move semantics. However environment variables in Rust do not
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
63 //! have move semantics and even modifying them is very dangerous.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
64 //!
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
65 //! Because of this, the crate only allows initialization when there's only one thread running.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
66 //! However that still doesn't prevent all possible problems: if some other code closes file
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
67 //! descriptors stored in those environment variables you can get an invalid socket.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
68 //!
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
69 //! This situation is obviously ridiculous because there shouldn't be a reason to use another
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
70 //! library to do the same thing. It could also be argued that whichever code doesn't clear the
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
71 //! evironment variable is broken (even though understandably) and it's not a fault of this library.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
72 //!
3
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
73 //! ## MSRV
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
74 //!
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
75 //! This crate must always compile with the latest Rust available in the latest Debian stable.
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
76 //! That is currently Rust 1.41.1. (Debian 10 - Buster)
0edcde404b02 Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 2
diff changeset
77
22
f33e2c048104 Enable `doc_auto_cfg` in docs.rs builds
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 21
diff changeset
78 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
79
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
80 #![deny(missing_docs)]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
81
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
82 pub mod error;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
83 mod resolv_addr;
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
84
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
85 use std::convert::{TryFrom, TryInto};
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
86 use std::fmt;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
87 use std::ffi::{OsStr, OsString};
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
88 use crate::error::*;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
89 use crate::resolv_addr::ResolvAddr;
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
90
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
91 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
6
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
92 use std::convert::Infallible as Never;
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
93
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
94 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
95 pub(crate) mod systemd_sockets {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
96 use std::fmt;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
97 use std::sync::Mutex;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
98 use libsystemd::activation::FileDescriptor;
20
2b78a483f84b Update `libsystemd`
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 18
diff changeset
99 use libsystemd::errors::SdError as LibSystemdError;
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
100
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
101 #[derive(Debug)]
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
102 pub(crate) struct Error(&'static Mutex<InitError>);
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
103
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
104 impl fmt::Display for Error {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
106 use std::error::Error as _;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
107
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
108 let guard = self.0.lock().expect("mutex poisoned");
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
109 fmt::Display::fmt(&*guard, f)?;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
110 let mut source_opt = guard.source();
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
111 while let Some(source) = source_opt {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
112 write!(f, ": {}", source)?;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
113 source_opt = source.source();
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
114 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
115 Ok(())
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
116 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
117 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
118
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
119 // No source we can't keep the mutex locked
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
120 impl std::error::Error for Error {}
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
121
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
122 pub(crate) unsafe fn init(protected: bool) -> Result<(), InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
123 SYSTEMD_SOCKETS.get_or_try_init(|| SystemdSockets::new(protected, true).map(Ok)).map(drop)
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
124 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
125
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
126 pub(crate) fn take(name: &str) -> Result<Option<FileDescriptor>, Error> {
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
127 let sockets = SYSTEMD_SOCKETS.get_or_init(|| SystemdSockets::new_protected(false).map_err(Mutex::new));
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
128 match sockets {
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
129 Ok(sockets) => Ok(sockets.take(name)),
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
130 Err(error) => Err(Error(error))
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
131 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
132 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
133
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
134 #[derive(Debug)]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
135 pub(crate) enum InitError {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
136 OpenStatus(std::io::Error),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
137 ReadStatus(std::io::Error),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
138 ThreadCountNotFound,
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
139 MultipleThreads,
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
140 LibSystemd(LibSystemdError),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
141 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
142
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
143 impl From<LibSystemdError> for InitError {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
144 fn from(value: LibSystemdError) -> Self {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
145 Self::LibSystemd(value)
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
146 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
147 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
148
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
149 impl fmt::Display for InitError {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
150 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
151 match self {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
152 Self::OpenStatus(_) => write!(f, "failed to open /proc/self/status"),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
153 Self::ReadStatus(_) => write!(f, "failed to read /proc/self/status"),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
154 Self::ThreadCountNotFound => write!(f, "/proc/self/status doesn't contain Threads entry"),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
155 Self::MultipleThreads => write!(f, "there is more than one thread running"),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
156 // We have nothing to say about the error, let's flatten it
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
157 Self::LibSystemd(error) => fmt::Display::fmt(error, f),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
158 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
159 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
160 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
161
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
162 impl std::error::Error for InitError {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
163 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
164 match self {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
165 Self::OpenStatus(error) => Some(error),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
166 Self::ReadStatus(error) => Some(error),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
167 Self::ThreadCountNotFound => None,
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
168 Self::MultipleThreads => None,
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
169 // We have nothing to say about the error, let's flatten it
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
170 Self::LibSystemd(error) => error.source(),
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
171 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
172 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
173 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
174
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
175 struct SystemdSockets(std::sync::Mutex<std::collections::HashMap<String, FileDescriptor>>);
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
176
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
177 impl SystemdSockets {
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
178 fn new_protected(explicit: bool) -> Result<Self, InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
179 unsafe { Self::new(true, explicit) }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
180 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
181
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
182 unsafe fn new(protected: bool, explicit: bool) -> Result<Self, InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
183 if explicit {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
184 if std::env::var_os("LISTEN_PID").is_none() && std::env::var_os("LISTEN_FDS").is_none() && std::env::var_os("LISTEN_FDNAMES").is_none() {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
185 // Systemd is not used - make the map empty
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
186 return Ok(SystemdSockets(Mutex::new(Default::default())));
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
187 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
188 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
189
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
190 if protected { Self::check_single_thread()? }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
191 // MUST BE true FOR SAFETY!!!
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
192 let map = libsystemd::activation::receive_descriptors_with_names(/*unset env = */ protected)?.into_iter().map(|(fd, name)| (name, fd)).collect();
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
193 Ok(SystemdSockets(Mutex::new(map)))
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
194 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
195
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
196 fn check_single_thread() -> Result<(), InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
197 use std::io::BufRead;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
198
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
199 let status = std::fs::File::open("/proc/self/status").map_err(InitError::OpenStatus)?;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
200 let mut status = std::io::BufReader::new(status);
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
201 let mut line = String::new();
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
202 loop {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
203 if status.read_line(&mut line).map_err(InitError::ReadStatus)? == 0 {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
204 return Err(InitError::ThreadCountNotFound);
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
205 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
206 if let Some(threads) = line.strip_prefix("Threads:") {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
207 if threads.trim() == "1" {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
208 break;
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
209 } else {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
210 return Err(InitError::MultipleThreads);
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
211 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
212 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
213 line.clear();
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
214 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
215 Ok(())
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
216 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
217
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
218 fn take(&self, name: &str) -> Option<FileDescriptor> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
219 // MUST remove THE SOCKET FOR SAFETY!!!
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
220 self.0.lock().expect("poisoned mutex").remove(name)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
221 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
222 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
223
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
224 static SYSTEMD_SOCKETS: once_cell::sync::OnceCell<Result<SystemdSockets, Mutex<InitError>>> = once_cell::sync::OnceCell::new();
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
225 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
226
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
227 /// Socket address that can be an ordinary address or a systemd socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
228 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
229 /// This is the core type of this crate that abstracts possible addresses.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
230 /// It can be (fallibly) converted from various types of strings or deserialized with `serde`.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
231 /// After it's created, it can be bound as `TcpListener` from `std` or even `tokio` or `async_std`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
232 /// if the appropriate feature is enabled.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
233 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
234 /// Optional dependencies on `parse_arg` and `serde` make it trivial to use with
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
235 /// [`configure_me`](https://crates.io/crates/configure_me).
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
236 #[derive(Debug)]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
237 #[cfg_attr(feature = "serde", derive(serde_crate::Deserialize), serde(crate = "serde_crate", try_from = "serde_str_helpers::DeserBorrowStr"))]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
238 pub struct SocketAddr(SocketAddrInner);
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
239
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
240 impl SocketAddr {
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
241 /// Creates SocketAddr from systemd name directly, without requiring `systemd://` prefix.
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
242 ///
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
243 /// Always fails with systemd unsupported error if systemd is not supported.
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
244 pub fn from_systemd_name<T: Into<String>>(name: T) -> Result<Self, ParseError> {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
245 Self::inner_from_systemd_name(name.into(), false)
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
246 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
247
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
248 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
249 fn inner_from_systemd_name(name: String, prefixed: bool) -> Result<Self, ParseError> {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
250 let real_systemd_name = if prefixed {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
251 &name[SYSTEMD_PREFIX.len()..]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
252 } else {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
253 &name
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
254 };
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
255
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
256 let name_len = real_systemd_name.len();
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
257 match real_systemd_name.chars().enumerate().find(|(_, c)| !c.is_ascii() || *c < ' ' || *c == ':') {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
258 None if name_len <= 255 && prefixed => Ok(SocketAddr(SocketAddrInner::Systemd(name))),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
259 None if name_len <= 255 && !prefixed => Ok(SocketAddr(SocketAddrInner::SystemdNoPrefix(name))),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
260 None => Err(ParseErrorInner::LongSocketName { string: name, len: name_len }.into()),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
261 Some((pos, c)) => Err(ParseErrorInner::InvalidCharacter { string: name, c, pos, }.into()),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
262 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
263 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
264
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
265
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
266 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
267 fn inner_from_systemd_name(name: String, _prefixed: bool) -> Result<Self, ParseError> {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
268 Err(ParseError(ParseErrorInner::SystemdUnsupported(name)))
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
269 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
270
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
271 /// Creates `std::net::TcpListener`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
272 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
273 /// This method either `binds` the socket, if the address was provided or uses systemd socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
274 /// if the socket name was provided.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
275 pub fn bind(self) -> Result<std::net::TcpListener, BindError> {
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
276 match self.0 {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
277 SocketAddrInner::Ordinary(addr) => match std::net::TcpListener::bind(addr) {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
278 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
279 Err(error) => Err(BindErrorInner::BindFailed { addr, error, }.into()),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
280 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
281 SocketAddrInner::WithHostname(addr) => match std::net::TcpListener::bind(addr.as_str()) {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
282 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
283 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
284 },
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
285 SocketAddrInner::Systemd(socket_name) => Self::get_systemd(socket_name, true).map(|(socket, _)| socket),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
286 SocketAddrInner::SystemdNoPrefix(socket_name) => Self::get_systemd(socket_name, false).map(|(socket, _)| socket),
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
287 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
288 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
289
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
290 /// Creates `tokio::net::TcpListener`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
291 ///
21
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
292 /// To be specific, it binds the socket or converts systemd socket to `tokio` 1.0 socket.
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
293 ///
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
294 /// This method either `binds` the socket, if the address was provided or uses systemd socket
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
295 /// if the socket name was provided.
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
296 #[cfg(feature = "tokio")]
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
297 pub async fn bind_tokio(self) -> Result<tokio::net::TcpListener, TokioBindError> {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
298 match self.0 {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
299 SocketAddrInner::Ordinary(addr) => match tokio::net::TcpListener::bind(addr).await {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
300 Ok(socket) => Ok(socket),
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
301 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())),
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
302 },
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
303 SocketAddrInner::WithHostname(addr) => match tokio::net::TcpListener::bind(addr.as_str()).await {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
304 Ok(socket) => Ok(socket),
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
305 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())),
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
306 },
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
307 SocketAddrInner::Systemd(socket_name) => {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
308 let (socket, addr) = Self::get_systemd(socket_name, true)?;
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
309 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
310 },
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
311 SocketAddrInner::SystemdNoPrefix(socket_name) => {
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
312 let (socket, addr) = Self::get_systemd(socket_name, false)?;
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
313 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
314 },
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
315 }
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
316 }
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
317
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
318 /// Creates `tokio::net::TcpListener`
f6334887e3c8 Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 20
diff changeset
319 ///
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
320 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.2 socket.
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
321 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
322 /// This method either `binds` the socket, if the address was provided or uses systemd socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
323 /// if the socket name was provided.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
324 #[cfg(feature = "tokio_0_2")]
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
325 pub async fn bind_tokio_0_2(self) -> Result<tokio_0_2::net::TcpListener, TokioBindError> {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
326 match self.0 {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
327 SocketAddrInner::Ordinary(addr) => match tokio_0_2::net::TcpListener::bind(addr).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
328 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
329 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
330 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
331 SocketAddrInner::WithHostname(addr) => match tokio_0_2::net::TcpListener::bind(addr.as_str()).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
332 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
333 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
334 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
335 SocketAddrInner::Systemd(socket_name) => {
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
336 let (socket, addr) = Self::get_systemd(socket_name, true)?;
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
337 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
338 },
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
339 SocketAddrInner::SystemdNoPrefix(socket_name) => {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
340 let (socket, addr) = Self::get_systemd(socket_name, false)?;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
341 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
342 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
343 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
344 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
345
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
346 /// Creates `tokio::net::TcpListener`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
347 ///
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
348 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.3 socket.
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
349 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
350 /// This method either `binds` the socket, if the address was provided or uses systemd socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
351 /// if the socket name was provided.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
352 #[cfg(feature = "tokio_0_3")]
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
353 pub async fn bind_tokio_0_3(self) -> Result<tokio_0_3::net::TcpListener, TokioBindError> {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
354 match self.0 {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
355 SocketAddrInner::Ordinary(addr) => match tokio_0_3::net::TcpListener::bind(addr).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
356 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
357 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
358 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
359 SocketAddrInner::WithHostname(addr) => match tokio_0_3::net::TcpListener::bind(addr.as_str()).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
360 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
361 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
362 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
363 SocketAddrInner::Systemd(socket_name) => {
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
364 let (socket, addr) = Self::get_systemd(socket_name, true)?;
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
365 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
366 },
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
367 SocketAddrInner::SystemdNoPrefix(socket_name) => {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
368 let (socket, addr) = Self::get_systemd(socket_name, false)?;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
369 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
370 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
371 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
372 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
373
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
374 /// Creates `async_std::net::TcpListener`
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
375 ///
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
376 /// To be specific, it binds the socket or converts systemd socket to `async_std` socket.
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
377 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
378 /// This method either `binds` the socket, if the address was provided or uses systemd socket
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
379 /// if the socket name was provided.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
380 #[cfg(feature = "async-std")]
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
381 pub async fn bind_async_std(self) -> Result<async_std::net::TcpListener, BindError> {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
382 match self.0 {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
383 SocketAddrInner::Ordinary(addr) => match async_std::net::TcpListener::bind(addr).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
384 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
385 Err(error) => Err(BindErrorInner::BindFailed { addr, error, }.into()),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
386 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
387 SocketAddrInner::WithHostname(addr) => match async_std::net::TcpListener::bind(addr.as_str()).await {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
388 Ok(socket) => Ok(socket),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
389 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
390 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
391 SocketAddrInner::Systemd(socket_name) => {
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
392 let (socket, _) = Self::get_systemd(socket_name, true)?;
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
393 Ok(socket.into())
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
394 },
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
395 SocketAddrInner::SystemdNoPrefix(socket_name) => {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
396 let (socket, _) = Self::get_systemd(socket_name, false)?;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
397 Ok(socket.into())
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
398 },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
399 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
400 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
401
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
402 // We can't impl<T: Deref<Target=str> + Into<String>> TryFrom<T> for SocketAddr because of orphan
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
403 // rules.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
404 fn try_from_generic<'a, T>(string: T) -> Result<Self, ParseError> where T: 'a + std::ops::Deref<Target=str> + Into<String> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
405 if string.starts_with(SYSTEMD_PREFIX) {
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
406 Self::inner_from_systemd_name(string.into(), true)
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
407 } else {
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
408 match string.parse() {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
409 Ok(addr) => Ok(SocketAddr(SocketAddrInner::Ordinary(addr))),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
410 Err(_) => Ok(SocketAddr(SocketAddrInner::WithHostname(ResolvAddr::try_from_generic(string).map_err(ParseErrorInner::ResolvAddr)?))),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
411 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
412 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
413 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
414
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
415 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
416 fn get_systemd(socket_name: String, prefixed: bool) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> {
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
417 use libsystemd::activation::IsType;
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
418 use std::os::unix::io::{FromRawFd, IntoRawFd};
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
419
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
420 let real_systemd_name = if prefixed {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
421 &socket_name[SYSTEMD_PREFIX.len()..]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
422 } else {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
423 &socket_name
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
424 };
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
425
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
426 let socket = systemd_sockets::take(real_systemd_name).map_err(BindErrorInner::ReceiveDescriptors)?;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
427 // Safety: The environment variable is unset, so that no other calls can get the
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
428 // descriptors. The descriptors are taken from the map, not cloned, so they can't
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
429 // be duplicated.
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
430 unsafe {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
431 // match instead of combinators to avoid cloning socket_name
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
432 match socket {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
433 Some(socket) if socket.is_inet() => Ok((std::net::TcpListener::from_raw_fd(socket.into_raw_fd()), SocketAddrInner::Systemd(socket_name))),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
434 Some(_) => Err(BindErrorInner::NotInetSocket(socket_name).into()),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
435 None => Err(BindErrorInner::MissingDescriptor(socket_name).into())
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
436 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
437 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
438 }
6
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
439
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
440 // This approach makes the rest of the code much simpler as it doesn't require sprinkling it
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
441 // with #[cfg(all(target_os = "linux", feature = "enable_systemd"))] yet still statically guarantees it won't execute.
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
442 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
443 fn get_systemd(socket_name: Never, _prefixed: bool) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> {
6
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
444 match socket_name {}
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
445 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
446 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
447
24
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
448 /// Initializes the library while there's only a single thread.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
449 ///
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
450 /// Unfortunately, this library has to be initialized and, for soundness, this initialization must
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
451 /// happen when no other threads are running. This is attempted automatically when trying to bind a
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
452 /// systemd socket but at that time there may be other threads running and error reporting also
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
453 /// faces some restrictions. This function provides better control over the initialization point
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
454 /// and returns a more idiomatic error type.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
455 ///
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
456 /// You should generally call this at around the top of `main`, where no threads were created yet.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
457 #[inline]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
458 pub fn init() -> Result<(), error::InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
459 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
460 {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
461 // Calling with true is always sound
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
462 unsafe { systemd_sockets::init(true) }.map_err(error::InitError)
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
463 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
464 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
465 {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
466 Ok(())
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
467 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
468 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
469
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
470 /// Initializes the library without protection against double close.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
471 ///
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
472 /// Unfortunately, this library has to be initialized and, because double closing file descriptors
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
473 /// is unsound, the library has some protections against double close. However these protections
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
474 /// come with the limitation that the library must be initailized with a single thread.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
475 ///
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
476 /// If for any reason you're unable to call `init` in a single thread at around the top of `main`
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
477 /// (and this should be almost never) you may call this method if you've ensured that no other part
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
478 /// of your codebase is operating on systemd-provided file descriptors stored in the environment
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
479 /// variables.
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
480 pub unsafe fn init_unprotected() -> Result<(), error::InitError> {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
481 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
482 {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
483 systemd_sockets::init(false).map_err(error::InitError)
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
484 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
485 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
486 {
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
487 Ok(())
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
488 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
489 }
1941e9d9819c Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 22
diff changeset
490
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
491 /// Displays the address in format that can be parsed again.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
492 ///
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
493 /// **Important: While I don't expect this impl to change, don't rely on it!**
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
494 /// It should be used mostly for debugging/logging.
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
495 impl fmt::Display for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
496 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
497 fmt::Display::fmt(&self.0, f)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
498 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
499 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
500
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
501 impl fmt::Display for SocketAddrInner {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
502 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
503 match self {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
504 SocketAddrInner::Ordinary(addr) => fmt::Display::fmt(addr, f),
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
505 SocketAddrInner::Systemd(addr) => fmt::Display::fmt(addr, f),
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
506 SocketAddrInner::SystemdNoPrefix(addr) => write!(f, "{}{}", SYSTEMD_PREFIX, addr),
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
507 SocketAddrInner::WithHostname(addr) => fmt::Display::fmt(addr, f),
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
508 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
509 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
510 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
511
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
512 // PartialEq for testing, I'm not convinced it should be exposed
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
513 #[derive(Debug, PartialEq)]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
514 enum SocketAddrInner {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
515 Ordinary(std::net::SocketAddr),
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 3
diff changeset
516 WithHostname(resolv_addr::ResolvAddr),
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
517 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
518 Systemd(String),
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
519 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
6
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
520 #[allow(dead_code)]
a7893294e9b2 Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 4
diff changeset
521 Systemd(Never),
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
522 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
523 #[allow(dead_code)]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
524 SystemdNoPrefix(String),
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
525 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
526 #[allow(dead_code)]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
527 SystemdNoPrefix(Never),
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
528 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
529
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
530 const SYSTEMD_PREFIX: &str = "systemd://";
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
531
11
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
532 impl<I: Into<std::net::IpAddr>> From<(I, u16)> for SocketAddr {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
533 fn from(value: (I, u16)) -> Self {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
534 SocketAddr(SocketAddrInner::Ordinary(value.into()))
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
535 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
536 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
537
18
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
538 impl From<std::net::SocketAddr> for SocketAddr {
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
539 fn from(value: std::net::SocketAddr) -> Self {
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
540 SocketAddr(SocketAddrInner::Ordinary(value))
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
541 }
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
542 }
db1dc99252e2 Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 17
diff changeset
543
11
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
544 impl From<std::net::SocketAddrV4> for SocketAddr {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
545 fn from(value: std::net::SocketAddrV4) -> Self {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
546 SocketAddr(SocketAddrInner::Ordinary(value.into()))
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
547 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
548 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
549
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
550 impl From<std::net::SocketAddrV6> for SocketAddr {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
551 fn from(value: std::net::SocketAddrV6) -> Self {
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
552 SocketAddr(SocketAddrInner::Ordinary(value.into()))
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
553 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
554 }
13e2a5545167 Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 9
diff changeset
555
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
556 impl std::str::FromStr for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
557 type Err = ParseError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
558
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
559 fn from_str(s: &str) -> Result<Self, Self::Err> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
560 SocketAddr::try_from_generic(s)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
561 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
562 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
563
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
564 impl<'a> TryFrom<&'a str> for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
565 type Error = ParseError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
566
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
567 fn try_from(s: &'a str) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
568 SocketAddr::try_from_generic(s)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
569 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
570 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
571
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
572 impl TryFrom<String> for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
573 type Error = ParseError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
574
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
575 fn try_from(s: String) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
576 SocketAddr::try_from_generic(s)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
577 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
578 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
579
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
580 impl<'a> TryFrom<&'a OsStr> for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
581 type Error = ParseOsStrError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
582
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
583 fn try_from(s: &'a OsStr) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
584 s.to_str().ok_or(ParseOsStrError::InvalidUtf8)?.try_into().map_err(Into::into)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
585 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
586 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
587
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
588 impl TryFrom<OsString> for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
589 type Error = ParseOsStrError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
590
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
591 fn try_from(s: OsString) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
592 s.into_string().map_err(|_| ParseOsStrError::InvalidUtf8)?.try_into().map_err(Into::into)
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
593 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
594 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
595
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
596 #[cfg(feature = "serde")]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
597 impl<'a> TryFrom<serde_str_helpers::DeserBorrowStr<'a>> for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
598 type Error = ParseError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
599
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
600 fn try_from(s: serde_str_helpers::DeserBorrowStr<'a>) -> Result<Self, Self::Error> {
9
4fb70ca820a6 Upgrade serde_str_helpers
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 7
diff changeset
601 SocketAddr::try_from_generic(s)
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
602 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
603 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
604
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
605 #[cfg(feature = "parse_arg")]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
606 impl parse_arg::ParseArg for SocketAddr {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
607 type Error = ParseOsStrError;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
608
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
609 fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
610 std::net::SocketAddr::describe_type(&mut writer)?;
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
611 write!(writer, " or a systemd socket name prefixed with systemd://")
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
612 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
613
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
614 fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
615 arg.try_into()
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
616 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
617
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
618 fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
619 arg.try_into()
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
620 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
621 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
622
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
623 #[cfg(test)]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
624 mod tests {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
625 use super::{SocketAddr, SocketAddrInner};
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
626
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
627 #[test]
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
628 fn parse_ordinary() {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
629 assert_eq!("127.0.0.1:42".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Ordinary(([127, 0, 0, 1], 42).into()));
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
630 }
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
631
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
632 #[test]
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
633 #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
634 fn parse_systemd() {
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
635 assert_eq!("systemd://foo".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Systemd("systemd://foo".to_owned()));
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
636 }
2
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
637
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
638 #[test]
16
bc76507dd878 Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 13
diff changeset
639 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
7
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
640 #[should_panic]
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
641 fn parse_systemd() {
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
642 "systemd://foo".parse::<SocketAddr>().unwrap();
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
643 }
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
644
9731ff589d9c Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 6
diff changeset
645 #[test]
2
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
646 #[should_panic]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
647 fn parse_systemd_fail_control() {
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
648 "systemd://foo\n".parse::<SocketAddr>().unwrap();
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
649 }
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
650
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
651 #[test]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
652 #[should_panic]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
653 fn parse_systemd_fail_colon() {
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
654 "systemd://foo:".parse::<SocketAddr>().unwrap();
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
655 }
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
656
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
657 #[test]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
658 #[should_panic]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
659 fn parse_systemd_fail_non_ascii() {
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
660 "systemd://fooá".parse::<SocketAddr>().unwrap();
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
661 }
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
662
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
663 #[test]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
664 #[should_panic]
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
665 fn parse_systemd_fail_too_long() {
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
666 "systemd://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".parse::<SocketAddr>().unwrap();
cabc4aafdd85 Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 0
diff changeset
667 }
17
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
668
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
669 #[test]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
670 #[cfg_attr(not(all(target_os = "linux", feature = "enable_systemd")), should_panic)]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
671 fn no_prefix_parse_systemd() {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
672 SocketAddr::from_systemd_name("foo").unwrap();
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
673 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
674
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
675 #[test]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
676 #[should_panic]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
677 fn no_prefix_parse_systemd_fail_non_ascii() {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
678 SocketAddr::from_systemd_name("fooá").unwrap();
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
679 }
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
680
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
681 #[test]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
682 #[should_panic]
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
683 fn no_prefix_parse_systemd_fail_too_long() {
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
684 SocketAddr::from_systemd_name("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").unwrap();
dfb727367934 Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents: 16
diff changeset
685 }
0
a65053246c29 Initial commit
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
686 }