dmesg: use utmpx to get boot time.

This commit is contained in:
Fuad Ismail 2024-11-27 00:45:13 +07:00
parent 8efc8a01c2
commit f621687dd6
3 changed files with 35 additions and 40 deletions

38
Cargo.lock generated

@ -2,12 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]]
name = "aho-corasick"
version = "1.1.3"
@ -211,15 +205,6 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "crc32fast"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@ -294,16 +279,6 @@ version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
[[package]]
name = "flate2"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -430,15 +405,6 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "miniz_oxide"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
dependencies = [
"adler2",
]
[[package]]
name = "nix"
version = "0.29.0"
@ -619,8 +585,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc5b72d8145275d844d4b5f6d4e1eef00c8cd889edb6035c21675d1bb1f45c9f"
dependencies = [
"bitflags 2.6.0",
"chrono",
"flate2",
"hex",
"procfs-core",
"rustix 0.38.39",
@ -633,7 +597,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "239df02d8349b06fc07398a3a1697b06418223b1c7725085e801e7c0fc6a12ec"
dependencies = [
"bitflags 2.6.0",
"chrono",
"hex",
]
@ -1045,7 +1008,6 @@ version = "0.0.1"
dependencies = [
"chrono",
"clap",
"procfs",
"regex",
"serde",
"serde_json",

@ -17,7 +17,6 @@ regex = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
chrono = "0.4.38"
procfs = "0.17"
[features]
fixed-boot-time = []

@ -130,6 +130,40 @@ fn boot_time() -> DateTime<FixedOffset> {
}
#[cfg(not(feature = "fixed-boot-time"))]
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
fn boot_time() -> DateTime<FixedOffset> {
*BOOT_TIME.get_or_init(|| procfs::boot_time().unwrap().into())
*BOOT_TIME.get_or_init(|| boot_time_from_utmpx().unwrap())
}
#[cfg(not(feature = "fixed-boot-time"))]
#[cfg(windows)]
fn boot_time() -> DateTime<FixedOffset> {
// TODO: get windows boot time
*BOOT_TIME.get_or_init(|| chrono::DateTime::from_timestamp(0, 0).unwrap().into())
}
#[cfg(not(feature = "fixed-boot-time"))]
#[cfg(target_os = "openbsd")]
fn boot_time() -> DateTime<FixedOffset> {
// TODO: get openbsd boot time
*BOOT_TIME.get_or_init(|| chrono::DateTime::from_timestamp(0, 0).unwrap().into())
}
#[cfg(not(feature = "fixed-boot-time"))]
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
fn boot_time_from_utmpx() -> Option<DateTime<FixedOffset>> {
for record in uucore::utmpx::Utmpx::iter_all_records() {
if record.record_type() == uucore::utmpx::BOOT_TIME {
let t = record.login_time();
return Some(
chrono::DateTime::from_timestamp(t.unix_timestamp(), t.nanosecond())
.unwrap()
.with_timezone(&chrono::Local)
.into(),
);
}
}
None
}