forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.go
118 lines (94 loc) · 2.55 KB
/
power.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package toolbox
import (
"fmt"
"log"
"os/exec"
)
// GuestOsState enum as defined in open-vm-tools/lib/include/vmware/guestrpc/powerops.h
const (
_ = iota
powerStateHalt
powerStateReboot
powerStatePowerOn
powerStateResume
powerStateSuspend
)
var (
shutdown = "/sbin/shutdown"
)
type PowerCommand struct {
Handler func() error
out *ChannelOut
state int
name string
}
type PowerCommandHandler struct {
Halt PowerCommand
Reboot PowerCommand
PowerOn PowerCommand
Resume PowerCommand
Suspend PowerCommand
}
func registerPowerCommandHandler(service *Service) *PowerCommandHandler {
handler := new(PowerCommandHandler)
handlers := map[string]struct {
cmd *PowerCommand
state int
}{
"OS_Halt": {&handler.Halt, powerStateHalt},
"OS_Reboot": {&handler.Reboot, powerStateReboot},
"OS_PowerOn": {&handler.PowerOn, powerStatePowerOn},
"OS_Resume": {&handler.Resume, powerStateResume},
"OS_Suspend": {&handler.Suspend, powerStateSuspend},
}
for name, h := range handlers {
*h.cmd = PowerCommand{
name: name,
state: h.state,
out: service.out,
}
service.RegisterHandler(name, h.cmd.Dispatch)
}
return handler
}
func (c *PowerCommand) Dispatch([]byte) ([]byte, error) {
rc := rpciOK
log.Printf("dispatching power op %q", c.name)
if c.Handler == nil {
if c.state == powerStateHalt || c.state == powerStateReboot {
rc = rpciERR
}
}
msg := fmt.Sprintf("tools.os.statechange.status %s%d\x00", rc, c.state)
if _, err := c.out.Request([]byte(msg)); err != nil {
log.Printf("unable to send %q: %q", msg, err)
}
if c.Handler != nil {
if err := c.Handler(); err != nil {
log.Printf("%s: %s", c.name, err)
}
}
return nil, nil
}
func Halt() error {
log.Printf("Halting system...")
// #nosec: Subprocess launching with variable
return exec.Command(shutdown, "-h", "now").Run()
}
func Reboot() error {
log.Printf("Rebooting system...")
// #nosec: Subprocess launching with variable
return exec.Command(shutdown, "-r", "now").Run()
}