github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/configstate/configcore/watchdog.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package configcore 21 22 import ( 23 "fmt" 24 "os" 25 "sort" 26 "strings" 27 "time" 28 29 "github.com/snapcore/snapd/dirs" 30 "github.com/snapcore/snapd/osutil" 31 "github.com/snapcore/snapd/overlord/configstate/config" 32 "github.com/snapcore/snapd/sysconfig" 33 "github.com/snapcore/snapd/systemd" 34 ) 35 36 func init() { 37 // add supported configuration of this module 38 supportedConfigurations["core.watchdog.runtime-timeout"] = true 39 supportedConfigurations["core.watchdog.shutdown-timeout"] = true 40 } 41 42 func updateWatchdogConfig(config map[string]uint, opts *fsOnlyContext) error { 43 var sysd systemd.Systemd 44 45 dir := dirs.SnapSystemdConfDir 46 if opts != nil { 47 dir = dirs.SnapSystemdConfDirUnder(opts.RootDir) 48 } else { 49 sysd = systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.SystemMode, &sysdLogger{}) 50 } 51 52 name := "10-snapd-watchdog.conf" 53 dirContent := make(map[string]osutil.FileState, 1) 54 55 configStr := []string{} 56 for k, v := range config { 57 if v > 0 { 58 configStr = append(configStr, fmt.Sprintf("%s=%d\n", k, v)) 59 } 60 } 61 if len(configStr) > 0 { 62 if err := os.MkdirAll(dir, 0755); err != nil { 63 return err 64 } 65 66 // We order the variables to have predictable output 67 sort.Strings(configStr) 68 content := "[Manager]\n" + strings.Join(configStr, "") 69 dirContent[name] = &osutil.MemoryFileState{ 70 Content: []byte(content), 71 Mode: 0644, 72 } 73 } 74 75 glob := name 76 changed, removed, err := osutil.EnsureDirState(dir, glob, dirContent) 77 if err != nil { 78 return err 79 } 80 81 // something was changed, reexec systemd manager 82 if sysd != nil && (len(changed) > 0 || len(removed) > 0) { 83 return sysd.DaemonReexec() 84 } 85 86 return nil 87 } 88 89 func handleWatchdogConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error { 90 config := map[string]uint{} 91 92 for _, key := range []string{"runtime-timeout", "shutdown-timeout"} { 93 output, err := coreCfg(tr, "watchdog."+key) 94 if err != nil { 95 return err 96 } 97 secs, err := getSystemdConfSeconds(output) 98 if err != nil { 99 return fmt.Errorf("cannot set timer to %q: %v", output, err) 100 } 101 switch key { 102 case "runtime-timeout": 103 config["RuntimeWatchdogSec"] = secs 104 case "shutdown-timeout": 105 config["ShutdownWatchdogSec"] = secs 106 } 107 } 108 109 if err := updateWatchdogConfig(config, opts); err != nil { 110 return err 111 } 112 113 return nil 114 } 115 116 func getSystemdConfSeconds(timeStr string) (uint, error) { 117 if timeStr == "" { 118 return 0, nil 119 } 120 121 dur, err := time.ParseDuration(timeStr) 122 if err != nil { 123 return 0, fmt.Errorf("cannot parse %q: %v", timeStr, err) 124 } 125 if dur < 0 { 126 return 0, fmt.Errorf("cannot use negative duration %q: %v", timeStr, err) 127 } 128 129 return uint(dur.Seconds()), nil 130 } 131 132 func validateWatchdogOptions(tr config.ConfGetter) error { 133 for _, key := range []string{"runtime-timeout", "shutdown-timeout"} { 134 option, err := coreCfg(tr, "watchdog."+key) 135 if err != nil { 136 return err 137 } 138 if _, err = getSystemdConfSeconds(option); err != nil { 139 return err 140 } 141 } 142 143 return nil 144 }