Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "provider_registration.hpp" |
| 2 | |
| 3 | #include "command_table.hpp" |
| 4 | #include "main.hpp" |
| 5 | |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 6 | #include <dirent.h> |
| 7 | #include <dlfcn.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <host-ipmid/ipmid-api.h> |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
| 13 | |
| 14 | using namespace phosphor::logging; |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 15 | |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 16 | namespace provider |
| 17 | { |
| 18 | |
| 19 | int handler_select(const struct dirent* entry) |
| 20 | { |
| 21 | // Check for versioned libraries .so.* |
| 22 | if (strstr(entry->d_name, PROVIDER_SONAME_EXTN)) |
| 23 | { |
| 24 | return 1; |
| 25 | } |
| 26 | else |
| 27 | { |
| 28 | return 0; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void registerCallbackHandlers(const char* providerLibPath) |
| 33 | { |
| 34 | if (providerLibPath == NULL) |
| 35 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 36 | log<level::ERR>( |
| 37 | "Path not provided for registering IPMI provider libraries"); |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | |
| 41 | struct dirent** handlerList = nullptr; |
| 42 | std::string handlerPath(providerLibPath); |
| 43 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 44 | auto numLibs = |
| 45 | scandir(providerLibPath, &handlerList, handler_select, alphasort); |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 46 | if (numLibs < 0) |
| 47 | { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // dlopen each IPMI provider shared library |
| 52 | while (numLibs--) |
| 53 | { |
| 54 | handlerPath = providerLibPath; |
| 55 | handlerPath += handlerList[numLibs]->d_name; |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 56 | |
| 57 | auto lib_handler = dlopen(handlerPath.c_str(), RTLD_NOW); |
| 58 | |
| 59 | if (lib_handler == NULL) |
| 60 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 61 | log<level::ERR>("Error opening provider library", |
| 62 | entry("PATH=%s", handlerPath.c_str()), |
| 63 | entry("ERROR=%s", dlerror())); |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 64 | } |
| 65 | free(handlerList[numLibs]); |
| 66 | } |
| 67 | |
| 68 | free(handlerList); |
| 69 | } |
| 70 | |
| 71 | } // namespace provider |
| 72 | |
| 73 | /* |
| 74 | * @brief Method that gets called from IPMI provider shared libraries to get |
| 75 | * the command handlers registered. |
| 76 | * |
| 77 | * When the IPMI provider shared library is loaded, the dynamic loader program |
| 78 | * looks for special section(.ctors on ELF) which contains references to the |
| 79 | * functions marked with the constructor attributes. This function is invoked |
| 80 | * in such manner. |
| 81 | * |
| 82 | * @param[in] netfn - Network Function code |
| 83 | * @param[in] cmd - Command |
| 84 | * @param[in] context - User specific data |
| 85 | * @param[in] handler - The callback routine for the command |
Tom Joseph | 935606c | 2017-01-27 10:53:33 +0530 | [diff] [blame] | 86 | * @param[in] priv - IPMI Command Privilege |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 87 | */ |
| 88 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 89 | ipmi_context_t context, ipmid_callback_t handler, |
| 90 | ipmi_cmd_privilege_t priv) |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 91 | { |
| 92 | uint16_t netFn = netfn << 10; |
| 93 | |
| 94 | // The payload type of IPMI commands provided by the shared libraries |
| 95 | // is IPMI |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 96 | command::CommandID command = { |
| 97 | ((static_cast<uint32_t>(message::PayloadType::IPMI)) << 16) | netFn | |
| 98 | cmd}; |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 99 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 100 | std::get<command::Table&>(singletonPool) |
| 101 | .registerCommand(command, std::make_unique<command::ProviderIpmidEntry>( |
| 102 | command, handler, |
| 103 | static_cast<session::Privilege>(priv))); |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 104 | } |