| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* D-Bus Match Rules |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-list.h> |
| 8 |
#include <c-rbtree.h> |
| 9 |
#include <c-stdaux.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include "dbus/address.h" |
| 12 |
#include "util/user.h" |
| 13 |
|
| 14 |
typedef struct MatchCounters MatchCounters; |
| 15 |
typedef struct MatchFilter MatchFilter; |
| 16 |
typedef struct MatchKeys MatchKeys; |
| 17 |
typedef struct MatchOwner MatchOwner; |
| 18 |
typedef struct MatchRegistryByKeys MatchRegistryByKeys; |
| 19 |
typedef struct MatchRegistryByMember MatchRegistryByMember; |
| 20 |
typedef struct MatchRegistryByInterface MatchRegistryByInterface; |
| 21 |
typedef struct MatchRegistryByPath MatchRegistryByPath; |
| 22 |
typedef struct MatchRegistry MatchRegistry; |
| 23 |
typedef struct MatchRule MatchRule; |
| 24 |
typedef struct MessageMetadata MessageMetadata; |
| 25 |
|
| 26 |
#define MATCH_RULE_LENGTH_MAX (1024UL) /* taken from dbus-daemon(1) */ |
| 27 |
|
| 28 |
enum { |
| 29 |
_MATCH_E_SUCCESS, |
| 30 |
|
| 31 |
MATCH_E_EOF, |
| 32 |
MATCH_E_INVALID, |
| 33 |
MATCH_E_QUOTA, |
| 34 |
}; |
| 35 |
|
| 36 |
struct MatchFilter { |
| 37 |
uint8_t type; |
| 38 |
uint64_t sender; |
| 39 |
const char *interface; |
| 40 |
const char *member; |
| 41 |
const char *path; |
| 42 |
const char *args[64]; |
| 43 |
size_t n_args; |
| 44 |
const char *argpaths[64]; |
| 45 |
size_t n_argpaths; |
| 46 |
}; |
| 47 |
|
| 48 |
#define MATCH_FILTER_INIT { \ |
| 49 |
.type = DBUS_MESSAGE_TYPE_INVALID, \ |
| 50 |
.sender = ADDRESS_ID_INVALID, \ |
| 51 |
} |
| 52 |
|
| 53 |
struct MatchKeys { |
| 54 |
MatchFilter filter; |
| 55 |
const char *destination; |
| 56 |
const char *sender; |
| 57 |
const char *path_namespace; |
| 58 |
const char *arg0namespace; |
| 59 |
|
| 60 |
size_t n_buffer; |
| 61 |
char buffer[]; |
| 62 |
}; |
| 63 |
|
| 64 |
#define MATCH_KEYS_NULL { \ |
| 65 |
.filter = MATCH_FILTER_INIT, \ |
| 66 |
} |
| 67 |
|
| 68 |
struct MatchRule { |
| 69 |
unsigned long int n_user_refs; |
| 70 |
MatchRegistryByKeys *registry_by_keys; |
| 71 |
CList registry_link; |
| 72 |
MatchRegistry *registry; |
| 73 |
MatchOwner *owner; |
| 74 |
CRBNode owner_node; |
| 75 |
MatchCounters *counters; |
| 76 |
|
| 77 |
UserCharge charge[2]; |
| 78 |
MatchKeys keys; |
| 79 |
/* @keys must be last, as it contains a VLA */ |
| 80 |
}; |
| 81 |
|
| 82 |
#define MATCH_RULE_NULL(_x) { \ |
| 83 |
.registry_link = C_LIST_INIT((_x).registry_link), \ |
| 84 |
.owner_node = C_RBNODE_INIT((_x).owner_node), \ |
| 85 |
.charge = { USER_CHARGE_INIT, USER_CHARGE_INIT }, \ |
| 86 |
.keys = MATCH_KEYS_NULL, \ |
| 87 |
} |
| 88 |
|
| 89 |
struct MatchOwner { |
| 90 |
size_t n_owner_subscriptions; |
| 91 |
CRBTree rule_tree; |
| 92 |
CList destinations_link; |
| 93 |
}; |
| 94 |
|
| 95 |
#define MATCH_OWNER_INIT(_x) { \ |
| 96 |
.rule_tree = C_RBTREE_INIT, \ |
| 97 |
.destinations_link = C_LIST_INIT((_x).destinations_link), \ |
| 98 |
} |
| 99 |
|
| 100 |
struct MatchRegistryByKeys { |
| 101 |
unsigned long n_refs; |
| 102 |
CList rule_list; |
| 103 |
MatchRegistryByMember *registry_by_member; |
| 104 |
CRBNode registry_node; |
| 105 |
MatchKeys keys; |
| 106 |
/* @keys must be last, as it contains a VLA */ |
| 107 |
}; |
| 108 |
|
| 109 |
#define MATCH_REGISTRY_BY_KEYS_INIT(_x) { \ |
| 110 |
.n_refs = 1, \ |
| 111 |
.rule_list = C_LIST_INIT((_x).rule_list), \ |
| 112 |
.registry_node = C_RBNODE_INIT((_x).registry_node), \ |
| 113 |
.keys = MATCH_KEYS_NULL, \ |
| 114 |
} |
| 115 |
|
| 116 |
struct MatchRegistryByMember { |
| 117 |
unsigned long n_refs; |
| 118 |
CRBTree keys_tree; |
| 119 |
MatchRegistryByInterface *registry_by_interface; |
| 120 |
CRBNode registry_node; |
| 121 |
char member[]; |
| 122 |
}; |
| 123 |
|
| 124 |
#define MATCH_REGISTRY_BY_MEMBER_INIT(_x) { \ |
| 125 |
.n_refs = 1, \ |
| 126 |
.keys_tree = C_RBTREE_INIT, \ |
| 127 |
.registry_node = C_RBNODE_INIT((_x).registry_node), \ |
| 128 |
} |
| 129 |
|
| 130 |
struct MatchRegistryByInterface { |
| 131 |
unsigned long n_refs; |
| 132 |
CRBTree member_tree; |
| 133 |
MatchRegistryByPath *registry_by_path; |
| 134 |
CRBNode registry_node; |
| 135 |
char interface[]; |
| 136 |
}; |
| 137 |
|
| 138 |
#define MATCH_REGISTRY_BY_INTERFACE_INIT(_x) { \ |
| 139 |
.n_refs = 1, \ |
| 140 |
.member_tree = C_RBTREE_INIT, \ |
| 141 |
.registry_node = C_RBNODE_INIT((_x).registry_node), \ |
| 142 |
} |
| 143 |
|
| 144 |
struct MatchRegistryByPath { |
| 145 |
unsigned long n_refs; |
| 146 |
CRBTree interface_tree; |
| 147 |
CRBNode registry_node; |
| 148 |
char path[]; |
| 149 |
}; |
| 150 |
|
| 151 |
#define MATCH_REGISTRY_BY_PATH_INIT(_x) { \ |
| 152 |
.n_refs = 1, \ |
| 153 |
.interface_tree = C_RBTREE_INIT, \ |
| 154 |
.registry_node = C_RBNODE_INIT((_x).registry_node), \ |
| 155 |
} |
| 156 |
|
| 157 |
struct MatchRegistry { |
| 158 |
CRBTree subscription_tree; |
| 159 |
CRBTree monitor_tree; |
| 160 |
}; |
| 161 |
|
| 162 |
#define MATCH_REGISTRY_INIT(_x) { \ |
| 163 |
.subscription_tree = C_RBTREE_INIT, \ |
| 164 |
.monitor_tree = C_RBTREE_INIT, \ |
| 165 |
} |
| 166 |
|
| 167 |
struct MatchCounters { |
| 168 |
size_t n_subscriptions; |
| 169 |
size_t n_subscriptions_peak; |
| 170 |
size_t n_owner_subscriptions_peak; |
| 171 |
}; |
| 172 |
|
| 173 |
#define MATCH_COUNTERS_INIT {} |
| 174 |
|
| 175 |
/* rules */ |
| 176 |
|
| 177 |
MatchRule *match_rule_user_ref(MatchRule *rule); |
| 178 |
MatchRule *match_rule_user_unref(MatchRule *rule); |
| 179 |
|
| 180 |
int match_rule_link(MatchRule *rule, MatchCounters *counters, MatchRegistry *registry, bool monitor); |
| 181 |
void match_rule_unlink(MatchRule *rule); |
| 182 |
|
| 183 |
C_DEFINE_CLEANUP(MatchRule *, match_rule_user_unref); |
| 184 |
|
| 185 |
/* owners */ |
| 186 |
|
| 187 |
void match_owner_init(MatchOwner *owner); |
| 188 |
void match_owner_deinit(MatchOwner *owner); |
| 189 |
|
| 190 |
void match_owner_get_stats(MatchOwner *owner, unsigned int *n_bytesp, unsigned int *n_matchesp); |
| 191 |
void match_owner_move(MatchOwner *to, MatchOwner *from); |
| 192 |
int match_owner_ref_rule(MatchOwner *owner, MatchRule **rulep, User *user, const char *rule_string, bool allow_eavesdrop); |
| 193 |
int match_owner_find_rule(MatchOwner *owner, MatchRule **rulep, const char *rule_string); |
| 194 |
|
| 195 |
/* registry */ |
| 196 |
|
| 197 |
void match_registry_init(MatchRegistry *registry); |
| 198 |
void match_registry_deinit(MatchRegistry *registry); |
| 199 |
|
| 200 |
void match_registry_get_subscribers(MatchRegistry *matches, CList *destinations, MessageMetadata *metadata); |
| 201 |
void match_registry_get_monitors(MatchRegistry *matches, CList *destinations, MessageMetadata *metadata); |
| 202 |
|
| 203 |
void match_registry_flush(MatchRegistry *registry); |