zbus/fdo/
peer.rs

1//! D-Bus standard interfaces.
2//!
3//! The D-Bus specification defines the message bus messages and some standard interfaces that may
4//! be useful across various D-Bus applications. This module provides their proxy.
5
6use super::{Error, Result};
7
8pub(crate) struct Peer;
9
10/// Service-side implementation for the `org.freedesktop.DBus.Peer` interface.
11/// This interface is implemented automatically for any object registered to the
12/// [ObjectServer](crate::ObjectServer).
13#[crate::interface(
14    name = "org.freedesktop.DBus.Peer",
15    introspection_docs = false,
16    proxy(visibility = "pub")
17)]
18impl Peer {
19    /// On receipt, an application should do nothing other than reply as usual. It does not matter
20    /// which object path a ping is sent to.
21    fn ping(&self) {}
22
23    /// An application should reply the containing a hex-encoded UUID representing the identity of
24    /// the machine the process is running on. This UUID must be the same for all processes on a
25    /// single system at least until that system next reboots. It should be the same across reboots
26    /// if possible, but this is not always possible to implement and is not guaranteed. It does not
27    /// matter which object path a GetMachineId is sent to.
28    fn get_machine_id(&self) -> Result<String> {
29        let mut id = match std::fs::read_to_string("/var/lib/dbus/machine-id") {
30            Ok(id) => id,
31            Err(e) => {
32                if let Ok(id) = std::fs::read_to_string("/etc/machine-id") {
33                    id
34                } else {
35                    return Err(Error::IOError(format!(
36                        "Failed to read from /var/lib/dbus/machine-id or /etc/machine-id: {e}"
37                    )));
38                }
39            }
40        };
41
42        let len = id.trim_end().len();
43        id.truncate(len);
44        Ok(id)
45    }
46}