szaszm commented on a change in pull request #1138: URL: https://github.com/apache/nifi-minifi-cpp/pull/1138#discussion_r698370235
########## File path: libminifi/src/core/extension/DynamicLibrary.cpp ########## @@ -0,0 +1,201 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <memory> +#ifndef WIN32 +#include <dlfcn.h> +#define DLL_EXPORT +#else +#define WIN32_LEAN_AND_MEAN 1 +#include <Windows.h> // Windows specific libraries for collecting software metrics. +#include <Psapi.h> +#pragma comment(lib, "psapi.lib" ) +#define DLL_EXPORT __declspec(dllexport) +#define RTLD_LAZY 0 +#define RTLD_NOW 0 + +#define RTLD_GLOBAL (1 << 1) +#define RTLD_LOCAL (1 << 2) +#endif + +#include "core/extension/DynamicLibrary.h" +#include "core/extension/Extension.h" +#include "utils/GeneralUtils.h" +#include "core/logging/LoggerConfiguration.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace core { +namespace extension { + +std::shared_ptr<logging::Logger> DynamicLibrary::logger_ = logging::LoggerFactory<DynamicLibrary>::getLogger(); + +DynamicLibrary::DynamicLibrary(std::string name, std::filesystem::path library_path) + : Module(std::move(name)), + library_path_(std::move(library_path)) { +} + +bool DynamicLibrary::load() { + dlerror(); + handle_ = dlopen(library_path_.c_str(), RTLD_NOW | RTLD_LOCAL); + if (!handle_) { + logger_->log_error("Failed to load extension '%s' at '%s': %s", name_, library_path_, dlerror()); + return false; + } else { + logger_->log_trace("Loaded extension '%s' at '%s'", name_, library_path_); + return true; + } +} + +bool DynamicLibrary::unload() { + logger_->log_trace("Unloading library '%s' at '%s'", name_, library_path_); + if (!handle_) { + logger_->log_error("Extension does not have a handle_ '%s' at '%s'", name_, library_path_); + return true; + } + dlerror(); + if (dlclose(handle_)) { + logger_->log_error("Failed to unload extension '%s' at '%': %s", name_, library_path_, dlerror()); + return false; + } + logger_->log_trace("Unloaded extension '%s' at '%s'", name_, library_path_); + handle_ = nullptr; + return true; +} + +DynamicLibrary::~DynamicLibrary() = default; + +#ifdef WIN32 + +void DynamicLibrary::store_error() { + auto error = GetLastError(); + + if (error == 0) { + error_str_ = ""; + return; + } + + LPSTR messageBuffer = nullptr; + size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); + + current_error_ = std::string(messageBuffer, size); + + // Free the buffer. + LocalFree(messageBuffer); +} Review comment: - FORMAT_MESSAGE_ALLOCATE_BUFFER: For allocation, this is handled internally with system_error - FORMAT_MESSAGE_FROM_SYSTEM: Looks in the system table, makes FormatMessage usable with GetLastError(). Same as system_error - FORMAT_MESSAGE_IGNORE_INSERTS: Also passed by system_error: https://github.com/microsoft/STL/blob/31bed7ae0d2ff22029e7ed3e8c58d6ee429974fc/stl/src/syserror.cpp#L119 - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT): system_error uses the default behavior instead (passing 0), which is very similar to this and it's fine IMO. https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org