#file-descriptor #systemd #linux #pure #api-bindings #accept #helper

sd-listen-fds

Minimal, pure-rust helper to accept file descriptors from systemd

2 unstable releases

0.2.0 Aug 27, 2023
0.1.0 Jul 31, 2023

#514 in Unix APIs

Download history 162/week @ 2024-01-08 131/week @ 2024-01-15 217/week @ 2024-01-22 208/week @ 2024-01-29 167/week @ 2024-02-05 231/week @ 2024-02-12 407/week @ 2024-02-19 598/week @ 2024-02-26 418/week @ 2024-03-04 384/week @ 2024-03-11 542/week @ 2024-03-18 792/week @ 2024-03-25 700/week @ 2024-04-01 919/week @ 2024-04-08 419/week @ 2024-04-15 1119/week @ 2024-04-22

3,191 downloads per month

MIT/Apache

8KB
114 lines

sd-listen-fds

Documentation License: Apache 2.0 License: MIT

Exposes file descriptors passed in by systemd, the Linux init daemon, without dependencies, foreign or otherwise. Enables easy implementation of socket-activated services in pure Rust.

Unlike services that open sockets themselves, socket-activated services are started on-demand, may start up concurrently with their dependencies, and may be restarted without losing inbound data. Portable applications can call sd_listen_fds::get() and open their own socket if it succeeds with an empty Vec.

See also the sd_listen_fds API exposed by the foreign libsystemd library. Compared to using libsystemd bindings, this crate is smaller, safer, and builds everywhere.

Example

let fds = sd_listen_fds::get().unwrap();
let (_name, fd) = fds
    .into_iter()
    .next()
    .expect("must be launched as a systemd socket-activated service");
let socket = TcpListener::from(fd);

my-service.socket (see also systemd.socket)

[Socket]
# TCP port number. Other types of sockets are also possible.
ListenStream=1234

[Install]
WantedBy=sockets.target

[Unit]
Description=My Rust service
Documentation=https://example.com/my-service/

my-service.service (see also systemd.service)

[Service]
ExecStart=/path/to/my-service

[Unit]
Requires=my-service.socket
Description=My Rust service
Documentation=https://example.com/my-service/

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps