vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <dlfcn.h> |
| 3 | #include <iostream> |
| 4 | #include <unistd.h> |
| 5 | #include <assert.h> |
| 6 | #include <dirent.h> |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 7 | #include <systemd/sd-bus.h> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <map> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 11 | #include <memory> |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 13 | #include <sys/time.h> |
| 14 | #include <errno.h> |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 15 | #include <mapper.h> |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 16 | #include "sensorhandler.h" |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 17 | #include <vector> |
| 18 | #include <algorithm> |
| 19 | #include <iterator> |
Patrick Williams | 4b9efaa | 2016-08-12 21:59:51 -0500 | [diff] [blame] | 20 | #include <ipmiwhitelist.hpp> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 21 | #include <sdbusplus/bus.hpp> |
| 22 | #include <sdbusplus/bus/match.hpp> |
| 23 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 24 | #include "sensorhandler.h" |
| 25 | #include "ipmid.hpp" |
| 26 | #include "settings.hpp" |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 27 | #include <host-cmd-manager.hpp> |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 28 | #include <host-ipmid/ipmid-host-cmd.hpp> |
| 29 | #include <timer.hpp> |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 30 | #include "host-ipmid/oemrouter.hpp" |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 31 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 32 | using namespace phosphor::logging; |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 33 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 34 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 35 | sd_bus *bus = NULL; |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 36 | sd_bus_slot *ipmid_slot = NULL; |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 37 | sd_event *events = nullptr; |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 38 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 39 | // Need this to use new sdbusplus compatible interfaces |
| 40 | sdbusPtr sdbusp; |
| 41 | |
| 42 | // Global Host Bound Command manager |
| 43 | using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>; |
| 44 | cmdManagerPtr cmdManager; |
| 45 | |
Ratan Gupta | 7a7f012 | 2018-03-07 12:31:05 +0530 | [diff] [blame] | 46 | // Global timer for network changes |
| 47 | std::unique_ptr<phosphor::ipmi::Timer> networkTimer = nullptr; |
| 48 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 49 | // Command and handler tuple. Used when clients ask the command to be put |
| 50 | // into host message queue |
| 51 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 52 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 53 | // Initialise restricted mode to true |
| 54 | bool restricted_mode = true; |
| 55 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 56 | FILE *ipmiio, *ipmidbus, *ipmicmddetails; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 57 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 58 | void print_usage(void) { |
| 59 | fprintf(stderr, "Options: [-d mask]\n"); |
| 60 | fprintf(stderr, " mask : 0x01 - Print ipmi packets\n"); |
| 61 | fprintf(stderr, " mask : 0x02 - Print DBUS operations\n"); |
| 62 | fprintf(stderr, " mask : 0x04 - Print ipmi command details\n"); |
| 63 | fprintf(stderr, " mask : 0xFF - Print all trace\n"); |
| 64 | } |
| 65 | |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 66 | const char * DBUS_INTF = "org.openbmc.HostIpmi"; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 67 | |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 68 | const char * FILTER = "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'"; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 69 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 70 | typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t; |
| 71 | typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t; |
| 72 | |
| 73 | // Global data structure that contains the IPMI command handler's registrations. |
| 74 | std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map; |
| 75 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 76 | // IPMI Spec, shared Reservation ID. |
| 77 | unsigned short g_sel_reserve = 0xFFFF; |
| 78 | |
| 79 | unsigned short get_sel_reserve_id(void) |
| 80 | { |
| 81 | return g_sel_reserve; |
| 82 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 83 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 84 | namespace internal |
| 85 | { |
| 86 | |
| 87 | constexpr auto restrictionModeIntf = |
| 88 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 89 | |
| 90 | namespace cache |
| 91 | { |
| 92 | |
| 93 | std::unique_ptr<settings::Objects> objects = nullptr; |
| 94 | |
| 95 | } // namespace cache |
| 96 | } // namespace internal |
| 97 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 98 | #ifndef HEXDUMP_COLS |
| 99 | #define HEXDUMP_COLS 16 |
| 100 | #endif |
| 101 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 102 | void hexdump(FILE *s, void *mem, size_t len) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 103 | { |
| 104 | unsigned int i, j; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 105 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 106 | for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) |
| 107 | { |
| 108 | /* print offset */ |
| 109 | if(i % HEXDUMP_COLS == 0) |
| 110 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 111 | fprintf(s,"0x%06x: ", i); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 112 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 113 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 114 | /* print hex data */ |
| 115 | if(i < len) |
| 116 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 117 | fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 118 | } |
| 119 | else /* end of block, just aligning for ASCII dump */ |
| 120 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 121 | fprintf(s," "); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 122 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 123 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 124 | /* print ASCII dump */ |
| 125 | if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) |
| 126 | { |
| 127 | for(j = i - (HEXDUMP_COLS - 1); j <= i; j++) |
| 128 | { |
| 129 | if(j >= len) /* end of block, not really printing */ |
| 130 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 131 | fputc(' ', s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 132 | } |
| 133 | else if(isprint(((char*)mem)[j])) /* printable char */ |
| 134 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 135 | fputc(0xFF & ((char*)mem)[j], s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 136 | } |
| 137 | else /* other char */ |
| 138 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 139 | fputc('.',s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 142 | fputc('\n',s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 148 | // Method that gets called by shared libraries to get their command handlers registered |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 149 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_context_t context, |
| 150 | ipmid_callback_t handler, ipmi_cmd_privilege_t priv) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 151 | { |
| 152 | // Pack NetFn and Command in one. |
| 153 | auto netfn_and_cmd = std::make_pair(netfn, cmd); |
| 154 | |
| 155 | // Pack Function handler and Data in another. |
| 156 | auto handler_and_context = std::make_pair(handler, context); |
| 157 | |
| 158 | // Check if the registration has already been made.. |
| 159 | auto iter = g_ipmid_router_map.find(netfn_and_cmd); |
| 160 | if(iter != g_ipmid_router_map.end()) |
| 161 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 162 | log<level::ERR>("Duplicate registration", |
| 163 | entry("NETFN=0x%X", netfn), |
| 164 | entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 165 | } |
| 166 | else |
| 167 | { |
| 168 | // This is a fresh registration.. Add it to the map. |
| 169 | g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context); |
| 170 | } |
| 171 | |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | // Looks at the map and calls corresponding handler functions. |
| 176 | ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, |
| 177 | ipmi_response_t response, ipmi_data_len_t data_len) |
| 178 | { |
| 179 | // return from the Command handlers. |
| 180 | ipmi_ret_t rc = IPMI_CC_INVALID; |
| 181 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 182 | // If restricted mode is true and command is not whitelisted, don't |
| 183 | // execute the command |
| 184 | if(restricted_mode) |
| 185 | { |
| 186 | if (!std::binary_search(whitelist.cbegin(), whitelist.cend(), |
| 187 | std::make_pair(netfn, cmd))) |
| 188 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 189 | log<level::ERR>("Net function not whitelisted", |
| 190 | entry("NETFN=0x%X", netfn), |
| 191 | entry("CMD=0x%X", cmd)); |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 192 | rc = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
| 193 | memcpy(response, &rc, IPMI_CC_LEN); |
| 194 | *data_len = IPMI_CC_LEN; |
| 195 | return rc; |
| 196 | } |
| 197 | } |
| 198 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 199 | // Walk the map that has the registered handlers and invoke the approprite |
| 200 | // handlers for matching commands. |
| 201 | auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd)); |
| 202 | if(iter == g_ipmid_router_map.end()) |
| 203 | { |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 204 | /* By default should only print on failure to find wildcard command. */ |
| 205 | #ifdef __IPMI_DEBUG__ |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 206 | log<level::ERR>( |
| 207 | "No registered handlers for NetFn, trying Wilcard implementation", |
| 208 | entry("NET_FUN=0x%X", netfn) |
| 209 | entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 210 | #endif |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 211 | |
| 212 | // Now that we did not find any specific [NetFn,Cmd], tuple, check for |
| 213 | // NetFn, WildCard command present. |
| 214 | iter = g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD)); |
| 215 | if(iter == g_ipmid_router_map.end()) |
| 216 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 217 | log<level::ERR>("No Registered handlers for NetFn", |
| 218 | entry("NET_FUN=0x%X", netfn), |
| 219 | entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 220 | |
| 221 | // Respond with a 0xC1 |
| 222 | memcpy(response, &rc, IPMI_CC_LEN); |
| 223 | *data_len = IPMI_CC_LEN; |
| 224 | return rc; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #ifdef __IPMI_DEBUG__ |
| 229 | // We have either a perfect match -OR- a wild card atleast, |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 230 | log<level::ERR>("Calling Net function", |
| 231 | entry("NET_FUN=0x%X", netfn) |
| 232 | entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 233 | #endif |
| 234 | |
| 235 | // Extract the map data onto appropriate containers |
| 236 | auto handler_and_context = iter->second; |
| 237 | |
| 238 | // Creating a pointer type casted to char* to make sure we advance 1 byte |
| 239 | // when we advance pointer to next's address. advancing void * would not |
| 240 | // make sense. |
| 241 | char *respo = &((char *)response)[IPMI_CC_LEN]; |
| 242 | |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 243 | try |
| 244 | { |
| 245 | // Response message from the plugin goes into a byte post the base |
| 246 | // response |
| 247 | rc = (handler_and_context.first) (netfn, cmd, request, respo, |
| 248 | data_len, handler_and_context.second); |
| 249 | } |
| 250 | // IPMI command handlers can throw unhandled exceptions, catch those |
| 251 | // and return sane error code. |
| 252 | catch (const std::exception &e) |
| 253 | { |
| 254 | log<level::ERR>(e.what(), entry("NET_FUN=0x%X", netfn), |
| 255 | entry("CMD=0x%X", cmd)); |
| 256 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 257 | *data_len = 0; |
| 258 | // fall through |
| 259 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 260 | // Now copy the return code that we got from handler and pack it in first |
| 261 | // byte. |
| 262 | memcpy(response, &rc, IPMI_CC_LEN); |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 263 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 264 | // Data length is now actual data + completion code. |
| 265 | *data_len = *data_len + IPMI_CC_LEN; |
| 266 | |
| 267 | return rc; |
| 268 | } |
| 269 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 270 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 271 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 272 | |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 273 | static int send_ipmi_message(sd_bus_message *req, unsigned char seq, unsigned char netfn, unsigned char lun, unsigned char cmd, unsigned char cc, unsigned char *buf, unsigned char len) { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 274 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 275 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 276 | sd_bus_message *reply = NULL, *m=NULL; |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 277 | const char *dest, *path; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 278 | int r, pty; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 279 | |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 280 | dest = sd_bus_message_get_sender(req); |
| 281 | path = sd_bus_message_get_path(req); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 282 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 283 | r = sd_bus_message_new_method_call(bus,&m,dest,path, |
| 284 | DBUS_INTF, |
| 285 | "sendMessage"); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 286 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 287 | log<level::ERR>("Failed to add the method object", |
| 288 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 289 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 290 | } |
| 291 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 292 | |
Chris Austen | abfb5e8 | 2015-10-13 12:29:24 -0500 | [diff] [blame] | 293 | // Responses in IPMI require a bit set. So there ya go... |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 294 | netfn |= 0x01; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 295 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 296 | |
| 297 | // Add the bytes needed for the methods to be called |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 298 | r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 299 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 300 | log<level::ERR>("Failed add the netfn and others", |
| 301 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 302 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 303 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 304 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 305 | r = sd_bus_message_append_array(m, 'y', buf, len); |
| 306 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 307 | log<level::ERR>("Failed to add the string of response bytes", |
| 308 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 309 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 315 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 316 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 317 | log<level::ERR>("Failed to call the method", |
| 318 | entry("DEST=%s", dest), |
| 319 | entry("PATH=%s", path), |
| 320 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 321 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | r = sd_bus_message_read(reply, "x", &pty); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 325 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 326 | log<level::ERR>("Failed to get a reply from the method", |
| 327 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 330 | final: |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 331 | sd_bus_error_free(&error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 332 | m = sd_bus_message_unref(m); |
| 333 | reply = sd_bus_message_unref(reply); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 334 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 335 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 338 | void cache_restricted_mode() |
| 339 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 340 | restricted_mode = false; |
| 341 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
| 342 | using namespace internal; |
| 343 | using namespace internal::cache; |
| 344 | sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection()); |
| 345 | const auto& restrictionModeSetting = |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 346 | objects->map.at(restrictionModeIntf).front(); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 347 | auto method = dbus.new_method_call( |
| 348 | objects->service(restrictionModeSetting, |
| 349 | restrictionModeIntf).c_str(), |
| 350 | restrictionModeSetting.c_str(), |
| 351 | "org.freedesktop.DBus.Properties", |
| 352 | "Get"); |
| 353 | method.append(restrictionModeIntf, "RestrictionMode"); |
| 354 | auto resp = dbus.call(method); |
| 355 | if (resp.is_method_error()) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 356 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 357 | log<level::ERR>("Error in RestrictionMode Get"); |
| 358 | // Fail-safe to true. |
| 359 | restricted_mode = true; |
| 360 | return; |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 361 | } |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 362 | sdbusplus::message::variant<std::string> result; |
| 363 | resp.read(result); |
| 364 | auto restrictionMode = |
| 365 | RestrictionMode::convertModesFromString(result.get<std::string>()); |
| 366 | if(RestrictionMode::Modes::Whitelist == restrictionMode) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 367 | { |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 368 | restricted_mode = true; |
| 369 | } |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static int handle_restricted_mode_change(sd_bus_message *m, void *user_data, |
| 373 | sd_bus_error *ret_error) |
| 374 | { |
| 375 | cache_restricted_mode(); |
| 376 | return 0; |
| 377 | } |
| 378 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 379 | static int handle_ipmi_command(sd_bus_message *m, void *user_data, sd_bus_error |
| 380 | *ret_error) { |
| 381 | int r = 0; |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 382 | unsigned char sequence, netfn, lun, cmd; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 383 | const void *request; |
| 384 | size_t sz; |
| 385 | size_t resplen =MAX_IPMI_BUFFER; |
| 386 | unsigned char response[MAX_IPMI_BUFFER]; |
| 387 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 388 | memset(response, 0, MAX_IPMI_BUFFER); |
| 389 | |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 390 | r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 391 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 392 | log<level::ERR>("Failed to parse signal message", |
| 393 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | r = sd_bus_message_read_array(m, 'y', &request, &sz ); |
| 398 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 399 | log<level::ERR>("Failed to parse signal message", |
| 400 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 401 | return -1; |
| 402 | } |
| 403 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 404 | fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd); |
| 405 | hexdump(ipmiio, (void*)request, sz); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 406 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 407 | // Allow the length field to be used for both input and output of the |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 408 | // ipmi call |
| 409 | resplen = sz; |
| 410 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 411 | // Now that we have parsed the entire byte array from the caller |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 412 | // we can call the ipmi router to do the work... |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 413 | r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen); |
| 414 | if(r != 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 415 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 416 | #ifdef __IPMI_DEBUG__ |
| 417 | log<level::ERR>("ERROR in handling NetFn", |
| 418 | entry("ERRNO=0x%X", -r), |
| 419 | entry("NET_FUN=0x%X", netfn), |
| 420 | entry("CMD=0x%X", cmd)); |
| 421 | #endif |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 422 | resplen = 0; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | resplen = resplen - 1; // first byte is for return code. |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 429 | fprintf(ipmiio, "IPMI Response:\n"); |
| 430 | hexdump(ipmiio, (void*)response, resplen); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 431 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 432 | // Send the response buffer from the ipmi command |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 433 | r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0], |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 434 | ((unsigned char *)response) + 1, resplen); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 435 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 436 | log<level::ERR>("Failed to send the response message"); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 437 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 438 | } |
| 439 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 440 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 441 | return 0; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 442 | } |
| 443 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 444 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 445 | //---------------------------------------------------------------------- |
| 446 | // handler_select |
| 447 | // Select all the files ending with with .so. in the given diretcory |
| 448 | // @d: dirent structure containing the file name |
| 449 | //---------------------------------------------------------------------- |
| 450 | int handler_select(const struct dirent *entry) |
| 451 | { |
| 452 | // To hold ".so" from entry->d_name; |
| 453 | char dname_copy[4] = {0}; |
| 454 | |
| 455 | // We want to avoid checking for everything and isolate to the ones having |
Adriana Kobylak | 87e080b | 2016-07-10 13:16:53 -0500 | [diff] [blame] | 456 | // .so.* or .so in them. |
| 457 | // Check for versioned libraries .so.* |
| 458 | if(strstr(entry->d_name, IPMI_PLUGIN_SONAME_EXTN)) |
| 459 | { |
| 460 | return 1; |
| 461 | } |
| 462 | // Check for non versioned libraries .so |
| 463 | else if(strstr(entry->d_name, IPMI_PLUGIN_EXTN)) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 464 | { |
| 465 | // It is possible that .so could be anywhere in the string but unlikely |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 466 | // But being careful here. Get the base address of the string, move |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 467 | // until end and come back 3 steps and that gets what we need. |
| 468 | strcpy(dname_copy, (entry->d_name + strlen(entry->d_name)-strlen(IPMI_PLUGIN_EXTN))); |
| 469 | if(strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0) |
| 470 | { |
| 471 | return 1; |
| 472 | } |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | // This will do a dlopen of every .so in ipmi_lib_path and will dlopen everything so that they will |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 478 | // register a callback handler |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 479 | void ipmi_register_callback_handlers(const char* ipmi_lib_path) |
| 480 | { |
| 481 | // For walking the ipmi_lib_path |
| 482 | struct dirent **handler_list; |
| 483 | int num_handlers = 0; |
| 484 | |
| 485 | // This is used to check and abort if someone tries to register a bad one. |
| 486 | void *lib_handler = NULL; |
| 487 | |
| 488 | if(ipmi_lib_path == NULL) |
| 489 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 490 | log<level::ERR>("No handlers to be registered for ipmi.. Aborting"); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 491 | assert(0); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid" |
| 496 | // 2: Scan the directory for the files that end with .so |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 497 | // 3: For each one of them, just do a 'dlopen' so that they register |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 498 | // the handlers for callback routines. |
| 499 | |
| 500 | std::string handler_fqdn = ipmi_lib_path; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 501 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 502 | // Append a "/" since we need to add the name of the .so. If there is |
| 503 | // already a .so, adding one more is not any harm. |
| 504 | handler_fqdn += "/"; |
| 505 | |
| 506 | num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 507 | if (num_handlers < 0) |
| 508 | return; |
Jeremy Kerr | 5e8f85e | 2015-10-27 13:43:54 +0800 | [diff] [blame] | 509 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 510 | while(num_handlers--) |
| 511 | { |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 512 | handler_fqdn = ipmi_lib_path; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 513 | handler_fqdn += handler_list[num_handlers]->d_name; |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 514 | #ifdef __IPMI_DEBUG__ |
| 515 | log<level::DEBUG>("Registering handler", |
| 516 | entry("HANDLER=%s", handler_fqdn.c_str())); |
| 517 | #endif |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 518 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 519 | lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 520 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 521 | if(lib_handler == NULL) |
| 522 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 523 | log<level::ERR>("ERROR opening", |
| 524 | entry("HANDLER=%s", handler_fqdn.c_str()), |
| 525 | entry("ERROR=%s", dlerror())); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 526 | } |
| 527 | // Wipe the memory allocated for this particular entry. |
| 528 | free(handler_list[num_handlers]); |
| 529 | } |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 530 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 531 | // Done with all registration. |
| 532 | free(handler_list); |
| 533 | } |
| 534 | |
| 535 | // TODO : What to be done on the memory that is given by dlopen ?. |
| 536 | return; |
| 537 | } |
| 538 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 539 | sd_bus *ipmid_get_sd_bus_connection(void) { |
| 540 | return bus; |
| 541 | } |
| 542 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 543 | sd_event *ipmid_get_sd_event_connection(void) { |
| 544 | return events; |
| 545 | } |
| 546 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 547 | sd_bus_slot *ipmid_get_sd_bus_slot(void) { |
| 548 | return ipmid_slot; |
| 549 | } |
| 550 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 551 | // Calls host command manager to do the right thing for the command |
| 552 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) { |
| 553 | return cmdManager->execute(std::move(cmd)); |
| 554 | } |
| 555 | |
| 556 | cmdManagerPtr& ipmid_get_host_cmd_manager() { |
| 557 | return cmdManager; |
| 558 | } |
| 559 | |
| 560 | sdbusPtr& ipmid_get_sdbus_plus_handler() { |
| 561 | return sdbusp; |
| 562 | } |
| 563 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 564 | int main(int argc, char *argv[]) |
| 565 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 566 | int r; |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 567 | unsigned long tvalue; |
| 568 | int c; |
| 569 | |
| 570 | |
| 571 | |
| 572 | // This file and subsequient switch is for turning on levels |
| 573 | // of trace |
| 574 | ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w"); |
| 575 | |
| 576 | while ((c = getopt (argc, argv, "h:d:")) != -1) |
| 577 | switch (c) { |
| 578 | case 'd': |
| 579 | tvalue = strtoul(optarg, NULL, 16); |
| 580 | if (1&tvalue) { |
| 581 | ipmiio = stdout; |
| 582 | } |
| 583 | if (2&tvalue) { |
| 584 | ipmidbus = stdout; |
| 585 | } |
| 586 | if (4&tvalue) { |
| 587 | ipmicmddetails = stdout; |
| 588 | } |
| 589 | break; |
| 590 | case 'h': |
| 591 | case '?': |
| 592 | print_usage(); |
| 593 | return 1; |
| 594 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 595 | |
| 596 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 597 | /* Connect to system bus */ |
| 598 | r = sd_bus_open_system(&bus); |
| 599 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 600 | log<level::ERR>("Failed to connect to system bus", |
| 601 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 602 | goto finish; |
| 603 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 604 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 605 | /* Get an sd event handler */ |
| 606 | r = sd_event_default(&events); |
| 607 | if (r < 0) |
| 608 | { |
| 609 | log<level::ERR>("Failure to create sd_event handler", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 610 | entry("ERRNO=0x%X", -r)); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 611 | goto finish; |
| 612 | } |
| 613 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 614 | // Now create the Host Bound Command manager. Need sdbusplus |
| 615 | // to use the generated bindings |
| 616 | sdbusp = std::make_unique<sdbusplus::bus::bus>(bus); |
| 617 | cmdManager = std::make_unique<phosphor::host::command::Manager>( |
| 618 | *sdbusp, events); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 619 | |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 620 | // Activate OemRouter. |
| 621 | oem::mutableRouter()->activate(); |
| 622 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 623 | // Register all the handlers that provider implementation to IPMI commands. |
| 624 | ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH); |
| 625 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 626 | // Watch for BT messages |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 627 | r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 628 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 629 | log<level::ERR>("Failed: sd_bus_add_match", |
| 630 | entry("FILTER=%s", FILTER), |
| 631 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 632 | goto finish; |
| 633 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 634 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 635 | // Attach the bus to sd_event to service user requests |
| 636 | sd_bus_attach_event(bus, events, SD_EVENT_PRIORITY_NORMAL); |
| 637 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 638 | { |
| 639 | using namespace internal; |
| 640 | using namespace internal::cache; |
| 641 | sdbusplus::bus::bus dbus{bus}; |
| 642 | objects = std::make_unique<settings::Objects>( |
| 643 | dbus, |
| 644 | std::vector<settings::Interface>({restrictionModeIntf})); |
| 645 | // Initialize restricted mode |
| 646 | cache_restricted_mode(); |
| 647 | // Wait for changes on Restricted mode |
| 648 | sdbusplus::bus::match_t restrictedModeMatch( |
| 649 | dbus, |
| 650 | sdbusRule::propertiesChanged( |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 651 | objects->map.at(restrictionModeIntf).front(), |
| 652 | restrictionModeIntf), |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 653 | handle_restricted_mode_change); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 654 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 655 | for (;;) { |
| 656 | /* Process requests */ |
| 657 | r = sd_event_run(events, (uint64_t)-1); |
| 658 | if (r < 0) |
| 659 | { |
| 660 | log<level::ERR>("Failure in processing request", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 661 | entry("ERRNO=0x%X", -r)); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 662 | goto finish; |
| 663 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | finish: |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 668 | sd_event_unref(events); |
| 669 | sd_bus_detach_event(bus); |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 670 | sd_bus_slot_unref(ipmid_slot); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 671 | sd_bus_unref(bus); |
| 672 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 673 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 674 | } |