Skip to content

Commit 831c9d5

Browse files
committed
daemon/watchdog: fix watchdog detection when $WATCHDOG_PID is unset
This fixes SdWatchdogEnabled() behavior when $WATCHDOG_PID is unset, aligning it with the native method: ``` If the $WATCHDOG_USEC environment variable is set, and the $WATCHDOG_PID variable is unset or set to the PID of the current process, the service manager expects notifications from this process. ```
1 parent d7387fd commit 831c9d5

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

daemon/watchdog.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ func SdWatchdogEnabled() (time.Duration, error) {
3939
return 0, fmt.Errorf("error converting WATCHDOG_USEC: %s", err)
4040
}
4141
if s <= 0 {
42-
return 0, fmt.Errorf("error WATCHDOG_USEC must a positive number")
42+
return 0, fmt.Errorf("error WATCHDOG_USEC must be a positive number")
4343
}
44+
interval := time.Duration(s) * time.Microsecond
4445

4546
wpid := os.Getenv("WATCHDOG_PID")
4647
if wpid == "" {
47-
return 0, nil
48+
return interval, nil
4849
}
4950
p, err := strconv.Atoi(wpid)
5051
if err != nil {
@@ -54,5 +55,5 @@ func SdWatchdogEnabled() (time.Duration, error) {
5455
return 0, nil
5556
}
5657

57-
return time.Duration(s) * time.Microsecond, nil
58+
return interval, nil
5859
}

daemon/watchdog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func TestSdWatchdogEnabled(t *testing.T) {
4040
{"100", mypid, false, 100 * time.Microsecond},
4141
{"50", mypid, false, 50 * time.Microsecond},
4242
{"1", mypid, false, 1 * time.Microsecond},
43+
{"1", "", false, 1 * time.Microsecond},
4344

4445
// No-op cases
4546
{"", mypid, false, 0}, // WATCHDOG_USEC not set
46-
{"1", "", false, 0}, // WATCHDOG_PID not set
4747
{"1", "0", false, 0}, // WATCHDOG_PID doesn't match
4848
{"", "", false, 0}, // Both not set
4949

0 commit comments

Comments
 (0)