forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown_linux.go
102 lines (91 loc) · 2.41 KB
/
shutdown_linux.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 at a specified time, or immediately.
//
// Synopsis:
//
// shutdown [<-h|-r|-s|halt|reboot|suspend> [time [message...]]]
//
// Description:
//
// current operations are reboot (-r), suspend, and halt [-h].
// If no operation is specified halt is assumed.
// If a time is given, an opcode is not optional.
//
// Options:
//
// -r|reboot: reboot the machine.
// -h|halt: halt the machine.
// -s|suspend: suspend the machine.
//
// Time is specified as "now", +minutes, or RFC3339 format.
// All other arguments past time are printed as a message.
// This could be used, for example, as input to goexpect.
package main
import (
"fmt"
"log"
"os"
"time"
"golang.org/x/sys/unix"
)
const usageMessage = "shutdown [<-h|-r|-s|halt|reboot|suspend> [time [message...]]]"
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,
}
)
// shutdown calls unix.Reboot, with the type of shutdown defined in args, currently
// halt, reboot, or suspend. A time may be specified as "now",
// a future time parseable by time.ParseDuration, or in
// RFC3339 format. If dryrun is chosen, shutdown returns the opcode it
// would have used and an error, if any.
func shutdown(dryrun bool, args ...string) (uint, error) {
if len(args) == 0 {
args = append(args, "halt")
}
op, ok := opcodes[args[0]]
if !ok {
return 0, fmt.Errorf(usageMessage)
}
if len(args) < 2 {
args = append(args, "now")
}
when := time.Now()
switch {
case args[1] == "now":
case args[1][0] == '+':
m, err := time.ParseDuration(args[1][1:] + "m")
if err != nil {
return 0, err
}
when = when.Add(m)
default:
t, err := time.Parse(time.RFC3339, args[1])
if err != nil {
return 0, err
}
when = t
}
// TODO: broadcast args[2:]... via wall or a similar mechanism.
if !dryrun {
time.Sleep(time.Until(when))
}
if !dryrun {
if err := unix.Reboot(int(op)); err != nil {
return 0, err
}
}
return op, nil
}
func main() {
if _, err := shutdown(false, os.Args[1:]...); err != nil {
log.Fatal(err)
}
}