├── .github └── workflows │ ├── linux-test.yml │ └── static-analysis.yml ├── LICENSE.md ├── README.md ├── cmd └── watchdog │ └── main.go ├── go.mod ├── go.sum ├── watchdog.go ├── watchdog_linux.go ├── watchdog_linux_test.go └── watchdog_others.go /.github/workflows/linux-test.yml: -------------------------------------------------------------------------------- 1 | name: Linux Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - '*' 10 | 11 | jobs: 12 | build: 13 | strategy: 14 | matrix: 15 | go-version: [1.19] 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - name: Set up Go 20 | uses: actions/setup-go@v1 21 | with: 22 | go-version: ${{ matrix.go-version }} 23 | id: go 24 | 25 | - name: Check out code into the Go module directory 26 | uses: actions/checkout@v1 27 | 28 | - name: Run tests 29 | run: go test -race ./... 30 | -------------------------------------------------------------------------------- /.github/workflows/static-analysis.yml: -------------------------------------------------------------------------------- 1 | name: Static Analysis 2 | 3 | on: 4 | push: 5 | branches: 6 | - '*' 7 | pull_request: 8 | branches: 9 | - '*' 10 | 11 | jobs: 12 | build: 13 | strategy: 14 | matrix: 15 | go-version: [1.19] 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - name: Set up Go 20 | uses: actions/setup-go@v2 21 | with: 22 | go-version: ${{ matrix.go-version }} 23 | id: go 24 | 25 | - name: Check out code into the Go module directory 26 | uses: actions/checkout@v2 27 | 28 | - name: Install staticcheck 29 | run: go install honnef.co/go/tools/cmd/staticcheck@latest 30 | 31 | - name: Print staticcheck version 32 | run: staticcheck -version 33 | 34 | - name: Run staticcheck 35 | run: staticcheck ./... 36 | 37 | - name: Run go vet 38 | run: go vet ./... 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (C) 2020-2022 Matt Layher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # watchdog [![Linux Test Status](https://github.com/mdlayher/watchdog/workflows/Linux%20Test/badge.svg)](https://github.com/mdlayher/watchdog/actions) [![GoDoc](https://godoc.org/github.com/mdlayher/watchdog?status.svg)](https://godoc.org/github.com/mdlayher/watchdog) [![Go Report Card](https://goreportcard.com/badge/github.com/mdlayher/watchdog)](https://goreportcard.com/report/github.com/mdlayher/watchdog) 2 | 3 | Package `watchdog` implements control of hardware watchdog devices. MIT 4 | Licensed. 5 | -------------------------------------------------------------------------------- /cmd/watchdog/main.go: -------------------------------------------------------------------------------- 1 | // Command watchdog is a demo application which shows usage of the package 2 | // watchdog APIs. 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "log" 8 | "time" 9 | 10 | "github.com/mdlayher/watchdog" 11 | ) 12 | 13 | func main() { 14 | d, err := watchdog.Open() 15 | if err != nil { 16 | log.Fatalf("failed to open watchdog: %v", err) 17 | } 18 | 19 | // We purposely double-close the file to ensure that the explicit Close 20 | // later on also disarms the device as the program exits. Otherwise it's 21 | // possible we may exit early or with a subtle error and leave the system 22 | // in a doomed state. 23 | defer d.Close() 24 | 25 | timeout, err := d.Timeout() 26 | if err != nil { 27 | log.Fatalf("failed to fetch watchdog timeout: %v", err) 28 | } 29 | 30 | fmt.Printf("device: %q, timeout: %s\n", d.Identity, timeout) 31 | 32 | for i := 0; i < 3; i++ { 33 | if err := d.Ping(); err != nil { 34 | log.Fatalf("failed to ping watchdog: %v", err) 35 | } 36 | 37 | // Note progress to the caller. 38 | fmt.Print(".") 39 | time.Sleep(1 * time.Second) 40 | } 41 | 42 | fmt.Println() 43 | 44 | // Safely disarm the device before exiting. 45 | if err := d.Close(); err != nil { 46 | log.Fatalf("failed to disarm watchdog: %v", err) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mdlayher/watchdog 2 | 3 | go 1.19 4 | 5 | require golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI= 2 | golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 3 | -------------------------------------------------------------------------------- /watchdog.go: -------------------------------------------------------------------------------- 1 | // Package watchdog implements control of hardware watchdog devices. 2 | package watchdog 3 | 4 | import ( 5 | "os" 6 | "time" 7 | ) 8 | 9 | // A Device is a hardware watchdog device which can be pinged to keep the system 10 | // from rebooting once the device has been opened. 11 | type Device struct { 12 | // Identity is the name of the watchdog driver. 13 | Identity string 14 | 15 | f *os.File 16 | } 17 | 18 | // Open opens the primary watchdog device on this system ("/dev/watchdog" on 19 | // Linux, TBD on other platforms). If the device is not found, an error 20 | // compatible with os.ErrNotExist will be returned. 21 | // 22 | // Once a Device is opened, you must call Ping repeatedly to keep the system 23 | // from being rebooted. Call Close to disarm the watchdog device. 24 | func Open() (*Device, error) { return open() } 25 | 26 | // Ping pings the watchdog device to keep the device from rebooting the system. 27 | func (d *Device) Ping() error { return d.ping() } 28 | 29 | // Timeout returns the configured timeout of the watchdog device. 30 | func (d *Device) Timeout() (time.Duration, error) { return d.timeout() } 31 | 32 | // Close closes the handle to the watchdog device and attempts to gracefully 33 | // disarm the device, so no further Ping calls are required to keep the system 34 | // from rebooting. 35 | func (d *Device) Close() error { return d.close() } 36 | -------------------------------------------------------------------------------- /watchdog_linux.go: -------------------------------------------------------------------------------- 1 | //+build linux 2 | 3 | package watchdog 4 | 5 | import ( 6 | "os" 7 | "strings" 8 | "time" 9 | 10 | "golang.org/x/sys/unix" 11 | ) 12 | 13 | // All system calls in this code are part of the Linux watchdog API. For 14 | // reference, see: 15 | // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html. 16 | 17 | func open() (*Device, error) { 18 | // TODO(mdlayher): determine the significance of the "/dev/watchdogN" nodes 19 | // on Linux. It appears that my machine with only one device exposes both 20 | // "/dev/watchdog" and "/dev/watchdog0". 21 | // 22 | // According to Terin, /dev/watchdog is an alias for /dev/watchdog0 on 23 | // modern machines. It's possible there could be more than one device, so 24 | // we'll eventually want to support that. 25 | f, err := os.OpenFile("/dev/watchdog", os.O_WRONLY, 0) 26 | if err != nil { 27 | return nil, err 28 | } 29 | 30 | // Immediately fetch the device's information to return to the caller. 31 | info, err := unix.IoctlGetWatchdogInfo(int(f.Fd())) 32 | if err != nil { 33 | return nil, err 34 | } 35 | 36 | return &Device{ 37 | // Clean up any trailing NULL bytes. 38 | Identity: strings.TrimRight(string(info.Identity[:]), "\x00"), 39 | 40 | f: f, 41 | }, nil 42 | } 43 | 44 | func (d *Device) ping() error { return unix.IoctlWatchdogKeepalive(int(d.f.Fd())) } 45 | 46 | func (d *Device) timeout() (time.Duration, error) { 47 | s, err := unix.IoctlGetInt(int(d.f.Fd()), unix.WDIOC_GETTIMEOUT) 48 | if err != nil { 49 | return 0, err 50 | } 51 | 52 | // The time value is always returned in seconds. 53 | return time.Duration(s) * time.Second, nil 54 | } 55 | 56 | func (d *Device) close() error { 57 | // Attempt a Magic Close to disarm the watchdog device, since any call to 58 | // Close would be intentional and it's unlikely the user would want a system 59 | // reboot. Reference: 60 | // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html#magic-close-feature 61 | if _, err := d.f.Write([]byte("V")); err != nil { 62 | // Make sure the file descriptor is closed even if Magic Close fails. 63 | _ = d.f.Close() 64 | return err 65 | } 66 | 67 | return d.f.Close() 68 | } 69 | -------------------------------------------------------------------------------- /watchdog_linux_test.go: -------------------------------------------------------------------------------- 1 | //+build linux 2 | 3 | package watchdog_test 4 | 5 | import ( 6 | "errors" 7 | "os" 8 | "testing" 9 | 10 | "github.com/mdlayher/watchdog" 11 | ) 12 | 13 | func TestIntegrationDevice(t *testing.T) { 14 | // Since this test requires the presence of specific hardware, it's highly 15 | // likely to not pass on regular machines. Make sure to check the errors 16 | // and skip when necessary. 17 | d, err := watchdog.Open() 18 | if err != nil { 19 | switch { 20 | case errors.Is(err, os.ErrNotExist): 21 | t.Skipf("skipping, watchdog device does not exist: %v", err) 22 | case errors.Is(err, os.ErrPermission): 23 | t.Skipf("skipping, permission denied (try running as root): %v", err) 24 | default: 25 | t.Fatalf("failed to open device: %v", err) 26 | } 27 | } 28 | 29 | // Double-close to ensure we always disarm the watchdog and don't reboot 30 | // the host. 31 | defer d.Close() 32 | 33 | t.Logf("device: %q", d.Identity) 34 | 35 | if err := d.Ping(); err != nil { 36 | t.Fatalf("failed to ping: %v", err) 37 | } 38 | 39 | if err := d.Close(); err != nil { 40 | t.Fatalf("failed to disarm: %v", err) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /watchdog_others.go: -------------------------------------------------------------------------------- 1 | //+build !linux 2 | 3 | package watchdog 4 | 5 | import ( 6 | "fmt" 7 | "os" 8 | "runtime" 9 | "time" 10 | ) 11 | 12 | // errNotImplemented is a sentinel which indicates package watchdog does not 13 | // support this OS. 14 | var errNotImplemented = fmt.Errorf("watchdog: not implemented on %s: %w", runtime.GOOS, os.ErrNotExist) 15 | 16 | func open() (*Device, error) { return nil, errNotImplemented } 17 | func (*Device) ping() error { return errNotImplemented } 18 | func (*Device) timeout() (time.Duration, error) { return 0, errNotImplemented } 19 | func (*Device) close() error { return errNotImplemented } 20 | --------------------------------------------------------------------------------