2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Sorcery Data Access Layer API
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/logger.h"
35 #include "asterisk/sorcery.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/format.h"
38 #include "asterisk/format_cap.h"
39 #include "asterisk/strings.h"
40 #include "asterisk/config_options.h"
41 #include "asterisk/netsock2.h"
42 #include "asterisk/module.h"
43 #include "asterisk/taskprocessor.h"
44 #include "asterisk/threadpool.h"
45 #include "asterisk/json.h"
46 #include "asterisk/format_pref.h"
48 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
52 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
53 #define WIZARD_BUCKETS 7
55 /*! \brief Number of buckets for types (should be prime for performance reasons) */
56 #define TYPE_BUCKETS 53
58 /*! \brief Number of buckets for instances (should be prime for performance reasons) */
59 #define INSTANCE_BUCKETS 17
61 /*! \brief Thread pool for observers */
62 static struct ast_threadpool *threadpool;
64 /*! \brief Structure for internal sorcery object information */
65 struct ast_sorcery_object {
66 /*! \brief Unique identifier of this object */
69 /*! \brief Type of object */
70 char type[MAX_OBJECT_TYPE];
72 /*! \brief Optional object destructor */
73 ao2_destructor_fn destructor;
75 /*! \brief Extended object fields */
76 struct ast_variable *extended;
79 /*! \brief Structure for registered object type */
80 struct ast_sorcery_object_type {
81 /*! \brief Unique name of the object type */
82 char name[MAX_OBJECT_TYPE];
84 /*! \brief Optional transformation callback */
85 sorcery_transform_handler transform;
87 /*! \brief Optional object set apply callback */
88 sorcery_apply_handler apply;
90 /*! \brief Optional object copy callback */
91 sorcery_copy_handler copy;
93 /*! \brief Optional object diff callback */
94 sorcery_diff_handler diff;
96 /*! \brief Wizard instances */
97 struct ao2_container *wizards;
99 /*! \brief Object fields */
100 struct ao2_container *fields;
102 /*! \brief Configuration framework general information */
103 struct aco_info *info;
105 /*! \brief Configuration framework file information */
106 struct aco_file *file;
108 /*! \brief Type details */
109 struct aco_type type;
111 /*! \brief Observers */
112 struct ao2_container *observers;
114 /*! \brief Serializer for observers */
115 struct ast_taskprocessor *serializer;
117 /*! \brief Specifies if object type is reloadable or not */
118 unsigned int reloadable:1;
121 /*! \brief Structure for registered object type observer */
122 struct ast_sorcery_object_type_observer {
123 /*! \brief Pointer to the observer implementation */
124 const struct ast_sorcery_observer *callbacks;
127 /*! \brief Structure used for observer invocations */
128 struct sorcery_observer_invocation {
129 /*! \brief Pointer to the object type */
130 struct ast_sorcery_object_type *object_type;
132 /*! \brief Pointer to the object */
136 /*! \brief Structure for registered object field */
137 struct ast_sorcery_object_field {
138 /*! \brief Name of the field */
139 char name[MAX_OBJECT_FIELD];
141 /*! \brief Callback function for translation of a single value */
142 sorcery_field_handler handler;
144 /*! \brief Callback function for translation of multiple values */
145 sorcery_fields_handler multiple_handler;
147 /*! \brief Position of the field */
151 /*! \brief Structure for a wizard instance which operates on objects */
152 struct ast_sorcery_object_wizard {
153 /*! \brief Wizard interface itself */
154 struct ast_sorcery_wizard *wizard;
156 /*! \brief Unique data for the wizard */
159 /*! \brief Wizard is acting as an object cache */
160 unsigned int caching:1;
163 /*! \brief Full structure for sorcery */
165 /*! \brief Container for known object types */
166 struct ao2_container *types;
167 /*! \brief The name of the module owning this sorcery instance */
171 /*! \brief Structure for passing load/reload details */
172 struct sorcery_load_details {
173 /*! \brief Sorcery structure in use */
174 const struct ast_sorcery *sorcery;
176 /*! \brief Type of object being loaded */
179 /*! \brief Whether this is a reload or not */
180 unsigned int reload:1;
183 /*! \brief Registered sorcery wizards */
184 static struct ao2_container *wizards;
186 /*! \brief Registered sorcery instances */
187 static struct ao2_container *instances;
189 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
191 int *field = (int *)(obj + args[0]);
192 return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
195 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
197 unsigned int *field = (unsigned int *)(obj + args[0]);
198 return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
201 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
203 double *field = (double *)(obj + args[0]);
204 return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
207 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
209 ast_string_field *field = (const char **)(obj + args[0]);
210 return !(*buf = ast_strdup(*field)) ? -1 : 0;
213 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
215 unsigned int *field = (unsigned int *)(obj + args[0]);
216 return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
219 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
221 struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
222 return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
225 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
227 char *field = (char *)(obj + args[0]);
228 return !(*buf = ast_strdup(field)) ? -1 : 0;
231 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
234 struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + args[0]);
235 ast_codec_pref_string(pref, tmp_buf, sizeof(tmp_buf));
236 return !(*buf = ast_strdup(tmp_buf));
239 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
242 case OPT_BOOL_T: return bool_handler_fn;
243 case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
244 case OPT_CODEC_T: return codec_handler_fn;
245 case OPT_DOUBLE_T: return double_handler_fn;
246 case OPT_INT_T: return int_handler_fn;
247 case OPT_SOCKADDR_T: return sockaddr_handler_fn;
248 case OPT_STRINGFIELD_T: return stringfield_handler_fn;
249 case OPT_UINT_T: return uint_handler_fn;
252 case OPT_CUSTOM_T: return NULL;
258 /*! \brief Hashing function for sorcery wizards */
259 static int sorcery_wizard_hash(const void *obj, const int flags)
261 const struct ast_sorcery_wizard *object;
264 switch (flags & OBJ_SEARCH_MASK) {
268 case OBJ_SEARCH_OBJECT:
276 return ast_str_hash(key);
279 /*! \brief Comparator function for sorcery wizards */
280 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
282 const struct ast_sorcery_wizard *object_left = obj;
283 const struct ast_sorcery_wizard *object_right = arg;
284 const char *right_key = arg;
287 switch (flags & OBJ_SEARCH_MASK) {
288 case OBJ_SEARCH_OBJECT:
289 right_key = object_right->name;
292 cmp = strcmp(object_left->name, right_key);
294 case OBJ_SEARCH_PARTIAL_KEY:
295 cmp = strncmp(object_left->name, right_key, strlen(right_key));
307 /*! \brief Cleanup function */
308 static void sorcery_exit(void)
310 ast_threadpool_shutdown(threadpool);
314 /*! \brief Cleanup function for graceful shutdowns */
315 static void sorcery_cleanup(void)
317 ao2_cleanup(wizards);
319 ao2_cleanup(instances);
323 /*! \brief Compare function for sorcery instances */
324 static int sorcery_instance_cmp(void *obj, void *arg, int flags)
326 const struct ast_sorcery *object_left = obj;
327 const struct ast_sorcery *object_right = arg;
328 const char *right_key = arg;
331 switch (flags & OBJ_SEARCH_MASK) {
332 case OBJ_SEARCH_OBJECT:
333 right_key = object_right->module_name;
336 cmp = strcmp(object_left->module_name, right_key);
338 case OBJ_SEARCH_PARTIAL_KEY:
339 cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
351 /*! \brief Hashing function for sorcery instances */
352 static int sorcery_instance_hash(const void *obj, const int flags)
354 const struct ast_sorcery *object;
357 switch (flags & OBJ_SEARCH_MASK) {
361 case OBJ_SEARCH_OBJECT:
363 key = object->module_name;
369 return ast_str_hash(key);
372 int ast_sorcery_init(void)
374 struct ast_threadpool_options options = {
375 .version = AST_THREADPOOL_OPTIONS_VERSION,
381 ast_assert(wizards == NULL);
383 if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
388 if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
389 ast_threadpool_shutdown(threadpool);
393 instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
394 sorcery_instance_hash, sorcery_instance_cmp);
401 ast_register_cleanup(sorcery_cleanup);
402 ast_register_atexit(sorcery_exit);
407 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
409 struct ast_sorcery_wizard *wizard;
412 ast_assert(!ast_strlen_zero(interface->name));
416 if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
417 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
422 if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
426 *wizard = *interface;
427 wizard->module = module;
429 ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
432 ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
441 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
443 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
446 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
453 /*! \brief Destructor called when sorcery structure is destroyed */
454 static void sorcery_destructor(void *obj)
456 struct ast_sorcery *sorcery = obj;
458 ao2_cleanup(sorcery->types);
461 /*! \brief Hashing function for sorcery types */
462 static int sorcery_type_hash(const void *obj, const int flags)
464 const struct ast_sorcery_object_type *object;
467 switch (flags & OBJ_SEARCH_MASK) {
471 case OBJ_SEARCH_OBJECT:
479 return ast_str_hash(key);
482 /*! \brief Comparator function for sorcery types */
483 static int sorcery_type_cmp(void *obj, void *arg, int flags)
485 const struct ast_sorcery_object_type *object_left = obj;
486 const struct ast_sorcery_object_type *object_right = arg;
487 const char *right_key = arg;
490 switch (flags & OBJ_SEARCH_MASK) {
491 case OBJ_SEARCH_OBJECT:
492 right_key = object_right->name;
495 cmp = strcmp(object_left->name, right_key);
497 case OBJ_SEARCH_PARTIAL_KEY:
498 cmp = strncmp(object_left->name, right_key, strlen(right_key));
510 struct ast_sorcery *__ast_sorcery_open(const char *module_name)
512 struct ast_sorcery *sorcery;
514 ast_assert(module_name != NULL);
516 ao2_wrlock(instances);
517 if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
521 if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
525 if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
526 ao2_ref(sorcery, -1);
531 strcpy(sorcery->module_name, module_name); /* Safe */
532 ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
535 ao2_unlock(instances);
539 /*! \brief Search function for sorcery instances */
540 struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
542 return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
545 /*! \brief Destructor function for object types */
546 static void sorcery_object_type_destructor(void *obj)
548 struct ast_sorcery_object_type *object_type = obj;
550 ao2_cleanup(object_type->wizards);
551 ao2_cleanup(object_type->fields);
552 ao2_cleanup(object_type->observers);
554 if (object_type->info) {
555 aco_info_destroy(object_type->info);
556 ast_free(object_type->info);
559 ast_free(object_type->file);
561 ast_taskprocessor_unreference(object_type->serializer);
564 /*! \brief Internal function which allocates an object type structure */
565 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
567 struct ast_sorcery_object_type *object_type;
568 char uuid[AST_UUID_STR_LEN];
570 if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
574 /* Order matters for object wizards */
575 if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
576 ao2_ref(object_type, -1);
580 if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
581 ao2_ref(object_type, -1);
585 if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
586 ao2_ref(object_type, -1);
590 if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
591 ao2_ref(object_type, -1);
595 if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
596 ao2_ref(object_type, -1);
600 if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
601 ao2_ref(object_type, -1);
605 if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
606 ao2_ref(object_type, -1);
610 object_type->info->files[0] = object_type->file;
611 object_type->info->files[1] = NULL;
612 object_type->info->module = module;
614 ast_copy_string(object_type->name, type, sizeof(object_type->name));
619 /*! \brief Object wizard destructor */
620 static void sorcery_object_wizard_destructor(void *obj)
622 struct ast_sorcery_object_wizard *object_wizard = obj;
624 if (object_wizard->data) {
625 object_wizard->wizard->close(object_wizard->data);
628 if (object_wizard->wizard) {
629 ast_module_unref(object_wizard->wizard->module);
633 /*! \brief Internal function which creates an object type and adds a wizard mapping */
634 static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data, unsigned int caching)
636 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
637 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
638 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
641 if (!wizard || !object_wizard) {
646 if (!(object_type = sorcery_object_type_alloc(type, module))) {
652 if (wizard->open && !(object_wizard->data = wizard->open(data))) {
656 ast_module_ref(wizard->module);
658 object_wizard->wizard = wizard;
659 object_wizard->caching = caching;
661 ao2_link(object_type->wizards, object_wizard);
664 ao2_link(sorcery->types, object_type);
670 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
672 struct ast_flags flags = { 0 };
673 struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
674 struct ast_variable *mapping;
677 if (!config || config == CONFIG_STATUS_FILEINVALID) {
681 for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
682 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
683 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
684 char *options = mapping_name;
685 char *type = strsep(&options, "/");
686 char *data = mapping_value;
687 char *wizard = strsep(&data, ",");
688 unsigned int caching = 0;
690 /* If no object type or wizard exists just skip, nothing we can do */
691 if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
695 /* If the wizard is configured as a cache treat it as such */
696 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
700 /* Any error immediately causes us to stop */
701 if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching)) {
707 ast_config_destroy(config);
712 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
714 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
716 /* Defaults can not be added if any existing mapping exists */
721 return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
724 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
726 return ast_sorcery_object_set_extended(obj, var->name, var->value);
729 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
731 const struct ast_sorcery_object_details *details = obj;
733 if (details->object->extended) {
734 *fields = ast_variables_dup(details->object->extended);
742 int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
744 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
746 if (!object_type || object_type->type.item_alloc) {
750 object_type->type.name = object_type->name;
751 object_type->type.type = ACO_ITEM;
752 object_type->type.category = ".?";
753 object_type->type.item_alloc = alloc;
754 object_type->type.hidden = hidden;
756 object_type->reloadable = reloadable;
757 object_type->transform = transform;
758 object_type->apply = apply;
759 object_type->file->types[0] = &object_type->type;
760 object_type->file->types[1] = NULL;
762 if (aco_info_init(object_type->info)) {
766 if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
773 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
775 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
781 object_type->copy = copy;
784 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
786 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
792 object_type->diff = diff;
795 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
797 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
798 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
800 if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
804 ast_copy_string(object_field->name, regex, sizeof(object_field->name));
805 object_field->multiple_handler = sorcery_handler;
807 ao2_link(object_type->fields, object_field);
808 __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
813 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
814 aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
816 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
817 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
821 if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
825 if (!sorcery_handler) {
826 sorcery_handler = sorcery_field_default_handler(opt_type);
829 if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
833 ast_copy_string(object_field->name, name, sizeof(object_field->name));
834 object_field->handler = sorcery_handler;
835 object_field->multiple_handler = multiple_handler;
837 va_start(args, argc);
838 for (pos = 0; pos < argc; pos++) {
839 object_field->args[pos] = va_arg(args, size_t);
843 ao2_link(object_type->fields, object_field);
845 /* TODO: Improve this hack */
847 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
848 } else if (argc == 1) {
849 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
850 object_field->args[0]);
851 } else if (argc == 2) {
852 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
853 object_field->args[0], object_field->args[1]);
854 } else if (argc == 3) {
855 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
856 object_field->args[0], object_field->args[1], object_field->args[2]);
858 ast_assert(0); /* The hack... she does us no good for this */
864 /*! \brief Retrieves whether or not the type is reloadable */
865 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
867 RAII_VAR(struct ast_sorcery_object_type *, object_type,
868 ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
869 return object_type && object_type->reloadable;
872 static int sorcery_wizard_load(void *obj, void *arg, int flags)
874 struct ast_sorcery_object_wizard *wizard = obj;
875 struct sorcery_load_details *details = arg;
876 void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
878 if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
879 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
880 "maintaining previous values\n", details->type);
884 load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
887 load(wizard->data, details->sorcery, details->type);
893 /*! \brief Destructor for observer invocation */
894 static void sorcery_observer_invocation_destroy(void *obj)
896 struct sorcery_observer_invocation *invocation = obj;
898 ao2_cleanup(invocation->object_type);
899 ao2_cleanup(invocation->object);
902 /*! \brief Allocator function for observer invocation */
903 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
905 struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
911 ao2_ref(object_type, +1);
912 invocation->object_type = object_type;
916 invocation->object = object;
922 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
923 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
925 const struct ast_sorcery_object_type_observer *observer = obj;
927 if (observer->callbacks->loaded) {
928 observer->callbacks->loaded(arg);
934 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
935 static int sorcery_observers_notify_loaded(void *data)
937 struct sorcery_observer_invocation *invocation = data;
939 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
940 ao2_cleanup(invocation);
945 static int sorcery_object_load(void *obj, void *arg, int flags)
947 struct ast_sorcery_object_type *type = obj;
948 struct sorcery_load_details *details = arg;
950 details->type = type->name;
951 ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
953 if (ao2_container_count(type->observers)) {
954 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
956 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
957 ao2_cleanup(invocation);
964 void ast_sorcery_load(const struct ast_sorcery *sorcery)
966 struct sorcery_load_details details = {
971 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
974 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
976 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
977 struct sorcery_load_details details = {
986 sorcery_object_load(object_type, &details, 0);
989 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
991 struct sorcery_load_details details = {
996 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
999 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
1001 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1002 struct sorcery_load_details details = {
1011 sorcery_object_load(object_type, &details, 0);
1014 void ast_sorcery_ref(struct ast_sorcery *sorcery)
1016 ao2_ref(sorcery, +1);
1019 static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1021 struct ast_variable *tmp = NULL;
1024 if (!object_field->handler) {
1028 if (!(object_field->handler(object, object_field->args, &buf))) {
1029 tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
1036 static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1038 struct ast_variable *tmp = NULL;
1040 if (!object_field->multiple_handler) {
1044 if (object_field->multiple_handler(object, &tmp)) {
1045 ast_variables_destroy(tmp);
1052 struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
1053 const void *object, enum ast_sorcery_field_handler_flags flags)
1055 const struct ast_sorcery_object_details *details = object;
1056 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1057 struct ao2_iterator i;
1058 struct ast_sorcery_object_field *object_field;
1059 struct ast_variable *head = NULL;
1060 struct ast_variable *tail = NULL;
1066 i = ao2_iterator_init(object_type->fields, 0);
1068 for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1069 struct ast_variable *tmp;
1072 case AST_HANDLER_PREFER_LIST:
1073 if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
1074 (tmp = get_single_field_as_var_list(object, object_field))) {
1078 case AST_HANDLER_PREFER_STRING:
1079 if ((tmp = get_single_field_as_var_list(object, object_field)) ||
1080 (tmp = get_multiple_fields_as_var_list(object, object_field))) {
1084 case AST_HANDLER_ONLY_LIST:
1085 if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
1089 case AST_HANDLER_ONLY_STRING:
1090 if ((tmp = get_single_field_as_var_list(object, object_field))) {
1098 tail = ast_variable_list_append_hint(&head, tail, tmp);
1101 ao2_iterator_destroy(&i);
1106 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
1108 const struct ast_sorcery_object_details *details = object;
1109 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1110 struct ao2_iterator i;
1111 struct ast_sorcery_object_field *object_field;
1112 struct ast_json *json = ast_json_object_create();
1115 if (!object_type || !json) {
1119 i = ao2_iterator_init(object_type->fields, 0);
1121 for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1122 if (object_field->multiple_handler) {
1123 struct ast_variable *tmp = NULL;
1124 struct ast_variable *field;
1126 if ((res = object_field->multiple_handler(object, &tmp))) {
1127 ast_variables_destroy(tmp);
1128 ao2_ref(object_field, -1);
1132 for (field = tmp; field; field = field->next) {
1133 struct ast_json *value = ast_json_string_create(field->value);
1135 if (!value || ast_json_object_set(json, field->name, value)) {
1141 ast_variables_destroy(tmp);
1142 } else if (object_field->handler) {
1144 struct ast_json *value = NULL;
1146 if ((res = object_field->handler(object, object_field->args, &buf))
1147 || !(value = ast_json_string_create(buf))
1148 || ast_json_object_set(json, object_field->name, value)) {
1158 ao2_iterator_destroy(&i);
1160 /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
1162 ast_json_unref(json);
1169 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
1171 const struct ast_sorcery_object_details *details = object;
1172 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1173 RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
1174 struct ast_variable *field;
1181 if (object_type->transform && (transformed = object_type->transform(objectset))) {
1182 field = transformed;
1187 for (; field; field = field->next) {
1188 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
1193 if (!res && object_type->apply) {
1194 res = object_type->apply(sorcery, object);
1200 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1202 const struct ast_variable *field;
1204 /* Search the linked list of fields to find the correct one */
1205 for (field = fields; field; field = field->next) {
1206 if (!strcmp(field->name, name)) {
1214 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1216 const struct ast_variable *field;
1221 /* Unless the ast_variable list changes when examined... it can't differ from itself */
1222 if (original == modified) {
1226 for (field = modified; field; field = field->next) {
1227 const struct ast_variable *old = sorcery_find_field(original, field->name);
1229 if (!old || strcmp(old->value, field->value)) {
1230 struct ast_variable *tmp;
1232 if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1237 tmp->next = *changes;
1242 /* If an error occurred do not return a partial changeset */
1244 ast_variables_destroy(*changes);
1251 static void sorcery_object_destructor(void *object)
1253 struct ast_sorcery_object_details *details = object;
1255 if (details->object->destructor) {
1256 details->object->destructor(object);
1259 ast_variables_destroy(details->object->extended);
1260 ast_free(details->object->id);
1263 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1265 void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1266 struct ast_sorcery_object_details *details = object;
1272 details->object = object + size;
1273 details->object->destructor = destructor;
1278 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1280 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1281 struct ast_sorcery_object_details *details;
1283 if (!object_type || !object_type->type.item_alloc ||
1284 !(details = object_type->type.item_alloc(id))) {
1288 if (ast_strlen_zero(id)) {
1289 char uuid[AST_UUID_STR_LEN];
1291 ast_uuid_generate_str(uuid, sizeof(uuid));
1292 details->object->id = ast_strdup(uuid);
1294 details->object->id = ast_strdup(id);
1297 ast_copy_string(details->object->type, type, sizeof(details->object->type));
1299 if (aco_set_defaults(&object_type->type, id, details)) {
1300 ao2_ref(details, -1);
1307 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1309 const struct ast_sorcery_object_details *details = object;
1310 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1311 struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1312 RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1317 } else if (object_type->copy) {
1318 res = object_type->copy(object, copy);
1319 } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1320 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1322 /* No native copy available and could not create an objectset, this copy has failed */
1334 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1336 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1340 if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1344 if (original == modified) {
1346 } else if (!object_type->diff) {
1347 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1348 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1350 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1351 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1353 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1355 return object_type->diff(original, modified, changes);
1359 /*! \brief Structure used when calling create, update, or delete */
1360 struct sorcery_details {
1361 /*! \brief Pointer to the sorcery instance */
1362 const struct ast_sorcery *sorcery;
1363 /*! \brief Pointer to the object itself */
1367 /*! \brief Internal function used to create an object in caching wizards */
1368 static int sorcery_cache_create(void *obj, void *arg, int flags)
1370 const struct ast_sorcery_object_wizard *object_wizard = obj;
1371 const struct sorcery_details *details = arg;
1373 if (!object_wizard->caching || !object_wizard->wizard->create) {
1377 object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1382 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1384 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1385 void *object = NULL;
1386 struct ao2_iterator i;
1387 struct ast_sorcery_object_wizard *wizard;
1388 unsigned int cached = 0;
1390 if (!object_type || ast_strlen_zero(id)) {
1394 i = ao2_iterator_init(object_type->wizards, 0);
1395 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1396 if (wizard->wizard->retrieve_id &&
1397 !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1401 cached = wizard->caching;
1403 ao2_ref(wizard, -1);
1406 ao2_iterator_destroy(&i);
1408 if (!cached && object) {
1409 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1415 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1417 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1418 void *object = NULL;
1419 struct ao2_iterator i;
1420 struct ast_sorcery_object_wizard *wizard;
1421 unsigned int cached = 0;
1427 /* If returning multiple objects create a container to store them in */
1428 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1429 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1434 /* Inquire with the available wizards for retrieval */
1435 i = ao2_iterator_init(object_type->wizards, 0);
1436 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1437 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1438 if (wizard->wizard->retrieve_multiple) {
1439 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1441 } else if (fields && wizard->wizard->retrieve_fields) {
1442 if (wizard->wizard->retrieve_fields) {
1443 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1447 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1451 cached = wizard->caching;
1453 ao2_ref(wizard, -1);
1456 ao2_iterator_destroy(&i);
1458 /* If we are returning a single object and it came from a non-cache source create it in any caches */
1459 if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1460 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1466 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1468 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1469 struct ao2_container *objects;
1470 struct ao2_iterator i;
1471 struct ast_sorcery_object_wizard *wizard;
1473 if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1477 i = ao2_iterator_init(object_type->wizards, 0);
1478 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1479 if (!wizard->wizard->retrieve_regex) {
1483 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1485 ao2_iterator_destroy(&i);
1490 /*! \brief Internal function which returns if the wizard has created the object */
1491 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1493 const struct ast_sorcery_object_wizard *object_wizard = obj;
1494 const struct sorcery_details *details = arg;
1496 return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1499 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1500 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1502 const struct ast_sorcery_object_type_observer *observer = obj;
1504 if (observer->callbacks->created) {
1505 observer->callbacks->created(arg);
1511 /*! \brief Internal callback function which notifies observers that an object has been created */
1512 static int sorcery_observers_notify_create(void *data)
1514 struct sorcery_observer_invocation *invocation = data;
1516 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1517 ao2_cleanup(invocation);
1522 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1524 const struct ast_sorcery_object_details *details = object;
1525 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1526 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1527 struct sorcery_details sdetails = {
1536 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1537 ao2_container_count(object_type->observers)) {
1538 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1540 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1541 ao2_cleanup(invocation);
1545 return object_wizard ? 0 : -1;
1548 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1549 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1551 const struct ast_sorcery_object_type_observer *observer = obj;
1553 if (observer->callbacks->updated) {
1554 observer->callbacks->updated(arg);
1560 /*! \brief Internal callback function which notifies observers that an object has been updated */
1561 static int sorcery_observers_notify_update(void *data)
1563 struct sorcery_observer_invocation *invocation = data;
1565 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1566 ao2_cleanup(invocation);
1571 /*! \brief Internal function which returns if a wizard has updated the object */
1572 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1574 const struct ast_sorcery_object_wizard *object_wizard = obj;
1575 const struct sorcery_details *details = arg;
1577 return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1578 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1581 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1583 const struct ast_sorcery_object_details *details = object;
1584 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1585 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1586 struct sorcery_details sdetails = {
1595 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1596 ao2_container_count(object_type->observers)) {
1597 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1599 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1600 ao2_cleanup(invocation);
1604 return object_wizard ? 0 : -1;
1607 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1608 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1610 const struct ast_sorcery_object_type_observer *observer = obj;
1612 if (observer->callbacks->deleted) {
1613 observer->callbacks->deleted(arg);
1619 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1620 static int sorcery_observers_notify_delete(void *data)
1622 struct sorcery_observer_invocation *invocation = data;
1624 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1625 ao2_cleanup(invocation);
1630 /*! \brief Internal function which returns if a wizard has deleted the object */
1631 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1633 const struct ast_sorcery_object_wizard *object_wizard = obj;
1634 const struct sorcery_details *details = arg;
1636 return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1637 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1640 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1642 const struct ast_sorcery_object_details *details = object;
1643 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1644 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1645 struct sorcery_details sdetails = {
1654 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1655 ao2_container_count(object_type->observers)) {
1656 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1658 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1659 ao2_cleanup(invocation);
1663 return object_wizard ? 0 : -1;
1666 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1669 /* One ref for what we just released, the other for the instances container. */
1670 ao2_wrlock(instances);
1671 if (ao2_ref(sorcery, -1) == 2) {
1672 ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
1674 ao2_unlock(instances);
1678 const char *ast_sorcery_object_get_id(const void *object)
1680 const struct ast_sorcery_object_details *details = object;
1681 return details->object->id;
1684 const char *ast_sorcery_object_get_type(const void *object)
1686 const struct ast_sorcery_object_details *details = object;
1687 return details->object->type;
1690 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1692 const struct ast_sorcery_object_details *details = object;
1693 struct ast_variable *field;
1695 for (field = details->object->extended; field; field = field->next) {
1696 if (!strcmp(field->name + 1, name)) {
1697 return field->value;
1704 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1706 RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1707 struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1708 const struct ast_sorcery_object_details *details = object;
1714 for (field = details->object->extended; field; previous = field, field = field->next) {
1715 if (!strcmp(field->name, name)) {
1717 previous->next = field->next;
1719 details->object->extended = field->next;
1726 extended->next = details->object->extended;
1727 details->object->extended = extended;
1732 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1734 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1735 struct ast_sorcery_object_type_observer *observer;
1738 if (!object_type || !callbacks) {
1742 if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1746 observer->callbacks = callbacks;
1748 if (!ao2_link(object_type->observers, observer)) {
1751 ao2_ref(observer, -1);
1756 /*! \brief Internal callback function for removing an observer */
1757 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1759 const struct ast_sorcery_object_type_observer *observer = obj;
1761 return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1764 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1766 RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
1767 struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
1772 object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1777 ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1778 sorcery_observer_remove, cbs);
1781 int ast_sorcery_object_id_compare(const void *obj_left, const void *obj_right, int flags)
1783 if (!obj_left || !obj_right) {
1786 return strcmp(ast_sorcery_object_get_id(obj_left), ast_sorcery_object_get_id(obj_right));