-
Notifications
You must be signed in to change notification settings - Fork 21
/
daemon.go
162 lines (135 loc) · 3.61 KB
/
daemon.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"errors"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/charlie0129/batt/smc"
)
var (
smcConn *smc.Connection
unixSocketPath = "/var/run/batt.sock"
)
func setupRoutes() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
router.Use(ginLogger(logrus.StandardLogger()))
router.GET("/config", getConfig)
router.PUT("/config", setConfig) // Should not be called by user.
router.GET("/limit", getLimit)
router.PUT("/limit", setLimit)
router.PUT("/lower-limit-delta", setLowerLimitDelta)
router.PUT("/prevent-idle-sleep", setPreventIdleSleep)
router.PUT("/disable-charging-pre-sleep", setDisableChargingPreSleep)
router.PUT("/adapter", setAdapter)
router.GET("/adapter", getAdapter)
router.GET("/charging", getCharging)
router.GET("/battery-info", getBatteryInfo)
router.PUT("/magsafe-led", setControlMagSafeLED)
router.GET("/current-charge", getCurrentCharge)
router.GET("/plugged-in", getPluggedIn)
return router
}
var (
alwaysAllowNonRootAccess = false
)
func runDaemon() {
router := setupRoutes()
err := loadConfig()
if err != nil {
logrus.Fatal(err)
}
logrus.Infof("config loaded: %#v", config)
if alwaysAllowNonRootAccess {
config.AllowNonRootAccess = true
logrus.Info("alwaysAllowNonRootAccess is set to true, allowing non-root access")
}
// Receive SIGHUP to reload config
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGHUP)
for range sigc {
err := loadConfig()
if err != nil {
logrus.Errorf("failed to reload config: %v", err)
}
logrus.Infof("config reloaded: %#v", config)
}
}()
srv := &http.Server{
Handler: router,
}
// Create the socket to listen on:
l, err := net.Listen("unix", unixSocketPath)
if err != nil {
logrus.Fatal(err)
return
}
if config.AllowNonRootAccess {
logrus.Infof("non-root access is allowed, chaning permissions of %s to 0777", unixSocketPath)
err = os.Chmod(unixSocketPath, 0777)
if err != nil {
logrus.Fatal(err)
return
}
}
// Serve HTTP on unix socket
go func() {
logrus.Infof("http server listening on %s", l.Addr().String())
if err := srv.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.Fatal(err)
}
}()
// Listen to system sleep notifications.
go func() {
err := listenNotifications()
if err != nil {
logrus.Errorf("failed to listen to system sleep notifications: %v", err)
os.Exit(1)
}
}()
// Open Apple SMC for read/writing
smcConn = smc.New()
if err := smcConn.Open(); err != nil {
logrus.Fatal(err)
}
go func() {
logrus.Debugln("main loop starts")
infiniteLoop()
logrus.Errorf("main loop exited unexpectedly")
}()
// Handle common process-killing signals, so we can gracefully shut down:
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
// Wait for a SIGINT or SIGTERM:
sig := <-sigc
logrus.Infof("caught signal \"%s\": shutting down.", sig)
logrus.Info("shutting down http server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err = srv.Shutdown(ctx)
if err != nil {
logrus.Errorf("failed to shutdown http server: %v", err)
}
cancel()
logrus.Info("stopping listening notifications")
stopListeningNotifications()
logrus.Info("closing smc connection")
err = smcConn.Close()
if err != nil {
logrus.Errorf("failed to close smc connection: %v", err)
}
logrus.Info("saving config")
err = saveConfig()
if err != nil {
logrus.Errorf("failed to save config: %v", err)
}
logrus.Info("exiting")
os.Exit(0)
}