From 1bf5f46404590cb744ab1df1515f1516665a4889 Mon Sep 17 00:00:00 2001 From: Hayodea Hakol Date: Thu, 24 Jul 2025 02:12:31 -0400 Subject: [PATCH] Provide dlopen() path searching hook to senseApi libs --- commonLibs/xcbXorg/xcbXorg.cpp | 9 +++++-- include/user/senseApiDesc.h | 29 ++++++++++++++++++++- smocore/include/senseApis/senseApiManager.h | 4 +++ smocore/senseApis/senseApiManager.cpp | 22 +++++++++++++--- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/commonLibs/xcbXorg/xcbXorg.cpp b/commonLibs/xcbXorg/xcbXorg.cpp index 3ab2b2a..b5f8926 100644 --- a/commonLibs/xcbXorg/xcbXorg.cpp +++ b/commonLibs/xcbXorg/xcbXorg.cpp @@ -457,13 +457,18 @@ static smo::sense_api::SenseApiDesc xcbXorgApiDesc = * its name, the implexors it exports, and the management operations it * supports. * + * @param callbacks Hooks provided by Salmanoff for library operations * @return A reference to the SenseApiDesc structure describing the API. */ extern SMO_UNMANGLED smo::sense_api::SMO_GET_SENSE_API_DESC_FN_TYPEDEF - SMO_GET_SENSE_API_DESC_FN_NAME; + SMO_GET_SENSE_API_DESC_FN_NAME; -const smo::sense_api::SenseApiDesc &SMO_GET_SENSE_API_DESC_FN_NAME(void) +static const smo::sense_api::SalmanoffCallbacks* xcbXorg_callbacks = nullptr; + +const smo::sense_api::SenseApiDesc &SMO_GET_SENSE_API_DESC_FN_NAME( + const smo::sense_api::SalmanoffCallbacks& callbacks) { + xcbXorg_callbacks = &callbacks; return xcbXorgApiDesc; } diff --git a/include/user/senseApiDesc.h b/include/user/senseApiDesc.h index 4d39547..fcafa13 100644 --- a/include/user/senseApiDesc.h +++ b/include/user/senseApiDesc.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace smo { namespace sense_api { @@ -13,6 +15,27 @@ typedef int (sal_mlo_finalizeIndFn)(void); typedef int (sal_mlo_attachDeviceReqFn)(const device::SenseDeviceSpec &desc); typedef int (sal_mlo_detachDeviceReqFn)(const device::SenseDeviceSpec &desc); +/** + * @brief Hooks provided by Salmanoff to senseApi libraries. + + * This structure contains function pointers that senseApi libraries can use + * to interact with Salmanoff's functionality, such as searching for commonLibs. + */ +struct SalmanoffCallbacks +{ + /** + * @brief Search for a library in Salmanoff's search paths + * @param libraryPath The relative filename of the library to search for + * @return Optional containing the full path if found, nullopt if not found + * + * This function searches for the given library in the same search paths + * that Salmanoff uses when loading senseApi libraries (user-specified + * paths via -p option, current directory, and executable directory). + */ + std::optional (*searchForLibInSmoSearchPaths)( + const std::string& libraryPath); +}; + struct Sal_Mgmt_LibOps { /* When Salmanoff loads a sense API lib, it calls this function to initialize @@ -105,8 +128,12 @@ public: * Smo what implexors can be used with it & what APIs it exports. * The SenseApiDesc struct also gives Smo pointers to API functions * to invoke for communication between Smo and the library. + * + * The SalmanoffCallbacks parameter provides the library with access to + * Salmanoff's hooks. */ -typedef const SenseApiDesc &(SMO_GET_SENSE_API_DESC_FN_TYPEDEF)(void); +typedef const SenseApiDesc &(SMO_GET_SENSE_API_DESC_FN_TYPEDEF)( + const SalmanoffCallbacks& callbacks); } // namespace sense_api } // namespace smo diff --git a/smocore/include/senseApis/senseApiManager.h b/smocore/include/senseApis/senseApiManager.h index 22b29ee..cf17fb4 100644 --- a/smocore/include/senseApis/senseApiManager.h +++ b/smocore/include/senseApis/senseApiManager.h @@ -52,6 +52,10 @@ private: SenseApiManager& operator=(const SenseApiManager&) = delete; std::vector> senseApiLibs; + +public: + static std::optional searchForLibInSmoSearchPaths( + const std::string& libraryPath); }; } // namespace sense_api diff --git a/smocore/senseApis/senseApiManager.cpp b/smocore/senseApis/senseApiManager.cpp index 9b780df..f3f3384 100644 --- a/smocore/senseApis/senseApiManager.cpp +++ b/smocore/senseApis/senseApiManager.cpp @@ -13,6 +13,15 @@ namespace fs = std::filesystem; namespace smo { namespace sense_api { +/* Hooks to be provided to senseApiLibs, enabling them to call into Salmanoff + * code. + */ +static SalmanoffCallbacks salmanoffCallbacks = +{ + .searchForLibInSmoSearchPaths = &SenseApiManager + ::searchForLibInSmoSearchPaths +}; + /** * @brief Searches for a library in predefined locations * @param libraryPath The name or path of the library to find @@ -27,7 +36,7 @@ namespace sense_api { * If the library is not found in any of these locations, returns nullopt and * falls back to system default library search paths (LD_LIBRARY_PATH, etc.) */ -static std::optional findLibraryPath( +static std::optional searchForLibInSmoSearchPaths( const std::string& libraryPath) { std::vector searchPaths = { @@ -66,9 +75,16 @@ static std::optional findLibraryPath( return std::nullopt; } +std::optional SenseApiManager::searchForLibInSmoSearchPaths( + const std::string& libraryPath) +{ + return ::smo::sense_api::searchForLibInSmoSearchPaths(libraryPath); +} + SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath) { - std::optional fullPath = findLibraryPath(libraryPath); + std::optional fullPath = searchForLibInSmoSearchPaths( + libraryPath); std::string resolvedPath = fullPath.value_or(libraryPath); // Clear any existing error @@ -106,7 +122,7 @@ SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath) + libraryPath + "'"); } - const SenseApiDesc &libApiDesc = func(); + const SenseApiDesc &libApiDesc = func(salmanoffCallbacks); auto lib = std::make_unique( libraryPath, dlopen_handle.release(), func); lib->setSenseApiDesc(libApiDesc);