| 163 | } |
| 164 | |
| 165 | fn preload_runtime_support_libraries(runtime_dir: &Path) -> Result<Vec<PathBuf>, String> { |
| 166 | let entries = std::fs::read_dir(runtime_dir) |
| 167 | .map_err(|e| format!("read {}: {e}", runtime_dir.display()))?; |
| 168 | |
| 169 | let mut support_libs: Vec<PathBuf> = entries |
| 170 | .filter_map(Result::ok) |
| 171 | .map(|entry| entry.path()) |
| 172 | .filter(|path| { |
| 173 | path.file_name() |
| 174 | .and_then(|name| name.to_str()) |
| 175 | .is_some_and(|name| { |
| 176 | #[cfg(target_os = "macos")] |
| 177 | { |
| 178 | name.starts_with("libkrunfw") |
| 179 | && Path::new(name) |
| 180 | .extension() |
| 181 | .is_some_and(|ext| ext.eq_ignore_ascii_case("dylib")) |
| 182 | } |
| 183 | #[cfg(not(target_os = "macos"))] |
| 184 | { |
| 185 | name.starts_with("libkrunfw") && name.contains(".so") |
| 186 | } |
| 187 | }) |
| 188 | }) |
| 189 | .collect(); |
| 190 | |
| 191 | support_libs.sort(); |
| 192 | for path in &support_libs { |
| 193 | let path_cstr = CString::new(path.to_string_lossy().as_bytes()) |
| 194 | .map_err(|e| format!("invalid support library path {}: {e}", path.display()))?; |
| 195 | let handle = |
| 196 | unsafe { libc::dlopen(path_cstr.as_ptr(), libc::RTLD_NOW | libc::RTLD_GLOBAL) }; |
| 197 | if handle.is_null() { |
| 198 | let error = unsafe { |
| 199 | let err = libc::dlerror(); |
| 200 | if err.is_null() { |
| 201 | "unknown dlopen error".to_string() |
| 202 | } else { |
| 203 | CStr::from_ptr(err).to_string_lossy().into_owned() |
| 204 | } |
| 205 | }; |
| 206 | return Err(format!( |
| 207 | "preload runtime support library {}: {error}", |
| 208 | path.display() |
| 209 | )); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | Ok(support_libs) |
| 214 | } |
| 215 | |
| 216 | fn load_symbol<T: Copy>(library: &'static Library, name: &[u8], path: &Path) -> Result<T, String> { |
| 217 | unsafe { |