kanit

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

run.rs (2002B)


      1 use std::path::Path;
      2 
      3 use async_trait::async_trait;
      4 use blocking::unblock;
      5 use log::info;
      6 use nix::mount::{MsFlags, mount};
      7 use nix::sys::stat::Mode;
      8 use nix::unistd::{Group, Uid, chown, mkdir};
      9 
     10 use kanit_common::constants;
     11 use kanit_common::error::{Context, ErrorKind, Result, StaticError};
     12 use kanit_unit::{Dependencies, Unit};
     13 
     14 use crate::mounts::try_mount_from_fstab;
     15 use crate::oneshot::ProcFs;
     16 use crate::unit_name;
     17 
     18 pub struct Run;
     19 
     20 #[async_trait]
     21 impl Unit for Run {
     22     unit_name!("run");
     23 
     24     fn dependencies(&self) -> Dependencies {
     25         Dependencies::new().need(ProcFs.name()).clone()
     26     }
     27 
     28     async fn start(&mut self) -> Result<()> {
     29         let path = Path::new("/run");
     30 
     31         if !path.exists() {
     32             Err(StaticError("/run doesn't exist")).kind(ErrorKind::Unrecoverable)?;
     33         }
     34 
     35         info!("mounting /run");
     36 
     37         if try_mount_from_fstab(path).await? {
     38             return Ok(());
     39         }
     40 
     41         unblock(move || {
     42             mount(
     43                 Some("none"),
     44                 path,
     45                 Some("tmpfs"),
     46                 MsFlags::MS_NODEV | MsFlags::MS_STRICTATIME | MsFlags::MS_NOSUID,
     47                 Some("mode=0755,nr_inodes=500k,size=10%"),
     48             )
     49         })
     50         .await
     51         .context("failed to mount run")?;
     52 
     53         info!("creating /run/lock");
     54 
     55         let lock = path.join("lock");
     56 
     57         unblock(move || -> Result<()> {
     58             mkdir(&lock, Mode::S_IROTH | Mode::S_IXOTH | Mode::S_IWUSR)?;
     59 
     60             let gid = Group::from_name("uucp")
     61                 .context("failed to get group uucp")?
     62                 .map(|g| g.gid);
     63 
     64             chown(&lock, Some(Uid::from_raw(0)), gid)
     65                 .context("failed to set permissions on /run/lock")?;
     66 
     67             Ok(())
     68         })
     69         .await?;
     70 
     71         info!("creating {}", constants::KAN_PIDS);
     72 
     73         unblock(move || mkdir(constants::KAN_PIDS, Mode::S_IRUSR | Mode::S_IWUSR))
     74             .await
     75             .context("failed to create pid directory")
     76     }
     77 }