From f7d38d261f202427e09c88757769fbcaef796b39 Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Fri, 25 Mar 2022 18:37:42 -0400 Subject: [PATCH 1/6] refactor(contexts): Abstract sysctlbyname calls into a function --- sentry-contexts/src/utils.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index b05b131a..0d30ded1 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -5,13 +5,18 @@ include!(concat!(env!("OUT_DIR"), "/constants.gen.rs")); #[cfg(target_os = "macos")] mod model_support { use libc::c_void; + use std::ffi::CString; use std::ptr; - pub fn get_model() -> Option { + fn sysctlbyname_call(name: &str) -> Option { unsafe { + let c_name = match CString::new(name) { + Ok(name) => name.into_bytes_with_nul(), + Err(_e) => return None, + }; let mut size = 0; let res = libc::sysctlbyname( - "hw.model\x00".as_ptr() as _, + c_name.as_ptr() as _, ptr::null_mut(), &mut size, ptr::null_mut(), @@ -20,9 +25,10 @@ mod model_support { if res != 0 { return None; } + let mut buf = vec![0u8; size as usize]; let res = libc::sysctlbyname( - "hw.model\x00".as_ptr() as _, + c_name.as_ptr() as _, buf.as_mut_ptr() as *mut c_void, &mut size, ptr::null_mut(), @@ -31,6 +37,7 @@ mod model_support { if res != 0 { return None; } + Some( buf.into_iter() .take(size) @@ -41,6 +48,10 @@ mod model_support { } } + pub fn get_model() -> Option { + sysctlbyname_call("hw.model") + } + pub fn get_family() -> Option { get_model().map(|mut s| { let len = s From 01bcd26344e5351654cfc56dfc89d064d508a25f Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Fri, 25 Mar 2022 18:48:11 -0400 Subject: [PATCH 2/6] feat(contexts): Get macOS product and build version --- sentry-contexts/src/utils.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index 0d30ded1..50eb059f 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -52,6 +52,14 @@ mod model_support { sysctlbyname_call("hw.model") } + pub fn get_macos_version() -> Option { + sysctlbyname_call("kern.osproductversion") + } + + pub fn get_macos_build() -> Option { + sysctlbyname_call("kern.osversion") + } + pub fn get_family() -> Option { get_model().map(|mut s| { let len = s @@ -71,6 +79,14 @@ mod model_support { let f = get_family().unwrap(); assert!(f.chars().all(|c| !c.is_digit(10))); } + + #[test] + fn test_macos_version_and_build() { + let v = get_macos_version().unwrap(); + assert!(v.chars().all(|c| c.is_digit(10) || c == '.')); + let b = get_macos_build().unwrap(); + assert!(b.chars().all(|c| c.is_ascii_alphabetic() || c.is_digit(10))); + } } #[cfg(not(target_os = "macos"))] @@ -82,6 +98,14 @@ mod model_support { pub fn get_family() -> Option { None } + + pub fn get_macos_version() -> Option { + None + } + + pub fn get_macos_build() -> Option { + None + } } /// Returns the server name (hostname) if available. From edfce71de7167be9bbe89ecdf810a2454aae697c Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Fri, 25 Mar 2022 19:09:10 -0400 Subject: [PATCH 3/6] feat(contexts): Report macOS name, version, and build --- sentry-contexts/src/utils.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index 50eb059f..4a4f8bca 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -119,15 +119,31 @@ pub fn os_context() -> Option { { use uname::uname; if let Ok(info) = uname() { - Some( - OsContext { - name: Some(info.sysname), - kernel_version: Some(info.version), - version: Some(info.release), - ..Default::default() - } - .into(), - ) + #[cfg(target_os = "macos")] + { + Some( + OsContext { + name: Some("macOS".into()), + kernel_version: Some(info.version), + version: model_support::get_macos_version(), + build: model_support::get_macos_build(), + ..Default::default() + } + .into(), + ) + } + #[cfg(not(target_os = "macos"))] + { + Some( + OsContext { + name: Some(info.sysname), + kernel_version: Some(info.version), + version: Some(info.release), + ..Default::default() + } + .into(), + ) + } } else { None } From 68401bdcbefccd7583c6e392719b2d4bb9ccb145 Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Fri, 25 Mar 2022 19:52:36 -0400 Subject: [PATCH 4/6] fix(contexts): Add patch version like sentry-native does --- sentry-contexts/src/utils.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index 4a4f8bca..24f17a9d 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -53,7 +53,12 @@ mod model_support { } pub fn get_macos_version() -> Option { - sysctlbyname_call("kern.osproductversion") + let version = sysctlbyname_call("kern.osproductversion")?; + let dot_count = version.split(".").count() - 1; + if dot_count < 2 { + return Some(version + ".0"); + } + Some(version) } pub fn get_macos_build() -> Option { @@ -84,6 +89,8 @@ mod model_support { fn test_macos_version_and_build() { let v = get_macos_version().unwrap(); assert!(v.chars().all(|c| c.is_digit(10) || c == '.')); + let dot_count = v.split(".").count() - 1; + assert_eq!(dot_count, 2); let b = get_macos_build().unwrap(); assert!(b.chars().all(|c| c.is_ascii_alphabetic() || c.is_digit(10))); } From 50108106e1cd7efb37f92fcf6da97944b7490067 Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Mon, 28 Mar 2022 13:02:27 -0400 Subject: [PATCH 5/6] chore: Remove unused code --- sentry-contexts/src/utils.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index 24f17a9d..5c34ab47 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -105,14 +105,6 @@ mod model_support { pub fn get_family() -> Option { None } - - pub fn get_macos_version() -> Option { - None - } - - pub fn get_macos_build() -> Option { - None - } } /// Returns the server name (hostname) if available. From 157364adf897c62148927422f73236d001ddbd51 Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Mon, 28 Mar 2022 13:08:45 -0400 Subject: [PATCH 6/6] chore: Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab7cae19..93f56116 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **Features**: - Request data can now be attached to Transactions and Spans via `set_transaction`. ([#439](https://github.com/getsentry/sentry-rust/pull/439)) +- macOS versions are now reported instead of the Darwin kernel version. ([#451](https://github.com/getsentry/sentry-rust/pull/451)) **Thank you**: