| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* Broker Controller |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-rbtree.h> |
| 8 |
#include <c-stdaux.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include "bus/activation.h" |
| 12 |
#include "bus/listener.h" |
| 13 |
#include "bus/policy.h" |
| 14 |
#include "dbus/connection.h" |
| 15 |
#include "util/string.h" |
| 16 |
#include "util/user.h" |
| 17 |
|
| 18 |
typedef struct Broker Broker; |
| 19 |
typedef struct Bus Bus; |
| 20 |
typedef struct Controller Controller; |
| 21 |
typedef struct ControllerName ControllerName; |
| 22 |
typedef struct ControllerListener ControllerListener; |
| 23 |
typedef struct ControllerReload ControllerReload; |
| 24 |
typedef struct Message Message; |
| 25 |
|
| 26 |
enum { |
| 27 |
_CONTROLLER_E_SUCCESS, |
| 28 |
|
| 29 |
CONTROLLER_E_EOF, |
| 30 |
|
| 31 |
CONTROLLER_E_SERIAL_EXHAUSTED, |
| 32 |
CONTROLLER_E_QUOTA, |
| 33 |
|
| 34 |
CONTROLLER_E_PROTOCOL_VIOLATION, |
| 35 |
|
| 36 |
CONTROLLER_E_INVALID_MESSAGE, |
| 37 |
|
| 38 |
CONTROLLER_E_UNEXPECTED_MESSAGE_TYPE, |
| 39 |
CONTROLLER_E_UNEXPECTED_PATH, |
| 40 |
CONTROLLER_E_UNEXPECTED_INTERFACE, |
| 41 |
CONTROLLER_E_UNEXPECTED_METHOD, |
| 42 |
CONTROLLER_E_UNEXPECTED_SIGNATURE, |
| 43 |
CONTROLLER_E_UNEXPECTED_REPLY, |
| 44 |
CONTROLLER_E_UNEXPECTED_ERROR, |
| 45 |
|
| 46 |
CONTROLLER_E_LISTENER_EXISTS, |
| 47 |
CONTROLLER_E_LISTENER_INVALID_FD, |
| 48 |
CONTROLLER_E_LISTENER_INVALID_POLICY, |
| 49 |
CONTROLLER_E_NAME_EXISTS, |
| 50 |
CONTROLLER_E_NAME_IS_ACTIVATABLE, |
| 51 |
CONTROLLER_E_NAME_INVALID, |
| 52 |
|
| 53 |
CONTROLLER_E_LISTENER_NOT_FOUND, |
| 54 |
CONTROLLER_E_NAME_NOT_FOUND, |
| 55 |
}; |
| 56 |
|
| 57 |
enum { |
| 58 |
_CONTROLLER_NAME_ERROR_INVALID = 0, |
| 59 |
CONTROLLER_NAME_ERROR_DESTRUCTIVE_TRANSACTION = 1, |
| 60 |
CONTROLLER_NAME_ERROR_UNKNOWN_UNIT = 2, |
| 61 |
CONTROLLER_NAME_ERROR_MASKED_UNIT = 3, |
| 62 |
CONTROLLER_NAME_ERROR_INVALID_UNIT = 4, |
| 63 |
CONTROLLER_NAME_ERROR_UNIT_FAILURE = 5, |
| 64 |
CONTROLLER_NAME_ERROR_STARTUP_FAILURE = 6, |
| 65 |
CONTROLLER_NAME_ERROR_STARTUP_SKIPPED = 7, |
| 66 |
CONTROLLER_NAME_ERROR_NAME_RELEASED = 8, |
| 67 |
_CONTROLLER_NAME_ERROR_N, |
| 68 |
}; |
| 69 |
|
| 70 |
struct ControllerName { |
| 71 |
Controller *controller; |
| 72 |
CRBNode controller_node; |
| 73 |
Activation activation; |
| 74 |
char path[]; |
| 75 |
}; |
| 76 |
|
| 77 |
struct ControllerListener { |
| 78 |
Controller *controller; |
| 79 |
CRBNode controller_node; |
| 80 |
Listener listener; |
| 81 |
char path[]; |
| 82 |
}; |
| 83 |
|
| 84 |
struct ControllerReload { |
| 85 |
Controller *controller; |
| 86 |
UserCharge charge; |
| 87 |
CRBNode controller_node; |
| 88 |
uint64_t sender_id; |
| 89 |
uint32_t sender_serial; |
| 90 |
uint32_t serial; |
| 91 |
}; |
| 92 |
|
| 93 |
#define CONTROLLER_RELOAD_NULL(_x) { \ |
| 94 |
.charge = USER_CHARGE_INIT, \ |
| 95 |
.controller_node = (CRBNode)C_RBNODE_INIT((_x).controller_node), \ |
| 96 |
.sender_id = ADDRESS_ID_INVALID, \ |
| 97 |
} |
| 98 |
|
| 99 |
struct Controller { |
| 100 |
Broker *broker; |
| 101 |
Connection connection; |
| 102 |
CRBTree name_tree; |
| 103 |
CRBTree listener_tree; |
| 104 |
CRBTree reload_tree; |
| 105 |
uint32_t serial; |
| 106 |
}; |
| 107 |
|
| 108 |
#define CONTROLLER_NULL(_x) { \ |
| 109 |
.connection = CONNECTION_NULL((_x).connection), \ |
| 110 |
.name_tree = C_RBTREE_INIT, \ |
| 111 |
.listener_tree = C_RBTREE_INIT, \ |
| 112 |
.reload_tree = C_RBTREE_INIT, \ |
| 113 |
} |
| 114 |
|
| 115 |
/* names */ |
| 116 |
|
| 117 |
ControllerName *controller_name_free(ControllerName *name); |
| 118 |
int controller_name_reset(ControllerName *name, uint64_t serial, unsigned int name_error); |
| 119 |
int controller_name_activate(ControllerName *name, uint64_t serial); |
| 120 |
|
| 121 |
C_DEFINE_CLEANUP(ControllerName *, controller_name_free); |
| 122 |
|
| 123 |
/* listeners */ |
| 124 |
|
| 125 |
ControllerListener *controller_listener_free(ControllerListener *listener); |
| 126 |
int controller_listener_set_policy(ControllerListener *listener, PolicyRegistry *policy); |
| 127 |
|
| 128 |
C_DEFINE_CLEANUP(ControllerListener *, controller_listener_free); |
| 129 |
|
| 130 |
/* reload */ |
| 131 |
ControllerReload *controller_reload_free(ControllerReload *reload); |
| 132 |
int controller_reload_completed(ControllerReload *reload); |
| 133 |
int controller_reload_invalid(ControllerReload *reload); |
| 134 |
|
| 135 |
C_DEFINE_CLEANUP(ControllerReload *, controller_reload_free); |
| 136 |
|
| 137 |
/* controller */ |
| 138 |
|
| 139 |
int controller_init(Controller *controller, Broker *broker, int controller_fd); |
| 140 |
void controller_deinit(Controller *controller); |
| 141 |
|
| 142 |
int controller_add_name(Controller *controller, |
| 143 |
ControllerName **namep, |
| 144 |
const char *path, |
| 145 |
const char *name_str, |
| 146 |
uid_t uid); |
| 147 |
int controller_add_listener(Controller *controller, |
| 148 |
ControllerListener **listenerp, |
| 149 |
const char *path, |
| 150 |
int listener_fd, |
| 151 |
PolicyRegistry *policy); |
| 152 |
int controller_request_reload(Controller *controller, |
| 153 |
User *user, |
| 154 |
uint64_t sender_id, |
| 155 |
uint32_t sender_serial); |
| 156 |
ControllerName *controller_find_name(Controller *controller, const char *path); |
| 157 |
ControllerListener *controller_find_listener(Controller *controller, const char *path); |
| 158 |
ControllerReload *controller_find_reload(Controller *controller, uint32_t serial); |
| 159 |
|
| 160 |
int controller_dbus_dispatch(Controller *controller, Message *message); |
| 161 |
int controller_dbus_send_activation(Controller *controller, const char *path, uint64_t serial); |
| 162 |
int controller_dbus_send_reload(Controller *controller, User *user, uint32_t serial); |
| 163 |
int controller_dbus_send_environment(Controller *controller, const char * const *env, size_t n_env); |
| 164 |
|
| 165 |
C_DEFINE_CLEANUP(Controller *, controller_deinit); |
| 166 |
|
| 167 |
/* inline helpers */ |
| 168 |
|
| 169 |
static inline ControllerName *CONTROLLER_NAME(Activation *activation) { |
| 170 |
/* |
| 171 |
* This function up-casts an Activation to its parent class |
| 172 |
* ControllerName. In our code base we pretend an Activation is an |
| 173 |
* abstract class with several virtual methods. However, we only do |
| 174 |
* this to clearly separate our code-bases. We never intended this to |
| 175 |
* be modular. Hence, instead of providing real vtables with userdata |
| 176 |
* pointers, we instead allow explicit up-casts to the parent type. |
| 177 |
* |
| 178 |
* This function performs the up-cast, relying on the fact that all our |
| 179 |
* Activation objects are always owned by a ControllerName object. |
| 180 |
*/ |
| 181 |
return c_container_of(activation, ControllerName, activation); |
| 182 |
} |
| 183 |
|
| 184 |
static const char *const controller_name_error_table[_CONTROLLER_NAME_ERROR_N] = { |
| 185 |
[CONTROLLER_NAME_ERROR_DESTRUCTIVE_TRANSACTION] = "org.bus1.DBus.Name.Error.DestructiveTransaction", |
| 186 |
[CONTROLLER_NAME_ERROR_UNKNOWN_UNIT] = "org.bus1.DBus.Name.Error.UnknownUnit", |
| 187 |
[CONTROLLER_NAME_ERROR_MASKED_UNIT] = "org.bus1.DBus.Name.Error.MaskedUnit", |
| 188 |
[CONTROLLER_NAME_ERROR_INVALID_UNIT] = "org.bus1.DBus.Name.Error.InvalidUnit", |
| 189 |
[CONTROLLER_NAME_ERROR_UNIT_FAILURE] = "org.bus1.DBus.Name.Error.UnitFailure", |
| 190 |
[CONTROLLER_NAME_ERROR_STARTUP_FAILURE] = "org.bus1.DBus.Name.Error.StartupFailure", |
| 191 |
[CONTROLLER_NAME_ERROR_STARTUP_SKIPPED] = "org.bus1.DBus.Name.Error.StartupSkipped", |
| 192 |
[CONTROLLER_NAME_ERROR_NAME_RELEASED] = "org.bus1.DBus.Name.Error.NameReleased", |
| 193 |
}; |
| 194 |
|
| 195 |
static const char *const controller_name_error_table_human_readable[_CONTROLLER_NAME_ERROR_N] = { |
| 196 |
[CONTROLLER_NAME_ERROR_DESTRUCTIVE_TRANSACTION] = "activation request failed: a concurrent deactivation request is already in progress", |
| 197 |
[CONTROLLER_NAME_ERROR_UNKNOWN_UNIT] = "activation request failed: unknown unit", |
| 198 |
[CONTROLLER_NAME_ERROR_MASKED_UNIT] = "activation request failed: unit is masked", |
| 199 |
[CONTROLLER_NAME_ERROR_INVALID_UNIT] = "activation request failed: unit is invalid", |
| 200 |
[CONTROLLER_NAME_ERROR_UNIT_FAILURE] = "unit failed", |
| 201 |
[CONTROLLER_NAME_ERROR_STARTUP_FAILURE] = "startup job failed", |
| 202 |
[CONTROLLER_NAME_ERROR_STARTUP_SKIPPED] = "startup job skipped", |
| 203 |
[CONTROLLER_NAME_ERROR_NAME_RELEASED] = "activation request cancelled: bus name was released", |
| 204 |
}; |
| 205 |
|
| 206 |
static inline const char *controller_name_error_to_string(unsigned int error) { |
| 207 |
c_assert(error > 0 && error < _CONTROLLER_NAME_ERROR_N); |
| 208 |
c_assert(controller_name_error_table[error]); |
| 209 |
|
| 210 |
return controller_name_error_table[error]; |
| 211 |
} |
| 212 |
|
| 213 |
static inline const char *controller_name_error_to_human_readable(unsigned int error) { |
| 214 |
c_assert(error > 0 && error < _CONTROLLER_NAME_ERROR_N); |
| 215 |
c_assert(controller_name_error_table_human_readable[error]); |
| 216 |
|
| 217 |
return controller_name_error_table_human_readable[error]; |
| 218 |
} |
| 219 |
|
| 220 |
static inline unsigned int controller_name_error_from_string(const char *error) { |
| 221 |
for (size_t i = 0; i < _CONTROLLER_NAME_ERROR_N; ++i) |
| 222 |
if (string_equal(error, controller_name_error_table[i])) |
| 223 |
return i; |
| 224 |
|
| 225 |
return 0; |
| 226 |
} |