// SPDX-FileCopyrightText: 2022 Matteo Settenvini // SPDX-License-Identifier: GPL-3.0-or-later use { anyhow::{ensure, Result}, std::ffi::CString, std::os::raw::c_int, std::os::unix::fs::symlink, std::path::PathBuf, std::sync::Once, }; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); static SETUP: Once = Once::new(); pub fn setup() -> Result<()> { unsafe { static mut NSS_CONFIG_STATUS: c_int = 0; SETUP.call_once(|| { let out_dir = PathBuf::from(env!("OUT_DIR")); let library_path = test_cdylib::build_current_project(); let mut library_filename = library_path.file_name().unwrap().to_owned(); library_filename.push(".2"); // required for NSS 2 modules let dest = out_dir.join(library_filename); symlink(&library_path, &dest).expect(&format!( "Unable to create symlink to library ({} -> {})", library_path.to_string_lossy(), dest.to_string_lossy() )); std::env::set_var("LD_LIBRARY_PATH", out_dir); let db = CString::new("hosts").unwrap(); let resolvers = CString::new("files malcontent [UNAVAIL=return] dns").unwrap(); NSS_CONFIG_STATUS = __nss_configure_lookup(db.as_ptr(), resolvers.as_ptr()); }); ensure!( NSS_CONFIG_STATUS == 0, "Unable to configure NSS to load module: __nss_configure_lookup() returned {}", NSS_CONFIG_STATUS ); Ok(()) } }