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