gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/shutdown/shutdown.go (about)

     1  // Copyright 2015-2017 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  // shutdown halts, suspends, or reboots.
     6  //
     7  // Synopsis:
     8  //     shutdown [-h|-r|halt|reboot|suspend]
     9  //
    10  // Description:
    11  //     current operations are reboot (-r), suspend, and halt [-h].
    12  //
    13  // Options:
    14  //     -r|reboot:	reboot the machine.
    15  //     -h|halt:		halt the machine.
    16  //     -s|suspend:	suspend the machine.
    17  package main
    18  
    19  import (
    20  	"log"
    21  	"os"
    22  
    23  	"golang.org/x/sys/unix"
    24  )
    25  
    26  var (
    27  	opcodes = map[string]uint{
    28  		"halt":    unix.LINUX_REBOOT_CMD_POWER_OFF,
    29  		"-h":      unix.LINUX_REBOOT_CMD_POWER_OFF,
    30  		"reboot":  unix.LINUX_REBOOT_CMD_RESTART,
    31  		"-r":      unix.LINUX_REBOOT_CMD_RESTART,
    32  		"suspend": unix.LINUX_REBOOT_CMD_SW_SUSPEND,
    33  		"-s":      unix.LINUX_REBOOT_CMD_SW_SUSPEND,
    34  	}
    35  	reboot = unix.Reboot
    36  )
    37  
    38  func usage() {
    39  	log.Fatalf("shutdown [-h|-r|-s|halt|reboot|suspend] (defaults to halt)")
    40  }
    41  
    42  func main() {
    43  	if len(os.Args) == 1 {
    44  		os.Args = append(os.Args, "halt")
    45  	}
    46  	op, ok := opcodes[os.Args[1]]
    47  	if !ok || len(os.Args) > 2 {
    48  		usage()
    49  	}
    50  	if err := reboot(int(op)); err != nil {
    51  		log.Fatalf(err.Error())
    52  	}
    53  }