Debian Codegraph @

src/bus/name.c

raw
1 /*
2 * Name Registry
3 */
4
5 #include <c-list.h>
6 #include <c-rbtree.h>
7 #include <c-stdaux.h>
8 #include <stdlib.h>
9 #include "bus/activation.h"
10 #include "bus/name.h"
11 #include "dbus/protocol.h"
12 #include "dbus/socket.h"
13 #include "util/error.h"
14 #include "util/misc.h"
15 #include "util/user.h"
16
17 /**
18 * name_change_init() - initialize notification
19 * @change: object to operate on
20 *
21 * Initialize a name-change notification object. All fields are cleared and set
22 * to their default values.
23 */
24 void name_change_init(NameChange *change) {
25 *change = (NameChange)NAME_CHANGE_INIT;
26 }
27
28 /**
29 * name_change_deinit() - deinitialize notification
30 * @change: object to operate on
31 *
32 * Clear a notification object and drop all references. The object is reset to
33 * its default state, as if name_change_init() was called afterwards.
34 */
35 void name_change_deinit(NameChange *change) {
36 change->name = name_unref(change->name);
37 *change = (NameChange)NAME_CHANGE_INIT;
38 }
39
40 static int name_ownership_compare(CRBTree *tree, void *k, CRBNode *rb) {
41 NameOwnership *ownership = c_container_of(rb, NameOwnership, owner_node);
42
43 if ((Name *)k < ownership->name)
44 return -1;
45 if ((Name *)k > ownership->name)
46 return 1;
47
48 return 0;
49 }
50
51 static void name_ownership_link(NameOwnership *ownership, CRBNode *parent, CRBNode **slot) {
52 c_assert(!c_rbnode_is_linked(&ownership->owner_node));
53
54 c_rbtree_add(&ownership->owner->ownership_tree, parent, slot, &ownership->owner_node);
55 }
56
57 static NameOwnership *name_ownership_free(NameOwnership *ownership) {
58 if (!ownership)
59 return NULL;
60
61 c_assert(!c_list_is_linked(&ownership->name_link));
62
63 user_charge_deinit(&ownership->charge);
64 c_rbnode_unlink(&ownership->owner_node);
65 name_unref(ownership->name);
66 free(ownership);
67
68 return NULL;
69 }
70
71 C_DEFINE_CLEANUP(NameOwnership *, name_ownership_free);
72
73 static int name_ownership_new(NameOwnership **ownershipp, NameOwner *owner, User *user, Name *name) {
74 _c_cleanup_(name_ownership_freep) NameOwnership *ownership = NULL;
75 int r;
76
77 ownership = calloc(1, sizeof(*ownership));
78 if (!ownership)
79 return error_origin(-ENOMEM);
80
81 *ownership = (NameOwnership)NAME_OWNERSHIP_NULL(*ownership);
82 ownership->owner = owner;
83 ownership->name = name_ref(name);
84
85 r = user_charge(user, &ownership->charge, NULL, USER_SLOT_OBJECTS, 1);
86 if (r)
87 return (r == USER_E_QUOTA) ? NAME_E_QUOTA : error_fold(r);
88
89 *ownershipp = ownership;
90 ownership = NULL;
91 return 0;
92 }
93
94 /**
95 * name_ownership_is_primary() - check primary state
96 * @ownership: object to operate on
97 *
98 * This checks whether the given ownership is the primary one.
99 *
100 * Return: True if @ownership is primary, false otherwise.
101 */
102 bool name_ownership_is_primary(NameOwnership *ownership) {
103 return ownership == name_primary(ownership->name);
104 }
105
106 /**
107 * name_ownership_release() - release name ownership
108 * @ownership: object to operate on
109 * @change: notification object
110 *
111 * This releases and destroys the name-ownership object @ownership. This is
112 * meant as replacement for name_ownership_free(). Releasing name ownership
113 * might require the caller to trigger notifications. Hence, rather than calling
114 * name_ownership_free(), you must call its wrapper name_ownership_release(),
115 * which then additionally returns required information to you.
116 */
117 void name_ownership_release(NameOwnership *ownership, NameChange *change) {
118 NameOwnership *primary;
119 Name *name = ownership->name;
120
121 c_assert(!change->name);
122 c_assert(!change->old_owner);
123 c_assert(!change->new_owner);
124
125 primary = name_primary(name);
126 c_list_unlink(&ownership->name_link);
127
128 if (ownership == primary) {
129 --ownership->owner->n_owner_primaries;
130 --name->registry->n_primaries;
131
132 primary = name_primary(name);
133
134 change->name = name_ref(name);
135 change->old_owner = ownership->owner;
136 change->new_owner = primary ? primary->owner : NULL;
137
138 if (primary) {
139 // skip `n_primaries_peak` as `n_primaries` did not change
140 ++name->registry->n_primaries;
141 ++primary->owner->n_owner_primaries;
142 util_peak_update(&name->registry->n_owner_primaries_peak, primary->owner->n_owner_primaries);
143 }
144 }
145
146 name_ownership_free(ownership);
147 }
148
149 static int name_ownership_update(NameOwnership *ownership, uint32_t flags, NameChange *change) {
150 Name *name = ownership->name;
151 NameOwnership *primary;
152 int r;
153
154 c_assert(!change->name);
155 c_assert(!change->old_owner);
156 c_assert(!change->new_owner);
157
158 primary = name_primary(name);
159 ownership->flags = flags;
160
161 if (!primary) {
162 /* there is no primary owner */
163 change->name = name_ref(name);
164 change->old_owner = NULL;
165 change->new_owner = ownership->owner;
166
167 /* @owner cannot already be linked */
168 c_assert(!c_list_is_linked(&ownership->name_link));
169
170 c_list_link_front(&name->ownership_list, &ownership->name_link);
171
172 ++name->registry->n_primaries;
173 ++ownership->owner->n_owner_primaries;
174 util_peak_update(&name->registry->n_primaries_peak, name->registry->n_primaries);
175 util_peak_update(&name->registry->n_owner_primaries_peak, ownership->owner->n_owner_primaries);
176
177 r = 0;
178 } else if (primary == ownership) {
179 /* we are already the primary owner */
180 r = NAME_E_ALREADY_OWNER;
181 } else if ((ownership->flags & DBUS_NAME_FLAG_REPLACE_EXISTING) &&
182 (primary->flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)) {
183 /* we replace the primary owner */
184 change->name = name_ref(name);
185 change->old_owner = primary->owner;
186 change->new_owner = ownership->owner;
187
188 c_list_unlink(&ownership->name_link);
189 c_list_link_front(&name->ownership_list, &ownership->name_link);
190
191 // skip `n_primaries_peak` as `n_primaries` did not change
192 --primary->owner->n_owner_primaries;
193 ++ownership->owner->n_owner_primaries;
194 util_peak_update(&name->registry->n_owner_primaries_peak, ownership->owner->n_owner_primaries);
195
196 /* drop previous primary owner, if queuing is not requested */
197 if (primary->flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) {
198 c_list_unlink(&primary->name_link);
199 name_ownership_free(primary);
200 }
201
202 r = 0;
203 } else if (!(ownership->flags & DBUS_NAME_FLAG_DO_NOT_QUEUE)) {
204 /* we are appended to the queue */
205 if (!c_list_is_linked(&ownership->name_link))
206 c_list_link_tail(&name->ownership_list, &ownership->name_link);
207 r = NAME_E_IN_QUEUE;
208 } else {
209 /* we are dropped */
210 c_list_unlink(&ownership->name_link);
211 r = NAME_E_EXISTS;
212 }
213
214 return r;
215 }
216
217 static int name_compare(CRBTree *tree, void *k, CRBNode *rb) {
218 Name *name = c_container_of(rb, Name, registry_node);
219
220 return strcmp(k, name->name);
221 }
222
223 static void name_link(Name *name, CRBNode *parent, CRBNode **slot) {
224 c_assert(!c_rbnode_is_linked(&name->registry_node));
225
226 c_rbtree_add(&name->registry->name_tree, parent, slot, &name->registry_node);
227 }
228
229 static int name_new(Name **namep, NameRegistry *registry, const char *name_str) {
230 _c_cleanup_(name_unrefp) Name *name = NULL;
231 size_t n_name;
232
233 n_name = strlen(name_str);
234 name = malloc(sizeof(*name) + n_name + 1);
235 if (!name)
236 return error_origin(-ENOMEM);
237
238 *name = (Name)NAME_INIT(*name);
239 name->registry = registry;
240 c_memcpy(name->name, name_str, n_name + 1);
241
242 *namep = name;
243 name = NULL;
244 return 0;
245 }
246
247 void name_free(_Atomic unsigned long *n_refs, void *userdata) {
248 Name *name = c_container_of(n_refs, Name, n_refs);
249
250 c_assert(c_list_is_empty(&name->ownership_list));
251 c_assert(!name->activation);
252
253 match_registry_deinit(&name->name_owner_changed_matches);
254 match_registry_deinit(&name->sender_matches);
255 c_rbnode_unlink(&name->registry_node);
256 free(name);
257 }
258
259 /**
260 * name_owner_init() - initialize owner
261 * @owner: object to operate on
262 *
263 * This initializes a name-owner context.
264 */
265 void name_owner_init(NameOwner *owner) {
266 *owner = (NameOwner)NAME_OWNER_INIT;
267 }
268
269 /**
270 * name_owner_deinit() - deinitialize owner
271 * @owner: object to operate on
272 *
273 * This deinitializes a name-owner context. The caller must make sure that the
274 * context is unused and does not own any names.
275 */
276 void name_owner_deinit(NameOwner *owner) {
277 c_assert(c_rbtree_is_empty(&owner->ownership_tree));
278 }
279
280 /**
281 * name_owner_get_stats() - return accounting statistics
282 * @owner: object to operate on
283 * @n_objectsp: return argument for total numbers of accounted objects
284 *
285 * This calculates the accounting statistics of all names owned by @owner.
286 */
287 void name_owner_get_stats(NameOwner *owner, unsigned int *n_objectsp) {
288 NameOwnership *ownership;
289 unsigned int n_objects = 0;
290
291 c_rbtree_for_each_entry(ownership, &owner->ownership_tree, owner_node)
292 n_objects += ownership->charge.charge;
293
294 *n_objectsp = n_objects;
295 }
296
297 /**
298 * name_registry_init() - initialize registry
299 * @registry: object to operate on
300 *
301 * This initializes a name registry.
302 */
303 void name_registry_init(NameRegistry *registry) {
304 *registry = (NameRegistry)NAME_REGISTRY_INIT;
305 }
306
307 /**
308 * name_registry_deinit() - deinitialize registry
309 * @registry: object to operate on
310 *
311 * This deinitializes a name registry. The caller must make sure that the
312 * registry is unused and none of its names is pinned, anymore.
313 */
314 void name_registry_deinit(NameRegistry *registry) {
315 c_assert(c_rbtree_is_empty(&registry->name_tree));
316 }
317
318 /**
319 * name_registry_get_activation_stats_for() - calculate activation-statistics
320 * for a peer
321 * @registry: object to operate on
322 * @owner_id: unique ID of the name owner to calculate stats for
323 * @n_bytesp: return argument for total numbers of accounted bytes
324 * @n_fdsp: return argument for total numbers of accounted fds
325 *
326 * This calculates the statistics for all pending activation messages of a
327 * given owner on any names. Note that this information is not indexed, so it
328 * is calculated by traversing all names and pending activation messages. You
329 * must not use this for anything but debugging.
330 */
331 void name_registry_get_activation_stats_for(NameRegistry *registry,
332 uint64_t owner_id,
333 unsigned int *n_bytesp,
334 unsigned int *n_fdsp) {
335 unsigned int n_bytes, n_fds;
336 Name *name;
337
338 *n_bytesp = 0;
339 *n_fdsp = 0;
340
341 c_rbtree_for_each_entry(name, &registry->name_tree, registry_node) {
342 if (name->activation) {
343 activation_get_stats_for(name->activation, owner_id,
344 &n_bytes, &n_fds);
345 *n_bytesp += n_bytes;
346 *n_fdsp += n_fds;
347 }
348 }
349 }
350
351 /**
352 * name_registry_ref_name() - reference name object
353 * @registry: registry to operate on
354 * @namep: output argument for name object
355 * @name_str: name to lookup
356 *
357 * This either creates a new name object with a single reference, or creates a
358 * new reference to the name, if it already exists.
359 *
360 * The pointer to the name is returned in @namep.
361 *
362 * Return: 0 on success, negative error code on failure.
363 */
364 int name_registry_ref_name(NameRegistry *registry, Name **namep, const char *name_str) {
365 CRBNode **slot, *parent;
366 int r;
367
368 slot = c_rbtree_find_slot(&registry->name_tree, name_compare, name_str, &parent);
369 if (!slot) {
370 *namep = name_ref(c_container_of(parent, Name, registry_node));
371 } else {
372 r = name_new(namep, registry, name_str);
373 if (r)
374 return error_trace(r);
375
376 name_link(*namep, parent, slot);
377 }
378
379 return 0;
380 }
381
382 /**
383 * name_registry_find_name() - find name object
384 * @registry: registry to operate on
385 * @name_str: name to lookup
386 *
387 * This looks for a name-entry for name @name_str and returns it.
388 *
389 * Return: Pointer to name-entry, or NULL if not found.
390 */
391 Name *name_registry_find_name(NameRegistry *registry, const char *name_str) {
392 return c_rbtree_find_entry(&registry->name_tree, name_compare, name_str, Name, registry_node);
393 }
394
395 /**
396 * name_registry_request_name() - request name ownership
397 * @registry: registry to operate on
398 * @owner: owner to act as
399 * @user: user to charge
400 * @name_str: name to request
401 * @flags: flags that affect the operation
402 * @change: notification context
403 *
404 * This performs a RequestName() operation as defined by the D-Bus
405 * specification.
406 *
407 * Return: 0 on success, NAME_E_QUOTA if @user exceeded its quota,
408 * NAME_E_ALREADY_OWNER if @owner is already the owner of @name_str,
409 * NAME_E_IN_QUEUE of @owner is now queued @name_str, NAME_E_EXISTS if
410 * the name could not be acquired, negative error code on failure.
411 */
412 int name_registry_request_name(NameRegistry *registry,
413 NameOwner *owner,
414 User *user,
415 const char *name_str,
416 uint32_t flags,
417 NameChange *change) {
418 _c_cleanup_(name_unrefp) Name *name = NULL;
419 NameOwnership *ownership;
420 CRBNode **slot, *parent;
421 int r;
422
423 r = name_registry_ref_name(registry, &name, name_str);
424 if (r)
425 return error_trace(r);
426
427 slot = c_rbtree_find_slot(&owner->ownership_tree,
428 name_ownership_compare,
429 name,
430 &parent);
431 if (!slot) {
432 ownership = c_container_of(parent, NameOwnership, owner_node);
433 } else {
434 r = name_ownership_new(&ownership, owner, user, name);
435 if (r)
436 return error_trace(r);
437
438 name_ownership_link(ownership, parent, slot);
439 }
440
441 r = name_ownership_update(ownership, flags, change);
442 if (!c_list_is_linked(&ownership->name_link))
443 name_ownership_free(ownership);
444 return error_trace(r);
445 }
446
447 /**
448 * name_registry_release_name() - release name ownership
449 * @registry: registry to operate on
450 * @owner: owner to act as
451 * @name_str: name to release
452 * @change: notification context
453 *
454 * This performs a ReleaseName() operation as defined by the D-Bus
455 * specification.
456 *
457 * Return: 0 on success, NAME_E_NOT_FOUND if the name does not exist,
458 * NAME_E_NOT_OWNER if @owner does not own the name, negative error
459 * code on failure.
460 */
461 int name_registry_release_name(NameRegistry *registry,
462 NameOwner *owner,
463 const char *name_str,
464 NameChange *change) {
465 NameOwnership *ownership;
466 Name *name;
467
468 name = name_registry_find_name(registry, name_str);
469 if (!name || !name_primary(name))
470 return NAME_E_NOT_FOUND;
471
472 ownership = c_rbtree_find_entry(&owner->ownership_tree,
473 name_ownership_compare,
474 name,
475 NameOwnership,
476 owner_node);
477 if (!ownership)
478 return NAME_E_NOT_OWNER;
479
480 name_ownership_release(ownership, change);
481 return 0;
482 }
483
484 /**
485 * name_snapshot_new() - create a snapshot of a name owner
486 * @snapshotp: output argument for snapshot
487 * @owner: owner to take snapshot of
488 *
489 * The names owned by a name owner are dynamic. However, sometimes we may want
490 * to store the names owned at a given point in time. This allows an immutable
491 * snapshot to be created of all the names the name owner owns.
492 *
493 * Return: 0 on success, negative error code on failure.
494 */
495 int name_snapshot_new(NameSnapshot **snapshotp, NameOwner *owner) {
496 NameOwnership *ownership;
497 NameSnapshot *snapshot;
498 size_t n_names = 0;
499
500 c_rbtree_for_each_entry(ownership, &owner->ownership_tree, owner_node)
501 ++n_names;
502
503 snapshot = malloc(sizeof(*snapshot) + n_names * sizeof(snapshot->names[0]));
504 if (!snapshot)
505 return error_origin(-ENOMEM);
506
507 *snapshot = (NameSnapshot)NAME_SNAPSHOT_NULL;
508
509 c_rbtree_for_each_entry(ownership, &owner->ownership_tree, owner_node)
510 snapshot->names[snapshot->n_names++] = name_ref(ownership->name);
511
512 c_assert(n_names == snapshot->n_names);
513
514 *snapshotp = snapshot;
515 return 0;
516 }
517
518 /**
519 * name_snapshot_free() - free a name snapshot
520 * @snapshot: object to free
521 *
522 * This releases all the resources and frees the snapshot.
523 *
524 * Return: NULL is returned.
525 */
526 NameSnapshot *name_snapshot_free(NameSnapshot *snapshot) {
527 if (!snapshot)
528 return NULL;
529
530 for (size_t i = 0; i < snapshot->n_names; ++i)
531 name_unref(snapshot->names[i]);
532
533 free(snapshot);
534
535 return NULL;
536 }