1 // Copyright 2016 CoreOS, Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
24 // SdWatchdogEnabled return watchdog information for a service.
25 // Process should send daemon.SdNotify("WATCHDOG=1") every time / 2.
26 // If `unsetEnvironment` is true, the environment variables `WATCHDOG_USEC`
27 // and `WATCHDOG_PID` will be unconditionally unset.
29 // It returns one of the following:
30 // (0, nil) - watchdog isn't enabled or we aren't the watched PID.
31 // (0, err) - an error happened (e.g. error converting time).
32 // (time, nil) - watchdog is enabled and we can send ping.
33 // time is delay before inactive service will be killed.
34 func SdWatchdogEnabled(unsetEnvironment bool) (time.Duration, error) {
35 wusec := os.Getenv("WATCHDOG_USEC")
36 wpid := os.Getenv("WATCHDOG_PID")
38 wusecErr := os.Unsetenv("WATCHDOG_USEC")
39 wpidErr := os.Unsetenv("WATCHDOG_PID")
51 s, err := strconv.Atoi(wusec)
53 return 0, fmt.Errorf("error converting WATCHDOG_USEC: %s", err)
56 return 0, fmt.Errorf("error WATCHDOG_USEC must be a positive number")
58 interval := time.Duration(s) * time.Microsecond
63 p, err := strconv.Atoi(wpid)
65 return 0, fmt.Errorf("error converting WATCHDOG_PID: %s", err)