Skip to content

Commit bce6abe

Browse files
committed
Add watchdog checker
Add a function SdWatchdogEnabled() who check if service have to report its status This is inspirated from the behavior of sd_watchdog_enabled
1 parent 64d5cd7 commit bce6abe

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

daemon/watchdog.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2016 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package daemon
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strconv"
21+
"time"
22+
)
23+
24+
// SdWatchdogEnabled return watchdog information for a service.
25+
// Process should send daemon.SdNotify("WATCHDOG=1") every time / 2.
26+
//
27+
// It returns one of the following:
28+
// (0, nil) - watchdog isn't enabled or we aren't the watched PID.
29+
// (0, err) - an error happened (e.g. error converting time).
30+
// (time, nil) - watchdog is enabled and we can send ping.
31+
// time is delay before inactive service will be killed.
32+
func SdWatchdogEnabled() (time.Duration, error) {
33+
wusec := os.Getenv("WATCHDOG_USEC")
34+
if wusec == "" {
35+
return 0, nil
36+
}
37+
s, err := strconv.Atoi(wusec)
38+
if err != nil {
39+
return 0, fmt.Errorf("error converting WATCHDOG_USEC: %s", err)
40+
}
41+
if s <= 0 {
42+
return 0, fmt.Errorf("error WATCHDOG_USEC must a positive number")
43+
}
44+
45+
wpid := os.Getenv("WATCHDOG_PID")
46+
if wpid == "" {
47+
return 0, nil
48+
}
49+
p, err := strconv.Atoi(wpid)
50+
if err != nil {
51+
return 0, fmt.Errorf("error converting WATCHDOG_PID: %s", err)
52+
}
53+
if os.Getpid() != p {
54+
return 0, nil
55+
}
56+
57+
return time.Duration(s) * time.Microsecond, nil
58+
}

daemon/watchdog_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2016 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package daemon
16+
17+
import (
18+
"os"
19+
"strconv"
20+
"testing"
21+
)
22+
23+
func TestSdWatchdogEnabled(t *testing.T) {
24+
// (time, nil)
25+
err := os.Setenv("WATCHDOG_USEC", "100")
26+
if err != nil {
27+
panic(err)
28+
}
29+
err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid()))
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
delay, err := SdWatchdogEnabled()
35+
if delay == 0 || err != nil {
36+
t.Errorf("TEST: Watchdog enabled FAILED")
37+
}
38+
39+
// (0, nil) PID doesnt match
40+
err = os.Setenv("WATCHDOG_USEC", "100")
41+
if err != nil {
42+
panic(err)
43+
}
44+
err = os.Setenv("WATCHDOG_PID", "0")
45+
if err != nil {
46+
panic(err)
47+
}
48+
delay, err = SdWatchdogEnabled()
49+
if delay != 0 || err != nil {
50+
t.Errorf("TEST: PID doesn't match FAILED")
51+
}
52+
53+
// (0, nil) WATCHDOG_USEC doen't exist
54+
err = os.Unsetenv("WATCHDOG_USEC")
55+
if err != nil {
56+
panic(err)
57+
}
58+
err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid()))
59+
if err != nil {
60+
panic(err)
61+
}
62+
delay, err = SdWatchdogEnabled()
63+
if delay != 0 || err != nil {
64+
t.Errorf("TEST: WATCHDOG_USEC doen't exist FAILED")
65+
}
66+
67+
// (0, nil) WATCHDOG_PID doen't exist
68+
err = os.Setenv("WATCHDOG_USEC", "1")
69+
if err != nil {
70+
panic(err)
71+
}
72+
err = os.Unsetenv("WATCHDOG_PID")
73+
if err != nil {
74+
panic(err)
75+
}
76+
delay, err = SdWatchdogEnabled()
77+
if delay != 0 || err != nil {
78+
t.Errorf("TEST: WATCHDOG_PID doen't exist FAILED")
79+
}
80+
81+
// (0, err) USEC negative
82+
err = os.Setenv("WATCHDOG_USEC", "-1")
83+
if err != nil {
84+
panic(err)
85+
}
86+
err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid()))
87+
if err != nil {
88+
panic(err)
89+
}
90+
_, err = SdWatchdogEnabled()
91+
if err == nil {
92+
t.Errorf("TEST: USEC negative FAILED")
93+
}
94+
95+
// (0, err) Bad USEC
96+
err = os.Setenv("WATCHDOG_USEC", "string")
97+
if err != nil {
98+
panic(err)
99+
}
100+
err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid()))
101+
if err != nil {
102+
panic(err)
103+
}
104+
_, err = SdWatchdogEnabled()
105+
if err == nil {
106+
t.Errorf("TEST: Bad USEC FAILED")
107+
}
108+
109+
// (0, err) Bad PID
110+
err = os.Setenv("WATCHDOG_USEC", "1")
111+
if err != nil {
112+
panic(err)
113+
}
114+
err = os.Setenv("WATCHDOG_PID", "string")
115+
if err != nil {
116+
panic(err)
117+
}
118+
_, err = SdWatchdogEnabled()
119+
if err == nil {
120+
t.Errorf("TEST: Bad PID FAILED")
121+
}
122+
}

0 commit comments

Comments
 (0)