hostname.rs (809B)
1 use async_trait::async_trait; 2 use blocking::unblock; 3 use nix::unistd::sethostname; 4 5 use kanit_common::error::{Context, ErrorKind, Result}; 6 use kanit_unit::{Dependencies, Unit}; 7 8 use crate::oneshot::Clock; 9 use crate::unit_name; 10 11 pub struct Hostname; 12 13 #[async_trait] 14 impl Unit for Hostname { 15 unit_name!("hostname"); 16 17 fn dependencies(&self) -> Dependencies { 18 Dependencies::new().after(Clock.name()).clone() 19 } 20 21 async fn start(&mut self) -> Result<()> { 22 let hostname = async_fs::read_to_string("/etc/hostname") 23 .await 24 .unwrap_or_else(|_| "homosexual".to_string()); 25 26 unblock(move || sethostname(hostname.trim_start().trim_end())) 27 .await 28 .context_kind("failed to set hostname", ErrorKind::Recoverable)?; 29 30 Ok(()) 31 } 32 }