media/mtransport/third_party/nrappkit/src/registry/registrycb.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 *
michael@0 3 * registrycb.c
michael@0 4 *
michael@0 5 * $Source: /Users/ekr/tmp/nrappkit-dump/nrappkit/src/registry/registrycb.c,v $
michael@0 6 * $Revision: 1.3 $
michael@0 7 * $Date: 2007/06/26 22:37:51 $
michael@0 8 *
michael@0 9 * Callback-related functions
michael@0 10 *
michael@0 11 *
michael@0 12 * Copyright (C) 2005, Network Resonance, Inc.
michael@0 13 * Copyright (C) 2006, Network Resonance, Inc.
michael@0 14 * All Rights Reserved
michael@0 15 *
michael@0 16 * Redistribution and use in source and binary forms, with or without
michael@0 17 * modification, are permitted provided that the following conditions
michael@0 18 * are met:
michael@0 19 *
michael@0 20 * 1. Redistributions of source code must retain the above copyright
michael@0 21 * notice, this list of conditions and the following disclaimer.
michael@0 22 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 23 * notice, this list of conditions and the following disclaimer in the
michael@0 24 * documentation and/or other materials provided with the distribution.
michael@0 25 * 3. Neither the name of Network Resonance, Inc. nor the name of any
michael@0 26 * contributors to this software may be used to endorse or promote
michael@0 27 * products derived from this software without specific prior written
michael@0 28 * permission.
michael@0 29 *
michael@0 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
michael@0 31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 40 * POSSIBILITY OF SUCH DAMAGE.
michael@0 41 *
michael@0 42 *
michael@0 43 */
michael@0 44
michael@0 45 #include <assert.h>
michael@0 46 #include <string.h>
michael@0 47 #include "registry.h"
michael@0 48 #include "registry_int.h"
michael@0 49 #include "r_assoc.h"
michael@0 50 #include "r_errors.h"
michael@0 51 #include "nr_common.h"
michael@0 52 #include "r_log.h"
michael@0 53 #include "r_macros.h"
michael@0 54
michael@0 55 static char CB_ACTIONS[] = { NR_REG_CB_ACTION_ADD,
michael@0 56 NR_REG_CB_ACTION_DELETE,
michael@0 57 NR_REG_CB_ACTION_CHANGE,
michael@0 58 NR_REG_CB_ACTION_FINAL };
michael@0 59
michael@0 60 typedef struct nr_reg_cb_info_ {
michael@0 61 char action;
michael@0 62 void (*cb)(void *cb_arg, char action, NR_registry name);
michael@0 63 void *cb_arg;
michael@0 64 NR_registry name;
michael@0 65 } nr_reg_cb_info;
michael@0 66
michael@0 67 /* callbacks that are registered, a mapping from names like "foo.bar.baz"
michael@0 68 * to an r_assoc which contains possibly several nr_reg_cb_info*'s */
michael@0 69 static r_assoc *nr_registry_callbacks = 0;
michael@0 70
michael@0 71 //static size_t SIZEOF_CB_ID = (sizeof(void (*)()) + 1);
michael@0 72 #define SIZEOF_CB_ID (sizeof(void (*)()) + 1)
michael@0 73
michael@0 74 static int nr_reg_validate_action(char action);
michael@0 75 static int nr_reg_assoc_destroy(void *ptr);
michael@0 76 static int compute_cb_id(void *cb, char action, unsigned char cb_id[SIZEOF_CB_ID]);
michael@0 77 static int nr_reg_info_free(void *ptr);
michael@0 78 static int nr_reg_raise_event_recurse(char *name, char *tmp, int action);
michael@0 79 static int nr_reg_register_callback(NR_registry name, char action, void (*cb)(void *cb_arg, char action, NR_registry name), void *cb_arg);
michael@0 80 static int nr_reg_unregister_callback(char *name, char action, void (*cb)(void *cb_arg, char action, NR_registry name));
michael@0 81
michael@0 82 int
michael@0 83 nr_reg_cb_init()
michael@0 84 {
michael@0 85 int r, _status;
michael@0 86
michael@0 87 if (nr_registry_callbacks == 0) {
michael@0 88 if ((r=r_assoc_create(&nr_registry_callbacks, r_assoc_crc32_hash_compute, 12)))
michael@0 89 ABORT(r);
michael@0 90 }
michael@0 91
michael@0 92 _status=0;
michael@0 93 abort:
michael@0 94 if (_status) {
michael@0 95 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "Couldn't init notifications: %s", nr_strerror(_status));
michael@0 96 }
michael@0 97 return(_status);
michael@0 98 }
michael@0 99
michael@0 100 int
michael@0 101 nr_reg_validate_action(char action)
michael@0 102 {
michael@0 103 int _status;
michael@0 104 int i;
michael@0 105
michael@0 106 for (i = 0; i < sizeof(CB_ACTIONS); ++i) {
michael@0 107 if (action == CB_ACTIONS[i])
michael@0 108 return 0;
michael@0 109 }
michael@0 110 ABORT(R_BAD_ARGS);
michael@0 111
michael@0 112 _status=0;
michael@0 113 abort:
michael@0 114 return(_status);
michael@0 115 }
michael@0 116
michael@0 117 int
michael@0 118 nr_reg_register_callback(NR_registry name, char action, void (*cb)(void *cb_arg, char action, NR_registry name), void *cb_arg)
michael@0 119 {
michael@0 120 int r, _status;
michael@0 121 r_assoc *assoc;
michael@0 122 int create_assoc = 0;
michael@0 123 nr_reg_cb_info *info;
michael@0 124 int create_info = 0;
michael@0 125 unsigned char cb_id[SIZEOF_CB_ID];
michael@0 126
michael@0 127 if (name == 0 || cb == 0)
michael@0 128 ABORT(R_BAD_ARGS);
michael@0 129
michael@0 130 if (nr_registry_callbacks == 0)
michael@0 131 ABORT(R_FAILED);
michael@0 132
michael@0 133 if ((r=nr_reg_is_valid(name)))
michael@0 134 ABORT(r);
michael@0 135
michael@0 136 if ((r=nr_reg_validate_action(action)))
michael@0 137 ABORT(r);
michael@0 138
michael@0 139 if ((r=r_assoc_fetch(nr_registry_callbacks, name, strlen(name)+1, (void*)&assoc))) {
michael@0 140 if (r == R_NOT_FOUND)
michael@0 141 create_assoc = 1;
michael@0 142 else
michael@0 143 ABORT(r);
michael@0 144 }
michael@0 145
michael@0 146 if (create_assoc) {
michael@0 147 if ((r=r_assoc_create(&assoc, r_assoc_crc32_hash_compute, 5)))
michael@0 148 ABORT(r);
michael@0 149
michael@0 150 if ((r=r_assoc_insert(nr_registry_callbacks, name, strlen(name)+1, assoc, 0, nr_reg_assoc_destroy, R_ASSOC_NEW)))
michael@0 151 ABORT(r);
michael@0 152 }
michael@0 153
michael@0 154 if ((r=compute_cb_id(cb, action, cb_id)))
michael@0 155 ABORT(r);
michael@0 156
michael@0 157 if ((r=r_assoc_fetch(assoc, (char*)cb_id, SIZEOF_CB_ID, (void*)&info))) {
michael@0 158 if (r == R_NOT_FOUND)
michael@0 159 create_info = 1;
michael@0 160 else
michael@0 161 ABORT(r);
michael@0 162 }
michael@0 163
michael@0 164 if (create_info) {
michael@0 165 if (!(info=(void*)RCALLOC(sizeof(*info))))
michael@0 166 ABORT(R_NO_MEMORY);
michael@0 167 }
michael@0 168
michael@0 169 strncpy(info->name, name, sizeof(info->name));
michael@0 170 info->action = action;
michael@0 171 info->cb = cb;
michael@0 172 info->cb_arg = cb_arg;
michael@0 173
michael@0 174 if (create_info) {
michael@0 175 if ((r=r_assoc_insert(assoc, (char*)cb_id, SIZEOF_CB_ID, info, 0, nr_reg_info_free, R_ASSOC_NEW)))
michael@0 176 ABORT(r);
michael@0 177 }
michael@0 178
michael@0 179 _status=0;
michael@0 180 abort:
michael@0 181 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "register callback %p on '%s' for '%s' %s", cb, name, nr_reg_action_name(action), (_status ? "FAILED" : "succeeded"));
michael@0 182
michael@0 183 if (_status) {
michael@0 184 if (create_info && info) RFREE(info);
michael@0 185 if (create_assoc && assoc) nr_reg_assoc_destroy(&assoc);
michael@0 186 }
michael@0 187 return(_status);
michael@0 188 }
michael@0 189
michael@0 190 int
michael@0 191 nr_reg_unregister_callback(char *name, char action, void (*cb)(void *cb_arg, char action, NR_registry name))
michael@0 192 {
michael@0 193 int r, _status;
michael@0 194 r_assoc *assoc;
michael@0 195 int size;
michael@0 196 unsigned char cb_id[SIZEOF_CB_ID];
michael@0 197
michael@0 198 if (name == 0 || cb == 0)
michael@0 199 ABORT(R_BAD_ARGS);
michael@0 200
michael@0 201 if (nr_registry_callbacks == 0)
michael@0 202 ABORT(R_FAILED);
michael@0 203
michael@0 204 if ((r=nr_reg_is_valid(name)))
michael@0 205 ABORT(r);
michael@0 206
michael@0 207 if ((r=nr_reg_validate_action(action)))
michael@0 208 ABORT(r);
michael@0 209
michael@0 210 if ((r=r_assoc_fetch(nr_registry_callbacks, name, strlen(name)+1, (void*)&assoc))) {
michael@0 211 if (r != R_NOT_FOUND)
michael@0 212 ABORT(r);
michael@0 213 }
michael@0 214 else {
michael@0 215 if ((r=compute_cb_id(cb, action, cb_id)))
michael@0 216 ABORT(r);
michael@0 217
michael@0 218 if ((r=r_assoc_delete(assoc, (char*)cb_id, SIZEOF_CB_ID))) {
michael@0 219 if (r != R_NOT_FOUND)
michael@0 220 ABORT(r);
michael@0 221 }
michael@0 222
michael@0 223 if ((r=r_assoc_num_elements(assoc, &size)))
michael@0 224 ABORT(r);
michael@0 225
michael@0 226 if (size == 0) {
michael@0 227 if ((r=r_assoc_delete(nr_registry_callbacks, name, strlen(name)+1)))
michael@0 228 ABORT(r);
michael@0 229 }
michael@0 230 }
michael@0 231
michael@0 232 _status=0;
michael@0 233 abort:
michael@0 234 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "unregister callback %p on '%s' for '%s' %s", cb, name, nr_reg_action_name(action), (_status ? "FAILED" : "succeeded"));
michael@0 235
michael@0 236 return(_status);
michael@0 237 }
michael@0 238
michael@0 239 int
michael@0 240 compute_cb_id(void *cb, char action, unsigned char cb_id[SIZEOF_CB_ID])
michael@0 241 {
michael@0 242 /* callbacks are identified by the pointer to the cb function plus
michael@0 243 * the action being watched */
michael@0 244 assert(sizeof(cb) == sizeof(void (*)()));
michael@0 245 assert(sizeof(cb) == (SIZEOF_CB_ID - 1));
michael@0 246
michael@0 247 memcpy(cb_id, &(cb), sizeof(cb));
michael@0 248 cb_id[SIZEOF_CB_ID-1] = action;
michael@0 249
michael@0 250 return 0;
michael@0 251 }
michael@0 252
michael@0 253 char *
michael@0 254 nr_reg_action_name(int action)
michael@0 255 {
michael@0 256 char *name = "*Unknown*";
michael@0 257
michael@0 258 switch (action) {
michael@0 259 case NR_REG_CB_ACTION_ADD: name = "add"; break;
michael@0 260 case NR_REG_CB_ACTION_DELETE: name = "delete"; break;
michael@0 261 case NR_REG_CB_ACTION_CHANGE: name = "change"; break;
michael@0 262 case NR_REG_CB_ACTION_FINAL: name = "final"; break;
michael@0 263 }
michael@0 264
michael@0 265 return name;
michael@0 266 }
michael@0 267
michael@0 268 int
michael@0 269 nr_reg_assoc_destroy(void *ptr)
michael@0 270 {
michael@0 271 return r_assoc_destroy((r_assoc**)&ptr);
michael@0 272 }
michael@0 273
michael@0 274 int
michael@0 275 nr_reg_info_free(void *ptr)
michael@0 276 {
michael@0 277 RFREE(ptr);
michael@0 278 return 0;
michael@0 279 }
michael@0 280
michael@0 281 /* call with tmp=0 */
michael@0 282 int
michael@0 283 nr_reg_raise_event_recurse(char *name, char *tmp, int action)
michael@0 284 {
michael@0 285 int r, _status;
michael@0 286 r_assoc *assoc;
michael@0 287 nr_reg_cb_info *info;
michael@0 288 r_assoc_iterator iter;
michael@0 289 char *key;
michael@0 290 int keyl;
michael@0 291 char *c;
michael@0 292 int free_tmp = 0;
michael@0 293 int count;
michael@0 294
michael@0 295 if (tmp == 0) {
michael@0 296 if (!(tmp = (char*)r_strdup(name)))
michael@0 297 ABORT(R_NO_MEMORY);
michael@0 298 free_tmp = 1;
michael@0 299 }
michael@0 300
michael@0 301 if ((r=r_assoc_fetch(nr_registry_callbacks, tmp, strlen(tmp)+1, (void*)&assoc))) {
michael@0 302 if (r != R_NOT_FOUND)
michael@0 303 ABORT(r);
michael@0 304
michael@0 305 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "No callbacks found on '%s'", tmp);
michael@0 306 }
michael@0 307 else {
michael@0 308 if (!r_assoc_num_elements(assoc, &count)) {
michael@0 309 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "%d callback%s found on '%s'",
michael@0 310 count, ((count == 1) ? "" : "s"), tmp);
michael@0 311 }
michael@0 312
michael@0 313 if ((r=r_assoc_init_iter(assoc, &iter)))
michael@0 314 ABORT(r);
michael@0 315
michael@0 316 for (;;) {
michael@0 317 if ((r=r_assoc_iter(&iter, (void*)&key, &keyl, (void*)&info))) {
michael@0 318 if (r == R_EOD)
michael@0 319 break;
michael@0 320 else
michael@0 321 ABORT(r);
michael@0 322 }
michael@0 323
michael@0 324 if (info->action == action) {
michael@0 325 r_log(NR_LOG_REGISTRY, LOG_DEBUG,
michael@0 326 "Invoking callback %p for '%s'",
michael@0 327 info->cb,
michael@0 328 nr_reg_action_name(info->action));
michael@0 329
michael@0 330 (void)info->cb(info->cb_arg, action, name);
michael@0 331 }
michael@0 332 else {
michael@0 333 r_log(NR_LOG_REGISTRY, LOG_DEBUG,
michael@0 334 "Skipping callback %p for '%s'",
michael@0 335 info->cb,
michael@0 336 nr_reg_action_name(info->action));
michael@0 337 }
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 if (strlen(tmp) > 0) {
michael@0 342 c = strrchr(tmp, '.');
michael@0 343 if (c != 0)
michael@0 344 *c = '\0';
michael@0 345 else
michael@0 346 tmp[0] = '\0';
michael@0 347
michael@0 348 if ((r=nr_reg_raise_event_recurse(name, tmp, action)))
michael@0 349 ABORT(r);
michael@0 350 }
michael@0 351
michael@0 352 _status=0;
michael@0 353 abort:
michael@0 354 if (free_tmp && tmp != 0) RFREE(tmp);
michael@0 355 return(_status);
michael@0 356 }
michael@0 357
michael@0 358
michael@0 359 /* NON-STATIC METHODS */
michael@0 360
michael@0 361 int
michael@0 362 nr_reg_raise_event(char *name, int action)
michael@0 363 {
michael@0 364 int r, _status;
michael@0 365 int count;
michael@0 366 char *event = nr_reg_action_name(action);
michael@0 367
michael@0 368 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "raising event '%s' on '%s'", event, name);
michael@0 369
michael@0 370 if (name == 0)
michael@0 371 ABORT(R_BAD_ARGS);
michael@0 372
michael@0 373 if ((r=nr_reg_validate_action(action)))
michael@0 374 ABORT(r);
michael@0 375
michael@0 376 if ((r=r_assoc_num_elements(nr_registry_callbacks, &count)))
michael@0 377 ABORT(r);
michael@0 378
michael@0 379 if (count > 0) {
michael@0 380 if ((r=nr_reg_raise_event_recurse(name, 0, action)))
michael@0 381 ABORT(r);
michael@0 382 }
michael@0 383 else {
michael@0 384 r_log(NR_LOG_REGISTRY, LOG_DEBUG, "No callbacks found");
michael@0 385 return 0;
michael@0 386 }
michael@0 387
michael@0 388 _status=0;
michael@0 389 abort:
michael@0 390 return(_status);
michael@0 391 }
michael@0 392
michael@0 393
michael@0 394 /* PUBLIC METHODS */
michael@0 395
michael@0 396 int
michael@0 397 NR_reg_register_callback(NR_registry name, char action, void (*cb)(void *cb_arg, char action, NR_registry name), void *cb_arg)
michael@0 398 {
michael@0 399 int r, _status;
michael@0 400 int i;
michael@0 401
michael@0 402 for (i = 0; i < sizeof(CB_ACTIONS); ++i) {
michael@0 403 if (action & CB_ACTIONS[i]) {
michael@0 404 if ((r=nr_reg_register_callback(name, CB_ACTIONS[i], cb, cb_arg)))
michael@0 405 ABORT(r);
michael@0 406
michael@0 407 action &= ~(CB_ACTIONS[i]);
michael@0 408 }
michael@0 409 }
michael@0 410
michael@0 411 if (action)
michael@0 412 ABORT(R_BAD_ARGS);
michael@0 413
michael@0 414 _status=0;
michael@0 415 abort:
michael@0 416 return(_status);
michael@0 417 }
michael@0 418
michael@0 419 int
michael@0 420 NR_reg_unregister_callback(char *name, char action, void (*cb)(void *cb_arg, char action, NR_registry name))
michael@0 421 {
michael@0 422 int r, _status;
michael@0 423 int i;
michael@0 424
michael@0 425 for (i = 0; i < sizeof(CB_ACTIONS); ++i) {
michael@0 426 if (action & CB_ACTIONS[i]) {
michael@0 427 if ((r=nr_reg_unregister_callback(name, CB_ACTIONS[i], cb)))
michael@0 428 ABORT(r);
michael@0 429
michael@0 430 action &= ~(CB_ACTIONS[i]);
michael@0 431 }
michael@0 432 }
michael@0 433
michael@0 434 if (action)
michael@0 435 ABORT(R_BAD_ARGS);
michael@0 436
michael@0 437 _status=0;
michael@0 438 abort:
michael@0 439 return(_status);
michael@0 440 }

mercurial