Skip to content

Commit ea06156

Browse files
committed
Support the timerfd API on FreeBSD.
FreeBSD has recently added support for Linux's timerfd API. Enable rustix's timerfd API on FreeBSD as well.
1 parent cb2c70d commit ea06156

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

src/backend/libc/time/syscalls.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::backend::c;
44
use crate::backend::conv::ret;
5-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
5+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
66
#[cfg(feature = "time")]
77
#[cfg(any(all(target_env = "gnu", fix_y2038), not(fix_y2038)))]
88
use crate::backend::time::types::LibcItimerspec;
@@ -13,7 +13,7 @@ use crate::io;
1313
use crate::timespec::LibcTimespec;
1414
use crate::timespec::Timespec;
1515
use core::mem::MaybeUninit;
16-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
16+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
1717
#[cfg(feature = "time")]
1818
use {
1919
crate::backend::conv::{borrowed_fd, ret_owned_fd},
@@ -27,11 +27,11 @@ weak!(fn __clock_gettime64(c::clockid_t, *mut LibcTimespec) -> c::c_int);
2727
weak!(fn __clock_settime64(c::clockid_t, *const LibcTimespec) -> c::c_int);
2828
#[cfg(all(target_env = "gnu", fix_y2038))]
2929
weak!(fn __clock_getres64(c::clockid_t, *mut LibcTimespec) -> c::c_int);
30-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
30+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
3131
#[cfg(all(target_env = "gnu", fix_y2038))]
3232
#[cfg(feature = "time")]
3333
weak!(fn __timerfd_gettime64(c::c_int, *mut LibcItimerspec) -> c::c_int);
34-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
34+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
3535
#[cfg(all(target_env = "gnu", fix_y2038))]
3636
#[cfg(feature = "time")]
3737
weak!(fn __timerfd_settime64(c::c_int, c::c_int, *const LibcItimerspec, *mut LibcItimerspec) -> c::c_int);
@@ -160,7 +160,7 @@ pub(crate) fn clock_gettime_dynamic(id: DynamicClockId<'_>) -> io::Result<Timesp
160160
return Err(io::Errno::INVAL);
161161
}
162162

163-
#[cfg(linux_kernel)]
163+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
164164
DynamicClockId::RealtimeAlarm => c::CLOCK_REALTIME_ALARM,
165165

166166
#[cfg(linux_kernel)]
@@ -278,13 +278,13 @@ fn clock_settime_old(id: ClockId, timespec: Timespec) -> io::Result<()> {
278278
unsafe { ret(c::clock_settime(id as c::clockid_t, &old_timespec)) }
279279
}
280280

281-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
281+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
282282
#[cfg(feature = "time")]
283283
pub(crate) fn timerfd_create(id: TimerfdClockId, flags: TimerfdFlags) -> io::Result<OwnedFd> {
284284
unsafe { ret_owned_fd(c::timerfd_create(id as c::clockid_t, bitflags_bits!(flags))) }
285285
}
286286

287-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
287+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
288288
#[cfg(feature = "time")]
289289
pub(crate) fn timerfd_settime(
290290
fd: BorrowedFd<'_>,
@@ -325,7 +325,7 @@ pub(crate) fn timerfd_settime(
325325
}
326326
}
327327

328-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
328+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
329329
#[cfg(fix_y2038)]
330330
#[cfg(feature = "time")]
331331
fn timerfd_settime_old(
@@ -393,7 +393,7 @@ fn timerfd_settime_old(
393393
})
394394
}
395395

396-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
396+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
397397
#[cfg(feature = "time")]
398398
pub(crate) fn timerfd_gettime(fd: BorrowedFd<'_>) -> io::Result<Itimerspec> {
399399
// Old 32-bit version: libc has `timerfd_gettime` but it is not y2038 safe
@@ -420,7 +420,7 @@ pub(crate) fn timerfd_gettime(fd: BorrowedFd<'_>) -> io::Result<Itimerspec> {
420420
}
421421
}
422422

