[systemd dbus
] client lib using [dbus-codegen
]
list units ```rust use systemdclient::{ buildblocking_client, manager::blocking::OrgFreedesktopSystemd1Manager, models::{IntoModel, Unit}, Result, SystemdObjectType, };
fn main() -> Result<()> {
let client = buildblockingclient(SystemdObjectType::Manager)?;
let units = client.listunits()?;
for unit in units {
let unit: Unit = unit.intomodel()?;
println!("{:#?}", unit);
}
Ok(())
}
create and start service
rust
use systemdclient::{
buildblockingclient, createunitconfigurationfile,
manager::blocking::OrgFreedesktopSystemd1Manager, models::IntoModel,
unit::blocking::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
UnitSubStateType,
};
/*
* Run example as superuser since we start a service
* sh
* cargo build --example start_service_blocking
* sudo ./target/debug/examples/start_service_blocking
*
*/
fn main() -> Result<()> {
let unitbuilder = UnitConfiguration::builder().description("test service");
let svcbuilder = ServiceConfiguration::builder().execstart(vec!["/bin/sleep", "3"]);
let svcunit = ServiceUnitConfiguration::builder()
.unit(unitbuilder)
.service(svcbuilder)
.build();
let svcunitliteral = format!("{}", svcunit);
// create /etc/systemd/system/test.service
createunitconfigurationfile("test.service", svcunitliteral.asbytes())?;
let client = buildblockingclient(SystemdObjectType::Manager)?;
let jobpath = client.startunit("test.service", "replace")?;
println!("{}", jobpath);
let svcunitpath = client.getunit("test.service")?;
println!("{}", svcunitpath);
// verify unit state given unit path
let client = buildblockingclient(SystemdObjectType::Unit(svcunitpath))?;
let unitprops = client.getunitproperties()?;
let unitprops: UnitProps = unitprops.intomodel()?;
println!("{:?}", unitprops);
asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded);
asserteq!(unitprops.activestate, UnitActiveStateType::Active);
asserteq!(unitprops.substate, UnitSubStateType::Running);
std::thread::sleep(std::time::Duration::fromsecs(4));
// service should exit after 3 sec
let unitprops = client.getunitproperties()?;
let unitprops: UnitProps = unitprops.intomodel()?;
println!("{:?}", unitprops);
asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded);
asserteq!(unitprops.activestate, UnitActiveStateType::Inactive);
asserteq!(unitprops.substate, UnitSubStateType::Dead);
Ok(())
}
```
list units ```rust use systemdclient::{ buildnonblock_client, manager::nonblock::OrgFreedesktopSystemd1Manager, models::{IntoModel, Unit}, Result, SystemdObjectType, };
pub async fn main() -> Result<()> {
let (client, jh) = buildnonblockclient(SystemdObjectType::Manager)?;
let units = client.listunits().await?;
for unit in units {
let unit: Unit = unit.intomodel()?;
println!("{:#?}", unit);
}
// close connection
jh.abort();
Ok(())
}
create and start service
rust
use systemdclient::{
buildnonblockclient, createunitconfigurationfile,
manager::nonblock::OrgFreedesktopSystemd1Manager, models::IntoModel,
unit::nonblock::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
UnitSubStateType,
};
/*
* Run example as superuser since we start a service
* sh
* cargo build --example start_service_nonblock
* sudo ./target/debug/examples/start_service_nonblock
*
*/
async fn main() -> Result<()> { let unitbuilder = UnitConfiguration::builder().description("test service"); let svcbuilder = ServiceConfiguration::builder().execstart(vec!["/bin/sleep", "3"]); let svcunit = ServiceUnitConfiguration::builder() .unit(unitbuilder) .service(svcbuilder) .build(); let svcunitliteral = format!("{}", svcunit); // create /etc/systemd/system/test.service createunitconfigurationfile("test.service", svcunitliteral.asbytes())?; let (client, jh) = buildnonblockclient(SystemdObjectType::Manager)?; let jobpath = client.startunit("test.service", "replace").await?; println!("{}", jobpath); let svcunitpath = client.getunit("test.service").await?; println!("{}", svcunitpath); // close connection jh.abort(); // verify unit state given unit path let (client, jh) = buildnonblockclient(SystemdObjectType::Unit(svcunitpath))?; let unitprops = client.getunitproperties().await?; let unitprops: UnitProps = unitprops.intomodel()?; println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Active); asserteq!(unitprops.substate, UnitSubStateType::Running); std::thread::sleep(std::time::Duration::fromsecs(4)); // service should exit after 3 sec let unitprops = client.getunitproperties().await?; let unitprops: UnitProps = unitprops.intomodel()?; println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Inactive); asserteq!(unitprops.substate, UnitSubStateType::Dead); // close connection jh.abort(); Ok(()) } ```
sh
sudo apt install libdbus-1-dev pkg-config
edit build.rs
and create module for dbus object