aboutsummaryrefslogtreecommitdiffstats
path: root/letmein-systemd/src/lib.rs
blob: c377ac4d260ae7d162e43462acbcbf5c3709dd89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// -*- coding: utf-8 -*-
//
// Copyright (C) 2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! This crate is an abstraction of the `systemd` interfaces needed by `letmein`.

use anyhow::{self as ah, format_err as err, Context as _};
use std::{net::TcpListener, os::fd::FromRawFd as _};

/// Create a new [TcpListener] with the socket provided by systemd.
///
/// All environment variables related to this operation will be cleared.
pub fn tcp_from_systemd() -> ah::Result<Option<TcpListener>> {
    if sd_notify::booted().unwrap_or(false) {
        let mut fds = sd_notify::listen_fds().context("Systemd listen_fds")?;
        if let Some(fd) = fds.next() {
            // SAFETY:
            // The fd from systemd is good and lives for the lifetime of the program.
            return Ok(Some(unsafe { TcpListener::from_raw_fd(fd) }));
        } else {
            return Err(err!(
                "Booted with systemd, but no listen_fds received from systemd."
            ));
        }
    }
    Ok(None)
}

/// Notify ready-status to systemd.
///
/// All environment variables related to this operation will be cleared.
pub fn systemd_notify_ready() -> ah::Result<()> {
    sd_notify::notify(true, &[sd_notify::NotifyState::Ready])?;
    Ok(())
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface