Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions apps/decodex/src/state/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
use std::mem::{self, MaybeUninit};
use std::sync::atomic::AtomicU64;
use std::env;
#[cfg(target_os = "macos")]
use std::ptr;

use libc::FD_CLOEXEC;
use libc::F_GETFD;
use libc::F_SETFD;
#[cfg(target_os = "macos")]
use libc::{
c_char,
c_void,
proc_bsdinfo,
PROC_PIDTBSDINFO,
};
#[cfg(target_os = "macos")]
use process::Command;
use rusqlite::{self, Row, types::Type};

static RUN_ACTIVITY_MARKER_WRITE_SEQUENCE: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -3682,18 +3683,47 @@ fn read_platform_host_boot_id() -> Option<String> {

#[cfg(target_os = "macos")]
fn read_platform_host_boot_id() -> Option<String> {
let output = Command::new("/usr/sbin/sysctl")
.args(["-n", "kern.boottime"])
.output()
.ok()?;
const BOOT_SESSION_UUID_SYSCTL: &[u8] = b"kern.bootsessionuuid\0";

let mut size = 0_usize;
let query_status = unsafe {
libc::sysctlbyname(
BOOT_SESSION_UUID_SYSCTL.as_ptr().cast::<c_char>(),
ptr::null_mut(),
&mut size,
ptr::null_mut(),
0,
)
};

if !output.status.success() {
if query_status != 0 || size == 0 {
return None;
}

String::from_utf8(output.stdout)
.ok()
.map(|boot_id| format!("macos:{boot_id}"))
let mut boot_id = vec![0_u8; size];
let read_status = unsafe {
libc::sysctlbyname(
BOOT_SESSION_UUID_SYSCTL.as_ptr().cast::<c_char>(),
boot_id.as_mut_ptr().cast::<c_void>(),
&mut size,
ptr::null_mut(),
0,
)
};

if read_status != 0 || size == 0 {
return None;
}

boot_id.truncate(size);

if boot_id.last() == Some(&0) {
boot_id.pop();
}

String::from_utf8(boot_id).ok().map(|boot_id| {
format!("macos_bootsessionuuid:{boot_id}")
})
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
Expand Down
15 changes: 15 additions & 0 deletions apps/decodex/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,21 @@ fn run_activity_marker_round_trips_marker_surfaces() {
assert_run_activity_marker_preserves_account_summary_after_stale_rewrite();
}

#[cfg(target_os = "macos")]
#[test]
fn macos_host_boot_id_uses_boot_session_uuid() {
let host_boot_id = state::current_host_boot_id().expect("macOS boot session UUID should read");

assert!(
host_boot_id.starts_with("macos_bootsessionuuid:"),
"macOS host boot identity should use boot-session UUID, got {host_boot_id}"
);
assert!(
!host_boot_id.contains("boottime") && !host_boot_id.contains("usec"),
"macOS host boot identity should not depend on kern.boottime timeval output"
);
}

fn assert_run_activity_marker_round_trips_clearable_auxiliary_fields() {
let temp_dir = TempDir::new().expect("tempdir should create");

Expand Down