| 1 | # sd-listen-fds |
| 2 | |
| 3 | [](https://docs.rs/sd-listen-fds/) |
| 4 | [](LICENSE-APACHE) |
| 5 | [](LICENSE-MIT) |
| 6 | |
| 7 | Exposes file descriptors passed in by systemd, the Linux init daemon, |
| 8 | without dependencies, foreign or otherwise. Enables easy |
| 9 | implementation of socket-activated services in pure Rust. |
| 10 | |
| 11 | Unlike services that open sockets themselves, socket-activated |
| 12 | services are started on-demand, may start up concurrently with their |
| 13 | dependencies, and may be restarted without losing inbound |
| 14 | data. Portable applications can call `sd_listen_fds::get()` and open |
| 15 | their own socket if it succeeds with an empty `Vec`. |
| 16 | |
| 17 | See also the |
| 18 | [sd_listen_fds](https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html) |
| 19 | API exposed by the foreign `libsystemd` library. Compared to using |
| 20 | `libsystemd` bindings, this crate is smaller, safer, and builds |
| 21 | everywhere. |
| 22 | |
| 23 | ### Example |
| 24 | |
| 25 | ```rust |
| 26 | let fds = sd_listen_fds::get().unwrap(); |
| 27 | let (_name, fd) = fds |
| 28 | .into_iter() |
| 29 | .next() |
| 30 | .expect("must be launched as a systemd socket-activated service"); |
| 31 | let socket = TcpListener::from(fd); |
| 32 | ``` |
| 33 | |
| 34 | my-service.socket (see also [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)) |
| 35 | ```ini |
| 36 | [Socket] |
| 37 | # TCP port number. Other types of sockets are also possible. |
| 38 | ListenStream=1234 |
| 39 | |
| 40 | [Install] |
| 41 | WantedBy=sockets.target |
| 42 | |
| 43 | [Unit] |
| 44 | Description=My Rust service |
| 45 | Documentation=https://example.com/my-service/ |
| 46 | ``` |
| 47 | |
| 48 | my-service.service (see also [systemd.service](https://www.freedesktop.org/software/systemd/man/systemd.service.html)) |
| 49 | ```ini |
| 50 | [Service] |
| 51 | ExecStart=/path/to/my-service |
| 52 | |
| 53 | [Unit] |
| 54 | Requires=my-service.socket |
| 55 | Description=My Rust service |
| 56 | Documentation=https://example.com/my-service/ |
| 57 | ``` |
| 58 | |
| 59 | ## License |
| 60 | |
| 61 | Licensed under either of |
| 62 | |
| 63 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 64 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 65 | |
| 66 | at your option. |
| 67 | |
| 68 | ### Contribution |
| 69 | |
| 70 | Unless you explicitly state otherwise, any contribution intentionally |
| 71 | submitted for inclusion in the work by you, as defined in the |
| 72 | Apache-2.0 license, shall be dual licensed as above, without any |
| 73 | additional terms or conditions. |