* Initializes the eventlog header with the given type and data, * and calculates the checksum. * buffer_get() points to the event to be initialized. * On success it returns 1, otherwise 0. */
| 727 | * On success it returns 1, otherwise 0. |
| 728 | */ |
| 729 | int eventlog_init_event(const struct buffer *buf, uint8_t type, |
| 730 | const void *data, int data_size) |
| 731 | { |
| 732 | struct event_header *event; |
| 733 | time_t secs = time(NULL); |
| 734 | struct tm tm; |
| 735 | |
| 736 | /* Must have at least size for data + checksum byte */ |
| 737 | if (buffer_size(buf) < (size_t)data_size + 1) |
| 738 | return 0; |
| 739 | |
| 740 | event = buffer_get(buf); |
| 741 | |
| 742 | event->type = type; |
| 743 | gmtime_r(&secs, &tm); |
| 744 | /* Month should be +1, since gmtime uses 0 as first month */ |
| 745 | elog_fill_timestamp(event, tm.tm_sec, tm.tm_min, tm.tm_hour, |
| 746 | tm.tm_mday, tm.tm_mon + 1, tm.tm_year); |
| 747 | |
| 748 | if (data && data_size) { |
| 749 | uint32_t *ptr = (uint32_t *)&event[1]; |
| 750 | memcpy(ptr, data, data_size); |
| 751 | } |
| 752 | |
| 753 | /* Header + data + checksum */ |
| 754 | event->length = sizeof(*event) + data_size + 1; |
| 755 | |
| 756 | /* Zero the checksum byte and then compute checksum */ |
| 757 | elog_update_checksum(event, 0); |
| 758 | elog_update_checksum(event, -(elog_checksum_event(event))); |
| 759 | |
| 760 | return 1; |
| 761 | } |
no test coverage detected