From fe01757156337109dc3d7ac4bd4428a9f872694e Mon Sep 17 00:00:00 2001 From: Alexis Lopez Zubieta Date: Fri, 5 Apr 2019 15:57:31 -0400 Subject: [PATCH 1/4] Allow creating a DLHandle from a list of sonames --- src/libappimage/utils/DLHandle.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/libappimage/utils/DLHandle.h b/src/libappimage/utils/DLHandle.h index 8da21d3b..d4a7b149 100644 --- a/src/libappimage/utils/DLHandle.h +++ b/src/libappimage/utils/DLHandle.h @@ -25,13 +25,37 @@ namespace appimage { * @param libName * @param mode */ - explicit DLHandle(const std::string& libName, int mode) : libName(libName) { + explicit DLHandle(const std::string& libName, int mode) : handle(nullptr), libName(libName) { handle = dlopen(libName.c_str(), mode); if (!handle) throw DLHandleError("Unable to load " + libName); } + /** + * Load one of the libraries listed in with the given flags. For details about + * the allowed flags see the dlopen doc. + * @param libName + * @param mode + */ + explicit DLHandle(std::initializer_list libNames, int mode) : handle(nullptr) { + for (const auto& item: libNames) { + handle = dlopen(item.c_str(), mode); + if (handle) { + libName = item; + break; + } + } + + if (!handle) { + std::string libNamesStr; + for (const auto& item: libNames) + libNamesStr += " " + item; + + throw DLHandleError("Unable to load any of: " + libNamesStr); + } + } + virtual ~DLHandle() { dlclose(handle); }; From d9dbb1f4519a37dd0ce3b8e3c17513757bae8d8c Mon Sep 17 00:00:00 2001 From: Alexis Lopez Zubieta Date: Fri, 5 Apr 2019 15:59:20 -0400 Subject: [PATCH 2/4] Fix libgobject loading in centos In Centos systems libgobject has by soname libgobject-2.0.so.0 which differs from the one in debian systems. This commit will try that soname if the libgobject-2.0.so fails to load. --- src/libappimage/utils/IconHandle.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libappimage/utils/IconHandle.cpp b/src/libappimage/utils/IconHandle.cpp index f83234cf..8818eab3 100644 --- a/src/libappimage/utils/IconHandle.cpp +++ b/src/libappimage/utils/IconHandle.cpp @@ -226,12 +226,13 @@ namespace appimage { /** * @brief Load libgobject-2.0.so and resolve the symbol addresses required by the IconHandle. * + * Note: in Centos libgobject has 'libgobject-2.0.so.0' as soname. * Mode comments: * RTLD_LAZY - load the lib only the required symbols * RTLD_NODELETE - do not unload the lib, as it wasn't designed to be used this way it * will produce a big crash. */ - GLibOjbectHandle() : DLHandle("libgobject-2.0.so", RTLD_LAZY | RTLD_NODELETE) { + GLibOjbectHandle() : DLHandle({"libgobject-2.0.so", "libgobject-2.0.so.0"}, RTLD_LAZY | RTLD_NODELETE) { DLHandle::loadSymbol(object_unref, "g_object_unref"); } }; From 222a09af47c63e22c404d2f8e0b0d17b0d67a559 Mon Sep 17 00:00:00 2001 From: Alexis Lopez Zubieta Date: Mon, 8 Apr 2019 17:48:04 -0400 Subject: [PATCH 3/4] Use explicit nullptr checks --- src/libappimage/utils/DLHandle.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libappimage/utils/DLHandle.h b/src/libappimage/utils/DLHandle.h index d4a7b149..86942ff1 100644 --- a/src/libappimage/utils/DLHandle.h +++ b/src/libappimage/utils/DLHandle.h @@ -28,7 +28,7 @@ namespace appimage { explicit DLHandle(const std::string& libName, int mode) : handle(nullptr), libName(libName) { handle = dlopen(libName.c_str(), mode); - if (!handle) + if (handle == nullptr) throw DLHandleError("Unable to load " + libName); } @@ -41,13 +41,13 @@ namespace appimage { explicit DLHandle(std::initializer_list libNames, int mode) : handle(nullptr) { for (const auto& item: libNames) { handle = dlopen(item.c_str(), mode); - if (handle) { + if (handle != nullptr) { libName = item; break; } } - if (!handle) { + if (handle == nullptr) { std::string libNamesStr; for (const auto& item: libNames) libNamesStr += " " + item; From 89bdc9e8e0909bc1b3e97108932873c31c1b0954 Mon Sep 17 00:00:00 2001 From: Alexis Lopez Zubieta Date: Mon, 8 Apr 2019 17:51:58 -0400 Subject: [PATCH 4/4] anotate known libgobject-2 library names in the different distributions --- src/libappimage/utils/IconHandle.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libappimage/utils/IconHandle.cpp b/src/libappimage/utils/IconHandle.cpp index 8818eab3..329cc850 100644 --- a/src/libappimage/utils/IconHandle.cpp +++ b/src/libappimage/utils/IconHandle.cpp @@ -224,9 +224,12 @@ namespace appimage { g_object_unref_t object_unref = nullptr; /** - * @brief Load libgobject-2.0.so and resolve the symbol addresses required by the IconHandle. + * @brief Load libgobject-2 and resolve the symbol addresses required by the IconHandle. + * + * Known library name by distribution: + * - CentOS: libgobject-2.0.so.0 + * - Debian/Ubuntu: libgobject-2.0.so * - * Note: in Centos libgobject has 'libgobject-2.0.so.0' as soname. * Mode comments: * RTLD_LAZY - load the lib only the required symbols * RTLD_NODELETE - do not unload the lib, as it wasn't designed to be used this way it