-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbus.go
180 lines (155 loc) · 3.93 KB
/
dbus.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
package objtree
import (
"github.com/godbus/dbus"
"strings"
"sync"
"sync/atomic"
)
const (
fdtDBusName = "org.freedesktop.DBus"
fdtAddMatch = fdtDBusName + ".AddMatch"
fdtRemoveMatch = fdtDBusName + ".RemoveMatch"
fdtIntrospectable = fdtDBusName + ".Introspectable"
fdtPeer = fdtDBusName + ".Peer"
)
// Acts as a root to the object tree
type BusManager struct {
*Object
conn *dbus.Conn
state *mgrState
}
func NewAnonymousBusManager(
busfn func(dbus.Handler, dbus.SignalHandler) (*dbus.Conn, error),
) (*BusManager, error) {
state := &mgrState{sigref: make(map[string]uint64)}
handler := &BusManager{
Object: newObjectFromImpl("", nil, nil, nil),
state: state,
}
handler.bus = handler
conn, err := busfn(handler, handler)
if err != nil {
return nil, err
}
err = conn.Auth(nil)
if err != nil {
conn.Close()
return nil, err
}
err = conn.Hello()
if err != nil {
conn.Close()
return nil, err
}
handler.conn = conn
return handler, nil
}
func NewBusManager(
busfn func(dbus.Handler, dbus.SignalHandler) (*dbus.Conn, error),
name string,
) (*BusManager, error) {
handler, err := NewAnonymousBusManager(busfn)
if err != nil {
return nil, err
}
err = handler.RequestName(name)
if err != nil {
handler.conn.Close()
return nil, err
}
return handler, nil
}
func NewSessionBusManager(name string) (*BusManager, error) {
return NewBusManager(dbus.SessionBusPrivateHandler, name)
}
func NewAnonymousSessionBusManager() (*BusManager, error) {
return NewAnonymousBusManager(dbus.SessionBusPrivateHandler)
}
func NewSystemBusManager(name string) (*BusManager, error) {
return NewBusManager(dbus.SystemBusPrivateHandler, name)
}
func NewAnonymousSystemBusManager() (*BusManager, error) {
return NewAnonymousBusManager(dbus.SystemBusPrivateHandler)
}
func (mgr *BusManager) Conn() *dbus.Conn {
return mgr.conn
}
func (mgr *BusManager) RequestName(name string) error {
_, err := mgr.conn.RequestName(name, 0)
if err != nil {
return err
}
return nil
}
func (mgr *BusManager) LookupObject(path dbus.ObjectPath) (dbus.ServerObject, bool) {
if string(path) == "/" {
return mgr, true
}
ps := strings.Split(string(path), "/")
if ps[0] == "" {
ps = ps[1:]
}
return mgr.lookupObjectPath(ps)
}
func (mgr *BusManager) Call(
path dbus.ObjectPath,
ifaceName string,
method string,
args ...interface{},
) ([]interface{}, error) {
object, ok := mgr.LookupObject(path)
if !ok {
return nil, dbus.ErrMsgNoObject
}
return object.(*Object).Call(ifaceName, method, args...)
}
func (mgr *BusManager) DeliverSignal(iface, member string, signal *dbus.Signal) {
objects := mgr.objects.Load().(map[string]*Object)
for _, obj := range objects {
obj.DeliverSignal(iface, member, signal)
}
}
type multiWriterValue struct {
value atomic.Value
writelk sync.Mutex
}
func (value *multiWriterValue) Load() interface{} {
return value.value.Load()
}
func (value *multiWriterValue) Update(fn func(interface{}) interface{}) {
value.writelk.Lock()
value.value.Store(fn(value.value.Load()))
value.writelk.Unlock()
}
type mgrState struct {
mu sync.Mutex
sigref map[string]uint64
}
func mkSignalKey(iface, member string) string {
return iface + "." + member
}
func (s *mgrState) AddMatchSignal(conn *dbus.Conn, iface, member string) {
// Only register for signal if not already registered
s.mu.Lock()
defer s.mu.Unlock()
key := mkSignalKey(iface, member)
if s.sigref[key] == 0 {
conn.BusObject().Call(fdtAddMatch, 0,
"type='signal',interface='"+iface+"',member='"+member+"'")
}
s.sigref[key] = s.sigref[key] + 1
}
func (s *mgrState) RemoveMatchSignal(conn *dbus.Conn, iface, member string) {
// Only deregister if this is the last request
s.mu.Lock()
defer s.mu.Unlock()
key := mkSignalKey(iface, member)
if s.sigref[key] == 0 {
return
}
s.sigref[key] = s.sigref[key] - 1
if s.sigref[key] == 0 {
conn.BusObject().Call(fdtRemoveMatch, 0,
"type='signal',interface='"+iface+"',member='"+member+"'")
}
}