File: dbus-name-cache.c

package info (click to toggle)
iwd 1.14-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,888 kB
  • sloc: ansic: 113,786; sh: 4,325; makefile: 601
file content (313 lines) | stat: -rw-r--r-- 6,795 bytes parent folder | download | duplicates (4)
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
/*
 *
 *  Embedded Linux library
 *
 *  Copyright (C) 2016  Intel Corporation. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>

#include "util.h"
#include "hashmap.h"
#include "idle.h"
#include "dbus.h"
#include "dbus-private.h"

struct _dbus_name_cache {
	struct l_dbus *bus;
	struct l_hashmap *names;
	const struct _dbus_name_ops *driver;
	unsigned int last_watch_id;
	struct l_idle *watch_remove_work;
};

struct service_watch {
	l_dbus_watch_func_t connect_func;
	l_dbus_watch_func_t disconnect_func;
	l_dbus_destroy_func_t destroy;
	void *user_data;
	unsigned int id;
	struct service_watch *next;
};

struct name_cache_entry {
	int ref_count;
	char *unique_name;
	struct service_watch *watches;
};

struct _dbus_name_cache *_dbus_name_cache_new(struct l_dbus *bus,
					const struct _dbus_name_ops *driver)
{
	struct _dbus_name_cache *cache;

	cache = l_new(struct _dbus_name_cache, 1);

	cache->bus = bus;
	cache->driver = driver;

	return cache;
}

static void service_watch_destroy(void *data)
{
	struct service_watch *watch = data;

	if (watch->destroy)
		watch->destroy(watch->user_data);

	l_free(watch);
}

static void name_cache_entry_destroy(void *data)
{
	struct name_cache_entry *entry = data;
	struct service_watch *watch;

	while (entry->watches) {
		watch = entry->watches;
		entry->watches = watch->next;

		service_watch_destroy(watch);
	}

	l_free(entry->unique_name);

	l_free(entry);
}

void _dbus_name_cache_free(struct _dbus_name_cache *cache)
{
	if (!cache)
		return;

	if (cache->watch_remove_work)
		l_idle_remove(cache->watch_remove_work);

	l_hashmap_destroy(cache->names, name_cache_entry_destroy);

	l_free(cache);
}

bool _dbus_name_cache_add(struct _dbus_name_cache *cache, const char *name)
{
	struct name_cache_entry *entry;

	if (!_dbus_valid_bus_name(name))
		return false;

	if (!cache->names)
		cache->names = l_hashmap_string_new();

	entry = l_hashmap_lookup(cache->names, name);

	if (!entry) {
		entry = l_new(struct name_cache_entry, 1);

		l_hashmap_insert(cache->names, name, entry);

		cache->driver->get_name_owner(cache->bus, name);
	}

	entry->ref_count++;

	return true;
}

bool _dbus_name_cache_remove(struct _dbus_name_cache *cache, const char *name)
{
	struct name_cache_entry *entry;

	entry = l_hashmap_lookup(cache->names, name);

	if (!entry)
		return false;

	if (--entry->ref_count)
		return true;

	l_hashmap_remove(cache->names, name);

	name_cache_entry_destroy(entry);

	return true;
}

const char *_dbus_name_cache_lookup(struct _dbus_name_cache *cache,
					const char *name)
{
	struct name_cache_entry *entry;

	entry = l_hashmap_lookup(cache->names, name);

	if (!entry)
		return NULL;

	return entry->unique_name;
}

void _dbus_name_cache_notify(struct _dbus_name_cache *cache,
				const char *name, const char *owner)
{
	struct name_cache_entry *entry;
	struct service_watch *watch;
	bool prev_connected, connected;

	if (!cache)
		return;

	entry = l_hashmap_lookup(cache->names, name);

	if (!entry)
		return;

	prev_connected = !!entry->unique_name;
	connected = owner && *owner != '\0';

	l_free(entry->unique_name);

	entry->unique_name = connected ? l_strdup(owner) : NULL;

	/*
	 * This check also means we notify all watchers who have a connected
	 * callback when we first learn that the service is in fact connected.
	 */
	if (connected == prev_connected)
		return;

	for (watch = entry->watches; watch; watch = watch->next)
		if (connected && watch->connect_func)
			watch->connect_func(cache->bus, watch->user_data);
		else if (!connected && watch->disconnect_func)
			watch->disconnect_func(cache->bus, watch->user_data);
}

unsigned int _dbus_name_cache_add_watch(struct _dbus_name_cache *cache,
					const char *name,
					l_dbus_watch_func_t connect_func,
					l_dbus_watch_func_t disconnect_func,
					void *user_data,
					l_dbus_destroy_func_t destroy)
{
	struct name_cache_entry *entry;
	struct service_watch *watch;

	if (!_dbus_name_cache_add(cache, name))
		return 0;

	watch = l_new(struct service_watch, 1);
	watch->id = ++cache->last_watch_id;
	watch->connect_func = connect_func;
	watch->disconnect_func = disconnect_func;
	watch->user_data = user_data;
	watch->destroy = destroy;

	entry = l_hashmap_lookup(cache->names, name);

	watch->next = entry->watches;
	entry->watches = watch;

	if (entry->unique_name && connect_func)
		watch->connect_func(cache->bus, watch->user_data);

	return watch->id;
}

static bool service_watch_remove(const void *key, void *value, void *user_data)
{
	struct name_cache_entry *entry = value;
	struct service_watch **watch, *tmp;

	for (watch = &entry->watches; *watch;) {
		if ((*watch)->id) {
			watch = &(*watch)->next;
			continue;
		}

		tmp = *watch;
		*watch = tmp->next;

		service_watch_destroy(tmp);

		entry->ref_count--;
	}

	if (entry->ref_count)
		return false;

	name_cache_entry_destroy(entry);

	return true;
}

static void service_watch_remove_all(struct l_idle *idle, void *user_data)
{
	struct _dbus_name_cache *cache = user_data;

	l_idle_remove(cache->watch_remove_work);
	cache->watch_remove_work = NULL;

	l_hashmap_foreach_remove(cache->names, service_watch_remove, cache);
}

static void service_watch_mark(const void *key, void *value, void *user_data)
{
	struct name_cache_entry *entry = value;
	struct service_watch *watch;
	unsigned int *id = user_data;

	if (!*id)
		return;

	for (watch = entry->watches; watch; watch = watch->next)
		if (watch->id == *id) {
			watch->id = 0;
			watch->connect_func = NULL;
			watch->disconnect_func = NULL;

			if (watch->destroy) {
				watch->destroy(watch->user_data);
				watch->destroy = NULL;
			}

			*id = 0;
			break;
		}
}

bool _dbus_name_cache_remove_watch(struct _dbus_name_cache *cache,
					unsigned int id)
{
	l_hashmap_foreach(cache->names, service_watch_mark, &id);

	if (id)
		return false;

	if (!cache->watch_remove_work)
		cache->watch_remove_work = l_idle_create(
						service_watch_remove_all,
						cache, NULL);

	return true;
}