github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/recovery/securerecoverer.go (about) 1 // Copyright 2017-2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package recovery 6 7 import ( 8 "log" 9 "math/rand" 10 "syscall" 11 "time" 12 ) 13 14 // DebugTimeout sets the timeout for how long 15 // the debug message is shown before power cycle. 16 const DebugTimeout time.Duration = 10 17 18 // SecureRecoverer properties 19 // Reboot: does a reboot if true 20 // Sync: sync file descriptors and devices 21 // Debug: enables debug messages 22 type SecureRecoverer struct { 23 Reboot bool 24 Sync bool 25 Debug bool 26 RandWait bool 27 } 28 29 // Recover by reboot or poweroff without or with sync 30 func (sr SecureRecoverer) Recover(message string) error { 31 if sr.Sync { 32 syscall.Sync() 33 } 34 35 if sr.Debug { 36 if message != "" { 37 log.SetPrefix("recovery: ") 38 log.Print(message) 39 } 40 time.Sleep(DebugTimeout * time.Second) 41 } 42 43 if sr.RandWait { 44 rd := time.Duration(rand.Intn(15)) 45 time.Sleep(rd * time.Second) 46 log.SetPrefix("recovery: ") 47 log.Printf("Reboot in %s seconds", rd) 48 } 49 50 if sr.Reboot { 51 if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART); err != nil { 52 return err 53 } 54 } else { 55 if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF); err != nil { 56 return err 57 } 58 } 59 60 return nil 61 }