33use core:: str;
44use std:: process:: Command ;
55
6- use anyhow:: { bail, Result } ;
6+ use anyhow:: { bail, Context , Result } ;
77use log:: error;
88use tokio_util:: sync:: CancellationToken ;
99use tracing:: info;
@@ -198,7 +198,6 @@ pub async fn start_service(service: UnitId, cancel_token: CancellationToken) ->
198198 // god these select macros are ugly, is there really no better way to select?
199199 tokio:: select! {
200200 _ = cancel_token. cancelled( ) => {
201- // The token was cancelled
202201 anyhow:: bail!( "cancelled" ) ;
203202 }
204203 result = start_service( service) => {
@@ -218,7 +217,6 @@ pub async fn stop_service(service: UnitId, cancel_token: CancellationToken) -> R
218217 // god these select macros are ugly, is there really no better way to select?
219218 tokio:: select! {
220219 _ = cancel_token. cancelled( ) => {
221- // The token was cancelled
222220 anyhow:: bail!( "cancelled" ) ;
223221 }
224222 result = stop_service( service) => {
@@ -227,6 +225,29 @@ pub async fn stop_service(service: UnitId, cancel_token: CancellationToken) -> R
227225 }
228226}
229227
228+ pub async fn reload ( scope : UnitScope , cancel_token : CancellationToken ) -> Result < ( ) > {
229+ async fn reload_ ( scope : UnitScope ) -> Result < ( ) > {
230+ let connection = get_connection ( scope) . await ?;
231+ let manager_proxy: ManagerProxy < ' _ > = ManagerProxy :: new ( & connection) . await ?;
232+ let error_message = match scope {
233+ UnitScope :: Global => "Failed to reload units, probably because superuser permissions are needed. Try running `sudo systemctl daemon-reload`" ,
234+ UnitScope :: User => "Failed to reload units. Try running `systemctl --user daemon-reload`" ,
235+ } ;
236+ manager_proxy. reload ( ) . await . context ( error_message) ?;
237+ Ok ( ( ) )
238+ }
239+
240+ // god these select macros are ugly, is there really no better way to select?
241+ tokio:: select! {
242+ _ = cancel_token. cancelled( ) => {
243+ anyhow:: bail!( "cancelled" ) ;
244+ }
245+ result = reload_( scope) => {
246+ result
247+ }
248+ }
249+ }
250+
230251async fn get_connection ( scope : UnitScope ) -> Result < Connection , anyhow:: Error > {
231252 match scope {
232253 UnitScope :: Global => Ok ( Connection :: system ( ) . await ?) ,
@@ -285,6 +306,10 @@ pub trait Manager {
285306 #[ dbus_proxy( name = "StopUnit" ) ]
286307 fn stop_unit ( & self , name : String , mode : String ) -> zbus:: Result < zvariant:: OwnedObjectPath > ;
287308
309+ /// [📖](https://www.freedesktop.org/software/systemd/man/systemd.directives.html#ReloadUnit()) Call interface method `ReloadUnit`.
310+ #[ dbus_proxy( name = "ReloadUnit" ) ]
311+ fn reload_unit ( & self , name : String , mode : String ) -> zbus:: Result < zvariant:: OwnedObjectPath > ;
312+
288313 /// [📖](https://www.freedesktop.org/software/systemd/man/systemd.directives.html#RestartUnit()) Call interface method `RestartUnit`.
289314 #[ dbus_proxy( name = "RestartUnit" ) ]
290315 fn restart_unit ( & self , name : String , mode : String ) -> zbus:: Result < zvariant:: OwnedObjectPath > ;
0 commit comments