-
Notifications
You must be signed in to change notification settings - Fork 458
/
acpi.go
105 lines (85 loc) · 2.13 KB
/
acpi.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package acpi
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
)
const (
// PowerButtonEvent is the ACPI event name associated with the power off
// button.
PowerButtonEvent = "button/power"
// See https://github.com/torvalds/linux/blob/master/drivers/acpi/event.c
acpiGenlFamilyName = "acpi_event"
acpiGenlMcastGroupName = "acpi_mc_group"
)
// StartACPIListener starts listening for ACPI netlink events.
//
//nolint: gocyclo
func StartACPIListener() (err error) {
// Get the acpi_event family.
conn, err := genetlink.Dial(nil)
if err != nil {
return err
}
f, err := conn.GetFamily(acpiGenlFamilyName)
if errors.Is(err, os.ErrNotExist) {
// nolint: errcheck
conn.Close()
return fmt.Errorf(acpiGenlFamilyName+" not available: %w", err)
}
var id uint32
for _, group := range f.Groups {
if group.Name == acpiGenlMcastGroupName {
id = group.ID
}
}
if err = conn.JoinGroup(id); err != nil {
// nolint: errcheck
conn.Close()
return err
}
// nolint: errcheck
defer conn.Close()
for {
msgs, _, err := conn.Receive()
if err != nil {
return fmt.Errorf("error reading from ACPI channel: %w", err)
}
if len(msgs) > 0 {
ok, err := parse(msgs, PowerButtonEvent)
if err != nil {
log.Printf("failed to parse netlink message: %v", err)
continue
}
if !ok {
continue
}
return nil
}
}
}
func parse(msgs []genetlink.Message, event string) (bool, error) {
var result *multierror.Error
for _, msg := range msgs {
ad, err := netlink.NewAttributeDecoder(msg.Data)
if err != nil {
result = multierror.Append(result, fmt.Errorf("failed to create attribute decoder: %w", err))
continue
}
for ad.Next() {
if strings.HasPrefix(ad.String(), event) {
return true, nil
}
log.Printf("ignoring ACPI event: %q", ad.String())
}
}
return false, result.ErrorOrNil()
}