github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/cmds/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 or reboots. 6 // 7 // Synopsis: 8 // shutdown [-dryrun] [operation] 9 // 10 // Description: 11 // shutdown will either do or simulate the operation. 12 // current operations are reboot and halt. 13 // 14 // Options: 15 // -dryrun: do not do really do it. 16 package main 17 18 import ( 19 "flag" 20 "log" 21 "os" 22 23 "golang.org/x/sys/unix" 24 ) 25 26 var ( 27 dryrun = flag.Bool("dryrun", false, "Do not do reboot system calls") 28 op = "reboot" 29 opcodes = map[string]uint32{ 30 "halt": unix.LINUX_REBOOT_CMD_POWER_OFF, 31 "reboot": unix.LINUX_REBOOT_CMD_RESTART, 32 "suspend": unix.LINUX_REBOOT_CMD_SW_SUSPEND, 33 } 34 ) 35 36 func usage() { 37 log.Fatalf("shutdown [-dryrun] [halt|reboot|suspend] (defaults to reboot)") 38 } 39 40 func main() { 41 flag.Parse() 42 switch len(flag.Args()) { 43 default: 44 usage() 45 case 0: 46 case 1: 47 op = flag.Args()[0] 48 } 49 50 f, ok := opcodes[op] 51 if !ok { 52 usage() 53 } 54 55 if *dryrun { 56 log.Printf("unix.Reboot(0x%x)", f) 57 os.Exit(0) 58 } 59 60 if err := unix.Reboot(int(f)); err != nil { 61 log.Fatalf(err.Error()) 62 } 63 }