Resolve some memory leaks due to incorrect for loop / ao2 ref usage.
[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/strings.h"
38 #include "asterisk/config_options.h"
39 #include "asterisk/netsock2.h"
40 #include "asterisk/module.h"
41 #include "asterisk/taskprocessor.h"
42 #include "asterisk/threadpool.h"
43 #include "asterisk/json.h"
44
45 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
46 #undef open
47 #undef close
48
49 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
50 #define WIZARD_BUCKETS 7
51
52 /*! \brief Number of buckets for types (should be prime for performance reasons) */
53 #define TYPE_BUCKETS 53
54
55 /*! \brief Maximum length of an object field name */
56 #define MAX_OBJECT_FIELD 128
57
58 /*! \brief Thread pool for observers */
59 static struct ast_threadpool *threadpool;
60
61 /*! \brief Structure for internal sorcery object information */
62 struct ast_sorcery_object {
63         /*! \brief Unique identifier of this object */
64         char *id;
65
66         /*! \brief Type of object */
67         char type[MAX_OBJECT_TYPE];
68
69         /*! \brief Optional object destructor */
70         ao2_destructor_fn destructor;
71
72         /*! \brief Extended object fields */
73         struct ast_variable *extended;
74 };
75
76 /*! \brief Structure for registered object type */
77 struct ast_sorcery_object_type {
78         /*! \brief Unique name of the object type */
79         char name[MAX_OBJECT_TYPE];
80
81         /*! \brief Optional transformation callback */
82         sorcery_transform_handler transform;
83
84         /*! \brief Optional object set apply callback */
85         sorcery_apply_handler apply;
86
87         /*! \brief Optional object copy callback */
88         sorcery_copy_handler copy;
89
90         /*! \brief Optional object diff callback */
91         sorcery_diff_handler diff;
92
93         /*! \brief Wizard instances */
94         struct ao2_container *wizards;
95
96         /*! \brief Object fields */
97         struct ao2_container *fields;
98
99         /*! \brief Configuration framework general information */
100         struct aco_info *info;
101
102         /*! \brief Configuration framework file information */
103         struct aco_file *file;
104
105         /*! \brief Type details */
106         struct aco_type type;
107
108         /*! \brief Observers */
109         struct ao2_container *observers;
110
111         /*! \brief Serializer for observers */
112         struct ast_taskprocessor *serializer;
113
114         /*! \brief Specifies if object type is reloadable or not */
115         unsigned int reloadable:1;
116 };
117
118 /*! \brief Structure for registered object type observer */
119 struct ast_sorcery_object_type_observer {
120         /*! \brief Pointer to the observer implementation */
121         const struct ast_sorcery_observer *callbacks;
122 };
123
124 /*! \brief Structure used for observer invocations */
125 struct sorcery_observer_invocation {
126         /*! \brief Pointer to the object type */
127         struct ast_sorcery_object_type *object_type;
128
129         /*! \brief Pointer to the object */
130         void *object;
131 };
132
133 /*! \brief Structure for registered object field */
134 struct ast_sorcery_object_field {
135         /*! \brief Name of the field */
136         char name[MAX_OBJECT_FIELD];
137
138         /*! \brief Callback function for translation of a single value */
139         sorcery_field_handler handler;
140
141         /*! \brief Callback function for translation of multiple values */
142         sorcery_fields_handler multiple_handler;
143
144         /*! \brief Position of the field */
145         intptr_t args[];
146 };
147
148 /*! \brief Structure for a wizard instance which operates on objects */
149 struct ast_sorcery_object_wizard {
150         /*! \brief Wizard interface itself */
151         struct ast_sorcery_wizard *wizard;
152
153         /*! \brief Unique data for the wizard */
154         void *data;
155
156         /*! \brief Wizard is acting as an object cache */
157         unsigned int caching:1;
158 };
159
160 /*! \brief Full structure for sorcery */
161 struct ast_sorcery {
162         /*! \brief Container for known object types */
163         struct ao2_container *types;
164 };
165
166 /*! \brief Structure for passing load/reload details */
167 struct sorcery_load_details {
168         /*! \brief Sorcery structure in use */
169         const struct ast_sorcery *sorcery;
170
171         /*! \brief Type of object being loaded */
172         const char *type;
173
174         /*! \brief Whether this is a reload or not */
175         unsigned int reload:1;
176 };
177
178 /*! \brief Registered sorcery wizards */
179 struct ao2_container *wizards;
180
181 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
182 {
183         int *field = (int *)(obj + args[0]);
184         return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
185 }
186
187 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
188 {
189         unsigned int *field = (unsigned int *)(obj + args[0]);
190         return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
191 }
192
193 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
194 {
195         double *field = (double *)(obj + args[0]);
196         return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
197 }
198
199 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
200 {
201         ast_string_field *field = (const char **)(obj + args[0]);
202         return !(*buf = ast_strdup(*field)) ? -1 : 0;
203 }
204
205 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
206 {
207         unsigned int *field = (unsigned int *)(obj + args[0]);
208         return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
209 }
210
211 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
212 {
213         struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
214         return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
215 }
216
217 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
218 {
219         char *field = (char *)(obj + args[0]);
220         return !(*buf = ast_strdup(field)) ? -1 : 0;
221 }
222
223 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
224 {
225         switch(type) {
226         case OPT_BOOL_T: return bool_handler_fn;
227         case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
228         case OPT_DOUBLE_T: return double_handler_fn;
229         case OPT_INT_T: return int_handler_fn;
230         case OPT_SOCKADDR_T: return sockaddr_handler_fn;
231         case OPT_STRINGFIELD_T: return stringfield_handler_fn;
232         case OPT_UINT_T: return uint_handler_fn;
233
234         default:
235         case OPT_CUSTOM_T: return NULL;
236         }
237
238         return NULL;
239 }
240
241 /*! \brief Hashing function for sorcery wizards */
242 static int sorcery_wizard_hash(const void *obj, const int flags)
243 {
244         const struct ast_sorcery_wizard *wizard = obj;
245         const char *name = obj;
246
247         return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
248 }
249
250 /*! \brief Comparator function for sorcery wizards */
251 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
252 {
253         struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
254         const char *name = arg;
255
256         return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
257 }
258
259 /*! \brief Cleanup function */
260 static void sorcery_exit(void)
261 {
262         ast_threadpool_shutdown(threadpool);
263         threadpool = NULL;
264 }
265
266 /*! \brief Cleanup function for graceful shutdowns */
267 static void sorcery_cleanup(void)
268 {
269         ao2_cleanup(wizards);
270 }
271
272 int ast_sorcery_init(void)
273 {
274         struct ast_threadpool_options options = {
275                 .version = AST_THREADPOOL_OPTIONS_VERSION,
276                 .auto_increment = 1,
277                 .max_size = 0,
278                 .idle_timeout = 60,
279                 .initial_size = 0,
280         };
281         ast_assert(wizards == NULL);
282
283         if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
284                 threadpool = NULL;
285                 return -1;
286         }
287
288         if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
289                 ast_threadpool_shutdown(threadpool);
290                 return -1;
291         }
292
293         ast_register_cleanup(sorcery_cleanup);
294         ast_register_atexit(sorcery_exit);
295
296         return 0;
297 }
298
299 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
300 {
301         struct ast_sorcery_wizard *wizard;
302         int res = -1;
303
304         ast_assert(!ast_strlen_zero(interface->name));
305
306         ao2_lock(wizards);
307
308         if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
309                 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
310                         interface->name);
311                 goto done;
312         }
313
314         if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
315                 goto done;
316         }
317
318         *wizard = *interface;
319         wizard->module = module;
320
321         ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
322         res = 0;
323
324         ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
325
326 done:
327         ao2_cleanup(wizard);
328         ao2_unlock(wizards);
329
330         return res;
331 }
332
333 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
334 {
335         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
336
337         if (wizard) {
338                 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
339                 return 0;
340         } else {
341                 return -1;
342         }
343 }
344
345 /*! \brief Destructor called when sorcery structure is destroyed */
346 static void sorcery_destructor(void *obj)
347 {
348         struct ast_sorcery *sorcery = obj;
349
350         ao2_cleanup(sorcery->types);
351 }
352
353 /*! \brief Hashing function for sorcery types */
354 static int sorcery_type_hash(const void *obj, const int flags)
355 {
356         const struct ast_sorcery_object_type *type = obj;
357         const char *name = obj;
358
359         return ast_str_hash(flags & OBJ_KEY ? name : type->name);
360 }
361
362 /*! \brief Comparator function for sorcery types */
363 static int sorcery_type_cmp(void *obj, void *arg, int flags)
364 {
365         struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
366         const char *name = arg;
367
368         return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
369 }
370
371 struct ast_sorcery *ast_sorcery_open(void)
372 {
373         struct ast_sorcery *sorcery;
374
375         if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
376                 return NULL;
377         }
378
379         if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
380                 ao2_ref(sorcery, -1);
381                 sorcery = NULL;
382         }
383
384         return sorcery;
385 }
386
387 /*! \brief Destructor function for object types */
388 static void sorcery_object_type_destructor(void *obj)
389 {
390         struct ast_sorcery_object_type *object_type = obj;
391
392         ao2_cleanup(object_type->wizards);
393         ao2_cleanup(object_type->fields);
394         ao2_cleanup(object_type->observers);
395
396         if (object_type->info) {
397                 aco_info_destroy(object_type->info);
398                 ast_free(object_type->info);
399         }
400
401         ast_free(object_type->file);
402
403         ast_taskprocessor_unreference(object_type->serializer);
404 }
405
406 /*! \brief Internal function which allocates an object type structure */
407 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
408 {
409         struct ast_sorcery_object_type *object_type;
410         char uuid[AST_UUID_STR_LEN];
411
412         if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
413                 return NULL;
414         }
415
416         /* Order matters for object wizards */
417         if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
418                 ao2_ref(object_type, -1);
419                 return NULL;
420         }
421
422         if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
423                 ao2_ref(object_type, -1);
424                 return NULL;
425         }
426
427         if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
428                 ao2_ref(object_type, -1);
429                 return NULL;
430         }
431
432         if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
433                 ao2_ref(object_type, -1);
434                 return NULL;
435         }
436
437         if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
438                 ao2_ref(object_type, -1);
439                 return NULL;
440         }
441
442         if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
443                 ao2_ref(object_type, -1);
444                 return NULL;
445         }
446
447         if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
448                 ao2_ref(object_type, -1);
449                 return NULL;
450         }
451
452         object_type->info->files[0] = object_type->file;
453         object_type->info->files[1] = NULL;
454         object_type->info->module = module;
455
456         ast_copy_string(object_type->name, type, sizeof(object_type->name));
457
458         return object_type;
459 }
460
461 /*! \brief Object wizard destructor */
462 static void sorcery_object_wizard_destructor(void *obj)
463 {
464         struct ast_sorcery_object_wizard *object_wizard = obj;
465
466         if (object_wizard->data) {
467                 object_wizard->wizard->close(object_wizard->data);
468         }
469
470         if (object_wizard->wizard) {
471                 ast_module_unref(object_wizard->wizard->module);
472         }
473 }
474
475 /*! \brief Internal function which creates an object type and adds a wizard mapping */
476 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)
477 {
478         RAII_VAR(struct ast_sorcery_object_type *, object_type,  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
479         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
480         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
481         int created = 0;
482
483         if (!wizard || !object_wizard) {
484                 return -1;
485         }
486
487         if (!object_type) {
488                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
489                         return -1;
490                 }
491                 created = 1;
492         }
493
494         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
495                 return -1;
496         }
497
498         ast_module_ref(wizard->module);
499
500         object_wizard->wizard = wizard;
501         object_wizard->caching = caching;
502
503         ao2_link(object_type->wizards, object_wizard);
504
505         if (created) {
506                 ao2_link(sorcery->types, object_type);
507         }
508
509         return 0;
510 }
511
512 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
513 {
514         struct ast_flags flags = { 0 };
515         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
516         struct ast_variable *mapping;
517         int res = 0;
518
519         if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
520                 return -1;
521         }
522
523         for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
524                 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
525                 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
526                 char *options = mapping_name, *name = strsep(&options, "/");
527                 char *data = mapping_value, *wizard = strsep(&data, ",");
528                 unsigned int caching = 0;
529
530                 /* If no wizard exists just skip, nothing we can do */
531                 if (ast_strlen_zero(wizard)) {
532                         continue;
533                 }
534
535                 /* If the wizard is configured as a cache treat it as such */
536                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
537                         caching = 1;
538                 }
539
540                 /* Any error immediately causes us to stop */
541                 if ((res = sorcery_apply_wizard_mapping(sorcery, name, module, wizard, data, caching))) {
542                         break;
543                 }
544         }
545
546         ast_config_destroy(config);
547
548         return res;
549 }
550
551 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
552 {
553         RAII_VAR(struct ast_sorcery_object_type *, object_type,  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
554
555         /* Defaults can not be added if any existing mapping exists */
556         if (object_type) {
557                 return -1;
558         }
559
560         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
561 }
562
563 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
564 {
565         return ast_sorcery_object_set_extended(obj, var->name, var->value);
566 }
567
568 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
569 {
570         const struct ast_sorcery_object_details *details = obj;
571
572         if (details->object->extended) {
573                 *fields = ast_variables_dup(details->object->extended);
574         } else {
575                 *fields = NULL;
576         }
577
578         return 0;
579 }
580
581 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)
582 {
583         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
584
585         if (!object_type || object_type->type.item_alloc) {
586                 return -1;
587         }
588
589         object_type->type.name = object_type->name;
590         object_type->type.type = ACO_ITEM;
591         object_type->type.category = ".?";
592         object_type->type.item_alloc = alloc;
593         object_type->type.hidden = hidden;
594
595         object_type->reloadable = reloadable;
596         object_type->transform = transform;
597         object_type->apply = apply;
598         object_type->file->types[0] = &object_type->type;
599         object_type->file->types[1] = NULL;
600
601         if (aco_info_init(object_type->info)) {
602                 return -1;
603         }
604
605         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
606                 return -1;
607         }
608
609         return 0;
610 }
611
612 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
613 {
614         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
615
616         if (!object_type) {
617                 return;
618         }
619
620         object_type->copy = copy;
621 }
622
623 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
624 {
625         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
626
627         if (!object_type) {
628                 return;
629         }
630
631         object_type->diff = diff;
632 }
633
634 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)
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_object_field *, object_field, NULL, ao2_cleanup);
638
639         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
640                 return -1;
641         }
642
643         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
644         object_field->multiple_handler = sorcery_handler;
645
646         ao2_link(object_type->fields, object_field);
647         __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
648
649         return 0;
650 }
651
652 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,
653                                         aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
654 {
655         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
656         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
657         int pos;
658         va_list args;
659
660         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
661                 return -1;
662         }
663
664         if (!sorcery_handler) {
665                 sorcery_handler = sorcery_field_default_handler(opt_type);
666         }
667
668         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
669                 return -1;
670         }
671
672         ast_copy_string(object_field->name, name, sizeof(object_field->name));
673         object_field->handler = sorcery_handler;
674
675         va_start(args, argc);
676         for (pos = 0; pos < argc; pos++) {
677                 object_field->args[pos] = va_arg(args, size_t);
678         }
679         va_end(args);
680
681         ao2_link(object_type->fields, object_field);
682
683         /* TODO: Improve this hack */
684         if (!argc) {
685                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
686         } else if (argc == 1) {
687                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
688                                       object_field->args[0]);
689         } else if (argc == 2) {
690                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
691                                       object_field->args[0], object_field->args[1]);
692         } else if (argc == 3) {
693                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
694                                       object_field->args[0], object_field->args[1], object_field->args[2]);
695         } else {
696                 ast_assert(0); /* The hack... she does us no good for this */
697         }
698
699         return 0;
700 }
701
702 /*! \brief Retrieves whether or not the type is reloadable */
703 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
704 {
705         RAII_VAR(struct ast_sorcery_object_type *, object_type,
706                  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
707         return object_type && object_type->reloadable;
708 }
709
710 static int sorcery_wizard_load(void *obj, void *arg, int flags)
711 {
712         struct ast_sorcery_object_wizard *wizard = obj;
713         struct sorcery_load_details *details = arg;
714         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
715
716         if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
717                 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
718                         "maintaining previous values\n", details->type);
719                 return 0;
720         }
721
722         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
723
724         if (load) {
725                 load(wizard->data, details->sorcery, details->type);
726         }
727
728         return 0;
729 }
730
731 /*! \brief Destructor for observer invocation */
732 static void sorcery_observer_invocation_destroy(void *obj)
733 {
734         struct sorcery_observer_invocation *invocation = obj;
735
736         ao2_cleanup(invocation->object_type);
737         ao2_cleanup(invocation->object);
738 }
739
740 /*! \brief Allocator function for observer invocation */
741 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
742 {
743         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
744
745         if (!invocation) {
746                 return NULL;
747         }
748
749         ao2_ref(object_type, +1);
750         invocation->object_type = object_type;
751
752         if (object) {
753                 ao2_ref(object, +1);
754                 invocation->object = object;
755         }
756
757         return invocation;
758 }
759
760 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
761 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
762 {
763         const struct ast_sorcery_object_type_observer *observer = obj;
764
765         if (observer->callbacks->loaded) {
766                 observer->callbacks->loaded(arg);
767         }
768
769         return 0;
770 }
771
772 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
773 static int sorcery_observers_notify_loaded(void *data)
774 {
775         struct sorcery_observer_invocation *invocation = data;
776
777         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
778         ao2_cleanup(invocation);
779
780         return 0;
781 }
782
783 static int sorcery_object_load(void *obj, void *arg, int flags)
784 {
785         struct ast_sorcery_object_type *type = obj;
786         struct sorcery_load_details *details = arg;
787
788         details->type = type->name;
789         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
790
791         if (ao2_container_count(type->observers)) {
792                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
793
794                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
795                         ao2_cleanup(invocation);
796                 }
797         }
798
799         return 0;
800 }
801
802 void ast_sorcery_load(const struct ast_sorcery *sorcery)
803 {
804         struct sorcery_load_details details = {
805                 .sorcery = sorcery,
806                 .reload = 0,
807         };
808
809         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
810 }
811
812 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
813 {
814         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
815         struct sorcery_load_details details = {
816                 .sorcery = sorcery,
817                 .reload = 0,
818         };
819
820         if (!object_type) {
821                 return;
822         }
823
824         sorcery_object_load(object_type, &details, 0);
825 }
826
827 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
828 {
829         struct sorcery_load_details details = {
830                 .sorcery = sorcery,
831                 .reload = 1,
832         };
833
834         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
835 }
836
837 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
838 {
839         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
840         struct sorcery_load_details details = {
841                 .sorcery = sorcery,
842                 .reload = 1,
843         };
844
845         if (!object_type) {
846                 return;
847         }
848
849         sorcery_object_load(object_type, &details, 0);
850 }
851
852 void ast_sorcery_ref(struct ast_sorcery *sorcery)
853 {
854         ao2_ref(sorcery, +1);
855 }
856
857 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
858 {
859         const struct ast_sorcery_object_details *details = object;
860         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
861         struct ao2_iterator i;
862         struct ast_sorcery_object_field *object_field;
863         struct ast_variable *fields = NULL;
864         int res = 0;
865
866         if (!object_type) {
867                 return NULL;
868         }
869
870         i = ao2_iterator_init(object_type->fields, 0);
871
872         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
873                 struct ast_variable *tmp = NULL;
874
875                 if (object_field->multiple_handler) {
876                         if ((res = object_field->multiple_handler(object, &tmp))) {
877                                 ast_variables_destroy(tmp);
878                         }
879                 } else if (object_field->handler) {
880                         char *buf = NULL;
881
882                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
883                                 !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
884                                 res = -1;
885                         }
886
887                         ast_free(buf);
888                 } else {
889                         continue;
890                 }
891
892                 if (!res && tmp) {
893                         tmp->next = fields;
894                         fields = tmp;
895                 }
896         }
897
898         ao2_iterator_destroy(&i);
899
900         /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
901         if (res) {
902                 ast_variables_destroy(fields);
903                 fields = NULL;
904         }
905
906         return fields;
907 }
908
909 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
910 {
911         const struct ast_sorcery_object_details *details = object;
912         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
913         struct ao2_iterator i;
914         struct ast_sorcery_object_field *object_field;
915         struct ast_json *json = ast_json_object_create();
916         int res = 0;
917
918         if (!object_type || !json) {
919                 return NULL;
920         }
921
922         i = ao2_iterator_init(object_type->fields, 0);
923
924         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
925                 if (object_field->multiple_handler) {
926                         struct ast_variable *tmp = NULL;
927                         struct ast_variable *field;
928
929                         if ((res = object_field->multiple_handler(object, &tmp))) {
930                                 ao2_ref(object_field, -1);
931                                 break;
932                         }
933
934                         for (field = tmp; field; field = field->next) {
935                                 struct ast_json *value = ast_json_string_create(field->value);
936
937                                 if (value && ast_json_object_set(json, field->name, value)) {
938                                         ast_json_unref(value);
939                                         res = -1;
940                                 }
941                         }
942
943                         ast_variables_destroy(tmp);
944                 } else if (object_field->handler) {
945                         char *buf = NULL;
946                         struct ast_json *value = NULL;
947
948                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
949                                 !(value = ast_json_string_create(buf)) ||
950                                 ast_json_object_set(json, object_field->name, value)) {
951                                 ast_json_unref(value);
952                                 res = -1;
953                         }
954
955                         ast_free(buf);
956                 } else {
957                         continue;
958                 }
959         }
960
961         ao2_iterator_destroy(&i);
962
963         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
964         if (res) {
965                 ast_json_unref(json);
966                 json = NULL;
967         }
968
969         return json;
970 }
971
972 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
973 {
974         const struct ast_sorcery_object_details *details = object;
975         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
976         RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
977         struct ast_variable *field;
978         int res = 0;
979
980         if (!object_type) {
981                 return -1;
982         }
983
984         if (object_type->transform && (transformed = object_type->transform(objectset))) {
985                 field = transformed;
986         } else {
987                 field = objectset;
988         }
989
990         for (; field; field = field->next) {
991                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
992                         break;
993                 }
994         }
995
996         if (!res && object_type->apply) {
997                 res = object_type->apply(sorcery, object);
998         }
999
1000         return res;
1001 }
1002
1003 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1004 {
1005         const struct ast_variable *field;
1006
1007         /* Search the linked list of fields to find the correct one */
1008         for (field = fields; field; field = field->next) {
1009                 if (!strcmp(field->name, name)) {
1010                         return field;
1011                 }
1012         }
1013
1014         return NULL;
1015 }
1016
1017 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1018 {
1019         const struct ast_variable *field;
1020         int res = 0;
1021
1022         *changes = NULL;
1023
1024         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1025         if (original == modified) {
1026                 return 0;
1027         }
1028
1029         for (field = modified; field; field = field->next) {
1030                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1031
1032                 if (!old || strcmp(old->value, field->value)) {
1033                         struct ast_variable *tmp;
1034
1035                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1036                                 res = -1;
1037                                 break;
1038                         }
1039
1040                         tmp->next = *changes;
1041                         *changes = tmp;
1042                 }
1043         }
1044
1045         /* If an error occurred do not return a partial changeset */
1046         if (res) {
1047                 ast_variables_destroy(*changes);
1048                 *changes = NULL;
1049         }
1050
1051         return res;
1052 }
1053
1054 static void sorcery_object_destructor(void *object)
1055 {
1056         struct ast_sorcery_object_details *details = object;
1057
1058         if (details->object->destructor) {
1059                 details->object->destructor(object);
1060         }
1061
1062         ast_variables_destroy(details->object->extended);
1063         ast_free(details->object->id);
1064 }
1065
1066 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1067 {
1068         void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1069         struct ast_sorcery_object_details *details = object;
1070
1071         if (!object) {
1072                 return NULL;
1073         }
1074
1075         details->object = object + size;
1076         details->object->destructor = destructor;
1077
1078         return object;
1079 }
1080
1081 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1082 {
1083         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1084         struct ast_sorcery_object_details *details;
1085
1086         if (!object_type || !object_type->type.item_alloc ||
1087             !(details = object_type->type.item_alloc(id))) {
1088                 return NULL;
1089         }
1090
1091         if (ast_strlen_zero(id)) {
1092                 char uuid[AST_UUID_STR_LEN];
1093
1094                 ast_uuid_generate_str(uuid, sizeof(uuid));
1095                 details->object->id = ast_strdup(uuid);
1096         } else {
1097                 details->object->id = ast_strdup(id);
1098         }
1099
1100         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1101
1102         if (aco_set_defaults(&object_type->type, id, details)) {
1103                 ao2_ref(details, -1);
1104                 return NULL;
1105         }
1106
1107         return details;
1108 }
1109
1110 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1111 {
1112         const struct ast_sorcery_object_details *details = object;
1113         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1114         struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1115         RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1116         int res = 0;
1117
1118         if (!copy) {
1119                 return NULL;
1120         } else if (object_type->copy) {
1121                 res = object_type->copy(object, copy);
1122         } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1123                 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1124         } else {
1125                 /* No native copy available and could not create an objectset, this copy has failed */
1126                 res = -1;
1127         }
1128
1129         if (res) {
1130                 ao2_cleanup(copy);
1131                 copy = NULL;
1132         }
1133
1134         return copy;
1135 }
1136
1137 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1138 {
1139         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1140
1141         *changes = NULL;
1142
1143         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1144                 return -1;
1145         }
1146
1147         if (original == modified) {
1148                 return 0;
1149         } else if (!object_type->diff) {
1150                 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1151                 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1152
1153                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1154                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1155
1156                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1157         } else {
1158                 return object_type->diff(original, modified, changes);
1159         }
1160 }
1161
1162 /*! \brief Structure used when calling create, update, or delete */
1163 struct sorcery_details {
1164         /*! \brief Pointer to the sorcery instance */
1165         const struct ast_sorcery *sorcery;
1166         /*! \brief Pointer to the object itself */
1167         void *obj;
1168 };
1169
1170 /*! \brief Internal function used to create an object in caching wizards */
1171 static int sorcery_cache_create(void *obj, void *arg, int flags)
1172 {
1173         const struct ast_sorcery_object_wizard *object_wizard = obj;
1174         const struct sorcery_details *details = arg;
1175
1176         if (!object_wizard->caching || !object_wizard->wizard->create) {
1177                 return 0;
1178         }
1179
1180         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1181
1182         return 0;
1183 }
1184
1185 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1186 {
1187         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1188         void *object = NULL;
1189         struct ao2_iterator i;
1190         struct ast_sorcery_object_wizard *wizard;
1191         unsigned int cached = 0;
1192
1193         if (!object_type || ast_strlen_zero(id)) {
1194                 return NULL;
1195         }
1196
1197         i = ao2_iterator_init(object_type->wizards, 0);
1198         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1199                 if (wizard->wizard->retrieve_id &&
1200                     !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1201                         continue;
1202                 }
1203
1204                 cached = wizard->caching;
1205
1206                 ao2_ref(wizard, -1);
1207                 break;
1208         }
1209         ao2_iterator_destroy(&i);
1210
1211         if (!cached && object) {
1212                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1213         }
1214
1215         return object;
1216 }
1217
1218 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1219 {
1220         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1221         void *object = NULL;
1222         struct ao2_iterator i;
1223         struct ast_sorcery_object_wizard *wizard;
1224         unsigned int cached = 0;
1225
1226         if (!object_type) {
1227                 return NULL;
1228         }
1229
1230         /* If returning multiple objects create a container to store them in */
1231         if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1232                 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1233                         return NULL;
1234                 }
1235         }
1236
1237         /* Inquire with the available wizards for retrieval */
1238         i = ao2_iterator_init(object_type->wizards, 0);
1239         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1240                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1241                         if (wizard->wizard->retrieve_multiple) {
1242                                 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1243                         }
1244                 } else if (fields && wizard->wizard->retrieve_fields) {
1245                         if (wizard->wizard->retrieve_fields) {
1246                                 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1247                         }
1248                 }
1249
1250                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1251                         continue;
1252                 }
1253
1254                 cached = wizard->caching;
1255
1256                 ao2_ref(wizard, -1);
1257                 break;
1258         }
1259         ao2_iterator_destroy(&i);
1260
1261         /* If we are returning a single object and it came from a non-cache source create it in any caches */
1262         if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1263                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1264         }
1265
1266         return object;
1267 }
1268
1269 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1270 {
1271         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1272         struct ao2_container *objects;
1273         struct ao2_iterator i;
1274         struct ast_sorcery_object_wizard *wizard;
1275
1276         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1277                 return NULL;
1278         }
1279
1280         i = ao2_iterator_init(object_type->wizards, 0);
1281         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1282                 if (!wizard->wizard->retrieve_regex) {
1283                         continue;
1284                 }
1285
1286                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1287         }
1288         ao2_iterator_destroy(&i);
1289
1290         return objects;
1291 }
1292
1293 /*! \brief Internal function which returns if the wizard has created the object */
1294 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1295 {
1296         const struct ast_sorcery_object_wizard *object_wizard = obj;
1297         const struct sorcery_details *details = arg;
1298
1299         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1300 }
1301
1302 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1303 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1304 {
1305         const struct ast_sorcery_object_type_observer *observer = obj;
1306
1307         if (observer->callbacks->created) {
1308                 observer->callbacks->created(arg);
1309         }
1310
1311         return 0;
1312 }
1313
1314 /*! \brief Internal callback function which notifies observers that an object has been created */
1315 static int sorcery_observers_notify_create(void *data)
1316 {
1317         struct sorcery_observer_invocation *invocation = data;
1318
1319         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1320         ao2_cleanup(invocation);
1321
1322         return 0;
1323 }
1324
1325 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1326 {
1327         const struct ast_sorcery_object_details *details = object;
1328         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1329         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1330         struct sorcery_details sdetails = {
1331                 .sorcery = sorcery,
1332                 .obj = object,
1333         };
1334
1335         if (!object_type) {
1336                 return -1;
1337         }
1338
1339         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1340                 ao2_container_count(object_type->observers)) {
1341                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1342
1343                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1344                         ao2_cleanup(invocation);
1345                 }
1346         }
1347
1348         return object_wizard ? 0 : -1;
1349 }
1350
1351 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1352 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1353 {
1354         const struct ast_sorcery_object_type_observer *observer = obj;
1355
1356         if (observer->callbacks->updated) {
1357                 observer->callbacks->updated(arg);
1358         }
1359
1360         return 0;
1361 }
1362
1363 /*! \brief Internal callback function which notifies observers that an object has been updated */
1364 static int sorcery_observers_notify_update(void *data)
1365 {
1366         struct sorcery_observer_invocation *invocation = data;
1367
1368         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1369         ao2_cleanup(invocation);
1370
1371         return 0;
1372 }
1373
1374 /*! \brief Internal function which returns if a wizard has updated the object */
1375 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1376 {
1377         const struct ast_sorcery_object_wizard *object_wizard = obj;
1378         const struct sorcery_details *details = arg;
1379
1380         return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1381                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1382 }
1383
1384 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1385 {
1386         const struct ast_sorcery_object_details *details = object;
1387         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1388         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1389         struct sorcery_details sdetails = {
1390                 .sorcery = sorcery,
1391                 .obj = object,
1392         };
1393
1394         if (!object_type) {
1395                 return -1;
1396         }
1397
1398         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1399                 ao2_container_count(object_type->observers)) {
1400                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1401
1402                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1403                         ao2_cleanup(invocation);
1404                 }
1405         }
1406
1407         return object_wizard ? 0 : -1;
1408 }
1409
1410 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1411 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1412 {
1413         const struct ast_sorcery_object_type_observer *observer = obj;
1414
1415         if (observer->callbacks->deleted) {
1416                 observer->callbacks->deleted(arg);
1417         }
1418
1419         return 0;
1420 }
1421
1422 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1423 static int sorcery_observers_notify_delete(void *data)
1424 {
1425         struct sorcery_observer_invocation *invocation = data;
1426
1427         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1428         ao2_cleanup(invocation);
1429
1430         return 0;
1431 }
1432
1433 /*! \brief Internal function which returns if a wizard has deleted the object */
1434 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1435 {
1436         const struct ast_sorcery_object_wizard *object_wizard = obj;
1437         const struct sorcery_details *details = arg;
1438
1439         return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1440                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1441 }
1442
1443 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1444 {
1445         const struct ast_sorcery_object_details *details = object;
1446         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1447         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1448         struct sorcery_details sdetails = {
1449                 .sorcery = sorcery,
1450                 .obj = object,
1451         };
1452
1453         if (!object_type) {
1454                 return -1;
1455         }
1456
1457         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1458                 ao2_container_count(object_type->observers)) {
1459                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1460
1461                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1462                         ao2_cleanup(invocation);
1463                 }
1464         }
1465
1466         return object_wizard ? 0 : -1;
1467 }
1468
1469 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1470 {
1471         ao2_cleanup(sorcery);
1472 }
1473
1474 const char *ast_sorcery_object_get_id(const void *object)
1475 {
1476         const struct ast_sorcery_object_details *details = object;
1477         return details->object->id;
1478 }
1479
1480 const char *ast_sorcery_object_get_type(const void *object)
1481 {
1482         const struct ast_sorcery_object_details *details = object;
1483         return details->object->type;
1484 }
1485
1486 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1487 {
1488         const struct ast_sorcery_object_details *details = object;
1489         struct ast_variable *field;
1490
1491         for (field = details->object->extended; field; field = field->next) {
1492                 if (!strcmp(field->name + 1, name)) {
1493                         return field->value;
1494                 }
1495         }
1496
1497         return NULL;
1498 }
1499
1500 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1501 {
1502         RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1503         struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1504         const struct ast_sorcery_object_details *details = object;
1505
1506         if (!extended) {
1507                 return -1;
1508         }
1509
1510         for (field = details->object->extended; field; previous = field, field = field->next) {
1511                 if (!strcmp(field->name, name)) {
1512                         if (previous) {
1513                                 previous->next = field->next;
1514                         } else {
1515                                 details->object->extended = field->next;
1516                         }
1517                         field->next = NULL;
1518                         break;
1519                 }
1520         }
1521
1522         extended->next = details->object->extended;
1523         details->object->extended = extended;
1524
1525         return 0;
1526 }
1527
1528 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1529 {
1530         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1531         struct ast_sorcery_object_type_observer *observer;
1532
1533         if (!object_type || !callbacks) {
1534                 return -1;
1535         }
1536
1537         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1538                 return -1;
1539         }
1540
1541         observer->callbacks = callbacks;
1542         ao2_link(object_type->observers, observer);
1543         ao2_ref(observer, -1);
1544
1545         return 0;
1546 }
1547
1548 /*! \brief Internal callback function for removing an observer */
1549 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1550 {
1551         const struct ast_sorcery_object_type_observer *observer = obj;
1552
1553         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1554 }
1555
1556 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, struct ast_sorcery_observer *callbacks)
1557 {
1558         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1559
1560         if (!object_type) {
1561                 return;
1562         }
1563
1564         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_observer_remove, callbacks);
1565 }