kanit

Toy init system
Log | Files | Refs | README | LICENSE

mdev.rs (1432B)


      1 use async_process::Command;
      2 use async_trait::async_trait;
      3 use log::info;
      4 
      5 use kanit_common::error::{Context, ErrorKind, Result, StaticError};
      6 use kanit_unit::{Dependencies, Unit};
      7 
      8 use crate::oneshot::{DevFs, SysFs};
      9 use crate::unit_name;
     10 
     11 // TODO; write one for udev as well
     12 pub struct MDev;
     13 
     14 #[async_trait]
     15 impl Unit for MDev {
     16     unit_name!("mdev");
     17 
     18     fn dependencies(&self) -> Dependencies {
     19         Dependencies::new()
     20             .need(SysFs.name())
     21             .need(DevFs.name())
     22             .clone()
     23     }
     24 
     25     async fn start(&mut self) -> Result<()> {
     26         info!("initializing mdev");
     27 
     28         async_fs::write("/proc/sys/kernel/hotplug", "/sbin/mdev")
     29             .await
     30             .context("failed to initialize mdev for hotplug")?;
     31 
     32         info!("loading hardware for mdev");
     33 
     34         let succ = Command::new("mdev")
     35             .arg("-s")
     36             .spawn()
     37             .context("failed to spawn mdev")?
     38             .status()
     39             .await
     40             .context("failed to wait")?
     41             .success();
     42 
     43         if !succ {
     44             Err(StaticError("failed to spawn mdev")).kind(ErrorKind::Recoverable)?;
     45         }
     46 
     47         Ok(())
     48     }
     49 
     50     async fn stop(&mut self) -> Result<()> {
     51         info!("stopping mdev");
     52 
     53         async_fs::write("/proc/sys/kernel/hotplug", "/sbin/mdev")
     54             .await
     55             .context_kind("failed to stop mdev", ErrorKind::Recoverable)?;
     56 
     57         Ok(())
     58     }
     59 }