Skip to content

Commit

Permalink
Update use of libc::timespec to prepare for future libc version (#55)
Browse files Browse the repository at this point in the history
In a future release of the `libc` crate, `libc::timespec` will contain
private padding fields on `*-linux-musl` targets and so the struct will
no longer be able to be created using the literal initialization syntax.

Update `TS_ZERO` to create a value by initializing an array of the
correct size to `0` and then transmuting to `libc::timespec`. Update
struct literal use of `libc::timespec` to initialize to `TS_ZERO` and
then manually update the appropriate fields. Also updates a raw syscall
to use the libc function instead as on musl 1.2, it correctly handles
`libc::timespec` values which, in musl 1.2, are always 16 bytes in
length regardless of platform.
  • Loading branch information
wesleywiser committed Dec 3, 2022
1 parent 00e7eef commit 5343302
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ impl Poller {
it_interval: TS_ZERO,
it_value: match timeout {
None => TS_ZERO,
Some(t) => libc::timespec {
tv_sec: t.as_secs() as libc::time_t,
tv_nsec: (t.subsec_nanos() as libc::c_long).into(),
},
Some(t) => {
let mut ts = TS_ZERO;
ts.tv_sec = t.as_secs() as libc::time_t;
ts.tv_nsec = (t.subsec_nanos() as libc::c_long).into();
ts
}
},
};

syscall!(syscall(
libc::SYS_timerfd_settime,
syscall!(timerfd_settime(
timer_fd as libc::c_int,
0 as libc::c_int,
&new_val as *const libc::itimerspec,
Expand Down Expand Up @@ -262,10 +263,8 @@ impl Drop for Poller {
}

/// `timespec` value that equals zero.
const TS_ZERO: libc::timespec = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
const TS_ZERO: libc::timespec =
unsafe { std::mem::transmute([0u8; std::mem::size_of::<libc::timespec>()]) };

/// Epoll flags for all possible readability events.
fn read_flags() -> libc::c_int {
Expand Down

0 comments on commit 5343302

Please sign in to comment.