|
| 1 | +//! The fixed launch journal and the caller-saved VM's runtime record. |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +use serde::de::DeserializeOwned; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +use super::StateFile; |
| 10 | +use super::files; |
| 11 | +use super::locked::LockedState; |
| 12 | +use crate::host::process::ProcessRef; |
| 13 | +use crate::host::runtime::RuntimeIdentity; |
| 14 | +use crate::host::vm::{Prepared, SavedVm, VmDir, VmOutputs}; |
| 15 | +use crate::protocol::CAPABILITY_LEN; |
| 16 | +use crate::{Error, Result}; |
| 17 | + |
| 18 | +#[derive(Clone, Serialize, Deserialize)] |
| 19 | +#[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 20 | +pub(crate) struct LaunchJournal { |
| 21 | + pub(super) vm_dir: PathBuf, |
| 22 | + pub(super) capability: [u8; CAPABILITY_LEN], |
| 23 | + pub(super) log: PathBuf, |
| 24 | + pub(super) outcome: PathBuf, |
| 25 | + pub(super) endpoint: String, |
| 26 | + pub(super) console: Option<String>, |
| 27 | + pub(super) runtime: RuntimeIdentity, |
| 28 | + pub(super) process: Option<ProcessRef>, |
| 29 | +} |
| 30 | + |
| 31 | +impl fmt::Debug for LaunchJournal { |
| 32 | + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 33 | + formatter |
| 34 | + .debug_struct("LaunchJournal") |
| 35 | + .field("vm_dir", &self.vm_dir) |
| 36 | + .field("log", &self.log) |
| 37 | + .field("outcome", &self.outcome) |
| 38 | + .field("endpoint", &self.endpoint) |
| 39 | + .field("console", &self.console) |
| 40 | + .field("runtime", &self.runtime) |
| 41 | + .field("process", &self.process) |
| 42 | + .finish_non_exhaustive() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl LaunchJournal { |
| 47 | + pub(super) fn prepared( |
| 48 | + state: &LockedState, |
| 49 | + prepared: &Prepared, |
| 50 | + runtime: &RuntimeIdentity, |
| 51 | + ) -> Result<Self> { |
| 52 | + runtime.check()?; |
| 53 | + let vm_dir = VmDir::open(state.dir())?; |
| 54 | + let VmOutputs { |
| 55 | + log, |
| 56 | + outcome, |
| 57 | + endpoint, |
| 58 | + console, |
| 59 | + } = prepared.outputs(); |
| 60 | + if *log != vm_dir.path().join(StateFile::Log.name()) |
| 61 | + || *outcome != vm_dir.path().join(StateFile::Outcome.name()) |
| 62 | + { |
| 63 | + return Err(Error::other( |
| 64 | + "the prepared launch belongs to another sandbox directory", |
| 65 | + )); |
| 66 | + } |
| 67 | + Ok(Self { |
| 68 | + vm_dir: vm_dir.path().to_path_buf(), |
| 69 | + capability: *prepared.capability().as_bytes(), |
| 70 | + log: log.clone(), |
| 71 | + outcome: outcome.clone(), |
| 72 | + endpoint: endpoint.clone(), |
| 73 | + console: console.clone(), |
| 74 | + runtime: runtime.clone(), |
| 75 | + process: None, |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + pub(crate) fn process(&self) -> Option<&ProcessRef> { |
| 80 | + self.process.as_ref() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[derive(Serialize, Deserialize)] |
| 85 | +#[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 86 | +pub(super) struct RuntimeRecord { |
| 87 | + vm_dir: PathBuf, |
| 88 | + endpoint: String, |
| 89 | + capability: [u8; CAPABILITY_LEN], |
| 90 | + process: ProcessRef, |
| 91 | + runtime: RuntimeIdentity, |
| 92 | +} |
| 93 | + |
| 94 | +impl From<&SavedVm> for RuntimeRecord { |
| 95 | + fn from(saved: &SavedVm) -> Self { |
| 96 | + Self { |
| 97 | + vm_dir: saved.vm_dir.path().to_path_buf(), |
| 98 | + endpoint: saved.endpoint.clone(), |
| 99 | + capability: saved.capability, |
| 100 | + process: saved.process.clone(), |
| 101 | + runtime: saved.runtime.clone(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl LockedState { |
| 107 | + pub(crate) fn journal(&self) -> Result<Option<LaunchJournal>> { |
| 108 | + self.read_record(StateFile::Journal) |
| 109 | + } |
| 110 | + |
| 111 | + pub(crate) fn runtime(&self) -> Result<Option<SavedVm>> { |
| 112 | + let Some(record) = self.read_record::<RuntimeRecord>(StateFile::Runtime)? else { |
| 113 | + return Ok(None); |
| 114 | + }; |
| 115 | + let vm_dir = VmDir::open(self.dir())?; |
| 116 | + if record.vm_dir != vm_dir.path() { |
| 117 | + return Err(Error::io( |
| 118 | + "the runtime record belongs to another sandbox directory", |
| 119 | + )); |
| 120 | + } |
| 121 | + Ok(Some(SavedVm { |
| 122 | + vm_dir, |
| 123 | + endpoint: record.endpoint, |
| 124 | + capability: record.capability, |
| 125 | + process: record.process, |
| 126 | + runtime: record.runtime, |
| 127 | + })) |
| 128 | + } |
| 129 | + |
| 130 | + pub(crate) fn capability(&self) -> Result<Option<[u8; CAPABILITY_LEN]>> { |
| 131 | + files::read(self, StateFile::Capability)? |
| 132 | + .map(|bytes| { |
| 133 | + bytes.try_into().map_err(|_| { |
| 134 | + Error::io(format!( |
| 135 | + "the sandbox capability must hold {CAPABILITY_LEN} bytes" |
| 136 | + )) |
| 137 | + }) |
| 138 | + }) |
| 139 | + .transpose() |
| 140 | + } |
| 141 | + |
| 142 | + fn read_record<T: DeserializeOwned>(&self, name: StateFile) -> Result<Option<T>> { |
| 143 | + files::read(self, name)? |
| 144 | + .map(|bytes| { |
| 145 | + serde_json::from_slice(&bytes).map_err(|error| { |
| 146 | + Error::io(format!("cannot parse {}", self.path(name).display())) |
| 147 | + .with_source(error) |
| 148 | + }) |
| 149 | + }) |
| 150 | + .transpose() |
| 151 | + } |
| 152 | + |
| 153 | + pub(super) fn write_record(&self, name: StateFile, record: &impl Serialize) -> Result<()> { |
| 154 | + let bytes = serde_json::to_vec(record).map_err(|error| { |
| 155 | + Error::io(format!("cannot serialize {}", self.path(name).display())).with_source(error) |
| 156 | + })?; |
| 157 | + files::replace(self, name, &bytes) |
| 158 | + } |
| 159 | +} |
0 commit comments