| 1 |
/* |
| 2 |
* Broker Controller |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-rbtree.h> |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include "broker/broker.h" |
| 10 |
#include "broker/controller.h" |
| 11 |
#include "bus/activation.h" |
| 12 |
#include "bus/bus.h" |
| 13 |
#include "bus/driver.h" |
| 14 |
#include "bus/listener.h" |
| 15 |
#include "bus/policy.h" |
| 16 |
#include "dbus/connection.h" |
| 17 |
#include "dbus/message.h" |
| 18 |
#include "util/error.h" |
| 19 |
#include "util/sockopt.h" |
| 20 |
#include "util/user.h" |
| 21 |
|
| 22 |
static int controller_name_compare(CRBTree *t, void *k, CRBNode *rb) { |
| 23 |
ControllerName *name = c_container_of(rb, ControllerName, controller_node); |
| 24 |
|
| 25 |
return strcmp(k, name->path); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* controller_name_free() - XXX |
| 30 |
*/ |
| 31 |
ControllerName *controller_name_free(ControllerName *name) { |
| 32 |
if (!name) |
| 33 |
return NULL; |
| 34 |
|
| 35 |
activation_deinit(&name->activation); |
| 36 |
c_rbnode_unlink(&name->controller_node); |
| 37 |
free(name); |
| 38 |
|
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
static int controller_name_new(ControllerName **namep, Controller *controller, const char *path) { |
| 43 |
CRBNode **slot, *parent; |
| 44 |
ControllerName *name; |
| 45 |
size_t n_path; |
| 46 |
|
| 47 |
slot = c_rbtree_find_slot(&controller->name_tree, controller_name_compare, path, &parent); |
| 48 |
if (!slot) |
| 49 |
return CONTROLLER_E_NAME_EXISTS; |
| 50 |
|
| 51 |
n_path = strlen(path); |
| 52 |
name = calloc(1, sizeof(*name) + n_path + 1); |
| 53 |
if (!name) |
| 54 |
return error_origin(-ENOMEM); |
| 55 |
|
| 56 |
name->controller = controller; |
| 57 |
name->controller_node = (CRBNode)C_RBNODE_INIT(name->controller_node); |
| 58 |
name->activation = (Activation)ACTIVATION_NULL(name->activation); |
| 59 |
c_memcpy(name->path, path, n_path + 1); |
| 60 |
|
| 61 |
c_rbtree_add(&controller->name_tree, parent, slot, &name->controller_node); |
| 62 |
*namep = name; |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* controller_name_reset() - XXX |
| 68 |
*/ |
| 69 |
int controller_name_reset(ControllerName *name, uint64_t serial, unsigned int error) { |
| 70 |
int r; |
| 71 |
|
| 72 |
r = driver_name_activation_failed(&name->controller->broker->bus, &name->activation, serial, error); |
| 73 |
if (r) |
| 74 |
return error_fold(r); |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* controller_name_activate() - XXX |
| 81 |
*/ |
| 82 |
int controller_name_activate(ControllerName *name, uint64_t serial) { |
| 83 |
return controller_dbus_send_activation(name->controller, name->path, serial); |
| 84 |
} |
| 85 |
|
| 86 |
static int controller_reload_compare(CRBTree *t, void *k, CRBNode *rb) { |
| 87 |
ControllerReload *reload = c_container_of(rb, ControllerReload, controller_node); |
| 88 |
uint32_t serial = *(uint32_t *)k; |
| 89 |
|
| 90 |
if (serial < reload->serial) |
| 91 |
return -1; |
| 92 |
if (serial > reload->serial) |
| 93 |
return 1; |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
ControllerReload *controller_reload_free(ControllerReload *reload) { |
| 99 |
if (!reload) |
| 100 |
return NULL; |
| 101 |
|
| 102 |
user_charge_deinit(&reload->charge); |
| 103 |
c_rbnode_unlink(&reload->controller_node); |
| 104 |
free(reload); |
| 105 |
|
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
static int controller_reload_new(ControllerReload **reloadp, User *user, Controller *controller) { |
| 110 |
CRBNode **slot, *parent; |
| 111 |
ControllerReload *reload; |
| 112 |
uint32_t serial; |
| 113 |
int r; |
| 114 |
|
| 115 |
for (uint32_t i = 0; i < UINT32_MAX; i++) { |
| 116 |
serial = ++controller->serial; |
| 117 |
|
| 118 |
if (!serial) |
| 119 |
continue; |
| 120 |
|
| 121 |
slot = c_rbtree_find_slot(&controller->reload_tree, controller_reload_compare, &serial, &parent); |
| 122 |
if (slot) |
| 123 |
break; |
| 124 |
} |
| 125 |
if (!slot) |
| 126 |
return CONTROLLER_E_SERIAL_EXHAUSTED; |
| 127 |
|
| 128 |
reload = calloc(1, sizeof(*reload)); |
| 129 |
if (!reload) |
| 130 |
return error_origin(-ENOMEM); |
| 131 |
|
| 132 |
*reload = (ControllerReload)CONTROLLER_RELOAD_NULL(*reload); |
| 133 |
reload->controller = controller; |
| 134 |
reload->serial = serial; |
| 135 |
|
| 136 |
r = user_charge(controller->broker->bus.user, &reload->charge, user, USER_SLOT_BYTES, sizeof(ControllerReload)); |
| 137 |
if (r) |
| 138 |
return (r == USER_E_QUOTA) ? CONTROLLER_E_QUOTA : error_fold(r); |
| 139 |
|
| 140 |
c_rbtree_add(&controller->reload_tree, parent, slot, &reload->controller_node); |
| 141 |
*reloadp = reload; |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* controller_reload_completed() - XXX |
| 147 |
*/ |
| 148 |
int controller_reload_completed(ControllerReload *reload) { |
| 149 |
return error_fold(driver_reload_config_completed(&reload->controller->broker->bus, reload->sender_id, reload->sender_serial)); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* controller_reload_invalid() - XXX |
| 154 |
*/ |
| 155 |
int controller_reload_invalid(ControllerReload *reload) { |
| 156 |
return error_fold(driver_reload_config_invalid(&reload->controller->broker->bus, reload->sender_id, reload->sender_serial)); |
| 157 |
} |
| 158 |
|
| 159 |
static int controller_listener_compare(CRBTree *t, void *k, CRBNode *rb) { |
| 160 |
ControllerListener *listener = c_container_of(rb, ControllerListener, controller_node); |
| 161 |
|
| 162 |
return strcmp(k, listener->path); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* controller_listener_free() - XXX |
| 167 |
*/ |
| 168 |
ControllerListener *controller_listener_free(ControllerListener *listener) { |
| 169 |
if (!listener) |
| 170 |
return NULL; |
| 171 |
|
| 172 |
listener_deinit(&listener->listener); |
| 173 |
c_rbnode_unlink(&listener->controller_node); |
| 174 |
free(listener); |
| 175 |
|
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
static int controller_listener_new(ControllerListener **listenerp, Controller *controller, const char *path) { |
| 180 |
CRBNode **slot, *parent; |
| 181 |
ControllerListener *listener; |
| 182 |
size_t n_path; |
| 183 |
|
| 184 |
slot = c_rbtree_find_slot(&controller->listener_tree, controller_listener_compare, path, &parent); |
| 185 |
if (!slot) |
| 186 |
return CONTROLLER_E_LISTENER_EXISTS; |
| 187 |
|
| 188 |
n_path = strlen(path); |
| 189 |
listener = calloc(1, sizeof(*listener) + n_path + 1); |
| 190 |
if (!listener) |
| 191 |
return error_origin(-ENOMEM); |
| 192 |
|
| 193 |
listener->controller = controller; |
| 194 |
listener->controller_node = (CRBNode)C_RBNODE_INIT(listener->controller_node); |
| 195 |
c_memcpy(listener->path, path, n_path + 1); |
| 196 |
|
| 197 |
c_rbtree_add(&controller->listener_tree, parent, slot, &listener->controller_node); |
| 198 |
*listenerp = listener; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* controller_listener_set_policy() - XXX |
| 204 |
*/ |
| 205 |
int controller_listener_set_policy(ControllerListener *listener, PolicyRegistry *policy) { |
| 206 |
return error_fold(listener_set_policy(&listener->listener, policy)); |
| 207 |
} |
| 208 |
|
| 209 |
static int controller_dispatch_connection(DispatchFile *file) { |
| 210 |
Controller *controller = c_container_of(file, Controller, connection.socket_file); |
| 211 |
int r; |
| 212 |
|
| 213 |
r = connection_dispatch(&controller->connection, dispatch_file_events(file)); |
| 214 |
if (r) |
| 215 |
return error_fold(r); |
| 216 |
|
| 217 |
do { |
| 218 |
_c_cleanup_(message_unrefp) Message *m = NULL; |
| 219 |
|
| 220 |
r = connection_dequeue(&controller->connection, &m); |
| 221 |
if (r) { |
| 222 |
if (r == CONNECTION_E_EOF) |
| 223 |
r = CONTROLLER_E_EOF; |
| 224 |
else |
| 225 |
return error_fold(r); |
| 226 |
} else { |
| 227 |
if (!m) |
| 228 |
break; |
| 229 |
|
| 230 |
r = controller_dbus_dispatch(controller, m); |
| 231 |
} |
| 232 |
} while (!r); |
| 233 |
|
| 234 |
if (r == CONTROLLER_E_EOF) { |
| 235 |
connection_shutdown(&controller->connection); |
| 236 |
return connection_is_running(&controller->connection) ? 0 : DISPATCH_E_EXIT; |
| 237 |
} else if (r == CONTROLLER_E_PROTOCOL_VIOLATION) { |
| 238 |
connection_close(&controller->connection); |
| 239 |
return connection_is_running(&controller->connection) ? 0 : DISPATCH_E_EXIT; |
| 240 |
} |
| 241 |
|
| 242 |
return error_fold(r); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* controller_init() - XXX |
| 247 |
*/ |
| 248 |
int controller_init(Controller *c, Broker *broker, int controller_fd) { |
| 249 |
_c_cleanup_(controller_deinitp) Controller *controller = c; |
| 250 |
int r; |
| 251 |
|
| 252 |
*controller = (Controller)CONTROLLER_NULL(*controller); |
| 253 |
controller->broker = broker; |
| 254 |
|
| 255 |
r = connection_init_server(&controller->connection, |
| 256 |
&broker->dispatcher, |
| 257 |
controller_dispatch_connection, |
| 258 |
broker->bus.user, |
| 259 |
"0123456789abcdef", |
| 260 |
controller_fd); |
| 261 |
if (r) |
| 262 |
return error_fold(r); |
| 263 |
|
| 264 |
controller = NULL; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* controller_deinit() - XXX |
| 270 |
*/ |
| 271 |
void controller_deinit(Controller *controller) { |
| 272 |
ControllerListener *listener, *listener_safe; |
| 273 |
ControllerName *name, *name_safe; |
| 274 |
ControllerReload *reload, *reload_safe; |
| 275 |
|
| 276 |
c_rbtree_for_each_entry_safe_postorder_unlink(reload, reload_safe, &controller->reload_tree, controller_node) |
| 277 |
controller_reload_free(reload); |
| 278 |
|
| 279 |
c_rbtree_for_each_entry_safe_postorder_unlink(name, name_safe, &controller->name_tree, controller_node) |
| 280 |
controller_name_free(name); |
| 281 |
|
| 282 |
c_rbtree_for_each_entry_safe_postorder_unlink(listener, listener_safe, &controller->listener_tree, controller_node) |
| 283 |
controller_listener_free(listener); |
| 284 |
|
| 285 |
connection_deinit(&controller->connection); |
| 286 |
controller->broker = NULL; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* controller_add_name() - XXX |
| 291 |
*/ |
| 292 |
int controller_add_name(Controller *controller, |
| 293 |
ControllerName **namep, |
| 294 |
const char *path, |
| 295 |
const char *name_str, |
| 296 |
uid_t uid) { |
| 297 |
_c_cleanup_(controller_name_freep) ControllerName *name = NULL; |
| 298 |
_c_cleanup_(name_unrefp) Name *name_entry = NULL; |
| 299 |
_c_cleanup_(user_unrefp) User *user_entry = NULL; |
| 300 |
int r; |
| 301 |
|
| 302 |
r = name_registry_ref_name(&controller->broker->bus.names, &name_entry, name_str); |
| 303 |
if (r) |
| 304 |
return error_fold(r); |
| 305 |
|
| 306 |
r = user_registry_ref_user(&controller->broker->bus.users, &user_entry, uid); |
| 307 |
if (r) |
| 308 |
return error_fold(r); |
| 309 |
|
| 310 |
r = controller_name_new(&name, controller, path); |
| 311 |
if (r) |
| 312 |
return error_trace(r); |
| 313 |
|
| 314 |
r = activation_init(&name->activation, &controller->broker->bus, name_entry, user_entry); |
| 315 |
if (r) |
| 316 |
return (r == ACTIVATION_E_ALREADY_ACTIVATABLE) ? CONTROLLER_E_NAME_IS_ACTIVATABLE : error_fold(r); |
| 317 |
|
| 318 |
*namep = name; |
| 319 |
name = NULL; |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* controller_add_listener() - XXX |
| 325 |
*/ |
| 326 |
int controller_add_listener(Controller *controller, |
| 327 |
ControllerListener **listenerp, |
| 328 |
const char *path, |
| 329 |
int listener_fd, |
| 330 |
PolicyRegistry *policy) { |
| 331 |
_c_cleanup_(controller_listener_freep) ControllerListener *listener = NULL; |
| 332 |
int r; |
| 333 |
|
| 334 |
r = controller_listener_new(&listener, controller, path); |
| 335 |
if (r) |
| 336 |
return error_trace(r); |
| 337 |
|
| 338 |
r = listener_init_with_fd(&listener->listener, |
| 339 |
&controller->broker->bus, |
| 340 |
&controller->broker->dispatcher, |
| 341 |
listener_fd, |
| 342 |
policy); |
| 343 |
if (r) |
| 344 |
return error_fold(r); |
| 345 |
|
| 346 |
*listenerp = listener; |
| 347 |
listener = NULL; |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* controller_request_reload() - XXX |
| 353 |
*/ |
| 354 |
int controller_request_reload(Controller *controller, |
| 355 |
User *sender_user, |
| 356 |
uint64_t sender_id, |
| 357 |
uint32_t sender_serial) { |
| 358 |
_c_cleanup_(controller_reload_freep) ControllerReload *reload = NULL; |
| 359 |
int r; |
| 360 |
|
| 361 |
r = controller_reload_new(&reload, sender_user, controller); |
| 362 |
if (r) |
| 363 |
return error_trace(r); |
| 364 |
|
| 365 |
reload->sender_id = sender_id; |
| 366 |
reload->sender_serial = sender_serial; |
| 367 |
|
| 368 |
r = controller_dbus_send_reload(controller, sender_user, reload->serial); |
| 369 |
if (r) |
| 370 |
return error_trace(r); |
| 371 |
|
| 372 |
reload = NULL; |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* controller_find_name() - XXX |
| 378 |
*/ |
| 379 |
ControllerName *controller_find_name(Controller *controller, const char *path) { |
| 380 |
return c_container_of(c_rbtree_find_node(&controller->name_tree, |
| 381 |
controller_name_compare, |
| 382 |
path), |
| 383 |
ControllerName, |
| 384 |
controller_node); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* controller_find_listener() - XXX |
| 389 |
*/ |
| 390 |
ControllerListener *controller_find_listener(Controller *controller, const char *path) { |
| 391 |
return c_container_of(c_rbtree_find_node(&controller->listener_tree, |
| 392 |
controller_listener_compare, |
| 393 |
path), |
| 394 |
ControllerListener, |
| 395 |
controller_node); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* controller_find_reload() - XXX |
| 400 |
*/ |
| 401 |
ControllerReload *controller_find_reload(Controller *controller, uint32_t serial) { |
| 402 |
return c_container_of(c_rbtree_find_node(&controller->reload_tree, |
| 403 |
controller_reload_compare, |
| 404 |
&serial), |
| 405 |
ControllerReload, |
| 406 |
controller_node); |
| 407 |
} |