| 1 |
/* |
| 2 |
* IPFlow Collector |
| 3 |
* Copyright (c) 2003 Christophe Fillot. |
| 4 |
* E-mail: [email protected] |
| 5 |
* |
| 6 |
* registry.c: Object Registry. |
| 7 |
*/ |
| 8 |
|
| 9 |
#define _GNU_SOURCE |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <stdarg.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <ctype.h> |
| 19 |
#include <time.h> |
| 20 |
#include <pthread.h> |
| 21 |
#include <sys/time.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/wait.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
#include "utils.h" |
| 28 |
#include "hash.h" |
| 29 |
#include "mempool.h" |
| 30 |
#include "registry.h" |
| 31 |
|
| 32 |
#define DEBUG_REGISTRY 0 |
| 33 |
|
| 34 |
static registry_t *registry = NULL; |
| 35 |
|
| 36 |
#define REGISTRY_LOCK() pthread_mutex_lock(®istry->lock) |
| 37 |
#define REGISTRY_UNLOCK() pthread_mutex_unlock(®istry->lock) |
| 38 |
|
| 39 |
/* Terminate the registry */ |
| 40 |
static void registry_terminate(void) |
| 41 |
{ |
| 42 |
mp_free(registry->ht_types); |
| 43 |
mp_free(registry->ht_names); |
| 44 |
mp_free_pool(®istry->mp); |
| 45 |
pthread_mutex_destroy(®istry->lock); |
| 46 |
free(registry); |
| 47 |
registry = NULL; |
| 48 |
} |
| 49 |
|
| 50 |
/* Initialize registry */ |
| 51 |
int registry_init(void) |
| 52 |
{ |
| 53 |
registry_entry_t *p; |
| 54 |
pthread_mutexattr_t attr; |
| 55 |
size_t len; |
| 56 |
int i; |
| 57 |
|
| 58 |
registry = malloc(sizeof(*registry)); |
| 59 |
assert(registry != NULL); |
| 60 |
|
| 61 |
pthread_mutexattr_init(&attr); |
| 62 |
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); |
| 63 |
pthread_mutex_init(®istry->lock,&attr); |
| 64 |
pthread_mutexattr_destroy(&attr); |
| 65 |
|
| 66 |
/* initialize registry memory pool */ |
| 67 |
mp_create_fixed_pool(®istry->mp,"registry"); |
| 68 |
|
| 69 |
registry->ht_name_entries = REGISTRY_HT_NAME_ENTRIES; |
| 70 |
registry->ht_type_entries = REGISTRY_MAX_TYPES; |
| 71 |
|
| 72 |
/* initialize hash table for names, with sentinels */ |
| 73 |
len = registry->ht_name_entries * sizeof(registry_entry_t); |
| 74 |
registry->ht_names = mp_alloc(®istry->mp,len); |
| 75 |
assert(registry->ht_names != NULL); |
| 76 |
|
| 77 |
for(i=0;i<registry->ht_name_entries;i++) { |
| 78 |
p = ®istry->ht_names[i]; |
| 79 |
p->hname_next = p->hname_prev = p; |
| 80 |
} |
| 81 |
|
| 82 |
/* initialize hash table for types, with sentinels */ |
| 83 |
len = registry->ht_type_entries * sizeof(registry_entry_t); |
| 84 |
registry->ht_types = mp_alloc(®istry->mp,len); |
| 85 |
assert(registry->ht_types != NULL); |
| 86 |
|
| 87 |
for(i=0;i<registry->ht_type_entries;i++) { |
| 88 |
p = ®istry->ht_types[i]; |
| 89 |
p->htype_next = p->htype_prev = p; |
| 90 |
} |
| 91 |
|
| 92 |
atexit(registry_terminate); |
| 93 |
|
| 94 |
return(0); |
| 95 |
} |
| 96 |
|
| 97 |
/* Insert a new entry */ |
| 98 |
static void registry_insert_entry(registry_entry_t *entry) |
| 99 |
{ |
| 100 |
registry_entry_t *bucket; |
| 101 |
u_int h_index; |
| 102 |
|
| 103 |
/* insert new entry in hash table for names */ |
| 104 |
h_index = str_hash(entry->name) % registry->ht_name_entries; |
| 105 |
bucket = ®istry->ht_names[h_index]; |
| 106 |
|
| 107 |
entry->hname_next = bucket->hname_next; |
| 108 |
entry->hname_prev = bucket; |
| 109 |
bucket->hname_next->hname_prev = entry; |
| 110 |
bucket->hname_next = entry; |
| 111 |
|
| 112 |
/* insert new entry in hash table for object types */ |
| 113 |
bucket = ®istry->ht_types[entry->object_type]; |
| 114 |
|
| 115 |
entry->htype_next = bucket->htype_next; |
| 116 |
entry->htype_prev = bucket; |
| 117 |
bucket->htype_next->htype_prev = entry; |
| 118 |
bucket->htype_next = entry; |
| 119 |
} |
| 120 |
|
| 121 |
/* Detach a registry entry */ |
| 122 |
static void registry_detach_entry(registry_entry_t *entry) |
| 123 |
{ |
| 124 |
entry->hname_prev->hname_next = entry->hname_next; |
| 125 |
entry->hname_next->hname_prev = entry->hname_prev; |
| 126 |
|
| 127 |
entry->htype_prev->htype_next = entry->htype_next; |
| 128 |
entry->htype_next->htype_prev = entry->htype_prev; |
| 129 |
} |
| 130 |
|
| 131 |
/* Remove a registry entry */ |
| 132 |
static void registry_remove_entry(registry_entry_t *entry) |
| 133 |
{ |
| 134 |
registry_detach_entry(entry); |
| 135 |
|
| 136 |
mp_free(entry); |
| 137 |
} |
| 138 |
|
| 139 |
/* Locate an entry */ |
| 140 |
static inline registry_entry_t *registry_find_entry(char *name,int object_type) |
| 141 |
{ |
| 142 |
registry_entry_t *entry,*bucket; |
| 143 |
u_int h_index; |
| 144 |
|
| 145 |
h_index = str_hash(name) % registry->ht_name_entries; |
| 146 |
bucket = ®istry->ht_names[h_index]; |
| 147 |
|
| 148 |
for(entry=bucket->hname_next;entry!=bucket;entry=entry->hname_next) |
| 149 |
if (!strcmp(entry->name,name) && (entry->object_type == object_type)) |
| 150 |
return entry; |
| 151 |
|
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
/* Add a new entry to the registry */ |
| 156 |
int registry_add(char *name,int object_type,void *data) |
| 157 |
{ |
| 158 |
registry_entry_t *entry; |
| 159 |
|
| 160 |
if (!name) |
| 161 |
return(-1); |
| 162 |
|
| 163 |
REGISTRY_LOCK(); |
| 164 |
|
| 165 |
/* check if we have already a reference for this name */ |
| 166 |
if ((entry = registry_find_entry(name,object_type))) { |
| 167 |
REGISTRY_UNLOCK(); |
| 168 |
return(-1); |
| 169 |
} |
| 170 |
|
| 171 |
/* create a new entry */ |
| 172 |
if (!(entry = mp_alloc(®istry->mp,sizeof(*entry)))) { |
| 173 |
REGISTRY_UNLOCK(); |
| 174 |
return(-1); |
| 175 |
} |
| 176 |
|
| 177 |
entry->name = name; |
| 178 |
entry->data = data; |
| 179 |
entry->object_type = object_type; |
| 180 |
entry->ref_count = 1; /* consider object is referenced by the caller */ |
| 181 |
registry_insert_entry(entry); |
| 182 |
|
| 183 |
#if DEBUG_REGISTRY |
| 184 |
printf("Registry: object %s: ref_count = %d after add.\n", |
| 185 |
entry->name, entry->ref_count); |
| 186 |
#endif |
| 187 |
|
| 188 |
REGISTRY_UNLOCK(); |
| 189 |
return(0); |
| 190 |
} |
| 191 |
|
| 192 |
/* Delete an entry from the registry */ |
| 193 |
int registry_delete(char *name,int object_type) |
| 194 |
{ |
| 195 |
registry_entry_t *entry; |
| 196 |
|
| 197 |
if (!name) return(-1); |
| 198 |
|
| 199 |
REGISTRY_LOCK(); |
| 200 |
|
| 201 |
if (!(entry = registry_find_entry(name,object_type))) { |
| 202 |
REGISTRY_UNLOCK(); |
| 203 |
return(-1); |
| 204 |
} |
| 205 |
|
| 206 |
/* if the entry is referenced, just decrement ref counter */ |
| 207 |
if (--entry->ref_count > 0) { |
| 208 |
#if DEBUG_REGISTRY |
| 209 |
printf("Registry: object %s: ref_count = %d after delete.\n", |
| 210 |
entry->name, entry->ref_count); |
| 211 |
#endif |
| 212 |
REGISTRY_UNLOCK(); |
| 213 |
return(0); |
| 214 |
} |
| 215 |
|
| 216 |
registry_remove_entry(entry); |
| 217 |
REGISTRY_UNLOCK(); |
| 218 |
return(0); |
| 219 |
} |
| 220 |
|
| 221 |
/* Rename an entry in the registry */ |
| 222 |
int registry_rename(char *name,char *newname,int object_type) |
| 223 |
{ |
| 224 |
registry_entry_t *entry; |
| 225 |
|
| 226 |
if (!name || !newname) return(-1); |
| 227 |
|
| 228 |
REGISTRY_LOCK(); |
| 229 |
|
| 230 |
if (!(entry = registry_find_entry(name,object_type))) { |
| 231 |
REGISTRY_UNLOCK(); |
| 232 |
return(-1); |
| 233 |
} |
| 234 |
|
| 235 |
if (registry_find_entry(newname,object_type)) { |
| 236 |
REGISTRY_UNLOCK(); |
| 237 |
return(-1); |
| 238 |
} |
| 239 |
|
| 240 |
registry_detach_entry(entry); |
| 241 |
entry->name = newname; |
| 242 |
registry_insert_entry(entry); |
| 243 |
|
| 244 |
REGISTRY_UNLOCK(); |
| 245 |
return(0); |
| 246 |
} |
| 247 |
|
| 248 |
/* Find an entry (increment the reference count) */ |
| 249 |
void *registry_find(char *name,int object_type) |
| 250 |
{ |
| 251 |
registry_entry_t *entry; |
| 252 |
void *data; |
| 253 |
|
| 254 |
if (!name) return NULL; |
| 255 |
|
| 256 |
REGISTRY_LOCK(); |
| 257 |
|
| 258 |
if ((entry = registry_find_entry(name,object_type))) { |
| 259 |
entry->ref_count++; |
| 260 |
#if DEBUG_REGISTRY |
| 261 |
printf("Registry: object %s: ref_count = %d after find.\n", |
| 262 |
entry->name, entry->ref_count); |
| 263 |
#endif |
| 264 |
data = entry->data; |
| 265 |
} else |
| 266 |
data = NULL; |
| 267 |
|
| 268 |
REGISTRY_UNLOCK(); |
| 269 |
return data; |
| 270 |
} |
| 271 |
|
| 272 |
/* Check if entry exists (does not change reference count) */ |
| 273 |
void *registry_exists(char *name,int object_type) |
| 274 |
{ |
| 275 |
registry_entry_t *entry; |
| 276 |
void *data = NULL; |
| 277 |
|
| 278 |
if (!name) |
| 279 |
return NULL; |
| 280 |
|
| 281 |
REGISTRY_LOCK(); |
| 282 |
entry = registry_find_entry(name,object_type); |
| 283 |
if (entry) |
| 284 |
data = entry->data; |
| 285 |
REGISTRY_UNLOCK(); |
| 286 |
return data; |
| 287 |
} |
| 288 |
|
| 289 |
/* Release a reference of an entry (decrement the reference count) */ |
| 290 |
int registry_unref(char *name,int object_type) |
| 291 |
{ |
| 292 |
registry_entry_t *entry; |
| 293 |
int res = -1; |
| 294 |
|
| 295 |
if (!name) return(-1); |
| 296 |
|
| 297 |
REGISTRY_LOCK(); |
| 298 |
|
| 299 |
if ((entry = registry_find_entry(name,object_type))) |
| 300 |
{ |
| 301 |
entry->ref_count--; |
| 302 |
|
| 303 |
#if DEBUG_REGISTRY |
| 304 |
printf("Registry: object %s: ref_count = %d after unref.\n", |
| 305 |
name, entry->ref_count); |
| 306 |
#endif |
| 307 |
|
| 308 |
if (entry->ref_count < 0) { |
| 309 |
fprintf(stderr,"Registry: object %s (type %d): negative ref_count.\n", |
| 310 |
name, object_type); |
| 311 |
} else |
| 312 |
res = 0; |
| 313 |
} |
| 314 |
|
| 315 |
REGISTRY_UNLOCK(); |
| 316 |
return(res); |
| 317 |
} |
| 318 |
|
| 319 |
/* |
| 320 |
* Execute action on an object if its reference count is less or equal to |
| 321 |
* the specified count. |
| 322 |
*/ |
| 323 |
int registry_exec_refcount(char *name,int object_type,int max_ref,int reg_del, |
| 324 |
registry_exec obj_action,void *opt_arg) |
| 325 |
{ |
| 326 |
registry_entry_t *entry; |
| 327 |
int res = -1; |
| 328 |
int status; |
| 329 |
|
| 330 |
if (!name) return(-1); |
| 331 |
|
| 332 |
REGISTRY_LOCK(); |
| 333 |
|
| 334 |
entry = registry_find_entry(name,object_type); |
| 335 |
|
| 336 |
if (entry) |
| 337 |
{ |
| 338 |
if (entry->ref_count <= max_ref) |
| 339 |
{ |
| 340 |
status = TRUE; |
| 341 |
|
| 342 |
if (obj_action != NULL) |
| 343 |
status = obj_action(entry->data,opt_arg); |
| 344 |
|
| 345 |
if (reg_del && status) |
| 346 |
registry_remove_entry(entry); |
| 347 |
|
| 348 |
res = 1; |
| 349 |
} else |
| 350 |
res = 0; |
| 351 |
} |
| 352 |
|
| 353 |
REGISTRY_UNLOCK(); |
| 354 |
return(res); |
| 355 |
} |
| 356 |
|
| 357 |
/* Delete object if unused */ |
| 358 |
int registry_delete_if_unused(char *name,int object_type, |
| 359 |
registry_exec obj_destructor,void *opt_arg) |
| 360 |
{ |
| 361 |
return(registry_exec_refcount(name,object_type,0,TRUE, |
| 362 |
obj_destructor,opt_arg)); |
| 363 |
} |
| 364 |
|
| 365 |
/* Execute a callback function for all objects of specified type */ |
| 366 |
int registry_foreach_type(int object_type,registry_foreach cb, |
| 367 |
void *opt,int *err) |
| 368 |
{ |
| 369 |
registry_entry_t *p,*bucket,*next; |
| 370 |
int count = 0; |
| 371 |
|
| 372 |
REGISTRY_LOCK(); |
| 373 |
|
| 374 |
bucket = ®istry->ht_types[object_type]; |
| 375 |
|
| 376 |
for(p=bucket->htype_next;p!=bucket;p=next) { |
| 377 |
next = p->htype_next; |
| 378 |
if (cb) cb(p,opt,err); |
| 379 |
count++; |
| 380 |
} |
| 381 |
|
| 382 |
REGISTRY_UNLOCK(); |
| 383 |
return(count); |
| 384 |
} |
| 385 |
|
| 386 |
/* Delete all objects of the specified type */ |
| 387 |
int registry_delete_type(int object_type,registry_exec cb,void *opt) |
| 388 |
{ |
| 389 |
registry_entry_t *p,*bucket,*next; |
| 390 |
int count = 0; |
| 391 |
int status; |
| 392 |
|
| 393 |
REGISTRY_LOCK(); |
| 394 |
|
| 395 |
bucket = ®istry->ht_types[object_type]; |
| 396 |
|
| 397 |
for(p=bucket->htype_next;p!=bucket;p=next) { |
| 398 |
next = p->htype_next; |
| 399 |
|
| 400 |
if (p->ref_count == 0) { |
| 401 |
status = TRUE; |
| 402 |
|
| 403 |
if (cb != NULL) |
| 404 |
status = cb(p->data,opt); |
| 405 |
|
| 406 |
if (status) { |
| 407 |
registry_remove_entry(p); |
| 408 |
count++; |
| 409 |
} |
| 410 |
} else { |
| 411 |
fprintf(stderr,"registry_delete_type: object \"%s\" (type %d) still " |
| 412 |
"referenced (count=%d)\n",p->name,object_type,p->ref_count); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
REGISTRY_UNLOCK(); |
| 417 |
return(count); |
| 418 |
} |
| 419 |
|
| 420 |
/* Dump the registry */ |
| 421 |
void registry_dump(void) |
| 422 |
{ |
| 423 |
registry_entry_t *p,*bucket; |
| 424 |
int i; |
| 425 |
|
| 426 |
REGISTRY_LOCK(); |
| 427 |
|
| 428 |
printf("Registry dump:\n"); |
| 429 |
|
| 430 |
printf(" Objects (from name hash table):\n"); |
| 431 |
|
| 432 |
/* dump hash table of names */ |
| 433 |
for(i=0;i<registry->ht_name_entries;i++) |
| 434 |
{ |
| 435 |
bucket = ®istry->ht_names[i]; |
| 436 |
|
| 437 |
for(p=bucket->hname_next;p!=bucket;p=p->hname_next) |
| 438 |
printf(" %s (type %d, ref_count=%d)\n", |
| 439 |
p->name,p->object_type,p->ref_count); |
| 440 |
} |
| 441 |
|
| 442 |
printf("\n Objects classed by types:\n"); |
| 443 |
|
| 444 |
/* dump hash table of types */ |
| 445 |
for(i=0;i<registry->ht_type_entries;i++) |
| 446 |
{ |
| 447 |
printf(" Type %d: ",i); |
| 448 |
|
| 449 |
bucket = ®istry->ht_types[i]; |
| 450 |
for(p=bucket->htype_next;p!=bucket;p=p->htype_next) |
| 451 |
printf("%s(%d) ",p->name,p->ref_count); |
| 452 |
|
| 453 |
printf("\n"); |
| 454 |
} |
| 455 |
|
| 456 |
REGISTRY_UNLOCK(); |
| 457 |
} |