Skip to content

Commit 9fef37f

Browse files
authored
Merge pull request #14 from tbu-/pr_watchdog_duration
Change `watchdog_enabled` to use `Duration`
2 parents 25a7e2c + 1e97fdf commit 9fef37f

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

@@ -474,10 +475,13 @@ fn fd_cloexec(fd: u32) -> io::Result<()> {
474475
/// ```no_run
475476
/// # use sd_notify;
476477
/// #
477-
/// let mut usec = 0;
478-
/// let enabled = sd_notify::watchdog_enabled(&mut usec);
478+
/// if let Some(duration) = sd_notify::watchdog_enabled() {
479+
/// // watchdog enabled with `duration`
480+
/// } else {
481+
/// // watchdog disabled
482+
/// }
479483
/// ```
480-
pub fn watchdog_enabled(usec: &mut u64) -> bool {
484+
pub fn watchdog_enabled() -> Option<Duration> {
481485
let s = env::var(WATCHDOG_USEC)
482486
.ok()
483487
.and_then(|s| u64::from_str(&s).ok());
@@ -487,10 +491,9 @@ pub fn watchdog_enabled(usec: &mut u64) -> bool {
487491

488492
match (s, p) {
489493
(Some(usec_val), Some(pid)) if pid == process::id() => {
490-
*usec = usec_val;
491-
true
494+
Some(Duration::from_micros(usec_val))
492495
}
493-
_ => false,
496+
_ => None,
494497
}
495498
}
496499

@@ -506,8 +509,8 @@ pub fn watchdog_enabled(usec: &mut u64) -> bool {
506509
/// preconditions. See its safety documentation for more details. It can only
507510
/// be safely called before threads are spawned, in particular before any
508511
/// `tokio` runtime initialization or `#[tokio::main]`.
509-
pub unsafe fn watchdog_enabled_and_unset_env(usec: &mut u64) -> bool {
510-
let result = watchdog_enabled(usec);
512+
pub unsafe fn watchdog_enabled_and_unset_env() -> Option<Duration> {
513+
let result = watchdog_enabled();
511514
unsafe {
512515
env::remove_var(WATCHDOG_USEC);
513516
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)