1 #define _GNU_SOURCE 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <errno.h> 5 #include <stddef.h> 6 #include <systemd/sd-bus.h> 7 #include "message.H" 8 #include "event_messaged_sdbus.h" 9 #include <syslog.h> 10 11 /*****************************************************************************/ 12 /* This set of functions are responsible for interactions with events over */ 13 /* dbus. Logs come in a couple of different ways... */ 14 /* 1) From the calls from acceptHostMessage, acceptTestMessage */ 15 /* 2) At startup and logs that exist alreafy are re-added */ 16 /* */ 17 /* event_record_t when loaded contain all strings and data stream for a log */ 18 /* */ 19 /* Functions naming convention */ 20 /* prop_x : callable dbus properties. */ 21 /* method_x : callable dbus functions. */ 22 /* */ 23 /*****************************************************************************/ 24 const char *event_path = "/org/openbmc/records/events"; 25 26 sd_bus *bus = NULL; 27 sd_bus_slot *slot = NULL; 28 29 event_record_t *gCachedRec = NULL; 30 31 typedef struct messageEntry_t { 32 33 size_t logid; 34 sd_bus_slot *messageslot; 35 sd_bus_slot *deleteslot; 36 sd_bus_slot *associationslot; 37 event_manager *em; 38 39 } messageEntry_t; 40 41 static int remove_log_from_dbus(messageEntry_t *node); 42 43 static void message_entry_close(messageEntry_t *m) 44 { 45 free(m); 46 return; 47 } 48 49 static void message_entry_new(messageEntry_t **m, uint16_t logid, event_manager *em) 50 { 51 *m = malloc(sizeof(messageEntry_t)); 52 (*m)->logid = logid; 53 (*m)->em = em; 54 return; 55 } 56 57 // After calling this function the gCachedRec will be set 58 static event_record_t* message_record_open(event_manager *em, uint16_t logid) 59 { 60 61 int r = 0; 62 event_record_t *rec; 63 64 // A simple caching technique because each 65 // property needs to extract data from the 66 // same data blob. 67 if (gCachedRec == NULL) { 68 if (message_load_log(em, logid, &rec)) { 69 gCachedRec = rec; 70 return gCachedRec; 71 } else 72 return NULL; 73 } 74 75 if (logid == gCachedRec->logid) { 76 r = 1; 77 78 } else { 79 message_free_log(em, gCachedRec); 80 gCachedRec = NULL; 81 82 r = message_load_log(em, logid, &rec); 83 if (r) 84 gCachedRec = rec; 85 } 86 87 return (r ? gCachedRec : NULL); 88 } 89 90 static int prop_message_assoc(sd_bus *bus, 91 const char *path, 92 const char *interface, 93 const char *property, 94 sd_bus_message *reply, 95 void *userdata, 96 sd_bus_error *error) 97 { 98 int r=0; 99 messageEntry_t *m = (messageEntry_t*) userdata; 100 event_record_t *rec; 101 char *p; 102 char *token, *saveptr; 103 104 rec = message_record_open(m->em, m->logid); 105 if (!rec) { 106 fprintf(stderr,"Warning missing event log for %lx\n", m->logid); 107 sd_bus_error_set(error, 108 SD_BUS_ERROR_FILE_NOT_FOUND, 109 "Could not find log file"); 110 return -1; 111 } 112 113 /* strtok manipulates a string. It turns out that message_record_open */ 114 /* implements a caching mechcanism which means the oiginal string is */ 115 /* To avoid that, I will make a copy and mess with that */ 116 p = strdup(rec->association); 117 118 if (!p) { 119 /* no association string == no associations */ 120 sd_bus_error_set(error, 121 SD_BUS_ERROR_NO_MEMORY, 122 "Not enough memory for association"); 123 return -1; 124 } 125 126 token = strtok(p, " "); 127 128 if (token) { 129 130 r = sd_bus_message_open_container(reply, 'a', "(sss)"); 131 if (r < 0) { 132 fprintf(stderr,"Error opening container %s to reply %s\n", token, strerror(-r)); 133 } 134 135 while(token) { 136 r = sd_bus_message_append(reply, "(sss)", "fru", "event", token); 137 if (r < 0) { 138 fprintf(stderr,"Error adding properties for %s to reply %s\n", token, strerror(-r)); 139 } 140 141 token = strtok(NULL, " "); 142 } 143 144 r = sd_bus_message_close_container(reply); 145 } 146 147 free(p); 148 149 return r; 150 } 151 152 153 static int prop_message(sd_bus *bus, 154 const char *path, 155 const char *interface, 156 const char *property, 157 sd_bus_message *reply, 158 void *userdata, 159 sd_bus_error *error) 160 { 161 int r=0; 162 messageEntry_t *m = (messageEntry_t*) userdata; 163 char *p; 164 struct tm *tm_info; 165 char buffer[36]; 166 event_record_t *rec; 167 168 rec = message_record_open(m->em, m->logid); 169 if (!rec) { 170 fprintf(stderr,"Warning missing event log for %lx\n", m->logid); 171 sd_bus_error_set(error, 172 SD_BUS_ERROR_FILE_NOT_FOUND, 173 "Could not find log file"); 174 return -1; 175 } 176 177 if (!strncmp("message", property, 7)) { 178 p = rec->message; 179 } else if (!strncmp("severity", property, 8)) { 180 p = rec->severity; 181 } else if (!strncmp("reported_by", property, 11)) { 182 p = rec->reportedby; 183 } else if (!strncmp("time", property, 4)) { 184 tm_info = localtime(&rec->timestamp); 185 strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info); 186 p = buffer; 187 } else { 188 p = ""; 189 } 190 191 r = sd_bus_message_append(reply, "s", p); 192 if (r < 0) { 193 fprintf(stderr,"Error adding property to reply %s\n", strerror(-r)); 194 } 195 196 return r; 197 } 198 199 200 static int prop_message_dd(sd_bus *bus, 201 const char *path, 202 const char *interface, 203 const char *property, 204 sd_bus_message *reply, 205 void *userdata, 206 sd_bus_error *error) 207 { 208 209 event_record_t *rec; 210 messageEntry_t *m = (messageEntry_t*) userdata; 211 212 213 rec = message_record_open(m->em, m->logid); 214 215 if (!rec) { 216 sd_bus_error_set(error, 217 SD_BUS_ERROR_FILE_NOT_FOUND, 218 "Could not find log file"); 219 220 return -1; 221 } 222 return sd_bus_message_append_array(reply, 'y', rec->p, rec->n); 223 } 224 225 ///////////////////////////////////////////////////////////// 226 // Receives an array of bytes as an esel error log 227 // returns the messageid in 2 byte format 228 // 229 // S1 - Message - Simple sentence about the fail 230 // S2 - Severity - How bad of a problem is this 231 // S3 - Association - sensor path 232 // ay - Detailed data - developer debug information 233 // 234 ///////////////////////////////////////////////////////////// 235 static int method_accept_host_message(sd_bus_message *m, 236 void *userdata, 237 sd_bus_error *ret_error) 238 { 239 char *message, *severity, *association; 240 size_t n = 4; 241 uint8_t *p; 242 int r; 243 uint16_t logid; 244 event_record_t rec; 245 event_manager *em = (event_manager *) userdata; 246 247 r = sd_bus_message_read(m, "sss", &message, &severity, &association); 248 if (r < 0) { 249 fprintf(stderr, "Error parsing strings: %s\n", strerror(-r)); 250 return r; 251 } 252 253 r = sd_bus_message_read_array(m, 'y', (const void **)&p, &n); 254 if (r < 0) { 255 fprintf(stderr, "Error parsing debug data: %s\n", strerror(-r)); 256 return r; 257 } 258 259 rec.message = (char*) message; 260 rec.severity = (char*) severity; 261 rec.association = (char*) association; 262 rec.reportedby = (char*) "Host"; 263 rec.p = (uint8_t*) p; 264 rec.n = n; 265 266 syslog(LOG_NOTICE, "%s %s (%s)", rec.severity, rec.message, rec.association); 267 268 logid = message_create_new_log_event(em, &rec); 269 270 if (logid) 271 r = send_log_to_dbus(em, logid, rec.association); 272 273 return sd_bus_reply_method_return(m, "q", logid); 274 } 275 276 277 static int method_accept_test_message(sd_bus_message *m, 278 void *userdata, 279 sd_bus_error *ret_error) 280 { 281 // Random debug data including, ascii, null, >signed int, max 282 uint8_t p[] = {0x30, 0x00, 0x13, 0x7F, 0x88, 0xFF}; 283 char *s; 284 uint16_t logid; 285 event_record_t rec; 286 event_manager *em = (event_manager *) userdata; 287 288 rec.message = (char*) "A Test event log just happened"; 289 rec.severity = (char*) "Info"; 290 rec.association = (char*) "/org/openbmc/inventory/system/chassis/motherboard/dimm3 " \ 291 "/org/openbmc/inventory/system/chassis/motherboard/dimm2"; 292 rec.reportedby = (char*) "Test"; 293 rec.p = (uint8_t*) p; 294 rec.n = 6; 295 296 297 syslog(LOG_NOTICE, "%s %s (%s)", rec.severity, rec.message, rec.association); 298 logid = message_create_new_log_event(em, &rec); 299 300 if (logid) 301 send_log_to_dbus(em, logid, rec.association); 302 303 return sd_bus_reply_method_return(m, "q", logid); 304 } 305 306 static int finish_delete_log(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) 307 { 308 return 0; 309 } 310 311 static int method_clearall(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) 312 { 313 event_manager *em = (event_manager *) userdata; 314 uint16_t logid; 315 char buffer[32]; 316 int r; 317 sd_bus_message *reply; 318 319 message_refresh_events(em); 320 321 while (logid = message_next_event(em)) { 322 snprintf(buffer, sizeof(buffer), 323 "%s/%d", event_path, logid); 324 325 r = sd_bus_call_method_async(bus, 326 NULL, 327 "org.openbmc.records.events", 328 buffer, 329 "org.openbmc.Object.Delete", 330 "delete", 331 finish_delete_log, 332 NULL, 333 NULL); 334 if (r < 0) { 335 fprintf(stderr, 336 "sd_bus_call_method_async Failed : %s\n", 337 strerror(-r)); 338 return -1; 339 } 340 } 341 342 return sd_bus_reply_method_return(m, "q", 0); 343 } 344 345 346 static int method_deletelog(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) 347 { 348 messageEntry_t *p = (messageEntry_t *) userdata; 349 350 message_delete_log(p->em, p->logid); 351 remove_log_from_dbus(p); 352 return sd_bus_reply_method_return(m, "q", 0); 353 } 354 355 356 357 static const sd_bus_vtable recordlog_vtable[] = { 358 SD_BUS_VTABLE_START(0), 359 SD_BUS_METHOD("acceptHostMessage", "sssay", "q", method_accept_host_message, SD_BUS_VTABLE_UNPRIVILEGED), 360 SD_BUS_METHOD("acceptTestMessage", NULL, "q", method_accept_test_message, SD_BUS_VTABLE_UNPRIVILEGED), 361 SD_BUS_METHOD("clear", NULL, "q", method_clearall, SD_BUS_VTABLE_UNPRIVILEGED), 362 SD_BUS_VTABLE_END 363 }; 364 365 static const sd_bus_vtable log_vtable[] = { 366 SD_BUS_VTABLE_START(0), 367 SD_BUS_PROPERTY("message", "s", prop_message, 0, SD_BUS_VTABLE_PROPERTY_CONST), 368 SD_BUS_PROPERTY("severity", "s", prop_message, 0, SD_BUS_VTABLE_PROPERTY_CONST), 369 SD_BUS_PROPERTY("reported_by", "s", prop_message, 0, SD_BUS_VTABLE_PROPERTY_CONST), 370 SD_BUS_PROPERTY("time", "s", prop_message, 0, SD_BUS_VTABLE_PROPERTY_CONST), 371 SD_BUS_PROPERTY("debug_data", "ay", prop_message_dd ,0, SD_BUS_VTABLE_PROPERTY_CONST), 372 SD_BUS_VTABLE_END 373 }; 374 375 376 static const sd_bus_vtable recordlog_delete_vtable[] = { 377 SD_BUS_VTABLE_START(0), 378 SD_BUS_METHOD("delete", NULL, "q", method_deletelog, SD_BUS_VTABLE_UNPRIVILEGED), 379 SD_BUS_VTABLE_END 380 }; 381 382 static const sd_bus_vtable recordlog_association_vtable[] = { 383 SD_BUS_VTABLE_START(0), 384 SD_BUS_PROPERTY("associations", "a(sss)", prop_message_assoc, 0, SD_BUS_VTABLE_PROPERTY_CONST), 385 SD_BUS_VTABLE_END 386 }; 387 388 static int remove_log_from_dbus(messageEntry_t *p) 389 { 390 int r; 391 char buffer[32]; 392 393 snprintf(buffer, sizeof(buffer), "%s/%lu", event_path, p->logid); 394 395 printf("Attempting to delete %s\n", buffer); 396 397 r = sd_bus_emit_object_removed(bus, buffer); 398 if (r < 0) { 399 fprintf(stderr, "Failed to emit the delete signal %s\n", strerror(-r)); 400 return -1; 401 } 402 sd_bus_slot_unref(p->messageslot); 403 sd_bus_slot_unref(p->deleteslot); 404 405 if (p->associationslot) 406 sd_bus_slot_unref(p->associationslot); 407 408 message_entry_close(p); 409 410 return 0; 411 } 412 413 int send_log_to_dbus(event_manager *em, const uint16_t logid, const char *association) 414 { 415 char loglocation[64]; 416 int r; 417 messageEntry_t *m; 418 419 snprintf(loglocation, sizeof(loglocation), "%s/%d", event_path, logid); 420 421 message_entry_new(&m, logid, em); 422 423 r = sd_bus_add_object_vtable(bus, 424 &m->messageslot, 425 loglocation, 426 "org.openbmc.record", 427 log_vtable, 428 m); 429 if (r < 0) { 430 fprintf(stderr, "Failed to acquire service name: %s %s\n", 431 loglocation, strerror(-r)); 432 message_entry_close(m); 433 return 0; 434 } 435 436 r = sd_bus_add_object_vtable(bus, 437 &m->deleteslot, 438 loglocation, 439 "org.openbmc.Object.Delete", 440 recordlog_delete_vtable, 441 m); 442 443 if (r < 0) { 444 fprintf(stderr, "Failed to add delete object for: %s, %s\n", 445 loglocation, strerror(-r)); 446 message_entry_close(m); 447 return 0; 448 } 449 450 m->associationslot = NULL; 451 if (strlen(association) > 0) { 452 r = sd_bus_add_object_vtable(bus, 453 &m->associationslot, 454 loglocation, 455 "org.openbmc.Associations", 456 recordlog_association_vtable, 457 m); 458 if (r < 0) { 459 fprintf(stderr, "Failed to add association object for: %s %s\n", 460 loglocation, strerror(-r)); 461 message_entry_close(m); 462 return 0; 463 } 464 } 465 466 r = sd_bus_emit_object_added(bus, loglocation); 467 if (r < 0) { 468 fprintf(stderr, "Failed to emit signal %s\n", strerror(-r)); 469 message_entry_close(m); 470 return 0; 471 } 472 473 return logid; 474 } 475 476 477 int start_event_monitor(void) 478 { 479 int r; 480 481 for (;;) { 482 483 r = sd_bus_process(bus, NULL); 484 if (r < 0) { 485 fprintf(stderr, "Error bus process: %s\n", strerror(-r)); 486 break; 487 } 488 489 if (r > 0) 490 continue; 491 492 r = sd_bus_wait(bus, (uint64_t) -1); 493 if (r < 0) { 494 fprintf(stderr, "Error in sd_bus_wait: %s\n", strerror(-r)); 495 break; 496 } 497 } 498 499 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; 500 } 501 502 503 /* Only thing we are doing in this function is to get a connection on the dbus */ 504 int build_bus(event_manager *em) 505 { 506 507 int r = 0; 508 509 /* Connect to the system bus */ 510 r = sd_bus_open_system(&bus); 511 if (r < 0) { 512 fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); 513 goto finish; 514 } 515 516 /* Install the object */ 517 r = sd_bus_add_object_vtable(bus, 518 &slot, 519 "/org/openbmc/records/events", 520 "org.openbmc.recordlog", 521 recordlog_vtable, 522 em); 523 if (r < 0) { 524 fprintf(stderr, "Error adding vtable: %s\n", strerror(-r)); 525 goto finish; 526 } 527 528 r = sd_bus_request_name(bus, "org.openbmc.records.events", 0); 529 if (r < 0) { 530 fprintf(stderr, "Error requesting name: %s\n", strerror(-r)); 531 } 532 533 /* You want to add an object manager to support deleting stuff */ 534 /* without it, dbus can show interfaces that no longer exist */ 535 r = sd_bus_add_object_manager(bus, NULL, event_path); 536 if (r < 0) { 537 fprintf(stderr, "Object Manager failure %s\n", strerror(-r)); 538 } 539 540 541 finish: 542 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; 543 } 544 545 void cleanup_event_monitor(void) 546 { 547 sd_bus_slot_unref(slot); 548 sd_bus_unref(bus); 549 }