Skip to content

Commit

Permalink
Merge #243
Browse files Browse the repository at this point in the history
243: systemd/bus: provide an example and mark some Message fields optional r=jmesmon a=jmesmon

Resolves #242

Co-authored-by: Cody P Schafer <[email protected]>
  • Loading branch information
bors[bot] and codyps authored Dec 14, 2021
2 parents 2b17eb3 + b43332f commit 92925bd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions examples/systemd-start-service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![warn(rust_2018_idioms)]
// WARNING: you may want to use a more tested/complete dbus library, or one that is pure rust.
// `zbus` may be a reasonable choice, and there are others too

use utf8_cstr::Utf8CStr;
// approximately this command:
// busctl --system call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StartUnit "ss" "foo.service" "fail"
#[cfg(feature = "bus")]
fn main() {
let mut bus = systemd::bus::Bus::default_system().unwrap();

let mut method_call = bus
.new_method_call(
systemd::bus::BusName::from_bytes(b"org.freedesktop.systemd1\0").unwrap(),
systemd::bus::ObjectPath::from_bytes(b"/org/freedesktop/systemd1\0").unwrap(),
systemd::bus::InterfaceName::from_bytes(b"org.freedesktop.systemd1.Manager\0").unwrap(),
systemd::bus::MemberName::from_bytes(b"StartUnit\0").unwrap(),
)
.unwrap();

// args
method_call
.append(Utf8CStr::from_bytes(b"foo.service\0").unwrap())
.unwrap();
method_call
.append(Utf8CStr::from_bytes(b"fail\0").unwrap())
.unwrap();

let res = method_call.call(0).unwrap();

eprintln!("done, result={:?}", *res);
}

#[cfg(not(feature = "bus"))]
fn main() {
println!("bus disabled");
}
30 changes: 18 additions & 12 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,31 +1447,37 @@ impl MessageRef {
/// This corresponds to [`sd_bus_message_get_path`]
///
/// [`sd_bus_message_get_path`]: https://www.freedesktop.org/software/systemd/man/sd_bus_message_get_path.html
pub fn path(&self) -> &CStr {
pub fn path(&self) -> Option<&CStr> {
let p = unsafe { ffi::bus::sd_bus_message_get_path(self.as_ptr()) };
assert!(!p.is_null());

unsafe { CStr::from_ptr(p) }
if p.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(p) })
}
}

/// This corresponds to [`sd_bus_message_get_interface`]
///
/// [`sd_bus_message_get_interface`]: https://www.freedesktop.org/software/systemd/man/sd_bus_message_get_interface.html
pub fn interface(&self) -> &CStr {
pub fn interface(&self) -> Option<&CStr> {
let p = unsafe { ffi::bus::sd_bus_message_get_interface(self.as_ptr()) };
assert!(!p.is_null());

unsafe { CStr::from_ptr(p) }
if p.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(p) })
}
}

/// This corresponds to [`sd_bus_message_get_member`]
///
/// [`sd_bus_message_get_member`]: https://www.freedesktop.org/software/systemd/man/sd_bus_message_get_member.html
pub fn member(&self) -> &CStr {
pub fn member(&self) -> Option<&CStr> {
let p = unsafe { ffi::bus::sd_bus_message_get_member(self.as_ptr()) };
assert!(!p.is_null());

unsafe { CStr::from_ptr(p) }
if p.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(p) })
}
}

/// This corresponds to [`sd_bus_message_get_sender`]
Expand Down

0 comments on commit 92925bd

Please sign in to comment.