Skip to content

Commit 1e97fdf

Browse files
committed
Change watchdog_enabled to use Duration
Also return the value via the return value instead of via an out parameter.
1 parent affc143 commit 1e97fdf

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::os::unix::io::RawFd;
3434
use std::os::unix::net::UnixDatagram;
3535
use std::process;
3636
use std::str::FromStr;
37+
use std::time::Duration;
3738

3839
use libc::CLOCK_MONOTONIC;
3940

@@ -472,10 +473,13 @@ fn fd_cloexec(fd: u32) -> io::Result<()> {
472473
/// ```no_run
473474
/// # use sd_notify;
474475
/// #
475-
/// let mut usec = 0;
476-
/// let enabled = sd_notify::watchdog_enabled(&mut usec);
476+
/// if let Some(duration) = sd_notify::watchdog_enabled() {
477+
/// // watchdog enabled with `duration`
478+
/// } else {
479+
/// // watchdog disabled
480+
/// }
477481
/// ```
478-
pub fn watchdog_enabled(usec: &mut u64) -> bool {
482+
pub fn watchdog_enabled() -> Option<Duration> {
479483
let s = env::var(WATCHDOG_USEC)
480484
.ok()
481485
.and_then(|s| u64::from_str(&s).ok());
@@ -485,10 +489,9 @@ pub fn watchdog_enabled(usec: &mut u64) -> bool {
485489

486490
match (s, p) {
487491
(Some(usec_val), Some(pid)) if pid == process::id() => {
488-
*usec = usec_val;
489-
true
492+
Some(Duration::from_micros(usec_val))
490493
}
491-
_ => false,
494+
_ => None,
492495
}
493496
}
494497

@@ -504,8 +507,8 @@ pub fn watchdog_enabled(usec: &mut u64) -> bool {
504507
/// preconditions. See its safety documentation for more details. It can only
505508
/// be safely called before threads are spawned, in particular before any
506509
/// `tokio` runtime initialization or `#[tokio::main]`.
507-
pub unsafe fn watchdog_enabled_and_unset_env(usec: &mut u64) -> bool {
508-
let result = watchdog_enabled(usec);
510+
pub unsafe fn watchdog_enabled_and_unset_env() -> Option<Duration> {
511+
let result = watchdog_enabled();
509512
unsafe {
510513
env::remove_var(WATCHDOG_USEC);
511514
env::remove_var(WATCHDOG_PID);

tests/watchdog_enabled.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::env;
55
use std::process;
6+
use std::time::Duration;
67

78
#[test]
89
fn watchdog_enabled() {
@@ -14,11 +15,9 @@ fn watchdog_enabled() {
1415
env::set_var("WATCHDOG_PID", "1");
1516
}
1617

17-
let mut usec = 0;
1818
unsafe {
19-
assert!(!sd_notify::watchdog_enabled_and_unset_env(&mut usec));
19+
assert_eq!(sd_notify::watchdog_enabled_and_unset_env(), None);
2020
}
21-
assert_eq!(usec, 0);
2221

2322
assert!(env::var_os("WATCHDOG_USEC").is_none());
2423
assert!(env::var_os("WATCHDOG_PID").is_none());
@@ -29,19 +28,15 @@ fn watchdog_enabled() {
2928
env::set_var("WATCHDOG_PID", process::id().to_string());
3029
}
3130

32-
let mut usec = 0;
3331
unsafe {
34-
assert!(!sd_notify::watchdog_enabled_and_unset_env(&mut usec));
32+
assert_eq!(sd_notify::watchdog_enabled_and_unset_env(), None);
3533
}
36-
assert_eq!(usec, 0);
3734

3835
assert!(env::var_os("WATCHDOG_USEC").is_none());
3936
assert!(env::var_os("WATCHDOG_PID").is_none());
4037

4138
// no usec, no pip no unset env
42-
let mut usec = 0;
43-
assert!(!sd_notify::watchdog_enabled(&mut usec));
44-
assert_eq!(usec, 0);
39+
assert_eq!(sd_notify::watchdog_enabled(), None);
4540

4641
assert!(env::var_os("WATCHDOG_USEC").is_none());
4742
assert!(env::var_os("WATCHDOG_PID").is_none());
@@ -52,9 +47,7 @@ fn watchdog_enabled() {
5247
env::set_var("WATCHDOG_PID", process::id().to_string());
5348
}
5449

55-
let mut usec = 0;
56-
assert!(sd_notify::watchdog_enabled(&mut usec));
57-
assert_eq!(usec, 5);
50+
assert_eq!(sd_notify::watchdog_enabled(), Some(Duration::from_micros(5)));
5851
assert!(env::var_os("WATCHDOG_USEC").is_some());
5952
assert!(env::var_os("WATCHDOG_PID").is_some());
6053
}

0 commit comments

Comments
 (0)