Fix uptime on macOS using sysctl kern.boottime fallback #8908
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.
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.