forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown.go
53 lines (47 loc) · 1.17 KB
/
shutdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2015-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// shutdown halts, suspends, or reboots.
//
// Synopsis:
// shutdown [-h|-r|halt|reboot|suspend]
//
// Description:
// current operations are reboot (-r), suspend, and halt [-h].
//
// Options:
// -r|reboot: reboot the machine.
// -h|halt: halt the machine.
// -s|suspend: suspend the machine.
package main
import (
"log"
"os"
"golang.org/x/sys/unix"
)
var (
opcodes = map[string]uint{
"halt": unix.LINUX_REBOOT_CMD_POWER_OFF,
"-h": unix.LINUX_REBOOT_CMD_POWER_OFF,
"reboot": unix.LINUX_REBOOT_CMD_RESTART,
"-r": unix.LINUX_REBOOT_CMD_RESTART,
"suspend": unix.LINUX_REBOOT_CMD_SW_SUSPEND,
"-s": unix.LINUX_REBOOT_CMD_SW_SUSPEND,
}
reboot = unix.Reboot
)
func usage() {
log.Fatalf("shutdown [-h|-r|-s|halt|reboot|suspend] (defaults to halt)")
}
func main() {
if len(os.Args) == 1 {
os.Args = append(os.Args, "halt")
}
op, ok := opcodes[os.Args[1]]
if !ok || len(os.Args) > 2 {
usage()
}
if err := reboot(int(op)); err != nil {
log.Fatalf(err.Error())
}
}