1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use libc::{dlopen, dlsym, RTLD_LAZY};
use std::ffi::CString;
use std::mem;
use std::path::Path;
use crate::command::Registry;
pub fn load_plugins(search_path: &[String], plugins: &[String], reg: &mut Registry) {
let sym_name = CString::new("register_commands").unwrap();
for name in plugins {
eprintln!("loading {}...", name);
let mut found = false;
for dir in search_path {
let path_str = format!("{}/lib{}.so", dir, name);
let path = Path::new(&path_str);
if path.exists() {
let c_path = CString::new(path_str.clone()).unwrap();
unsafe {
let so = dlopen(c_path.as_ptr(), RTLD_LAZY);
if so.is_null() {
panic!("failed to open plugin `{}`", path_str);
}
let sym = dlsym(so, sym_name.as_ptr());
if sym.is_null() {
panic!(
"failed to locate symbol `register_commands` in `{}`",
path_str
);
}
let f: fn(&mut Registry) = mem::transmute(sym);
f(reg);
}
found = true;
break;
}
}
if !found {
panic!(
"plugin `{}` was not found in search path ({:?})",
name, search_path
);
}
}
}