Skip to content
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

Provide musl libc compatibility #696

Merged
merged 9 commits into from
Apr 24, 2023
29 changes: 21 additions & 8 deletions ntp-os-clock/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) const EMPTY_TIMEX: libc::timex = libc::timex {
errcnt: 0,
stbcnt: 0,
tai: 0,
__padding: [0; 11]
__padding: [0; 11],
};

#[cfg(any(target_os = "freebsd", target_os = "macos"))]
Expand Down Expand Up @@ -209,15 +209,20 @@ impl UnixNtpClock {
}

fn ntp_adjtime(timex: &mut libc::timex) -> Result<(), Error> {
#[cfg(any(target_os = "freebsd", target_os = "macos", target_env = "gnu"))]
use libc::ntp_adjtime as adjtime;

// ntp_adjtime is equivalent to adjtimex for our purposes
//
// https://man7.org/linux/man-pages/man2/adjtimex.2.html
#[cfg(all(target_os = "linux", target_env = "musl"))]
use libc::adjtimex as adjtime;

// We don't care about the time status, so the non-error
// information in the return value of ntp_adjtime can be ignored.
// The ntp_adjtime call is safe because the reference always
// points to a valid libc::timex.
#[cfg(any(target_os = "freebsd", target_os = "macos", target_env = "gnu"))]
let errno = unsafe { libc::ntp_adjtime(timex) };
#[cfg(all(target_os = "linux", target_env = "musl"))]
let errno = unsafe { libc::adjtimex(timex) };
if errno == -1 {
if unsafe { adjtime(timex) } == -1 {
Err(convert_errno())
} else {
Ok(())
Expand All @@ -238,7 +243,15 @@ impl UnixNtpClock {

let mut timespec = self.clock_gettime()?;

timespec.tv_sec += offset_secs as libc::time_t;
timespec.tv_sec += {
// this looks a little strange. it is to work around the `libc::time_t` type being
// deprectated for musl in rust's libc.
Copy link
Contributor Author

@sanmai-NL sanmai-NL Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to explicitly silence this warning instead. There's no recommended migration path. It's questionable whether the deprecation will be upheld. See: rust-lang/libc#1848

(Typo: deprecated.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I read the issue, and ... it's really unclear what they actually want to happen there. it must eventually be changed because that is just what musl expects now, right? I'll amend this to ignore the warning, and link to the issue

if true {
offset_secs as _
} else {
timespec.tv_sec
}
};
timespec.tv_nsec += offset_nanos as libc::c_long;

self.clock_settime(timespec)?;
Expand All @@ -253,7 +266,7 @@ impl UnixNtpClock {
let mut timex = libc::timex {
modes: libc::ADJ_SETOFFSET | libc::MOD_NANO,
time: libc::timeval {
tv_sec: secs as libc::time_t,
tv_sec: secs as _,
tv_usec: nanos as libc::suseconds_t,
},
..crate::unix::EMPTY_TIMEX
Expand Down