11# systemd-client
2- [ ` systemd dbus ` ] client lib using [ ` dbus-codegen ` ]
2+ [ ` systemd dbus ` ] client lib using [ ` zbus ` ]
33## Examples
44### Blocking
55list units
66``` rust
7- use systemd_client :: {
8- build_blocking_client,
9- manager :: blocking :: OrgFreedesktopSystemd1Manager ,
10- models :: {IntoModel , Unit },
11- Result , SystemdObjectType ,
12- };
7+ use systemd_client :: {manager, models :: Unit , Result };
138
149fn main () -> Result <()> {
15- let client = build_blocking_client ( SystemdObjectType :: Manager )? ;
10+ let client = manager :: build_blocking_proxy ( )? ;
1611 let units = client . list_units ()? ;
1712 for unit in units {
18- let unit : Unit = unit . into_model () ? ;
13+ let unit : Unit = unit . into () ;
1914 println! (" {:#?}" , unit );
2015 }
2116 Ok (())
@@ -24,10 +19,8 @@ fn main() -> Result<()> {
2419create and start service
2520``` rust
2621use systemd_client :: {
27- build_blocking_client, create_unit_configuration_file,
28- manager :: blocking :: OrgFreedesktopSystemd1Manager , models :: IntoModel ,
29- unit :: blocking :: UnitProperties , Result , ServiceConfiguration , ServiceUnitConfiguration ,
30- SystemdObjectType , UnitActiveStateType , UnitConfiguration , UnitLoadStateType , UnitProps ,
22+ create_unit_configuration_file, manager, unit, Result , ServiceConfiguration ,
23+ ServiceUnitConfiguration , UnitActiveStateType , UnitConfiguration , UnitLoadStateType , UnitProps ,
3124 UnitSubStateType ,
3225};
3326
@@ -48,23 +41,23 @@ fn main() -> Result<()> {
4841 let svc_unit_literal = format! (" {}" , svc_unit );
4942 // create /etc/systemd/system/test.service
5043 create_unit_configuration_file (" test.service" , svc_unit_literal . as_bytes ())? ;
51- let client = build_blocking_client ( SystemdObjectType :: Manager )? ;
44+ let client = manager :: build_blocking_proxy ( )? ;
5245 let job_path = client . start_unit (" test.service" , " replace" )? ;
53- println! (" {}" , job_path );
46+ println! (" {}" , job_path . as_str () );
5447 let svc_unit_path = client . get_unit (" test.service" )? ;
55- println! (" {}" , svc_unit_path );
48+ println! (" {}" , svc_unit_path . as_str () );
5649 // verify unit state given unit path
57- let client = build_blocking_client ( SystemdObjectType :: Unit (svc_unit_path ) )? ;
58- let unit_props = client . get_unit_properties ()? ;
59- let unit_props : UnitProps = unit_props . into_model () ? ;
50+ let client = unit :: build_blocking_proxy (svc_unit_path )? ;
51+ let unit_props = client . get_properties ()? ;
52+ let unit_props : UnitProps = unit_props . into () ;
6053 println! (" {:?}" , unit_props );
6154 assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
6255 assert_eq! (unit_props . active_state, UnitActiveStateType :: Active );
6356 assert_eq! (unit_props . sub_state, UnitSubStateType :: Running );
6457 std :: thread :: sleep (std :: time :: Duration :: from_secs (4 ));
6558 // service should exit after 3 sec
66- let unit_props = client . get_unit_properties ()? ;
67- let unit_props : UnitProps = unit_props . into_model () ? ;
59+ let unit_props = client . get_properties ()? ;
60+ let unit_props : UnitProps = unit_props . into () ;
6861 println! (" {:?}" , unit_props );
6962 assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
7063 assert_eq! (unit_props . active_state, UnitActiveStateType :: Inactive );
@@ -75,33 +68,24 @@ fn main() -> Result<()> {
7568### Non Block
7669list units
7770``` rust
78- use systemd_client :: {
79- build_nonblock_client,
80- manager :: nonblock :: OrgFreedesktopSystemd1Manager ,
81- models :: {IntoModel , Unit },
82- Result , SystemdObjectType ,
83- };
71+ use systemd_client :: {manager, models :: Unit , Result };
8472
8573#[tokio:: main]
8674pub async fn main () -> Result <()> {
87- let ( client , jh ) = build_nonblock_client ( SystemdObjectType :: Manager ) ? ;
75+ let client = manager :: build_nonblock_proxy () . await ? ;
8876 let units = client . list_units (). await ? ;
8977 for unit in units {
90- let unit : Unit = unit . into_model () ? ;
78+ let unit : Unit = unit . into () ;
9179 println! (" {:#?}" , unit );
9280 }
93- // close connection
94- jh . abort ();
9581 Ok (())
9682}
9783```
9884create and start service
9985``` rust
10086use systemd_client :: {
101- build_nonblock_client, create_unit_configuration_file,
102- manager :: nonblock :: OrgFreedesktopSystemd1Manager , models :: IntoModel ,
103- unit :: nonblock :: UnitProperties , Result , ServiceConfiguration , ServiceUnitConfiguration ,
104- SystemdObjectType , UnitActiveStateType , UnitConfiguration , UnitLoadStateType , UnitProps ,
87+ create_unit_configuration_file, manager, unit, Result , ServiceConfiguration ,
88+ ServiceUnitConfiguration , UnitActiveStateType , UnitConfiguration , UnitLoadStateType ,
10589 UnitSubStateType ,
10690};
10791
@@ -123,41 +107,28 @@ async fn main() -> Result<()> {
123107 let svc_unit_literal = format! (" {}" , svc_unit );
124108 // create /etc/systemd/system/test.service
125109 create_unit_configuration_file (" test.service" , svc_unit_literal . as_bytes ())? ;
126- let ( client , jh ) = build_nonblock_client ( SystemdObjectType :: Manager ) ? ;
110+ let client = manager :: build_nonblock_proxy () . await ? ;
127111 let job_path = client . start_unit (" test.service" , " replace" ). await ? ;
128- println! (" {}" , job_path );
112+ println! (" {}" , job_path . as_str () );
129113 let svc_unit_path = client . get_unit (" test.service" ). await ? ;
130- println! (" {}" , svc_unit_path );
131- // close connection
132- jh . abort ();
114+ println! (" {}" , svc_unit_path . as_str ());
133115 // verify unit state given unit path
134- let (client , jh ) = build_nonblock_client (SystemdObjectType :: Unit (svc_unit_path ))? ;
135- let unit_props = client . get_unit_properties (). await ? ;
136- let unit_props : UnitProps = unit_props . into_model ()? ;
116+ let client = unit :: build_nonblock_proxy (svc_unit_path ). await ? ;
117+ let unit_props = client . get_properties (). await ? ;
137118 println! (" {:?}" , unit_props );
138119 assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
139120 assert_eq! (unit_props . active_state, UnitActiveStateType :: Active );
140121 assert_eq! (unit_props . sub_state, UnitSubStateType :: Running );
141122 std :: thread :: sleep (std :: time :: Duration :: from_secs (4 ));
142123 // service should exit after 3 sec
143- let unit_props = client . get_unit_properties (). await ? ;
144- let unit_props : UnitProps = unit_props . into_model ()? ;
124+ let unit_props = client . get_properties (). await ? ;
145125 println! (" {:?}" , unit_props );
146126 assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
147127 assert_eq! (unit_props . active_state, UnitActiveStateType :: Inactive );
148128 assert_eq! (unit_props . sub_state, UnitSubStateType :: Dead );
149- // close connection
150- jh . abort ();
151129 Ok (())
152130}
153131```
154- ## Development
155- ### Install Tools
156- ``` sh
157- sudo apt install libdbus-1-dev pkg-config
158- ```
159- ### Codegen
160- edit ` build.rs ` and create module for dbus object
161132
162133[ `systemd dbus` ] : https://www.freedesktop.org/software/systemd/man/org.freedesktop.systemd1.html
163- [ `dbus-codegen ` ] : https://github.com/diwic/ dbus-rs/tree/master/dbus-codegen
134+ [ `zbus ` ] : https://gitlab.freedesktop.org/ dbus/zbus
0 commit comments