Skip to content

Commit 0c43c08

Browse files
authored
Don't load plugins/volumes when plugins_loading/enabled:false (#1269)
1 parent bc8029b commit 0c43c08

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

commons/zenoh-util/src/lib_loader.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{
2020

2121
use libloading::Library;
2222
use tracing::{debug, warn};
23-
use zenoh_core::zconfigurable;
23+
use zenoh_core::{zconfigurable, zerror};
2424
use zenoh_result::{bail, ZResult};
2525

2626
zconfigurable! {
@@ -35,15 +35,13 @@ zconfigurable! {
3535
/// LibLoader allows search for libraries and to load them.
3636
#[derive(Clone, Debug)]
3737
pub struct LibLoader {
38-
search_paths: Vec<PathBuf>,
38+
search_paths: Option<Vec<PathBuf>>,
3939
}
4040

4141
impl LibLoader {
4242
/// Return an empty `LibLoader`.
4343
pub fn empty() -> LibLoader {
44-
LibLoader {
45-
search_paths: Vec::new(),
46-
}
44+
LibLoader { search_paths: None }
4745
}
4846

4947
/// Returns the list of search paths used by `LibLoader::default()`
@@ -83,12 +81,14 @@ impl LibLoader {
8381
}
8482
}
8583

86-
LibLoader { search_paths }
84+
LibLoader {
85+
search_paths: Some(search_paths),
86+
}
8787
}
8888

8989
/// Return the list of search paths used by this [LibLoader]
90-
pub fn search_paths(&self) -> &[PathBuf] {
91-
&self.search_paths
90+
pub fn search_paths(&self) -> Option<&[PathBuf]> {
91+
self.search_paths.as_deref()
9292
}
9393

9494
/// Load a library from the specified path.
@@ -118,21 +118,24 @@ impl LibLoader {
118118
///
119119
/// This function calls [libloading::Library::new()](https://docs.rs/libloading/0.7.0/libloading/struct.Library.html#method.new)
120120
/// which is unsafe.
121-
pub unsafe fn search_and_load(&self, name: &str) -> ZResult<(Library, PathBuf)> {
121+
pub unsafe fn search_and_load(&self, name: &str) -> ZResult<Option<(Library, PathBuf)>> {
122122
let filename = format!("{}{}{}", *LIB_PREFIX, name, *LIB_SUFFIX);
123123
let filename_ostr = OsString::from(&filename);
124124
tracing::debug!(
125125
"Search for library {} to load in {:?}",
126126
filename,
127127
self.search_paths
128128
);
129-
for dir in &self.search_paths {
129+
let Some(search_paths) = self.search_paths() else {
130+
return Ok(None);
131+
};
132+
for dir in search_paths {
130133
match dir.read_dir() {
131134
Ok(read_dir) => {
132135
for entry in read_dir.flatten() {
133136
if entry.file_name() == filename_ostr {
134137
let path = entry.path();
135-
return Ok((Library::new(path.clone())?, path));
138+
return Ok(Some((Library::new(path.clone())?, path)));
136139
}
137140
}
138141
}
@@ -142,7 +145,7 @@ impl LibLoader {
142145
),
143146
}
144147
}
145-
bail!("Library file '{}' not found", filename)
148+
Err(zerror!("Library file '{}' not found", filename).into())
146149
}
147150

148151
/// Search and load all libraries with filename starting with [struct@LIB_PREFIX]+`prefix` and ending with [struct@LIB_SUFFIX].
@@ -158,17 +161,16 @@ impl LibLoader {
158161
pub unsafe fn load_all_with_prefix(
159162
&self,
160163
prefix: Option<&str>,
161-
) -> Vec<(Library, PathBuf, String)> {
164+
) -> Option<Vec<(Library, PathBuf, String)>> {
162165
let lib_prefix = format!("{}{}", *LIB_PREFIX, prefix.unwrap_or(""));
163166
tracing::debug!(
164167
"Search for libraries {}*{} to load in {:?}",
165168
lib_prefix,
166169
*LIB_SUFFIX,
167170
self.search_paths
168171
);
169-
170172
let mut result = vec![];
171-
for dir in &self.search_paths {
173+
for dir in self.search_paths()? {
172174
match dir.read_dir() {
173175
Ok(read_dir) => {
174176
for entry in read_dir.flatten() {
@@ -199,7 +201,7 @@ impl LibLoader {
199201
),
200202
}
201203
}
202-
result
204+
Some(result)
203205
}
204206

205207
pub fn _plugin_name(path: &std::path::Path) -> Option<&str> {

plugins/zenoh-plugin-storage-manager/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ impl StorageRuntimeInner {
229229
self.plugins_manager
230230
.declare_dynamic_plugin_by_name(volume_id, backend_name, true)?
231231
};
232-
let loaded = declared.load()?;
232+
let loaded = declared
233+
.load()?
234+
.expect("Volumes should not loaded if if the storage-manager plugin is not loaded");
233235
loaded.start(config)?;
234236

235237
Ok(())

plugins/zenoh-plugin-trait/src/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::*;
2525

2626
pub trait DeclaredPlugin<StartArgs, Instance>: PluginStatus {
2727
fn as_status(&self) -> &dyn PluginStatus;
28-
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>>;
28+
fn load(&mut self) -> ZResult<Option<&mut dyn LoadedPlugin<StartArgs, Instance>>>;
2929
fn loaded(&self) -> Option<&dyn LoadedPlugin<StartArgs, Instance>>;
3030
fn loaded_mut(&mut self) -> Option<&mut dyn LoadedPlugin<StartArgs, Instance>>;
3131
}
@@ -88,7 +88,7 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> DeclaredPlugin<StartA
8888
fn as_status(&self) -> &dyn PluginStatus {
8989
self
9090
}
91-
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
91+
fn load(&mut self) -> ZResult<Option<&mut dyn LoadedPlugin<StartArgs, Instance>>> {
9292
self.0.load()
9393
}
9494
fn loaded(&self) -> Option<&dyn LoadedPlugin<StartArgs, Instance>> {

plugins/zenoh-plugin-trait/src/manager/dynamic_plugin.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use std::path::{Path, PathBuf};
1414

1515
use libloading::Library;
16-
use zenoh_result::{bail, ZResult};
16+
use zenoh_result::{bail, zerror, ZResult};
1717
use zenoh_util::LibLoader;
1818

1919
use crate::*;
@@ -28,19 +28,19 @@ pub enum DynamicPluginSource {
2828
}
2929

3030
impl DynamicPluginSource {
31-
fn load(&self) -> ZResult<(Library, PathBuf)> {
31+
fn load(&self) -> ZResult<Option<(Library, PathBuf)>> {
3232
match self {
3333
DynamicPluginSource::ByName((libloader, name)) => unsafe {
3434
libloader.search_and_load(name)
3535
},
3636
DynamicPluginSource::ByPaths(paths) => {
3737
for path in paths {
3838
match unsafe { LibLoader::load_file(path) } {
39-
Ok((l, p)) => return Ok((l, p)),
39+
Ok((l, p)) => return Ok(Some((l, p))),
4040
Err(e) => tracing::debug!("Attempt to load {} failed: {}", path, e),
4141
}
4242
}
43-
bail!("Plugin not found in {:?}", &paths)
43+
Err(zerror!("Plugin not found in {:?}", &paths).into())
4444
}
4545
}
4646
}
@@ -179,16 +179,22 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> DeclaredPlugin<StartA
179179
fn as_status(&self) -> &dyn PluginStatus {
180180
self
181181
}
182-
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
182+
fn load(&mut self) -> ZResult<Option<&mut dyn LoadedPlugin<StartArgs, Instance>>> {
183183
if self.starter.is_none() {
184-
let (lib, path) = self.source.load().add_error(&mut self.report)?;
184+
let Some((lib, path)) = self.source.load().add_error(&mut self.report)? else {
185+
tracing::warn!(
186+
"Plugin `{}` will not be loaded as plugin loading is disabled",
187+
self.name
188+
);
189+
return Ok(None);
190+
};
185191
let starter = DynamicPluginStarter::new(lib, path).add_error(&mut self.report)?;
186192
tracing::debug!("Plugin {} loaded from {}", self.name, starter.path());
187193
self.starter = Some(starter);
188194
} else {
189195
tracing::warn!("Plugin `{}` already loaded", self.name);
190196
}
191-
Ok(self)
197+
Ok(Some(self))
192198
}
193199
fn loaded(&self) -> Option<&dyn LoadedPlugin<StartArgs, Instance>> {
194200
if self.starter.is_some() {

plugins/zenoh-plugin-trait/src/manager/static_plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ where
8484
fn as_status(&self) -> &dyn PluginStatus {
8585
self
8686
}
87-
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
88-
Ok(self)
87+
fn load(&mut self) -> ZResult<Option<&mut dyn LoadedPlugin<StartArgs, Instance>>> {
88+
Ok(Some(self))
8989
}
9090
fn loaded(&self) -> Option<&dyn LoadedPlugin<StartArgs, Instance>> {
9191
Some(self)

zenoh/src/net/runtime/adminspace.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ impl AdminSpace {
131131
);
132132
loaded
133133
} else {
134-
declared.load()?
134+
match declared.load()? {
135+
Some(loaded) => loaded,
136+
None => {
137+
tracing::warn!(
138+
"Plugin `{}` will not be loaded as plugin loading is disabled",
139+
config.name
140+
);
141+
return Ok(());
142+
}
143+
}
135144
};
136145

137146
if let Some(started) = loaded.started_mut() {

0 commit comments

Comments
 (0)