syslog.rs (1213B)
1 use async_trait::async_trait; 2 use blocking::unblock; 3 use log::info; 4 use nix::sys::signal::{Signal, kill}; 5 use nix::unistd::Pid; 6 7 use kanit_common::error::Result; 8 use kanit_supervisor::{RestartPolicy, SupervisorBuilder}; 9 use kanit_unit::{Dependencies, Unit}; 10 11 use crate::oneshot::{Clock, Hostname, LocalMount}; 12 use crate::unit_name; 13 14 pub struct Syslog { 15 pid: u32, 16 } 17 18 impl Syslog { 19 #[allow(clippy::new_without_default)] 20 pub fn new() -> Self { 21 Self { pid: 0 } 22 } 23 } 24 25 #[async_trait] 26 impl Unit for Syslog { 27 unit_name!("syslog"); 28 29 fn dependencies(&self) -> Dependencies { 30 Dependencies::new() 31 .need(Clock.name()) 32 .need(Hostname.name()) 33 .need(LocalMount.name()) 34 .clone() 35 } 36 37 async fn start(&mut self) -> Result<()> { 38 info!("starting syslog"); 39 40 let child = SupervisorBuilder::new("syslogd", []) 41 .restart_policy(RestartPolicy::OnFailure) 42 .spawn()?; 43 44 self.pid = child.id(); 45 46 Ok(()) 47 } 48 49 async fn stop(&mut self) -> Result<()> { 50 let pid = self.pid; 51 let _ = unblock(move || kill(Pid::from_raw(pid as i32), Signal::SIGKILL)).await; 52 53 Ok(()) 54 } 55 }