File: names.go

package info (click to toggle)
golang-go-dbus 1~bzr20150122-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: makefile: 13
file content (332 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package dbus

import (
	"errors"
	"log"
	"sync"
)

type nameInfo struct {
	bus          *Connection
	busName      string
	signalWatch  *signalWatch
	lock         sync.Mutex
	currentOwner string
	watches      []*nameWatch
}

type nameWatch struct {
	info *nameInfo
	cb   func(string)
}

func newNameInfo(bus *Connection, busName string) (*nameInfo, error) {
	info := &nameInfo{
		bus:          bus,
		busName:      busName,
		currentOwner: "\x00",
		watches:      []*nameWatch{}}
	watch, err := bus.watchSignal(&MatchRule{
		Type:      TypeSignal,
		Sender:    BUS_DAEMON_NAME,
		Path:      BUS_DAEMON_PATH,
		Interface: BUS_DAEMON_IFACE,
		Member:    "NameOwnerChanged",
		Arg0:      busName}, func(msg *Message) {
		var busName, oldOwner, newOwner string
		if err := msg.Args(&busName, &oldOwner, &newOwner); err != nil {
			log.Println("Could not decode NameOwnerChanged message:", err)
			return
		}
		info.handleOwnerChange(newOwner, false)
	})
	if err != nil {
		return nil, err
	}
	info.signalWatch = watch

	// find the current name owner
	info.checkCurrentOwner()

	return info, nil
}

func (self *nameInfo) checkCurrentOwner() {
	currentOwner, err := self.bus.busProxy.GetNameOwner(self.busName)
	if err != nil {
		if dbusErr, ok := err.(*Error); !ok || dbusErr.Name != "org.freedesktop.DBus.Error.NameHasNoOwner" {
			log.Println("Unexpected error from GetNameOwner:", err)
		}
	}
	self.lock.Lock()
	defer self.lock.Unlock()
	if self.currentOwner == "\x00" {
		// Simulate an ownership change message.
		self.handleOwnerChange(currentOwner, true)
	}
}

func (self *nameInfo) handleOwnerChange(newOwner string, lockAcquired bool) {
	if !lockAcquired {
		self.lock.Lock()
		defer self.lock.Unlock()
	}
	self.currentOwner = newOwner
	for _, watch := range self.watches {
		watch.cb(self.currentOwner)
	}
}

func (p *Connection) ensureNameWatch(busName string, cb func(string)) (watch *nameWatch, err error) {
	p.nameInfoMutex.Lock()
	defer p.nameInfoMutex.Unlock()
	info, ok := p.nameInfo[busName]
	if !ok {
		if info, err = newNameInfo(p, busName); err != nil {
			return
		}
		p.nameInfo[busName] = info
	}

	info.lock.Lock()
	defer info.lock.Unlock()
	watch = &nameWatch{info, cb}
	info.watches = append(info.watches, watch)

	// if the nameInfo already knows the current name owner, tell
	// our callback.
	if info.currentOwner != "\x00" {
		cb(info.currentOwner)
	}
	return
}

func removeNameWatch(watch *nameWatch) error {
	info := watch.info
	info.bus.nameInfoMutex.Lock()
	defer info.bus.nameInfoMutex.Unlock()
	info.lock.Lock()
	defer info.lock.Unlock()

	found := false
	for i, other := range info.watches {
		if other == watch {
			info.watches[i] = info.watches[len(info.watches)-1]
			info.watches = info.watches[:len(info.watches)-1]
			found = true
			break
		}
	}
	if !found {
		return errors.New("NameOwnerWatch already cancelled")
	}
	if len(info.watches) != 0 {
		// There are other watches interested in this name, so
		// leave the nameOwner in place.
		return nil
	}
	delete(info.bus.nameInfo, info.busName)
	return info.signalWatch.cancel()
}

type NameWatch struct {
	watch      *nameWatch
	C          chan string
	cancelLock sync.Mutex
	cancelled  bool
}

func (p *Connection) WatchName(busName string) (watch *NameWatch, err error) {
	watch = &NameWatch{C: make(chan string, 1)}
	nameWatch, err := p.ensureNameWatch(busName, func(owner string) {
		watch.C <- owner
	})
	watch.watch = nameWatch
	return
}

func (watch *NameWatch) Cancel() error {
	watch.cancelLock.Lock()
	defer watch.cancelLock.Unlock()
	if watch.cancelled {
		return nil
	}
	err := removeNameWatch(watch.watch)
	if err != nil {
		return err
	}
	close(watch.C)
	watch.cancelled = true
	return err
}

// BusName acts as a handle for a well known bus name owned by this client.
type BusName struct {
	bus   *Connection
	Name  string
	Flags NameFlags
	C     chan error

	lock         sync.Mutex
	cancelled    bool
	needsRelease bool

	acquiredWatch *SignalWatch
	lostWatch     *SignalWatch
}

type NameFlags uint32

const (
	NameFlagAllowReplacement NameFlags = 1 << iota
	NameFlagReplaceExisting
	NameFlagDoNotQueue
)

var (
	ErrNameLost         = errors.New("name ownership lost")
	ErrNameInQueue      = errors.New("in queue for name ownership")
	ErrNameExists       = errors.New("name exists")
	ErrNameAlreadyOwned = errors.New("name already owned")
)

// RequestName requests ownership of a well known bus name.
//
// Name ownership is communicated over the the BusName's channel: a
// nil value indicates that the name was successfully acquired, and a
// non-nil value indicates that the name was lost or could not be
// acquired.
func (p *Connection) RequestName(busName string, flags NameFlags) *BusName {
	name := &BusName{
		bus:   p,
		Name:  busName,
		Flags: flags,
		C:     make(chan error, 1)}
	go name.request()
	return name
}

func (name *BusName) request() {
	name.lock.Lock()
	defer name.lock.Unlock()
	if name.cancelled {
		return
	}

	if !name.cancelled {
		watch, err := name.bus.WatchSignal(&MatchRule{
			Type:      TypeSignal,
			Sender:    BUS_DAEMON_NAME,
			Path:      BUS_DAEMON_PATH,
			Interface: BUS_DAEMON_IFACE,
			Member:    "NameLost",
			Arg0:      name.Name})
		if err != nil {
			log.Println("Could not set up NameLost signal watch")
			name.Release()
			return
		}
		name.lostWatch = watch
		go func() {
			for _ = range name.lostWatch.C {
				name.lock.Lock()
				defer name.lock.Unlock()
				name.C <- ErrNameLost
				name.release(false)
				break
			}
		}()

		watch, err = name.bus.WatchSignal(&MatchRule{
			Type:      TypeSignal,
			Sender:    BUS_DAEMON_NAME,
			Path:      BUS_DAEMON_PATH,
			Interface: BUS_DAEMON_IFACE,
			Member:    "NameAcquired",
			Arg0:      name.Name})
		if err != nil {
			log.Println("Could not set up NameLost signal watch")
			name.Release()
			return
		}
		name.acquiredWatch = watch
		go func() {
			for _ = range name.acquiredWatch.C {
				name.C <- nil
			}
		}()

		// XXX: if we disconnect from the bus, we should
		// report the name being lost.
	}

	result, err := name.bus.busProxy.RequestName(name.Name, uint32(name.Flags))
	if err != nil {
		log.Println("Error requesting bus name", name.Name, "err =", err)
		return
	}
	switch result {
	case 1:
		// DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
		name.needsRelease = true
	case 2:
		// DBUS_REQUEST_NAME_REPLY_IN_QUEUE
		name.needsRelease = true
		name.C <- ErrNameInQueue
	case 3:
		// DBUS_REQUEST_NAME_REPLY_EXISTS
		name.C <- ErrNameExists
		name.release(false)
	case 4:
		// DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
		name.C <- ErrNameAlreadyOwned
		name.release(false)
	default:
		// assume that other responses mean we couldn't own
		// the name
		name.C <- errors.New("Unknown error")
		name.release(false)
	}
}

func (name *BusName) checkNeedsRelease() bool {
	name.lock.Lock()
	defer name.lock.Unlock()
	return name.needsRelease
}

// Release releases well known name on the message bus.
func (name *BusName) Release() error {
	name.lock.Lock()
	defer name.lock.Unlock()
	return name.release(name.needsRelease)
}

func (name *BusName) release(needsRelease bool) error {
	if name.cancelled {
		return nil
	}
	if name.acquiredWatch != nil {
		if err := name.acquiredWatch.Cancel(); err != nil {
			return err
		}
	}
	if name.lostWatch != nil {
		if err := name.lostWatch.Cancel(); err != nil {
			return err
		}
	}
	if needsRelease {
		result, err := name.bus.busProxy.ReleaseName(name.Name)
		if err != nil {
			return err
		}
		if result != 1 { // DBUS_RELEASE_NAME_REPLY_RELEASED
			log.Println("Unexpected result when releasing name", name.Name, "result =", result)
		}
		name.needsRelease = false
	}
	close(name.C)
	name.cancelled = true
	return nil
}