1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "slap.h"
/*
* This module provides a mechanism for extending core server objects.
* This functionality is provided to plugin writers so that they may
* efficiently pass state information between plugin calls. Typically
* a plugin might register both a pre-op and post-op call. It's very
* convenient for the plugin to associate it's private data with the
* operation object that's passed through the PBlock.
*
* --- An interface is made available to the core server.
*
* int factory_register_type(const char *name, size_t offset)
* void *factory_create_extension(int type,void *object,void *parent)
* void factory_destroy_extension(int type,void *object,void *parent,void **extension)
*
* An object that wishes to make itself available for extension must
* register with the Factory. It passes it's name, say 'Operation',
* and an offset into the structure of where the extension block
* is to be stored. In return a type handle is passed back, which is
* used in place of the name in the creation and destruction calls.
*
* When an object is created, which has registered as extensible, it
* must call the factory_create_extension with its type handle so that
* the extension block can be constructed. A pointer to the block is
* returned that *must* be stored in the object structure at the offset
* declared by the call to factory_register_type.
*
* When an extensible object is destroyed the extension block must also
* be destroyed. The factory_destroy_extension call is provided to
* tidy up and free any extensions created for this object.
*
* --- An interface is made available to the plugins.
*
* int slapi_register_object_extension(
* const char* objectname,
* slapi_extension_constructor_fnptr constructor,
* slapi_extension_destructor_fnptr destructor,
* int *objecttype,
* int *extensionhandle)
* void *slapi_get_object_extension(int objecttype,void *object,int extensionhandle)
*
* When the plugin is initialised it must register its object extensions.
* It must provide the name of the object to be extended, say 'Operation',
* and constructor and destructor functions. These functions are called
* when the object is constructed and destroyed. The extension functions
* would probably allocate some memory and initialise it for its
* own use. The registration function will fail if any objects have already
* been created. This is why the registration *must* happen during plugin
* initialisation. In return the plugin will receive two handles, one for
* the object type, and one for the object extension. These only have meaning
* for the slapi_get_object_extension function.
*
* A plugin retrieves a pointer to its own extension by calling slapi_get_
* object_extension with the object from which the extension is to be
* retrieved. The factory uses the objecttype to find the offset into the
* object of where the extension block is stored. The extension handle is
* then used to find the appropriate extension within the block.
*
* Currently (Oct 98) the only supported objects are Operation and Connection.
*
* This documentation is available here...
*
* http://warp/server/directory-server/hydra/replication/objext.html
*/
/* JCM: Could implement simple object leak detection here */
/* ---------------------- Factory Extension ---------------------- */
struct factory_extension
{
const char *pluginname;
slapi_extension_constructor_fnptr constructor;
slapi_extension_destructor_fnptr destructor;
int removed;
};
static struct factory_extension *
new_factory_extension(
const char *pluginname,
slapi_extension_constructor_fnptr constructor,
slapi_extension_destructor_fnptr destructor)
{
struct factory_extension *fe = (struct factory_extension *)slapi_ch_calloc(1, sizeof(struct factory_extension));
fe->pluginname = slapi_ch_strdup(pluginname);
fe->constructor = constructor;
fe->destructor = destructor;
return fe;
}
static void
delete_factory_extension(struct factory_extension **fe)
{
slapi_ch_free((void **)&((*fe)->pluginname));
slapi_ch_free((void **)fe);
}
/* ---------------------- Factory Type ---------------------- */
#define MAX_EXTENSIONS 32
struct factory_type
{
char *name; /* The name of the object that can be extended */
int extension_count; /* The number of extensions registered for this object */
PRLock *extension_lock; /* Protect the array of extensions */
size_t extension_offset; /* The offset into the object where the extension pointer is */
long existence_count; /* Keep track of how many extensions blocks are in existence */
struct factory_extension *extensions[MAX_EXTENSIONS]; /* The extension registered for this object type */
};
static struct factory_type *
new_factory_type(const char *name, size_t offset)
{
struct factory_type *ft = (struct factory_type *)slapi_ch_calloc(1, sizeof(struct factory_type));
ft->name = slapi_ch_strdup(name);
ft->extension_lock = PR_NewLock();
ft->extension_count = 0;
ft->extension_offset = offset;
ft->existence_count = 0;
return ft;
}
static int
factory_type_add_extension(struct factory_type *ft, struct factory_extension *fe)
{
int extensionhandle = -1;
int added = 0;
int i;
PR_Lock(ft->extension_lock);
if (ft->extension_count < MAX_EXTENSIONS) {
for (i = 0; i < ft->extension_count; i++) {
if (strcasecmp(ft->extensions[i]->pluginname, fe->pluginname) == 0) {
if (ft->extensions[i]->removed) {
/* this extension was previously added, and then removed. Reuse the slot */
delete_factory_extension(&ft->extensions[i]);
extensionhandle = i;
ft->extensions[i] = fe;
added = 1;
break;
}
}
}
if (!added) {
extensionhandle = ft->extension_count;
ft->extensions[ft->extension_count] = fe;
ft->extension_count++;
}
} else {
slapi_log_err(SLAPI_LOG_ERR, "factory_type_add_extension",
"Registration of %s extension by %s failed.\n",
ft->name, fe->pluginname);
slapi_log_err(SLAPI_LOG_ERR, "factory_type_add_extension",
"%d extensions already registered. Max is %d\n",
ft->extension_count, MAX_EXTENSIONS);
}
PR_Unlock(ft->extension_lock);
return extensionhandle;
}
static void
factory_type_increment_existence(struct factory_type *ft)
{
ft->existence_count++;
}
static void
factory_type_decrement_existence(struct factory_type *ft)
{
ft->existence_count--;
if (ft->existence_count < 0) {
/* This just shouldn't happen */
slapi_log_err(SLAPI_LOG_ERR, "factory_type_decrement_existence",
"%d %s object extensions in existence.\n",
ft->extension_count, ft->name);
}
}
/* ---------------------- Factory Type Store ---------------------- */
#define MAX_TYPES 16
static PRLock *factory_type_store_lock;
static struct factory_type *factory_type_store[MAX_TYPES];
static int number_of_types = 0;
static void
factory_type_store_init(void)
{
int i = 0;
factory_type_store_lock = PR_NewLock(); /* JCM - Should really free this at shutdown */
for (i = 0; i < MAX_TYPES; i++) {
factory_type_store[number_of_types] = NULL;
}
}
static int
factory_type_store_add(struct factory_type *ft)
{
int type = number_of_types;
factory_type_store[type] = ft;
number_of_types++;
return type;
}
static struct factory_type *
factory_type_store_get_factory_type(int type)
{
if (type >= 0 && type < number_of_types) {
return factory_type_store[type];
} else {
return NULL;
}
}
static int
factory_type_store_name_to_type(const char *name)
{
int i;
for (i = 0; i < number_of_types; i++) {
if (strcasecmp(factory_type_store[i]->name, name) == 0) {
return i;
}
}
return -1;
}
/* ---------------------- Core Server Functions ---------------------- */
/*
* Function for core server usage.
* See documentation at head of file.
*/
int
factory_register_type(const char *name, size_t offset)
{
int type = 0;
if (number_of_types == 0) {
factory_type_store_init();
}
PR_Lock(factory_type_store_lock);
if (number_of_types < MAX_TYPES) {
struct factory_type *ft = new_factory_type(name, offset);
type = factory_type_store_add(ft);
} else {
slapi_log_err(SLAPI_LOG_ERR, "factory_register_type",
"Registration of %s object failed.\n", name);
slapi_log_err(SLAPI_LOG_ERR, "factory_register_type",
"%d objects already registered. Max is %d\n", number_of_types, MAX_TYPES);
type = -1;
}
PR_Unlock(factory_type_store_lock);
return type;
}
/*
* Function for core server usage.
* See documentation at head of file.
*/
void *
factory_create_extension(int type, void *object, void *parent)
{
int n;
void **extension = NULL;
struct factory_type *ft = factory_type_store_get_factory_type(type);
if (ft != NULL) {
PR_Lock(ft->extension_lock);
if ((n = ft->extension_count) > 0) {
int i;
factory_type_increment_existence(ft);
PR_Unlock(ft->extension_lock);
extension = (void **)slapi_ch_calloc(n + 1, sizeof(void *));
for (i = 0; i < n; i++) {
if (ft->extensions[i] == NULL || ft->extensions[i]->removed) {
continue;
}
slapi_extension_constructor_fnptr constructor = ft->extensions[i]->constructor;
if (constructor != NULL) {
extension[i] = (*constructor)(object, parent);
}
}
} else {
/* No extensions registered. That's OK */
PR_Unlock(ft->extension_lock);
}
} else {
/* The type wasn't registered. Programming error? */
slapi_log_err(SLAPI_LOG_ERR, "factory_create_extension",
"Object type handle %d not valid. Object not registered?\n", type);
}
return (void *)extension;
}
/*
* Function for core server usage.
* See documentation at head of file.
*/
void
factory_destroy_extension(int type, void *object, void *parent, void **extension)
{
if (extension != NULL && *extension != NULL) {
struct factory_type *ft = factory_type_store_get_factory_type(type);
if (ft != NULL) {
int i, n;
PR_Lock(ft->extension_lock);
n = ft->extension_count;
factory_type_decrement_existence(ft);
for (i = 0; i < n; i++) {
slapi_extension_destructor_fnptr destructor;
if (ft->extensions[i] == NULL) {
continue;
}
destructor = ft->extensions[i]->destructor;
if (destructor != NULL) {
void **extention_array = (void **)(*extension);
(*destructor)(extention_array[i], object, parent);
}
}
PR_Unlock(ft->extension_lock);
} else {
/* The type wasn't registered. Programming error? */
slapi_log_err(SLAPI_LOG_ERR, "factory_destroy_extension",
"Object type handle %d not valid. Object not registered?\n", type);
}
slapi_ch_free(extension);
}
}
/* ---------------------- Slapi Functions ---------------------- */
/*
* The caller should reset
*/
int
slapi_unregister_object_extension(
const char *pluginname __attribute__((unused)),
const char *objectname,
int *objecttype,
int *extensionhandle)
{
struct factory_type *ft;
int rc = 0;
*objecttype = factory_type_store_name_to_type(objectname);
ft = factory_type_store_get_factory_type(*objecttype);
if (ft) {
PR_Lock(ft->extension_lock);
ft->extensions[*extensionhandle]->removed = 1;
PR_Unlock(ft->extension_lock);
} else {
/* extension not found */
rc = -1;
}
return rc;
}
/*
* Function for plugin usage.
* See documentation at head of file.
*/
int
slapi_register_object_extension(
const char *pluginname,
const char *objectname,
slapi_extension_constructor_fnptr constructor,
slapi_extension_destructor_fnptr destructor,
int *objecttype,
int *extensionhandle)
{
int rc = 0;
struct factory_extension *fe;
struct factory_type *ft;
fe = new_factory_extension(pluginname, constructor, destructor);
*objecttype = factory_type_store_name_to_type(objectname);
ft = factory_type_store_get_factory_type(*objecttype);
if (ft != NULL) {
*extensionhandle = factory_type_add_extension(ft, fe);
if (*extensionhandle == -1) {
delete_factory_extension(&fe);
}
} else {
slapi_log_err(SLAPI_LOG_ERR, "slapi_register_object_extension",
"Plugin %s failed to register extension for object %s.\n", pluginname, objectname);
rc = -1;
delete_factory_extension(&fe);
}
return rc;
}
/*
* Function for plugin usage.
* See documentation at head of file.
*/
void *
slapi_get_object_extension(int objecttype, void *object, int extensionhandle)
{
void *object_extension = NULL;
struct factory_type *ft = factory_type_store_get_factory_type(objecttype);
if (ft != NULL) {
char *object_base = (char *)object;
void **o_extension = (void **)(object_base + ft->extension_offset);
void **extension_array = (void **)(*o_extension);
if (extension_array != NULL) {
object_extension = extension_array[extensionhandle];
}
}
return object_extension;
}
/*
* sometimes a plugin would like to change its extension, too.
*/
void
slapi_set_object_extension(int objecttype, void *object, int extensionhandle, void *extension)
{
struct factory_type *ft = factory_type_store_get_factory_type(objecttype);
if (ft != NULL) {
char *object_base = (char *)object;
void **o_extension = (void **)(object_base + ft->extension_offset);
void **extension_array = (void **)(*o_extension);
if (extension_array != NULL) {
extension_array[extensionhandle] = extension;
}
}
}
|