|
2 | 2 | // Copyright (C) 2022-2023 The crypto-auditing developers. |
3 | 3 |
|
4 | 4 | use anyhow::{Context as _, Result}; |
5 | | -use crypto_auditing::types::{ContextTracker, EventGroup}; |
| 5 | +use crypto_auditing::types::{ContextTracker, EventData, EventGroup}; |
6 | 6 | use pager::Pager; |
7 | 7 | use serde_cbor::de::Deserializer; |
8 | 8 | use std::io::{self, Write}; |
9 | | -use std::time::{Duration, UNIX_EPOCH}; |
| 9 | +use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
10 | 10 |
|
11 | 11 | mod config; |
12 | 12 |
|
| 13 | +fn get_boot_time_from_metadata(group: &EventGroup) -> Option<SystemTime> { |
| 14 | + for event in group.events() { |
| 15 | + if let Some(data) = event.data("boot_time") { |
| 16 | + match data { |
| 17 | + EventData::Word(secs) => { |
| 18 | + return Some(UNIX_EPOCH + Duration::from_secs(*secs as u64)); |
| 19 | + } |
| 20 | + _ => (), |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + None |
| 25 | +} |
| 26 | + |
13 | 27 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
14 | 28 | let config = config::Config::new()?; |
15 | 29 | Pager::new().setup(); |
16 | 30 |
|
17 | 31 | let log_file = std::fs::File::open(&config.log_file) |
18 | 32 | .with_context(|| format!("unable to read file `{}`", config.log_file.display()))?; |
19 | 33 |
|
20 | | - let mut tracker = ContextTracker::new( |
21 | | - config |
22 | | - .boot_time |
23 | | - .map(|secs| UNIX_EPOCH + Duration::from_secs(secs)), |
24 | | - ); |
25 | | - for group in Deserializer::from_reader(&log_file).into_iter::<EventGroup>() { |
| 34 | + let mut groups = Deserializer::from_reader(&log_file) |
| 35 | + .into_iter::<EventGroup>() |
| 36 | + .peekable(); |
| 37 | + |
| 38 | + // Figure out the system boot time, first from the config, and |
| 39 | + // then from the metadata group in the log. |
| 40 | + let boot_time = if let Some(secs) = config.boot_time { |
| 41 | + Some(UNIX_EPOCH + Duration::from_secs(secs)) |
| 42 | + } else if let Some(Ok(group)) = groups.peek() |
| 43 | + && group.is_metadata() |
| 44 | + { |
| 45 | + let boot_time = get_boot_time_from_metadata(&group); |
| 46 | + // Skip the metadata group. |
| 47 | + groups.next(); |
| 48 | + boot_time |
| 49 | + } else { |
| 50 | + None |
| 51 | + }; |
| 52 | + |
| 53 | + let mut tracker = ContextTracker::new(boot_time); |
| 54 | + for group in groups { |
26 | 55 | tracker.handle_event_group(&group?); |
27 | 56 | } |
28 | 57 | let root_contexts: Vec<_> = tracker |
|
0 commit comments