423-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
423+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
424424
#[cfg(fix_y2038)]
425425
#[cfg(feature = "time")]
426426
fn timerfd_gettime_old(fd: BorrowedFd<'_>) -> io::Result<Itimerspec> {

src/backend/libc/time/types.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
1+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
22
use crate::backend::c;
3-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
3+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
44
#[cfg(fix_y2038)]
55
use crate::timespec::LibcTimespec;
6-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
6+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
77
#[cfg(fix_y2038)]
88
use crate::timespec::Timespec;
9-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
9+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
1010
use bitflags::bitflags;
1111

1212
/// `struct itimerspec` for use with [`timerfd_gettime`] and
1313
/// [`timerfd_settime`].
1414
///
1515
/// [`timerfd_gettime`]: crate::time::timerfd_gettime
1616
/// [`timerfd_settime`]: crate::time::timerfd_settime
17-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
17+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
1818
#[cfg(not(fix_y2038))]
1919
pub type Itimerspec = c::itimerspec;
2020

@@ -23,7 +23,7 @@ pub type Itimerspec = c::itimerspec;
2323
///
2424
/// [`timerfd_gettime`]: crate::time::timerfd_gettime
2525
/// [`timerfd_settime`]: crate::time::timerfd_settime
26-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
26+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
2727
#[cfg(fix_y2038)]
2828
#[repr(C)]
2929
#[derive(Debug, Clone)]
@@ -35,13 +35,13 @@ pub struct Itimerspec {
3535
}
3636

3737
/// On most platforms, `LibcItimerspec` is just `Itimerspec`.
38-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
38+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
3939
#[cfg(not(fix_y2038))]
4040
pub(crate) type LibcItimerspec = Itimerspec;
4141

4242
/// On 32-bit glibc platforms, `LibcTimespec` differs from `Timespec`, so we
4343
/// define our own struct, with bidirectional `From` impls.
44-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
44+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
4545
#[cfg(fix_y2038)]
4646
#[repr(C)]
4747
#[derive(Debug, Clone)]
@@ -50,7 +50,7 @@ pub(crate) struct LibcItimerspec {
5050
pub it_value: LibcTimespec,
5151
}
5252

53-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
53+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
5454
#[cfg(fix_y2038)]
5555
impl From<LibcItimerspec> for Itimerspec {
5656
#[inline]
@@ -62,7 +62,7 @@ impl From<LibcItimerspec> for Itimerspec {
6262
}
6363
}
6464

65-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
65+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
6666
#[cfg(fix_y2038)]
6767
impl From<Itimerspec> for LibcItimerspec {
6868
#[inline]
@@ -74,7 +74,7 @@ impl From<Itimerspec> for LibcItimerspec {
7474
}
7575
}
7676

77-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
77+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
7878
bitflags! {
7979
/// `TFD_*` flags for use with [`timerfd_create`].
8080
///
@@ -95,7 +95,7 @@ bitflags! {
9595
}
9696
}
9797

98-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
98+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
9999
bitflags! {
100100
/// `TFD_TIMER_*` flags for use with [`timerfd_settime`].
101101
///
@@ -108,7 +108,7 @@ bitflags! {
108108
const ABSTIME = bitcast!(c::TFD_TIMER_ABSTIME);
109109

110110
/// `TFD_TIMER_CANCEL_ON_SET`
111-
#[cfg(linux_kernel)]
111+
#[cfg(any(linux_kernel, target_os = "freebsd"))]
112112
#[doc(alias = "TFD_TIMER_CANCEL_ON_SET")]
113113
const CANCEL_ON_SET = bitcast!(c::TFD_TIMER_CANCEL_ON_SET);
114114

@@ -120,7 +120,7 @@ bitflags! {
120120
/// `CLOCK_*` constants for use with [`timerfd_create`].
121121
///
122122
/// [`timerfd_create`]: crate::time::timerfd_create
123-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
123+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
124124
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
125125
#[repr(u32)]
126126
#[non_exhaustive]
@@ -157,6 +157,7 @@ pub enum TimerfdClockId {
157157
/// This clock is like `Realtime`, but can wake up a suspended system.
158158
///
159159
/// Use of this clock requires the `CAP_WAKE_ALARM` Linux capability.
160+
#[cfg(linux_kernel)]
160161
#[doc(alias = "CLOCK_REALTIME_ALARM")]
161162
RealtimeAlarm = bitcast!(c::CLOCK_REALTIME_ALARM),
162163

@@ -165,11 +166,12 @@ pub enum TimerfdClockId {
165166
/// This clock is like `Boottime`, but can wake up a suspended system.
166167
///
167168
/// Use of this clock requires the `CAP_WAKE_ALARM` Linux capability.
169+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
168170
#[doc(alias = "CLOCK_BOOTTIME_ALARM")]
169171
BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
170172
}
171173

172-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
174+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
173175
#[test]
174176
fn test_types() {
175177
assert_eq_size!(TimerfdFlags, c::c_int);

src/clockid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub enum DynamicClockId<'a> {
138138
Dynamic(BorrowedFd<'a>),
139139

140140
/// `CLOCK_REALTIME_ALARM`
141-
#[cfg(linux_kernel)]
141+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
142142
#[doc(alias = "CLOCK_REALTIME_ALARM")]
143143
RealtimeAlarm,
144144

src/time/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Time-related operations.
22
33
mod clock;
4-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
4+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
55
mod timerfd;
66

77
// TODO: Convert WASI'S clock APIs to use handles rather than ambient clock
88
// identifiers, update `wasi-libc`, and then add support in `rustix`.
99
pub use clock::*;
10-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
10+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
1111
pub use timerfd::*;

tests/time/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod monotonic;
1414
all(apple, not(target_os = "macos"))
1515
)))]
1616
mod settime;
17-
#[cfg(linux_kernel)]
17+
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
1818
mod timerfd;
1919
mod timespec;
2020
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]

0 commit comments

Comments
 (0)