1 //! How a plugin starts: the two arguments that the core gives it.2 //!3 //! The core creates a private directory with a filesystem socket and starts the4 //! plugin with `--socket <path>`. The path is the credential: it is random, it5 //! exists only in the core's private directory and in the child's argv, and a6 //! process that was not told it cannot guess it. The directory is mode 0700 and7 //! is the only path that a container engine sees.8 //!9 //! `--describe` prints the catalogue and stops. It does no loading, so it has no10 //! deadline and a plugin with a missing weight file still describes itself11 //! (spec section 16, rule 2, and requirement R1).12 //!13 //! A plugin's `main` is [`launch`]. That is the whole ABI, and it is in one14 //! place because every plugin must obey it.15 16 use std::io;17 use std::os::unix::net::UnixStream;18 use std::sync::Arc;19 20 use inf3r_proto::body::Catalogue;21 22 use crate::capability::Failure;23 use crate::serve::{Plugin, RuntimeVersion, serve};24 25 /// The name of the flag that carries a socket path.26 pub const SOCKET: &str = "--socket";27 28 /// The name of the flag that prints the catalogue.29 pub const DESCRIBE: &str = "--describe";30 31 /// The variable in which a runtime reports the version it loaded.32 pub const RUNTIME_VERSION: &str = "INF3R_RUNTIME_VERSION";33 34 /// Serves the plugin on the filesystem socket that the core named.35 pub fn serve_socket<P: Plugin>(path: &str, plugin: Arc<P>) -> io::Result<()> {36 let socket = UnixStream::connect(path)?;37 serve(socket, plugin)38 }39 40 /// The catalogue as the JSON of spec section 16.3.41 pub fn catalogue_json(catalogue: &Catalogue) -> String {42 inf3r_json::to_string(&inf3r_proto::mirror::to_json(&catalogue.to_value()))43 }44 45 /// Runs a plugin's two arguments.46 ///47 /// A plugin's `main` is:48 ///49 /// ```ignore50 /// fn main() -> std::io::Result<()> {51 /// inf3r_plugin_sdk::launch(std::sync::Arc::new(MyModel::new()?))52 /// }53 /// ```54 pub fn launch<P: Plugin>(plugin: Arc<P>) -> io::Result<()> {55 check_runtime(plugin.runtime())?;56 let args: Vec<String> = std::env::args().skip(1).collect();57 match args.first().map(String::as_str) {58 Some(DESCRIBE) => {59 let mut out = io::stdout();60 let mut catalogue = plugin.catalogue();61 if let Some(codec) = plugin.cache()62 && catalogue.cache.is_none()63 {64 catalogue.cache = Some(codec.spec());65 }66 std::io::Write::write_all(&mut out, catalogue_json(&catalogue).as_bytes())?;67 std::io::Write::write_all(&mut out, b"\n")?;68 Ok(())69 }70 Some(SOCKET) => match args.get(1) {71 Some(path) => serve_socket(path, plugin),72 None => Err(usage()),73 },74 _ => Err(usage()),75 }76 }77 78 /// Refuses a binding whose runtime reports another version.79 ///80 /// The variable is the runtime's own report. The core never sets it, because a81 /// hand-built library can load a runtime that the manifest does not name.82 fn check_runtime(expected: Option<RuntimeVersion>) -> io::Result<()> {83 let Some(expected) = expected else {84 return Ok(());85 };86 let actual = match std::env::var(RUNTIME_VERSION) {87 Ok(actual) => actual,88 Err(_) => {89 return Err(io::Error::new(90 io::ErrorKind::InvalidData,91 format!(92 "the binding was built for {} {} and the runtime reports no version",93 expected.name, expected.version94 ),95 ));96 }97 };98 if expected.version == actual {99 return Ok(());100 }101 Err(io::Error::new(102 io::ErrorKind::InvalidData,103 format!(104 "the binding was built for {} {} and the runtime reports {actual}",105 expected.name, expected.version106 ),107 ))108 }109 110 /// A load failure that a plugin's `new` can return, so that its `main` can use111 /// one error type.112 impl From<Failure> for io::Error {113 fn from(failure: Failure) -> io::Error {114 io::Error::other(failure.to_string())115 }116 }117 118 fn usage() -> io::Error {119 io::Error::new(120 io::ErrorKind::InvalidInput,121 format!("usage: {DESCRIBE} | {SOCKET} <path>"),122 )123 }