sorcery.c: Fix off-nominal path ref and memory leak in ast_sorcery_objectset_json_cre...
[asterisk/asterisk.git] / main / sorcery.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Sorcery Data Access Layer API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
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"
47
48 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
49 #undef open
50 #undef close
51
52 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
53 #define WIZARD_BUCKETS 7
54
55 /*! \brief Number of buckets for types (should be prime for performance reasons) */
56 #define TYPE_BUCKETS 53
57
58 /*! \brief Number of buckets for instances (should be prime for performance reasons) */
59 #define INSTANCE_BUCKETS 17
60
61 /*! \brief Thread pool for observers */
62 static struct ast_threadpool *threadpool;
63
64 /*! \brief Structure for internal sorcery object information */
65 struct ast_sorcery_object {
66         /*! \brief Unique identifier of this object */
67         char *id;
68
69         /*! \brief Type of object */
70         char type[MAX_OBJECT_TYPE];
71
72         /*! \brief Optional object destructor */
73         ao2_destructor_fn destructor;
74
75         /*! \brief Extended object fields */
76         struct ast_variable *extended;
77 };
78
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];
83
84         /*! \brief Optional transformation callback */
85         sorcery_transform_handler transform;
86
87         /*! \brief Optional object set apply callback */
88         sorcery_apply_handler apply;
89
90         /*! \brief Optional object copy callback */
91         sorcery_copy_handler copy;
92
93         /*! \brief Optional object diff callback */
94         sorcery_diff_handler diff;
95
96         /*! \brief Wizard instances */
97         struct ao2_container *wizards;
98
99         /*! \brief Object fields */
100         struct ao2_container *fields;
101
102         /*! \brief Configuration framework general information */
103         struct aco_info *info;
104
105         /*! \brief Configuration framework file information */
106         struct aco_file *file;
107
108         /*! \brief Type details */
109         struct aco_type type;
110
111         /*! \brief Observers */
112         struct ao2_container *observers;
113
114         /*! \brief Serializer for observers */
115         struct ast_taskprocessor *serializer;
116
117         /*! \brief Specifies if object type is reloadable or not */
118         unsigned int reloadable:1;
119 };
120
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;
125 };
126
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;
131
132         /*! \brief Pointer to the object */
133         void *object;
134 };
135
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];
140
141         /*! \brief Callback function for translation of a single value */
142         sorcery_field_handler handler;
143
144         /*! \brief Callback function for translation of multiple values */
145         sorcery_fields_handler multiple_handler;
146
147         /*! \brief Position of the field */
148         intptr_t args[];
149 };
150
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;
155
156         /*! \brief Unique data for the wizard */
157         void *data;
158
159         /*! \brief Wizard is acting as an object cache */
160         unsigned int caching:1;
161 };
162
163 /*! \brief Full structure for sorcery */
164 struct ast_sorcery {
165         /*! \brief Container for known object types */
166         struct ao2_container *types;
167         /*! \brief The name of the module owning this sorcery instance */
168         char module_name[0];
169 };
170
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;
175
176         /*! \brief Type of object being loaded */
177         const char *type;
178
179         /*! \brief Whether this is a reload or not */
180         unsigned int reload:1;
181 };
182
183 /*! \brief Registered sorcery wizards */
184 static struct ao2_container *wizards;
185
186 /*! \brief Registered sorcery instances */
187 static struct ao2_container *instances;
188
189 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
190 {
191         int *field = (int *)(obj + args[0]);
192         return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
193 }
194
195 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
196 {
197         unsigned int *field = (unsigned int *)(obj + args[0]);
198         return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
199 }
200
201 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
202 {
203         double *field = (double *)(obj + args[0]);
204         return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
205 }
206
207 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
208 {
209         ast_string_field *field = (const char **)(obj + args[0]);
210         return !(*buf = ast_strdup(*field)) ? -1 : 0;
211 }
212
213 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
214 {
215         unsigned int *field = (unsigned int *)(obj + args[0]);
216         return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
217 }
218
219 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
220 {
221         struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
222         return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
223 }
224
225 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
226 {
227         char *field = (char *)(obj + args[0]);
228         return !(*buf = ast_strdup(field)) ? -1 : 0;
229 }
230
231 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
232 {
233         char tmp_buf[256];
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));
237 }
238
239 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
240 {
241         switch(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;
250
251         default:
252         case OPT_CUSTOM_T: return NULL;
253         }
254
255         return NULL;
256 }
257
258 /*! \brief Hashing function for sorcery wizards */
259 static int sorcery_wizard_hash(const void *obj, const int flags)
260 {
261     const struct ast_sorcery_wizard *object;
262     const char *key;
263
264     switch (flags & OBJ_SEARCH_MASK) {
265     case OBJ_SEARCH_KEY:
266         key = obj;
267         break;
268     case OBJ_SEARCH_OBJECT:
269         object = obj;
270         key = object->name;
271         break;
272     default:
273         ast_assert(0);
274         return 0;
275     }
276     return ast_str_hash(key);
277 }
278
279 /*! \brief Comparator function for sorcery wizards */
280 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
281 {
282         const struct ast_sorcery_wizard *object_left = obj;
283         const struct ast_sorcery_wizard *object_right = arg;
284         const char *right_key = arg;
285         int cmp;
286
287         switch (flags & OBJ_SEARCH_MASK) {
288         case OBJ_SEARCH_OBJECT:
289                 right_key = object_right->name;
290                 /* Fall through */
291         case OBJ_SEARCH_KEY:
292                 cmp = strcmp(object_left->name, right_key);
293                 break;
294         case OBJ_SEARCH_PARTIAL_KEY:
295                 cmp = strncmp(object_left->name, right_key, strlen(right_key));
296                 break;
297         default:
298                 cmp = 0;
299                 break;
300         }
301         if (cmp) {
302                 return 0;
303         }
304         return CMP_MATCH;
305 }
306
307 /*! \brief Cleanup function */
308 static void sorcery_exit(void)
309 {
310         ast_threadpool_shutdown(threadpool);
311         threadpool = NULL;
312 }
313
314 /*! \brief Cleanup function for graceful shutdowns */
315 static void sorcery_cleanup(void)
316 {
317         ao2_cleanup(wizards);
318         wizards = NULL;
319         ao2_cleanup(instances);
320         instances = NULL;
321 }
322
323 /*! \brief Compare function for sorcery instances */
324 static int sorcery_instance_cmp(void *obj, void *arg, int flags)
325 {
326         const struct ast_sorcery *object_left = obj;
327         const struct ast_sorcery *object_right = arg;
328         const char *right_key = arg;
329         int cmp;
330
331         switch (flags & OBJ_SEARCH_MASK) {
332         case OBJ_SEARCH_OBJECT:
333                 right_key = object_right->module_name;
334                 /* Fall through */
335         case OBJ_SEARCH_KEY:
336                 cmp = strcmp(object_left->module_name, right_key);
337                 break;
338         case OBJ_SEARCH_PARTIAL_KEY:
339                 cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
340                 break;
341         default:
342                 cmp = 0;
343                 break;
344         }
345         if (cmp) {
346                 return 0;
347         }
348         return CMP_MATCH;
349 }
350
351 /*! \brief Hashing function for sorcery instances */
352 static int sorcery_instance_hash(const void *obj, const int flags)
353 {
354     const struct ast_sorcery *object;
355     const char *key;
356
357     switch (flags & OBJ_SEARCH_MASK) {
358     case OBJ_SEARCH_KEY:
359         key = obj;
360         break;
361     case OBJ_SEARCH_OBJECT:
362         object = obj;
363         key = object->module_name;
364         break;
365     default:
366         ast_assert(0);
367         return 0;
368     }
369     return ast_str_hash(key);
370 }
371
372 int ast_sorcery_init(void)
373 {
374         struct ast_threadpool_options options = {
375                 .version = AST_THREADPOOL_OPTIONS_VERSION,
376                 .auto_increment = 1,
377                 .max_size = 0,
378                 .idle_timeout = 60,
379                 .initial_size = 0,
380         };
381         ast_assert(wizards == NULL);
382
383         if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
384                 threadpool = NULL;
385                 return -1;
386         }
387
388         if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
389                 ast_threadpool_shutdown(threadpool);
390                 return -1;
391         }
392
393         instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
394                 sorcery_instance_hash, sorcery_instance_cmp);
395         if (!instances) {
396                 sorcery_cleanup();
397                 sorcery_exit();
398                 return -1;
399         }
400
401         ast_register_cleanup(sorcery_cleanup);
402         ast_register_atexit(sorcery_exit);
403
404         return 0;
405 }
406
407 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
408 {
409         struct ast_sorcery_wizard *wizard;
410         int res = -1;
411
412         ast_assert(!ast_strlen_zero(interface->name));
413
414         ao2_lock(wizards);
415
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",
418                         interface->name);
419                 goto done;
420         }
421
422         if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
423                 goto done;
424         }
425
426         *wizard = *interface;
427         wizard->module = module;
428
429         ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
430         res = 0;
431
432         ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
433
434 done:
435         ao2_cleanup(wizard);
436         ao2_unlock(wizards);
437
438         return res;
439 }
440
441 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
442 {
443         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
444
445         if (wizard) {
446                 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
447                 return 0;
448         } else {
449                 return -1;
450         }
451 }
452
453 /*! \brief Destructor called when sorcery structure is destroyed */
454 static void sorcery_destructor(void *obj)
455 {
456         struct ast_sorcery *sorcery = obj;
457
458         ao2_cleanup(sorcery->types);
459 }
460
461 /*! \brief Hashing function for sorcery types */
462 static int sorcery_type_hash(const void *obj, const int flags)
463 {
464     const struct ast_sorcery_object_type *object;
465     const char *key;
466
467     switch (flags & OBJ_SEARCH_MASK) {
468     case OBJ_SEARCH_KEY:
469         key = obj;
470         break;
471     case OBJ_SEARCH_OBJECT:
472         object = obj;
473         key = object->name;
474         break;
475     default:
476         ast_assert(0);
477         return 0;
478     }
479     return ast_str_hash(key);
480 }
481
482 /*! \brief Comparator function for sorcery types */
483 static int sorcery_type_cmp(void *obj, void *arg, int flags)
484 {
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;
488         int cmp;
489
490         switch (flags & OBJ_SEARCH_MASK) {
491         case OBJ_SEARCH_OBJECT:
492                 right_key = object_right->name;
493                 /* Fall through */
494         case OBJ_SEARCH_KEY:
495                 cmp = strcmp(object_left->name, right_key);
496                 break;
497         case OBJ_SEARCH_PARTIAL_KEY:
498                 cmp = strncmp(object_left->name, right_key, strlen(right_key));
499                 break;
500         default:
501                 cmp = 0;
502                 break;
503         }
504         if (cmp) {
505                 return 0;
506         }
507         return CMP_MATCH;
508 }
509
510 struct ast_sorcery *__ast_sorcery_open(const char *module_name)
511 {
512         struct ast_sorcery *sorcery;
513
514         ast_assert(module_name != NULL);
515
516         ao2_wrlock(instances);
517         if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
518                 goto done;
519         }
520
521         if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
522                 goto done;
523         }
524
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);
527                 sorcery = NULL;
528                 goto done;
529         }
530
531         strcpy(sorcery->module_name, module_name); /* Safe */
532         ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
533
534 done:
535         ao2_unlock(instances);
536         return sorcery;
537 }
538
539 /*! \brief Search function for sorcery instances */
540 struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
541 {
542         return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
543 }
544
545 /*! \brief Destructor function for object types */
546 static void sorcery_object_type_destructor(void *obj)
547 {
548         struct ast_sorcery_object_type *object_type = obj;
549
550         ao2_cleanup(object_type->wizards);
551         ao2_cleanup(object_type->fields);
552         ao2_cleanup(object_type->observers);
553
554         if (object_type->info) {
555                 aco_info_destroy(object_type->info);
556                 ast_free(object_type->info);
557         }
558
559         ast_free(object_type->file);
560
561         ast_taskprocessor_unreference(object_type->serializer);
562 }
563
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)
566 {
567         struct ast_sorcery_object_type *object_type;
568         char uuid[AST_UUID_STR_LEN];
569
570         if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
571                 return NULL;
572         }
573
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);
577                 return NULL;
578         }
579
580         if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
581                 ao2_ref(object_type, -1);
582                 return NULL;
583         }
584
585         if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
586                 ao2_ref(object_type, -1);
587                 return NULL;
588         }
589
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);
592                 return NULL;
593         }
594
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);
597                 return NULL;
598         }
599
600         if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
601                 ao2_ref(object_type, -1);
602                 return NULL;
603         }
604
605         if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
606                 ao2_ref(object_type, -1);
607                 return NULL;
608         }
609
610         object_type->info->files[0] = object_type->file;
611         object_type->info->files[1] = NULL;
612         object_type->info->module = module;
613
614         ast_copy_string(object_type->name, type, sizeof(object_type->name));
615
616         return object_type;
617 }
618
619 /*! \brief Object wizard destructor */
620 static void sorcery_object_wizard_destructor(void *obj)
621 {
622         struct ast_sorcery_object_wizard *object_wizard = obj;
623
624         if (object_wizard->data) {
625                 object_wizard->wizard->close(object_wizard->data);
626         }
627
628         if (object_wizard->wizard) {
629                 ast_module_unref(object_wizard->wizard->module);
630         }
631 }
632
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)
635 {
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);
639         int created = 0;
640
641         if (!wizard || !object_wizard) {
642                 return -1;
643         }
644
645         if (!object_type) {
646                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
647                         return -1;
648                 }
649                 created = 1;
650         }
651
652         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
653                 return -1;
654         }
655
656         ast_module_ref(wizard->module);
657
658         object_wizard->wizard = wizard;
659         object_wizard->caching = caching;
660
661         ao2_link(object_type->wizards, object_wizard);
662
663         if (created) {
664                 ao2_link(sorcery->types, object_type);
665         }
666
667         return 0;
668 }
669
670 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
671 {
672         struct ast_flags flags = { 0 };
673         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
674         struct ast_variable *mapping;
675         int res = 0;
676
677         if (!config || config == CONFIG_STATUS_FILEINVALID) {
678                 return -1;
679         }
680
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;
689
690                 /* If no object type or wizard exists just skip, nothing we can do */
691                 if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
692                         continue;
693                 }
694
695                 /* If the wizard is configured as a cache treat it as such */
696                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
697                         caching = 1;
698                 }
699
700                 /* Any error immediately causes us to stop */
701                 if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching)) {
702                         res = -1;
703                         break;
704                 }
705         }
706
707         ast_config_destroy(config);
708
709         return res;
710 }
711
712 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
713 {
714         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
715
716         /* Defaults can not be added if any existing mapping exists */
717         if (object_type) {
718                 return -1;
719         }
720
721         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
722 }
723
724 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
725 {
726         return ast_sorcery_object_set_extended(obj, var->name, var->value);
727 }
728
729 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
730 {
731         const struct ast_sorcery_object_details *details = obj;
732
733         if (details->object->extended) {
734                 *fields = ast_variables_dup(details->object->extended);
735         } else {
736                 *fields = NULL;
737         }
738
739         return 0;
740 }
741
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)
743 {
744         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
745
746         if (!object_type || object_type->type.item_alloc) {
747                 return -1;
748         }
749
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;
755
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;
761
762         if (aco_info_init(object_type->info)) {
763                 return -1;
764         }
765
766         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
767                 return -1;
768         }
769
770         return 0;
771 }
772
773 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
774 {
775         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
776
777         if (!object_type) {
778                 return;
779         }
780
781         object_type->copy = copy;
782 }
783
784 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
785 {
786         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
787
788         if (!object_type) {
789                 return;
790         }
791
792         object_type->diff = diff;
793 }
794
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)
796 {
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);
799
800         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
801                 return -1;
802         }
803
804         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
805         object_field->multiple_handler = sorcery_handler;
806
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);
809
810         return 0;
811 }
812
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, ...)
815 {
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);
818         int pos;
819         va_list args;
820
821         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
822                 return -1;
823         }
824
825         if (!sorcery_handler) {
826                 sorcery_handler = sorcery_field_default_handler(opt_type);
827         }
828
829         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
830                 return -1;
831         }
832
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;
836
837         va_start(args, argc);
838         for (pos = 0; pos < argc; pos++) {
839                 object_field->args[pos] = va_arg(args, size_t);
840         }
841         va_end(args);
842
843         ao2_link(object_type->fields, object_field);
844
845         /* TODO: Improve this hack */
846         if (!argc) {
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]);
857         } else {
858                 ast_assert(0); /* The hack... she does us no good for this */
859         }
860
861         return 0;
862 }
863
864 /*! \brief Retrieves whether or not the type is reloadable */
865 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
866 {
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;
870 }
871
872 static int sorcery_wizard_load(void *obj, void *arg, int flags)
873 {
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);
877
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);
881                 return 0;
882         }
883
884         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
885
886         if (load) {
887                 load(wizard->data, details->sorcery, details->type);
888         }
889
890         return 0;
891 }
892
893 /*! \brief Destructor for observer invocation */
894 static void sorcery_observer_invocation_destroy(void *obj)
895 {
896         struct sorcery_observer_invocation *invocation = obj;
897
898         ao2_cleanup(invocation->object_type);
899         ao2_cleanup(invocation->object);
900 }
901
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)
904 {
905         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
906
907         if (!invocation) {
908                 return NULL;
909         }
910
911         ao2_ref(object_type, +1);
912         invocation->object_type = object_type;
913
914         if (object) {
915                 ao2_ref(object, +1);
916                 invocation->object = object;
917         }
918
919         return invocation;
920 }
921
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)
924 {
925         const struct ast_sorcery_object_type_observer *observer = obj;
926
927         if (observer->callbacks->loaded) {
928                 observer->callbacks->loaded(arg);
929         }
930
931         return 0;
932 }
933
934 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
935 static int sorcery_observers_notify_loaded(void *data)
936 {
937         struct sorcery_observer_invocation *invocation = data;
938
939         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
940         ao2_cleanup(invocation);
941
942         return 0;
943 }
944
945 static int sorcery_object_load(void *obj, void *arg, int flags)
946 {
947         struct ast_sorcery_object_type *type = obj;
948         struct sorcery_load_details *details = arg;
949
950         details->type = type->name;
951         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
952
953         if (ao2_container_count(type->observers)) {
954                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
955
956                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
957                         ao2_cleanup(invocation);
958                 }
959         }
960
961         return 0;
962 }
963
964 void ast_sorcery_load(const struct ast_sorcery *sorcery)
965 {
966         struct sorcery_load_details details = {
967                 .sorcery = sorcery,
968                 .reload = 0,
969         };
970
971         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
972 }
973
974 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
975 {
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 = {
978                 .sorcery = sorcery,
979                 .reload = 0,
980         };
981
982         if (!object_type) {
983                 return;
984         }
985
986         sorcery_object_load(object_type, &details, 0);
987 }
988
989 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
990 {
991         struct sorcery_load_details details = {
992                 .sorcery = sorcery,
993                 .reload = 1,
994         };
995
996         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
997 }
998
999 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
1000 {
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 = {
1003                 .sorcery = sorcery,
1004                 .reload = 1,
1005         };
1006
1007         if (!object_type) {
1008                 return;
1009         }
1010
1011         sorcery_object_load(object_type, &details, 0);
1012 }
1013
1014 void ast_sorcery_ref(struct ast_sorcery *sorcery)
1015 {
1016         ao2_ref(sorcery, +1);
1017 }
1018
1019 static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1020 {
1021         struct ast_variable *tmp = NULL;
1022         char *buf = NULL;
1023
1024         if (!object_field->handler) {
1025                 return NULL;
1026         }
1027
1028         if (!(object_field->handler(object, object_field->args, &buf))) {
1029                 tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
1030         }
1031         ast_free(buf);
1032
1033         return tmp;
1034 }
1035
1036 static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1037 {
1038         struct ast_variable *tmp = NULL;
1039
1040         if (!object_field->multiple_handler) {
1041                 return NULL;
1042         }
1043
1044         if (object_field->multiple_handler(object, &tmp)) {
1045                 ast_variables_destroy(tmp);
1046                 tmp = NULL;
1047         }
1048
1049         return tmp;
1050 }
1051
1052 struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
1053         const void *object,     enum ast_sorcery_field_handler_flags flags)
1054 {
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;
1061
1062         if (!object_type) {
1063                 return NULL;
1064         }
1065
1066         i = ao2_iterator_init(object_type->fields, 0);
1067
1068         for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1069                 struct ast_variable *tmp;
1070
1071                 switch (flags) {
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))) {
1075                                 break;
1076                         }
1077                         continue;
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))) {
1081                                 break;
1082                         }
1083                         continue;
1084                 case AST_HANDLER_ONLY_LIST:
1085                         if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
1086                                 break;
1087                         }
1088                         continue;
1089                 case AST_HANDLER_ONLY_STRING:
1090                         if ((tmp = get_single_field_as_var_list(object, object_field))) {
1091                                 break;
1092                         }
1093                         continue;
1094                 default:
1095                         continue;
1096                 }
1097
1098                 tail = ast_variable_list_append_hint(&head, tail, tmp);
1099         }
1100
1101         ao2_iterator_destroy(&i);
1102
1103         return head;
1104 }
1105
1106 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
1107 {
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();
1113         int res = 0;
1114
1115         if (!object_type || !json) {
1116                 return NULL;
1117         }
1118
1119         i = ao2_iterator_init(object_type->fields, 0);
1120
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;
1125
1126                         if ((res = object_field->multiple_handler(object, &tmp))) {
1127                                 ast_variables_destroy(tmp);
1128                                 ao2_ref(object_field, -1);
1129                                 break;
1130                         }
1131
1132                         for (field = tmp; field; field = field->next) {
1133                                 struct ast_json *value = ast_json_string_create(field->value);
1134
1135                                 if (!value || ast_json_object_set(json, field->name, value)) {
1136                                         res = -1;
1137                                         break;
1138                                 }
1139                         }
1140
1141                         ast_variables_destroy(tmp);
1142                 } else if (object_field->handler) {
1143                         char *buf = NULL;
1144                         struct ast_json *value = NULL;
1145
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)) {
1149                                 res = -1;
1150                         }
1151
1152                         ast_free(buf);
1153                 } else {
1154                         continue;
1155                 }
1156         }
1157
1158         ao2_iterator_destroy(&i);
1159
1160         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
1161         if (res) {
1162                 ast_json_unref(json);
1163                 json = NULL;
1164         }
1165
1166         return json;
1167 }
1168
1169 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
1170 {
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;
1175         int res = 0;
1176
1177         if (!object_type) {
1178                 return -1;
1179         }
1180
1181         if (object_type->transform && (transformed = object_type->transform(objectset))) {
1182                 field = transformed;
1183         } else {
1184                 field = objectset;
1185         }
1186
1187         for (; field; field = field->next) {
1188                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
1189                         break;
1190                 }
1191         }
1192
1193         if (!res && object_type->apply) {
1194                 res = object_type->apply(sorcery, object);
1195         }
1196
1197         return res;
1198 }
1199
1200 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1201 {
1202         const struct ast_variable *field;
1203
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)) {
1207                         return field;
1208                 }
1209         }
1210
1211         return NULL;
1212 }
1213
1214 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1215 {
1216         const struct ast_variable *field;
1217         int res = 0;
1218
1219         *changes = NULL;
1220
1221         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1222         if (original == modified) {
1223                 return 0;
1224         }
1225
1226         for (field = modified; field; field = field->next) {
1227                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1228
1229                 if (!old || strcmp(old->value, field->value)) {
1230                         struct ast_variable *tmp;
1231
1232                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1233                                 res = -1;
1234                                 break;
1235                         }
1236
1237                         tmp->next = *changes;
1238                         *changes = tmp;
1239                 }
1240         }
1241
1242         /* If an error occurred do not return a partial changeset */
1243         if (res) {
1244                 ast_variables_destroy(*changes);
1245                 *changes = NULL;
1246         }
1247
1248         return res;
1249 }
1250
1251 static void sorcery_object_destructor(void *object)
1252 {
1253         struct ast_sorcery_object_details *details = object;
1254
1255         if (details->object->destructor) {
1256                 details->object->destructor(object);
1257         }
1258
1259         ast_variables_destroy(details->object->extended);
1260         ast_free(details->object->id);
1261 }
1262
1263 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1264 {
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;
1267
1268         if (!object) {
1269                 return NULL;
1270         }
1271
1272         details->object = object + size;
1273         details->object->destructor = destructor;
1274
1275         return object;
1276 }
1277
1278 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1279 {
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;
1282
1283         if (!object_type || !object_type->type.item_alloc ||
1284                 !(details = object_type->type.item_alloc(id))) {
1285                 return NULL;
1286         }
1287
1288         if (ast_strlen_zero(id)) {
1289                 char uuid[AST_UUID_STR_LEN];
1290
1291                 ast_uuid_generate_str(uuid, sizeof(uuid));
1292                 details->object->id = ast_strdup(uuid);
1293         } else {
1294                 details->object->id = ast_strdup(id);
1295         }
1296
1297         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1298
1299         if (aco_set_defaults(&object_type->type, id, details)) {
1300                 ao2_ref(details, -1);
1301                 return NULL;
1302         }
1303
1304         return details;
1305 }
1306
1307 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1308 {
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);
1313         int res = 0;
1314
1315         if (!copy) {
1316                 return NULL;
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);
1321         } else {
1322                 /* No native copy available and could not create an objectset, this copy has failed */
1323                 res = -1;
1324         }
1325
1326         if (res) {
1327                 ao2_cleanup(copy);
1328                 copy = NULL;
1329         }
1330
1331         return copy;
1332 }
1333
1334 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1335 {
1336         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1337
1338         *changes = NULL;
1339
1340         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1341                 return -1;
1342         }
1343
1344         if (original == modified) {
1345                 return 0;
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);
1349
1350                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1351                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1352
1353                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1354         } else {
1355                 return object_type->diff(original, modified, changes);
1356         }
1357 }
1358
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 */
1364         void *obj;
1365 };
1366
1367 /*! \brief Internal function used to create an object in caching wizards */
1368 static int sorcery_cache_create(void *obj, void *arg, int flags)
1369 {
1370         const struct ast_sorcery_object_wizard *object_wizard = obj;
1371         const struct sorcery_details *details = arg;
1372
1373         if (!object_wizard->caching || !object_wizard->wizard->create) {
1374                 return 0;
1375         }
1376
1377         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1378
1379         return 0;
1380 }
1381
1382 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1383 {
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;
1389
1390         if (!object_type || ast_strlen_zero(id)) {
1391                 return NULL;
1392         }
1393
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))) {
1398                         continue;
1399                 }
1400
1401                 cached = wizard->caching;
1402
1403                 ao2_ref(wizard, -1);
1404                 break;
1405         }
1406         ao2_iterator_destroy(&i);
1407
1408         if (!cached && object) {
1409                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1410         }
1411
1412         return object;
1413 }
1414
1415 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1416 {
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;
1422
1423         if (!object_type) {
1424                 return NULL;
1425         }
1426
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))) {
1430                         return NULL;
1431                 }
1432         }
1433
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);
1440                         }
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);
1444                         }
1445                 }
1446
1447                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1448                         continue;
1449                 }
1450
1451                 cached = wizard->caching;
1452
1453                 ao2_ref(wizard, -1);
1454                 break;
1455         }
1456         ao2_iterator_destroy(&i);
1457
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);
1461         }
1462
1463         return object;
1464 }
1465
1466 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1467 {
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;
1472
1473         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1474                 return NULL;
1475         }
1476
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) {
1480                         continue;
1481                 }
1482
1483                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1484         }
1485         ao2_iterator_destroy(&i);
1486
1487         return objects;
1488 }
1489
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)
1492 {
1493         const struct ast_sorcery_object_wizard *object_wizard = obj;
1494         const struct sorcery_details *details = arg;
1495
1496         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1497 }
1498
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)
1501 {
1502         const struct ast_sorcery_object_type_observer *observer = obj;
1503
1504         if (observer->callbacks->created) {
1505                 observer->callbacks->created(arg);
1506         }
1507
1508         return 0;
1509 }
1510
1511 /*! \brief Internal callback function which notifies observers that an object has been created */
1512 static int sorcery_observers_notify_create(void *data)
1513 {
1514         struct sorcery_observer_invocation *invocation = data;
1515
1516         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1517         ao2_cleanup(invocation);
1518
1519         return 0;
1520 }
1521
1522 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1523 {
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 = {
1528                 .sorcery = sorcery,
1529                 .obj = object,
1530         };
1531
1532         if (!object_type) {
1533                 return -1;
1534         }
1535
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);
1539
1540                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1541                         ao2_cleanup(invocation);
1542                 }
1543         }
1544
1545         return object_wizard ? 0 : -1;
1546 }
1547
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)
1550 {
1551         const struct ast_sorcery_object_type_observer *observer = obj;
1552
1553         if (observer->callbacks->updated) {
1554                 observer->callbacks->updated(arg);
1555         }
1556
1557         return 0;
1558 }
1559
1560 /*! \brief Internal callback function which notifies observers that an object has been updated */
1561 static int sorcery_observers_notify_update(void *data)
1562 {
1563         struct sorcery_observer_invocation *invocation = data;
1564
1565         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1566         ao2_cleanup(invocation);
1567
1568         return 0;
1569 }
1570
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)
1573 {
1574         const struct ast_sorcery_object_wizard *object_wizard = obj;
1575         const struct sorcery_details *details = arg;
1576
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;
1579 }
1580
1581 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1582 {
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 = {
1587                 .sorcery = sorcery,
1588                 .obj = object,
1589         };
1590
1591         if (!object_type) {
1592                 return -1;
1593         }
1594
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);
1598
1599                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1600                         ao2_cleanup(invocation);
1601                 }
1602         }
1603
1604         return object_wizard ? 0 : -1;
1605 }
1606
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)
1609 {
1610         const struct ast_sorcery_object_type_observer *observer = obj;
1611
1612         if (observer->callbacks->deleted) {
1613                 observer->callbacks->deleted(arg);
1614         }
1615
1616         return 0;
1617 }
1618
1619 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1620 static int sorcery_observers_notify_delete(void *data)
1621 {
1622         struct sorcery_observer_invocation *invocation = data;
1623
1624         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1625         ao2_cleanup(invocation);
1626
1627         return 0;
1628 }
1629
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)
1632 {
1633         const struct ast_sorcery_object_wizard *object_wizard = obj;
1634         const struct sorcery_details *details = arg;
1635
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;
1638 }
1639
1640 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1641 {
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 = {
1646                 .sorcery = sorcery,
1647                 .obj = object,
1648         };
1649
1650         if (!object_type) {
1651                 return -1;
1652         }
1653
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);
1657
1658                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1659                         ao2_cleanup(invocation);
1660                 }
1661         }
1662
1663         return object_wizard ? 0 : -1;
1664 }
1665
1666 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1667 {
1668         if (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);
1673                 }
1674                 ao2_unlock(instances);
1675         }
1676 }
1677
1678 const char *ast_sorcery_object_get_id(const void *object)
1679 {
1680         const struct ast_sorcery_object_details *details = object;
1681         return details->object->id;
1682 }
1683
1684 const char *ast_sorcery_object_get_type(const void *object)
1685 {
1686         const struct ast_sorcery_object_details *details = object;
1687         return details->object->type;
1688 }
1689
1690 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1691 {
1692         const struct ast_sorcery_object_details *details = object;
1693         struct ast_variable *field;
1694
1695         for (field = details->object->extended; field; field = field->next) {
1696                 if (!strcmp(field->name + 1, name)) {
1697                         return field->value;
1698                 }
1699         }
1700
1701         return NULL;
1702 }
1703
1704 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1705 {
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;
1709
1710         if (!extended) {
1711                 return -1;
1712         }
1713
1714         for (field = details->object->extended; field; previous = field, field = field->next) {
1715                 if (!strcmp(field->name, name)) {
1716                         if (previous) {
1717                                 previous->next = field->next;
1718                         } else {
1719                                 details->object->extended = field->next;
1720                         }
1721                         field->next = NULL;
1722                         break;
1723                 }
1724         }
1725
1726         extended->next = details->object->extended;
1727         details->object->extended = extended;
1728
1729         return 0;
1730 }
1731
1732 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1733 {
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;
1736         int res;
1737
1738         if (!object_type || !callbacks) {
1739                 return -1;
1740         }
1741
1742         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1743                 return -1;
1744         }
1745
1746         observer->callbacks = callbacks;
1747         res = 0;
1748         if (!ao2_link(object_type->observers, observer)) {
1749                 res = -1;
1750         }
1751         ao2_ref(observer, -1);
1752
1753         return res;
1754 }
1755
1756 /*! \brief Internal callback function for removing an observer */
1757 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1758 {
1759         const struct ast_sorcery_object_type_observer *observer = obj;
1760
1761         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1762 }
1763
1764 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1765 {
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. */
1768
1769         if (!sorcery) {
1770                 return;
1771         }
1772         object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1773         if (!object_type) {
1774                 return;
1775         }
1776
1777         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1778                 sorcery_observer_remove, cbs);
1779 }
1780
1781 int ast_sorcery_object_id_compare(const void *obj_left, const void *obj_right, int flags)
1782 {
1783         if (!obj_left || !obj_right) {
1784                 return 0;
1785         }
1786         return strcmp(ast_sorcery_object_get_id(obj_left), ast_sorcery_object_get_id(obj_right));
1787 }