1use std::os::unix::io::RawFd;
7
8#[cfg(feature = "systemd")]
14pub fn has_socket() -> bool {
15 systemd::daemon::booted().unwrap_or(false) && std::env::var("LISTEN_FDS").is_ok()
16}
17
18#[cfg(not(feature = "systemd"))]
19pub fn has_socket() -> bool {
20 false
21}
22
23#[cfg(feature = "systemd")]
31pub fn listen_fds() -> Option<Vec<RawFd>> {
32 systemd::daemon::listen_fds(false)
33 .ok()
34 .map(|fds| fds.iter().collect())
35}
36
37#[cfg(not(feature = "systemd"))]
38pub fn listen_fds() -> Option<Vec<RawFd>> {
39 None
40}
41
42#[cfg(feature = "systemd")]
46pub fn listen_fds_count() -> usize {
47 std::env::var("LISTEN_FDS")
48 .ok()
49 .and_then(|s| s.parse().ok())
50 .unwrap_or(0)
51}
52
53#[cfg(not(feature = "systemd"))]
54pub fn listen_fds_count() -> usize {
55 0
56}
57
58#[cfg(feature = "systemd")]
64pub fn fd_name_matches(fd_index: usize, expected_name: &str) -> bool {
65 if let Ok(names) = std::env::var("LISTEN_FDNAMES") {
66 if let Some(name) = names.split(':').nth(fd_index) {
67 return name == expected_name;
68 }
69 }
70 false
71}
72
73#[cfg(not(feature = "systemd"))]
74pub fn fd_name_matches(_fd_index: usize, _expected_name: &str) -> bool {
75 false
76}