| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* Name Registry |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-stdaux.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include "bus/match.h" |
| 10 |
#include "util/ref.h" |
| 11 |
#include "util/user.h" |
| 12 |
|
| 13 |
typedef struct Activation Activation; |
| 14 |
typedef struct Name Name; |
| 15 |
typedef struct NameChange NameChange; |
| 16 |
typedef struct NameOwner NameOwner; |
| 17 |
typedef struct NameOwnership NameOwnership; |
| 18 |
typedef struct NameRegistry NameRegistry; |
| 19 |
typedef struct NameSet NameSet; |
| 20 |
typedef struct NameSnapshot NameSnapshot; |
| 21 |
|
| 22 |
enum { |
| 23 |
_NAME_E_SUCCESS, |
| 24 |
|
| 25 |
NAME_E_NOT_FOUND, |
| 26 |
NAME_E_NOT_OWNER, |
| 27 |
|
| 28 |
NAME_E_QUOTA, |
| 29 |
NAME_E_ALREADY_OWNER, |
| 30 |
NAME_E_IN_QUEUE, |
| 31 |
NAME_E_EXISTS, |
| 32 |
}; |
| 33 |
|
| 34 |
struct NameChange { |
| 35 |
Name *name; |
| 36 |
NameOwner *old_owner; |
| 37 |
NameOwner *new_owner; |
| 38 |
}; |
| 39 |
|
| 40 |
#define NAME_CHANGE_INIT {} |
| 41 |
|
| 42 |
struct NameOwnership { |
| 43 |
NameOwner *owner; |
| 44 |
Name *name; |
| 45 |
CRBNode owner_node; |
| 46 |
CList name_link; |
| 47 |
uint64_t flags; |
| 48 |
UserCharge charge; |
| 49 |
}; |
| 50 |
|
| 51 |
#define NAME_OWNERSHIP_NULL(_x) { \ |
| 52 |
.owner_node = C_RBNODE_INIT((_x).owner_node), \ |
| 53 |
.name_link = C_LIST_INIT((_x).name_link), \ |
| 54 |
.charge = USER_CHARGE_INIT, \ |
| 55 |
} |
| 56 |
|
| 57 |
struct Name { |
| 58 |
_Atomic unsigned long n_refs; |
| 59 |
NameRegistry *registry; |
| 60 |
CRBNode registry_node; |
| 61 |
|
| 62 |
Activation *activation; |
| 63 |
MatchRegistry sender_matches; |
| 64 |
MatchRegistry name_owner_changed_matches; |
| 65 |
|
| 66 |
CList ownership_list; |
| 67 |
char name[]; |
| 68 |
}; |
| 69 |
|
| 70 |
#define NAME_INIT(_x) { \ |
| 71 |
.n_refs = REF_INIT, \ |
| 72 |
.registry_node = C_RBNODE_INIT((_x).registry_node), \ |
| 73 |
.sender_matches = MATCH_REGISTRY_INIT((_x).sender_matches), \ |
| 74 |
.name_owner_changed_matches = MATCH_REGISTRY_INIT((_x).name_owner_changed_matches), \ |
| 75 |
.ownership_list = C_LIST_INIT((_x).ownership_list), \ |
| 76 |
} |
| 77 |
|
| 78 |
struct NameOwner { |
| 79 |
size_t n_owner_primaries; |
| 80 |
CRBTree ownership_tree; |
| 81 |
}; |
| 82 |
|
| 83 |
#define NAME_OWNER_INIT { \ |
| 84 |
.ownership_tree = C_RBTREE_INIT, \ |
| 85 |
} |
| 86 |
|
| 87 |
struct NameRegistry { |
| 88 |
size_t n_primaries; |
| 89 |
size_t n_primaries_peak; |
| 90 |
size_t n_owner_primaries_peak; |
| 91 |
CRBTree name_tree; |
| 92 |
}; |
| 93 |
|
| 94 |
#define NAME_REGISTRY_INIT { \ |
| 95 |
.name_tree = C_RBTREE_INIT, \ |
| 96 |
} |
| 97 |
|
| 98 |
struct NameSnapshot { |
| 99 |
size_t n_names; |
| 100 |
Name *names[]; |
| 101 |
}; |
| 102 |
|
| 103 |
#define NAME_SNAPSHOT_NULL {} |
| 104 |
|
| 105 |
enum { |
| 106 |
NAME_SET_TYPE_OWNER, |
| 107 |
NAME_SET_TYPE_SNAPSHOT, |
| 108 |
NAME_SET_TYPE_EMPTY, |
| 109 |
}; |
| 110 |
|
| 111 |
struct NameSet { |
| 112 |
unsigned int type; |
| 113 |
union { |
| 114 |
NameOwner *owner; |
| 115 |
NameSnapshot *snapshot; |
| 116 |
}; |
| 117 |
}; |
| 118 |
|
| 119 |
#define NAME_SET_INIT_FROM_OWNER(_x) { \ |
| 120 |
.type = (_x) ? NAME_SET_TYPE_OWNER : NAME_SET_TYPE_EMPTY, \ |
| 121 |
.owner = (_x), \ |
| 122 |
} |
| 123 |
|
| 124 |
#define NAME_SET_INIT_FROM_SNAPSHOT(_x) { \ |
| 125 |
.type = (_x) ? NAME_SET_TYPE_SNAPSHOT : NAME_SET_TYPE_EMPTY, \ |
| 126 |
.snapshot = (_x), \ |
| 127 |
} |
| 128 |
|
| 129 |
/* notifications */ |
| 130 |
|
| 131 |
void name_change_init(NameChange *change); |
| 132 |
void name_change_deinit(NameChange *change); |
| 133 |
|
| 134 |
/* ownerships */ |
| 135 |
|
| 136 |
void name_ownership_release(NameOwnership *owner, NameChange *change); |
| 137 |
bool name_ownership_is_primary(NameOwnership *owner); |
| 138 |
|
| 139 |
/* names */ |
| 140 |
|
| 141 |
void name_free(_Atomic unsigned long *n_refs, void *userdata); |
| 142 |
|
| 143 |
/* owners */ |
| 144 |
|
| 145 |
void name_owner_init(NameOwner *owner); |
| 146 |
void name_owner_deinit(NameOwner *owner); |
| 147 |
|
| 148 |
void name_owner_get_stats(NameOwner *owner, unsigned int *n_objectsp); |
| 149 |
|
| 150 |
/* registry */ |
| 151 |
|
| 152 |
void name_registry_init(NameRegistry *registry); |
| 153 |
void name_registry_deinit(NameRegistry *registry); |
| 154 |
|
| 155 |
void name_registry_get_activation_stats_for(NameRegistry *registry, |
| 156 |
uint64_t owner_id, |
| 157 |
unsigned int *n_bytesp, |
| 158 |
unsigned int *n_fdsp); |
| 159 |
int name_registry_ref_name(NameRegistry *registry, Name **namep, const char *name_str); |
| 160 |
Name *name_registry_find_name(NameRegistry *registry, const char *name_str); |
| 161 |
|
| 162 |
int name_registry_request_name(NameRegistry *registry, |
| 163 |
NameOwner *owner, |
| 164 |
User *user, |
| 165 |
const char *name_str, |
| 166 |
uint32_t flags, |
| 167 |
NameChange *change); |
| 168 |
int name_registry_release_name(NameRegistry *registry, |
| 169 |
NameOwner *owner, |
| 170 |
const char *name_str, |
| 171 |
NameChange *change); |
| 172 |
|
| 173 |
/* snapshots */ |
| 174 |
|
| 175 |
int name_snapshot_new(NameSnapshot **snapshotp, NameOwner *owner); |
| 176 |
NameSnapshot *name_snapshot_free(NameSnapshot *snapshot); |
| 177 |
|
| 178 |
C_DEFINE_CLEANUP(NameSnapshot *, name_snapshot_free); |
| 179 |
|
| 180 |
/* inline helpers */ |
| 181 |
|
| 182 |
static inline Name *name_ref(Name *name) { |
| 183 |
if (name) |
| 184 |
ref_inc(&name->n_refs); |
| 185 |
return name; |
| 186 |
} |
| 187 |
|
| 188 |
static inline Name *name_unref(Name *name) { |
| 189 |
if (name) |
| 190 |
ref_dec(&name->n_refs, name_free, NULL); |
| 191 |
return NULL; |
| 192 |
} |
| 193 |
|
| 194 |
static inline NameOwnership *name_primary(Name *name) { |
| 195 |
return c_container_of(c_list_first(&name->ownership_list), NameOwnership, name_link); |
| 196 |
} |
| 197 |
|
| 198 |
C_DEFINE_CLEANUP(Name *, name_unref); |