-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix uptime on macOS using sysctl kern.boottime fallback #8908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
naoNao89
wants to merge
4
commits into
uutils:main
Choose a base branch
from
naoNao89:fix/issue-3621-uptime-macos-sysctl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c936518 to
7ba7816
Compare
If utmpx BOOT_TIME is unavailable, derive boot time via sysctl CTL_KERN.KERN_BOOTTIME to reduce intermittent macOS failures (e.g., uutils#3621). Context (blame/history): - 2774274 ("uptime: Support files in uptime (uutils#6400)"): added macOS utmpxname validation and non-fatal 'unknown uptime' fallback with tests (tests/by-util/test_uptime.rs). - 920d29f ("uptime: add support for OpenBSD using utmp"): reorganized uptime.rs and solidified utmp/utmpx-driven paths.
7ba7816 to
f99bfd1
Compare
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
Add unit tests for sysctl boottime availability and get_uptime reliability on macOS, verifying the fallback mechanism works correctly when utmpx BOOT_TIME is unavailable. Add integration tests to ensure uptime command consistently succeeds on macOS with various flags (default, --since) and produces properly formatted output. Enhance documentation of the sysctl fallback code with detailed comments explaining why it exists, the issue it addresses (uutils#3621), and comprehensive SAFETY comments for the unsafe sysctl call. All tests are properly gated with #[cfg(target_os = "macos")] to ensure they only run on macOS and don't interfere with other platforms.
cebba15 to
256dbd3
Compare
|
GNU testsuite comparison: |
sylvestre
reviewed
Oct 24, 2025
9e17aca to
3fd7774
Compare
|
GNU testsuite comparison: |
3fd7774 to
952befd
Compare
|
GNU testsuite comparison: |
952befd to
1a0313a
Compare
…ch for macOS boot time - Remove unsafe libc::sysctl() system call entirely - Replace with safe std::process::Command executing 'sysctl -n kern.boottime' - Parse sysctl output format to extract boot time seconds - Maintains same API and functionality while eliminating unsafe blocks - Addresses reviewer feedback to completely remove unsafe code
1a0313a to
7d2f40d
Compare
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
On macOS, the uptime command would sometimes fail in CI when utmpx couldn't provide a BOOT_TIME entry. This was causing flaky test failures, especially for uptime --since.
Fixes #3621
The fix adds a fallback using sysctl to query kern.boottime directly from the kernel. If we can't get boot time from utmpx, we now try sysctl before giving up. This makes uptime much more reliable on macOS.
The reviewer pointed out the sysctl fallback was using unsafe code, so I swapped the unsafe libc call for a safe command-line version that does the same thing. It still uses sysctl kern.boottime for the reliability fix from issue, but now there's no unsafe blocks anywhere. All the tests pass and it works the same, just safer.
blame:
2774274 added file support to uptime in, they added macOS-specific validation for utmpx paths but only implemented a graceful degradation fallback that printed "couldn't get boot time". Later when 047ec99 refactored uptime into uucore in, this utmpx-only approach was preserved. 6818bbe also had to relax error messages in due to macOS errno differences, showing ongoing macOS-specific issues.
None of these commits implemented the sysctl kern.boottime fallback that macOS has always provided as the canonical way to get boot time. This PR finally adds that missing piece that should have been there from the start.