-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright (c) 2023 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
#[repr(C)] | ||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub struct Compatibility { | ||
major: u64, | ||
minor: u64, | ||
patch: u64, | ||
stable: bool, | ||
commit: &'static str, | ||
} | ||
const RELEASE_AND_COMMIT: (&str, &str) = zenoh_macros::rustc_version_release!(); | ||
impl Compatibility { | ||
pub fn new() -> Self { | ||
let (release, commit) = RELEASE_AND_COMMIT; | ||
let (release, stable) = if let Some(p) = release.chars().position(|c| c == '-') { | ||
(&release[..p], false) | ||
} else { | ||
(release, true) | ||
}; | ||
let mut split = release.split('.').map(|s| s.trim()); | ||
Compatibility { | ||
major: split.next().unwrap().parse().unwrap(), | ||
minor: split.next().unwrap().parse().unwrap(), | ||
patch: split.next().unwrap().parse().unwrap(), | ||
stable, | ||
commit, | ||
} | ||
} | ||
pub fn are_compatible(a: &Self, b: &Self) -> bool { | ||
if a.stable && b.stable { | ||
a.major == b.major && a.minor == b.minor && a.patch == b.patch | ||
} else { | ||
a == b | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,12 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::vtable::*; | ||
use crate::*; | ||
use libloading::Library; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
use vtable::PluginVTable; | ||
use zenoh_result::{bail, zerror, ZResult}; | ||
use zenoh_util::LibLoader; | ||
|
||
|
@@ -96,7 +96,6 @@ impl<StartArgs: 'static, RunningPlugin: 'static> PluginsManager<StartArgs, Runni | |
running_plugins, | ||
.. | ||
} = self; | ||
let compat = crate::Compatibility::new().unwrap(); | ||
plugin_starters.iter().map(move |p| { | ||
let name = p.name(); | ||
let path = p.path(); | ||
|
@@ -105,29 +104,12 @@ impl<StartArgs: 'static, RunningPlugin: 'static> PluginsManager<StartArgs, Runni | |
path, | ||
match running_plugins.entry(name.into()) { | ||
std::collections::hash_map::Entry::Occupied(_) => Ok(None), | ||
std::collections::hash_map::Entry::Vacant(e) => { | ||
let compatible = match p.compatibility() { | ||
Some(Ok(c)) => { | ||
if Compatibility::are_compatible(&compat, &c) { | ||
Ok(()) | ||
} else { | ||
Err(zerror!("Plugin compatibility mismatch: host: {:?} - plugin: {:?}. This could lead to segfaults, so wer'e not starting it.", &compat, &c)) | ||
} | ||
} | ||
Some(Err(e)) => Err(zerror!(e => "Plugin {} (from {}) compatibility couldn't be recovered. This likely means it's very broken.", name, path)), | ||
None => Ok(()), | ||
}; | ||
if let Err(e) = compatible { | ||
Err(e.into()) | ||
} else { | ||
match p.start(args) { | ||
Ok(p) => Ok(Some(unsafe { | ||
std::mem::transmute(&e.insert((path.into(), p)).1) | ||
})), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
std::collections::hash_map::Entry::Vacant(e) => match p.start(args) { | ||
Ok(p) => Ok(Some(unsafe { | ||
std::mem::transmute(&e.insert((path.into(), p)).1) | ||
})), | ||
Err(e) => Err(e), | ||
}, | ||
}, | ||
) | ||
}) | ||
|
@@ -218,7 +200,6 @@ trait PluginStarter<StartArgs, RunningPlugin> { | |
fn name(&self) -> &str; | ||
fn path(&self) -> &str; | ||
fn start(&self, args: &StartArgs) -> ZResult<RunningPlugin>; | ||
fn compatibility(&self) -> Option<ZResult<Compatibility>>; | ||
fn deletable(&self) -> bool; | ||
} | ||
|
||
|
@@ -244,9 +225,6 @@ where | |
fn path(&self) -> &str { | ||
"<statically_linked>" | ||
} | ||
fn compatibility(&self) -> Option<ZResult<Compatibility>> { | ||
None | ||
} | ||
fn start(&self, args: &StartArgs) -> ZResult<RunningPlugin> { | ||
P::start(P::STATIC_NAME, args) | ||
} | ||
|
@@ -265,10 +243,7 @@ impl<StartArgs, RunningPlugin> PluginStarter<StartArgs, RunningPlugin> | |
self.path.to_str().unwrap() | ||
} | ||
fn start(&self, args: &StartArgs) -> ZResult<RunningPlugin> { | ||
self.vtable.start(self.name(), args) | ||
} | ||
fn compatibility(&self) -> Option<ZResult<Compatibility>> { | ||
Some(self.vtable.compatibility()) | ||
(self.vtable.start)(self.name(), args) | ||
} | ||
fn deletable(&self) -> bool { | ||
true | ||
|
@@ -283,21 +258,18 @@ pub struct DynamicPlugin<StartArgs, RunningPlugin> { | |
} | ||
|
||
impl<StartArgs, RunningPlugin> DynamicPlugin<StartArgs, RunningPlugin> { | ||
fn new(name: String, lib: Library, path: PathBuf) -> Result<Self, Option<PluginVTableVersion>> { | ||
fn new(name: String, lib: Library, path: PathBuf) -> Self { | ||
// TODO: check loader version and compatibility | ||
let load_plugin = unsafe { | ||
lib.get::<fn(PluginVTableVersion) -> LoadPluginResult<StartArgs, RunningPlugin>>( | ||
b"load_plugin", | ||
) | ||
.map_err(|_| None)? | ||
lib.get::<fn() -> PluginVTable<StartArgs, RunningPlugin>>(b"load_plugin") | ||
.map_err(|_| None)? | ||
Check failure on line 265 in plugins/zenoh-plugin-trait/src/loading.rs GitHub Actions / Run checks on ubuntu-latest
Check failure on line 265 in plugins/zenoh-plugin-trait/src/loading.rs GitHub Actions / Run tests on ubuntu-latest
Check failure on line 265 in plugins/zenoh-plugin-trait/src/loading.rs GitHub Actions / Run checks on macOS-latest
Check failure on line 265 in plugins/zenoh-plugin-trait/src/loading.rs GitHub Actions / Run tests on macOS-latest
Check failure on line 265 in plugins/zenoh-plugin-trait/src/loading.rs GitHub Actions / Run checks on windows-latest
|
||
}; | ||
match load_plugin(PLUGIN_VTABLE_VERSION) { | ||
Ok(vtable) => Ok(DynamicPlugin { | ||
_lib: lib, | ||
vtable, | ||
name, | ||
path, | ||
}), | ||
Err(plugin_version) => Err(Some(plugin_version)), | ||
let vtable = load_plugin(); | ||
Self { | ||
_lib: lib, | ||
vtable, | ||
name, | ||
path, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters