diff --git a/Cargo.toml b/Cargo.toml index a0ae037..26e8ee1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logind-dbus" -version = "0.1.1" +version = "1.0.0-beta.1" authors = [ "Jeremy Soller ", "Michael Aaron Murphy " @@ -11,7 +11,8 @@ readme = "README.md" license = "MIT" categories = ["os::unix-apis"] keywords = ["linux", "systemd", "logind", "dbus"] +edition = "2018" [dependencies] -dbus = "0.6" -cascade = "0.1.2" +zbus = { git = "https://gitlab.freedesktop.org/dbus/zbus/" } +zvariant = { git = "https://gitlab.freedesktop.org/dbus/zbus/" } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 8e599e6..17c1515 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,87 +1,450 @@ -//! Provides a DBus API for interacting with logind, which is useful for doing things such as inhibiting suspension. +//! # DBus interface proxy for: `org.freedesktop.login1.Manager` //! -//! ```rust,no_run -//! extern crate logind_dbus; -//! use logind_dbus::LoginManager; -//! -//! pub fn main() -> io::Result<()> { -//! let login_manager = LoginManager::new()?; -//! let suspend_lock = login_manager.connect().inhibit_suspend()?; -//! /// Do sensitive thing with the guarantee that suspend will not work. -//! } -//! ``` - -#[macro_use] -extern crate cascade; -extern crate dbus; - -use dbus::{arg, BusType, Connection, ConnPath}; -use std::ops::Deref; - -/// An interface to `org.freedesktop.login1.Manager`. -pub struct LoginManager { - conn: Connection -} +//! This code was generated by `zbus-xmlgen` `2.0.0-beta.7` from DBus introspection data. +//! Source: `Interface '/org/freedesktop/login1' from service 'org.freedesktop.login1' on system bus`. +//! +//! You may prefer to adapt it, instead of using it verbatim. +//! +//! More information can be found in the +//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html) +//! section of the zbus documentation. +//! +//! This DBus object implements +//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: +//! +//! * [`zbus::fdo::PeerProxy`] +//! * [`zbus::fdo::IntrospectableProxy`] +//! * [`zbus::fdo::PropertiesProxy`] +//! +//! …consequently `zbus-xmlgen` did not generate code for the above interfaces. -impl Deref for LoginManager { - type Target = Connection; - fn deref(&self) -> &Self::Target { - &self.conn - } -} +use zbus::dbus_proxy; -impl LoginManager { - pub fn new() -> Result { - Ok(Self { conn: Connection::get_private(BusType::System)? }) - } +#[dbus_proxy(interface = "org.freedesktop.login1.Manager")] +trait Manager { + /// ActivateSession method + fn activate_session(&self, session_id: &str) -> zbus::Result<()>; - pub fn connect(&self) -> LoginManagerConnection { - LoginManagerConnection { - conn: self.with_path("org.freedesktop.login1", "/org/freedesktop/login1", 1000) - } - } -} + /// ActivateSessionOnSeat method + fn activate_session_on_seat(&self, session_id: &str, seat_id: &str) -> zbus::Result<()>; -/// An established connection path for the login manager, through which the API is made accessible. -pub struct LoginManagerConnection<'a> { - conn: ConnPath<'a, &'a Connection> -} + /// AttachDevice method + fn attach_device(&self, seat_id: &str, sysfs_path: &str, interactive: bool) + -> zbus::Result<()>; + + /// CanHalt method + fn can_halt(&self) -> zbus::Result; + + /// CanHibernate method + fn can_hibernate(&self) -> zbus::Result; + + /// CanHybridSleep method + fn can_hybrid_sleep(&self) -> zbus::Result; + + /// CanPowerOff method + fn can_power_off(&self) -> zbus::Result; + + /// CanReboot method + fn can_reboot(&self) -> zbus::Result; + + /// CanRebootParameter method + fn can_reboot_parameter(&self) -> zbus::Result; + + /// CanRebootToBootLoaderEntry method + fn can_reboot_to_boot_loader_entry(&self) -> zbus::Result; + + /// CanRebootToBootLoaderMenu method + fn can_reboot_to_boot_loader_menu(&self) -> zbus::Result; + + /// CanRebootToFirmwareSetup method + fn can_reboot_to_firmware_setup(&self) -> zbus::Result; + + /// CanSuspend method + fn can_suspend(&self) -> zbus::Result; + + /// CanSuspendThenHibernate method + fn can_suspend_then_hibernate(&self) -> zbus::Result; + + /// CancelScheduledShutdown method + fn cancel_scheduled_shutdown(&self) -> zbus::Result; + + /// CreateSession method + fn create_session( + &self, + uid: u32, + pid: u32, + service: &str, + type_: &str, + class: &str, + desktop: &str, + seat_id: &str, + vtnr: u32, + tty: &str, + display: &str, + remote: bool, + remote_user: &str, + remote_host: &str, + properties: &[(&str, zbus::zvariant::Value<'_>)], + ) -> zbus::Result<( + String, + zbus::zvariant::OwnedObjectPath, + String, + zbus::zvariant::OwnedFd, + u32, + String, + u32, + bool, + )>; + + /// FlushDevices method + fn flush_devices(&self, interactive: bool) -> zbus::Result<()>; + + /// GetSeat method + fn get_seat(&self, seat_id: &str) -> zbus::Result; + + /// GetSession method + fn get_session(&self, session_id: &str) -> zbus::Result; + + /// GetSessionByPID method + fn get_session_by_pid(&self, pid: u32) -> zbus::Result; + + /// GetUser method + fn get_user(&self, uid: u32) -> zbus::Result; + + /// GetUserByPID method + fn get_user_by_pid(&self, pid: u32) -> zbus::Result; + + /// Halt method + fn halt(&self, interactive: bool) -> zbus::Result<()>; + + /// HaltWithFlags method + fn halt_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// Hibernate method + fn hibernate(&self, interactive: bool) -> zbus::Result<()>; + + /// HibernateWithFlags method + fn hibernate_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// HybridSleep method + fn hybrid_sleep(&self, interactive: bool) -> zbus::Result<()>; + + /// HybridSleepWithFlags method + fn hybrid_sleep_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// Inhibit method + fn inhibit( + &self, + what: &str, + who: &str, + why: &str, + mode: &str, + ) -> zbus::Result; + + /// KillSession method + fn kill_session(&self, session_id: &str, who: &str, signal_number: i32) -> zbus::Result<()>; + + /// KillUser method + fn kill_user(&self, uid: u32, signal_number: i32) -> zbus::Result<()>; + + /// ListInhibitors method + fn list_inhibitors(&self) -> zbus::Result>; + + /// ListSeats method + fn list_seats(&self) -> zbus::Result>; + + /// ListSessions method + fn list_sessions( + &self, + ) -> zbus::Result>; + + /// ListUsers method + fn list_users(&self) -> zbus::Result>; + + /// LockSession method + fn lock_session(&self, session_id: &str) -> zbus::Result<()>; + + /// LockSessions method + fn lock_sessions(&self) -> zbus::Result<()>; + + /// PowerOff method + fn power_off(&self, interactive: bool) -> zbus::Result<()>; + + /// PowerOffWithFlags method + fn power_off_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// Reboot method + fn reboot(&self, interactive: bool) -> zbus::Result<()>; + + /// RebootWithFlags method + fn reboot_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// ReleaseSession method + fn release_session(&self, session_id: &str) -> zbus::Result<()>; + + /// ScheduleShutdown method + fn schedule_shutdown(&self, type_: &str, usec: u64) -> zbus::Result<()>; + + /// SetRebootParameter method + fn set_reboot_parameter(&self, parameter: &str) -> zbus::Result<()>; + + /// SetRebootToBootLoaderEntry method + fn set_reboot_to_boot_loader_entry(&self, boot_loader_entry: &str) -> zbus::Result<()>; + + /// SetRebootToBootLoaderMenu method + fn set_reboot_to_boot_loader_menu(&self, timeout: u64) -> zbus::Result<()>; + + /// SetRebootToFirmwareSetup method + fn set_reboot_to_firmware_setup(&self, enable: bool) -> zbus::Result<()>; + + /// SetUserLinger method + fn set_user_linger(&self, uid: u32, enable: bool, interactive: bool) -> zbus::Result<()>; + + /// SetWallMessage method + fn set_wall_message(&self, wall_message: &str, enable: bool) -> zbus::Result<()>; + + /// Suspend method + fn suspend(&self, interactive: bool) -> zbus::Result<()>; + + /// SuspendThenHibernate method + fn suspend_then_hibernate(&self, interactive: bool) -> zbus::Result<()>; + + /// SuspendThenHibernateWithFlags method + fn suspend_then_hibernate_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// SuspendWithFlags method + fn suspend_with_flags(&self, flags: u64) -> zbus::Result<()>; + + /// TerminateSeat method + fn terminate_seat(&self, seat_id: &str) -> zbus::Result<()>; + + /// TerminateSession method + fn terminate_session(&self, session_id: &str) -> zbus::Result<()>; + + /// TerminateUser method + fn terminate_user(&self, uid: u32) -> zbus::Result<()>; + + /// UnlockSession method + fn unlock_session(&self, session_id: &str) -> zbus::Result<()>; + + /// UnlockSessions method + fn unlock_sessions(&self) -> zbus::Result<()>; + + /// PrepareForShutdown signal + #[dbus_proxy(signal)] + fn prepare_for_shutdown(&self, start: bool) -> zbus::Result<()>; + + /// PrepareForSleep signal + #[dbus_proxy(signal)] + fn prepare_for_sleep(&self, start: bool) -> zbus::Result<()>; + + /// SeatNew signal + #[dbus_proxy(signal)] + fn seat_new( + &self, + seat_id: &str, + object_path: zbus::zvariant::ObjectPath<'_>, + ) -> zbus::Result<()>; + + /// SeatRemoved signal + #[dbus_proxy(signal)] + fn seat_removed( + &self, + seat_id: &str, + object_path: zbus::zvariant::ObjectPath<'_>, + ) -> zbus::Result<()>; + + /// SessionNew signal + #[dbus_proxy(signal)] + fn session_new( + &self, + session_id: &str, + object_path: zbus::zvariant::ObjectPath<'_>, + ) -> zbus::Result<()>; + + /// SessionRemoved signal + #[dbus_proxy(signal)] + fn session_removed( + &self, + session_id: &str, + object_path: zbus::zvariant::ObjectPath<'_>, + ) -> zbus::Result<()>; + + /// UserNew signal + #[dbus_proxy(signal)] + fn user_new(&self, uid: u32, object_path: zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>; + + /// UserRemoved signal + #[dbus_proxy(signal)] + fn user_removed( + &self, + uid: u32, + object_path: zbus::zvariant::ObjectPath<'_>, + ) -> zbus::Result<()>; + + /// BlockInhibited property + #[dbus_proxy(property)] + fn block_inhibited(&self) -> zbus::Result; + + /// BootLoaderEntries property + #[dbus_proxy(property)] + fn boot_loader_entries(&self) -> zbus::Result>; + + /// DelayInhibited property + #[dbus_proxy(property)] + fn delay_inhibited(&self) -> zbus::Result; + + /// Docked property + #[dbus_proxy(property)] + fn docked(&self) -> zbus::Result; + + /// EnableWallMessages property + #[dbus_proxy(property)] + fn enable_wall_messages(&self) -> zbus::Result; + #[dbus_proxy(property)] + fn set_enable_wall_messages(&self, value: bool) -> zbus::Result<()>; + + /// HandleHibernateKey property + #[dbus_proxy(property)] + fn handle_hibernate_key(&self) -> zbus::Result; + + /// HandleLidSwitch property + #[dbus_proxy(property)] + fn handle_lid_switch(&self) -> zbus::Result; + + /// HandleLidSwitchDocked property + #[dbus_proxy(property)] + fn handle_lid_switch_docked(&self) -> zbus::Result; + + /// HandleLidSwitchExternalPower property + #[dbus_proxy(property)] + fn handle_lid_switch_external_power(&self) -> zbus::Result; + + /// HandlePowerKey property + #[dbus_proxy(property)] + fn handle_power_key(&self) -> zbus::Result; + + /// HandleSuspendKey property + #[dbus_proxy(property)] + fn handle_suspend_key(&self) -> zbus::Result; + + /// HoldoffTimeoutUSec property + #[dbus_proxy(property)] + fn holdoff_timeout_usec(&self) -> zbus::Result; + + /// IdleAction property + #[dbus_proxy(property)] + fn idle_action(&self) -> zbus::Result; + + /// IdleActionUSec property + #[dbus_proxy(property)] + fn idle_action_usec(&self) -> zbus::Result; + + /// IdleHint property + #[dbus_proxy(property)] + fn idle_hint(&self) -> zbus::Result; + + /// IdleSinceHint property + #[dbus_proxy(property)] + fn idle_since_hint(&self) -> zbus::Result; + + /// IdleSinceHintMonotonic property + #[dbus_proxy(property)] + fn idle_since_hint_monotonic(&self) -> zbus::Result; + + /// InhibitDelayMaxUSec property + #[dbus_proxy(property)] + fn inhibit_delay_max_usec(&self) -> zbus::Result; + + /// InhibitorsMax property + #[dbus_proxy(property)] + fn inhibitors_max(&self) -> zbus::Result; + + /// KillExcludeUsers property + #[dbus_proxy(property)] + fn kill_exclude_users(&self) -> zbus::Result>; + + /// KillOnlyUsers property + #[dbus_proxy(property)] + fn kill_only_users(&self) -> zbus::Result>; + + /// KillUserProcesses property + #[dbus_proxy(property)] + fn kill_user_processes(&self) -> zbus::Result; + + /// LidClosed property + #[dbus_proxy(property)] + fn lid_closed(&self) -> zbus::Result; + + /// NAutoVTs property + #[dbus_proxy(property)] + fn nauto_vts(&self) -> zbus::Result; + + /// NCurrentInhibitors property + #[dbus_proxy(property)] + fn ncurrent_inhibitors(&self) -> zbus::Result; + + /// NCurrentSessions property + #[dbus_proxy(property)] + fn ncurrent_sessions(&self) -> zbus::Result; + + /// OnExternalPower property + #[dbus_proxy(property)] + fn on_external_power(&self) -> zbus::Result; + + /// PreparingForShutdown property + #[dbus_proxy(property)] + fn preparing_for_shutdown(&self) -> zbus::Result; + + /// PreparingForSleep property + #[dbus_proxy(property)] + fn preparing_for_sleep(&self) -> zbus::Result; + + /// RebootParameter property + #[dbus_proxy(property)] + fn reboot_parameter(&self) -> zbus::Result; + + /// RebootToBootLoaderEntry property + #[dbus_proxy(property)] + fn reboot_to_boot_loader_entry(&self) -> zbus::Result; + + /// RebootToBootLoaderMenu property + #[dbus_proxy(property)] + fn reboot_to_boot_loader_menu(&self) -> zbus::Result; + + /// RebootToFirmwareSetup property + #[dbus_proxy(property)] + fn reboot_to_firmware_setup(&self) -> zbus::Result; + + /// RemoveIPC property + #[dbus_proxy(property)] + fn remove_ipc(&self) -> zbus::Result; + + /// RuntimeDirectoryInodesMax property + #[dbus_proxy(property)] + fn runtime_directory_inodes_max(&self) -> zbus::Result; + + /// RuntimeDirectorySize property + #[dbus_proxy(property)] + fn runtime_directory_size(&self) -> zbus::Result; + + /// ScheduledShutdown property + #[dbus_proxy(property)] + fn scheduled_shutdown(&self) -> zbus::Result<(String, u64)>; + + /// SessionsMax property + #[dbus_proxy(property)] + fn sessions_max(&self) -> zbus::Result; + + /// UserStopDelayUSec property + #[dbus_proxy(property)] + fn user_stop_delay_usec(&self) -> zbus::Result; + + /// WallMessage property + #[dbus_proxy(property)] + fn wall_message(&self) -> zbus::Result; -impl<'a> LoginManagerConnection<'a> { - /// Inhibit is the only API necessary to take a lock. It takes four arguments: - /// - /// - **What** is a colon-separated list of lock types, i.e. `shutdown`, `sleep`, `idle`, - /// `handle-power-key`, `handle-suspend-key`, `handle-hibernate-key`, `handle-lid-switch`. - /// Example: "shutdown:idle" - /// - **Who** is a human-readable, descriptive string of who is taking the lock. Example: "Package Updater" - /// - **Why** is a human-readable, descriptive string of why the lock is taken. Example: "Package Update in Progress" - /// - **Mode** is one of `block` or `delay`. - /// - /// # Notes - /// - /// A root user session cannot use systemd inhibitors. - pub fn inhibit(&self, what: &str, who: &str, why: &str, mode: &str) -> Result { - let mut m = self.conn.method_call_with_args( - &"org.freedesktop.login1.Manager".into(), - &"Inhibit".into(), - |msg| { - cascade! { - arg::IterAppend::new(msg); - ..append(what); - ..append(who); - ..append(why); - ..append(mode); - } - })?; - - m.as_result()?; - Ok(m.iter_init().read::()?) - } - - /// Convenience method for inhibiting suspend. - /// - /// Equivalent to `connection.inhibit("idle:shutdown:sleep", who, why, "block")`. - pub fn inhibit_suspend(&self, who: &str, why: &str) -> Result { - self.inhibit("idle:shutdown:sleep", who, why, "block") - } + // #[dbus_proxy(property)] + // fn set_wall_message(&self, value: &str) -> zbus::Result<()>; }