-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
295 lines (265 loc) · 8.87 KB
/
event.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2013, 2022, 2024 Vadim Vygonets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"errors"
"log"
"os/exec"
"time"
dbus "github.com/godbus/dbus/v5"
)
const (
defaultTimeout = 5 * time.Second // default max inhibit time
defaultDelay = 500 * time.Millisecond // default delay after command
)
var ErrDBusSignal = errors.New("invalid D-Bus signal")
/*
Backend is the interface for backends.
The Handle method handles a received signal, returning a flag
indicating whether the signal is a sleep signal, and an error.
If the flag is true, the error must be nil. For signals other
than sleep and wakeup, (false, ErrDBusSignal) must be returned.
The Release method is called after the sleep preparation is
complete, in order to release the sleep inhibit lock, if one is
taken.
The MaxInhibit method returns the current maximum inhibit delay.
If the query is not supported, the returned Duration must be -1.
*/
type Backend interface {
Name() string // return backend name
Filter() string // return string for DBus.AddMatch
Handle(*dbus.Signal) (bool, error) // handle signal
Release() error // release sleep inhibit lock
MaxInhibit() (time.Duration, error) // return maximum inhibit delay
}
// newBackend returns a Backend, or nil if none is available.
func newBackend(conn *dbus.Conn) Backend {
var be Backend
for _, f := range []func(*dbus.Conn) Backend{
NewSystemdBackend,
NewUPowerBackend,
} {
if be = f(conn); be != nil {
break
}
}
return be
}
// openConn initialises the D-Bus connection and returns backend
// and signal channel.
func openConn() (Backend, chan *dbus.Signal) {
if conf.debug {
debugln("using backend debug")
return newDebugBackend()
}
conn, err := dbus.SystemBus()
if err != nil {
log.Fatalln("connect to D-Bus system bus:", err)
}
be := newBackend(conn)
if be == nil {
log.Fatalln("no backend available")
}
debugln("using backend", be.Name(), "with filter", be.Filter())
const add = "org.freedesktop.DBus.AddMatch"
if err := conn.BusObject().Call(add, 0, be.Filter()).Err; err != nil {
log.Fatalln("add signal filter:", err)
}
sc := make(chan *dbus.Signal, 4)
conn.Signal(sc)
return be, sc
}
func wait(cmd *exec.Cmd, stopped chan<- error) {
stopped <- cmd.Wait()
}
// _run starts the command, returning an error if it cannot be
// started. If the error is nil, the wait status will be sent to
// stopped upon termination.
func _run(stopped chan<- error) error {
cmd := exec.Command(conf.cmd[0], conf.cmd[1:]...)
err := cmd.Start()
if err == nil {
go wait(cmd, stopped)
}
return err
}
var run = _run
// setTimeout sets *timeout according to the maximum inhibit
// delay max. max is reduced by a safety margin of 1/16. In
// background mode max is then capped to conf.delay.
func setTimeout(timeout *time.Duration, max time.Duration) {
max -= max >> 4 // safety margin of 1/16 of maximum inhibit delay
if conf.bg && max > conf.delay {
max = conf.delay
}
if max != *timeout {
*timeout = max
debugln("timeout =", max)
}
}
func updateTimeout(timeout *time.Duration, be Backend) {
if max, err := be.MaxInhibit(); err != nil {
logln(be.Name()+".MaxInhibit:", err)
} else if max >= 0 {
setTimeout(timeout, max)
}
}
/*
loop initialises D-Bus connection and a Backend and runs the
event loop.
The event loop reacts to sleep and wakeup D-Bus signals, command
termination (exited or killed) and release timer expiring,
tracking state represented by two boolean variables:
- running, indicating whether the command is running;
- locked, indicating whether the release timer is running.
After sleep signal is received, the command is run if it's not
already running, and the release timer is started, stopped and
restarted as needed. When the release timer expires, the backend
Release method is called, allowing the system to sleep.
The systemd backend takes a sleep inhibit lock at start and when
wakeup signal is received. Any old lock held is released prior
to that. If inhibiting fails, no state transition or action is
performed. Its Release method releases the lock.
The UPower backend doesn't support wakeup signals and inhibiting
sleep. Its Release method is a no-op.
State transitions and actions. Empty: no action beyond state
change; "-": event does not occur in state.
R=running, L=locked, T=true, f=false.
+-----------------------+---------+-----------------------+
| | | initial state (R,L) |
| | state +-----+-----+-----+-----+
| event received | change | f,f | f,T | T,f | T,T |
+-----------------------+---------+-----+-----+-----+-----+
| sleep, exec ok | R=T L=T | [a] | [a] | - | - |
| sleep, exec failed | L=T | [b] | [b] | - | - |
| sleep (no exec) | L=T | - | - | [b] | |
| wakeup, inhibit ok | L=f | | [c] | | [c] |
| release timer expired | L=f | - | [d] | - | [d] |
| command terminated | R=f | - | - | | [e] |
+-----------------------+---------+-----+-----+-----+-----+
[a] set release timer to timeout and deadline to now+timeout.
[b] set release timer to expire immediately.
[c] stop release timer.
[d] release sleep inhibit lock.
[e] in foreground mode, set release timer: if exit 0,
to delay or until deadline, whichever is earlier;
if exit non-zero or killed, to expire immediately.
*/
func loop() {
be, sc := openConn()
var (
locked bool // sleep actively inhibited
running bool // command is running
start time.Time // command start time
stopped = make(chan error) // command status channel
timeout = conf.delay // inhibit release timeout
release = time.NewTimer(time.Hour) // inhibit release timer
)
release.Stop()
// The the effective timeout is capped to the maximum
// inhibit delay minus a safety margin to account for
// code runtime. It is initially set according to the
// default inhibit delay. With systemd backend the
// current maximum inhibit delay is queried and timeout
// is adjusted after executing the command.
setTimeout(&timeout, defaultTimeout)
for {
select {
case sig := <-sc:
debugln("signal received:", sig)
if sleep, err := be.Handle(sig); err != nil {
// wake-up signal but Inhibit failed,
// or unknown signal
logln(be.Name()+".Handle:", err)
break
} else if !sleep {
debugln("wakeup")
// Wake-up signal means that the old sleep
// inhibit lock was released and a new one
// taken. If the release timer if running,
// stop it to avoid releasing the new lock.
if locked {
if !release.Stop() {
<-release.C
}
locked = false
}
break
}
// handling sleep signal
if running {
logln("exec: already running")
// if previous timeouts/delays are active,
// keep waiting, otherwise release immediately
if !locked {
locked = true
release.Reset(0)
}
break
}
if !conf.bg {
start = time.Now()
}
if locked && !release.Stop() {
<-release.C
}
locked = true
debugln("running command")
if err := run(stopped); err != nil {
// execution failed, release immediately
logln(err)
release.Reset(0)
break
}
running = true
// release after timeout
updateTimeout(&timeout, be)
release.Reset(timeout)
case <-release.C:
locked = false
if running && !conf.bg {
logln("command timed out, consider using -b")
}
debugln("releasing inhibit lock")
if err := be.Release(); err != nil {
logln(be.Name()+".Release:", err)
}
case err := <-stopped:
running = false
if err != nil {
logln("wait:", err)
}
debugln("command finished")
if locked && !conf.bg {
// foreground, finished before timeout
if !release.Stop() {
<-release.C
}
// if command exited with status 0, release
// after delay or at deadline, whichever is
// earlier; otherwise release immediately.
delay := time.Duration(0)
if err == nil {
delay = timeout - time.Since(start)
if delay > conf.delay {
delay = conf.delay
}
}
release.Reset(delay)
}
}
}
}