github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/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  	"syscall"
    10  	"time"
    11  )
    12  
    13  // DebugTimeout sets the timeout for how long
    14  // the debug message is shown before power cycle.
    15  const DebugTimeout time.Duration = 10
    16  
    17  // SecureRecoverer properties
    18  // Reboot: does a reboot if true
    19  // Sync: sync file descriptors and devices
    20  // Debug: enables debug messages
    21  type SecureRecoverer struct {
    22  	Reboot bool
    23  	Sync   bool
    24  	Debug  bool
    25  }
    26  
    27  // Recover by reboot or poweroff without or with sync
    28  func (sr SecureRecoverer) Recover(message string) error {
    29  	if sr.Sync {
    30  		syscall.Sync()
    31  	}
    32  
    33  	if sr.Debug {
    34  		if message != "" {
    35  			log.Print(message)
    36  		}
    37  		time.Sleep(DebugTimeout * time.Second)
    38  	}
    39  
    40  	if sr.Reboot {
    41  		if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART); err != nil {
    42  			return err
    43  		}
    44  	} else {
    45  		if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }