sorcery.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  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. /*! \file
  19. *
  20. * \brief Sorcery Data Access Layer API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/logger.h"
  29. #include "asterisk/sorcery.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/format.h"
  32. #include "asterisk/format_cap.h"
  33. #include "asterisk/strings.h"
  34. #include "asterisk/config_options.h"
  35. #include "asterisk/netsock2.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/taskprocessor.h"
  38. #include "asterisk/threadpool.h"
  39. #include "asterisk/json.h"
  40. #include "asterisk/vector.h"
  41. /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
  42. #undef open
  43. #undef close
  44. /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
  45. #define WIZARD_BUCKETS 7
  46. /*! \brief Number of buckets for types (should be prime for performance reasons) */
  47. #define TYPE_BUCKETS 53
  48. /*! \brief Number of buckets for instances (should be prime for performance reasons) */
  49. #define INSTANCE_BUCKETS 17
  50. /*! \brief Number of buckets for object fields (should be prime for performance reasons) */
  51. #define OBJECT_FIELD_BUCKETS 29
  52. #define NOTIFY_GENERIC_OBSERVERS(container, type, callback, ...) ({ \
  53. struct ao2_iterator i = ao2_iterator_init(container, 0); \
  54. struct type *observer; \
  55. ao2_rdlock(container); \
  56. while ((observer = ao2_iterator_next(&i))) { \
  57. if (observer->callbacks->callback) { \
  58. observer->callbacks->callback(__VA_ARGS__); \
  59. } \
  60. ao2_cleanup(observer); \
  61. } \
  62. ao2_unlock(container); \
  63. ao2_iterator_cleanup(&i); \
  64. })
  65. #define NOTIFY_GLOBAL_OBSERVERS(container, callback, ...) \
  66. NOTIFY_GENERIC_OBSERVERS(container, sorcery_global_observer, callback, __VA_ARGS__)
  67. #define NOTIFY_INSTANCE_OBSERVERS(container, callback, ...) \
  68. NOTIFY_GENERIC_OBSERVERS(container, sorcery_instance_observer, callback, __VA_ARGS__)
  69. #define NOTIFY_WIZARD_OBSERVERS(container, callback, ...) \
  70. NOTIFY_GENERIC_OBSERVERS(container, sorcery_wizard_observer, callback, __VA_ARGS__)
  71. /*! \brief Thread pool for observers */
  72. static struct ast_threadpool *threadpool;
  73. /*! \brief Structure for an internal wizard instance */
  74. struct ast_sorcery_internal_wizard {
  75. /*!
  76. * \brief Wizard interface itself
  77. * \warning Callbacks must always be declared first in this structure
  78. * so an ao2_ref on &callbacks will adjust the ref count on
  79. * internal_wizard.
  80. */
  81. struct ast_sorcery_wizard callbacks;
  82. /*! \brief Observers */
  83. struct ao2_container *observers;
  84. };
  85. /*! \brief Structure for a wizard instance which operates on objects */
  86. struct ast_sorcery_object_wizard {
  87. /*! \brief Wizard interface itself */
  88. struct ast_sorcery_internal_wizard *wizard;
  89. /*! \brief Unique data for the wizard */
  90. void *data;
  91. /*! \brief Wizard is acting as an object cache */
  92. unsigned int caching:1;
  93. /*! \brief Wizard is read_only */
  94. unsigned int read_only:1;
  95. /*! \brief Wizard allows others of the same type */
  96. unsigned int allow_duplicates:1;
  97. /*! \brief Wizard arguments */
  98. char wizard_args[0];
  99. };
  100. /*! \brief Interface for a sorcery object type wizards */
  101. AST_VECTOR_RW(ast_sorcery_object_wizards, struct ast_sorcery_object_wizard *);
  102. /*! \brief Structure for internal sorcery object information */
  103. struct ast_sorcery_object {
  104. /*! \brief Unique identifier of this object */
  105. char *id;
  106. /*! \brief Type of object */
  107. char type[MAX_OBJECT_TYPE];
  108. /*! \brief Optional object destructor */
  109. ao2_destructor_fn destructor;
  110. /*! \brief Extended object fields */
  111. struct ast_variable *extended;
  112. /*! \brief Time that the object was created */
  113. struct timeval created;
  114. /*! \brief Whether this object has dynamic contents or not */
  115. unsigned int has_dynamic_contents:1;
  116. };
  117. /*! \brief Structure for registered object type */
  118. struct ast_sorcery_object_type {
  119. /*! \brief Unique name of the object type */
  120. char name[MAX_OBJECT_TYPE];
  121. /*! \brief Optional transformation callback */
  122. sorcery_transform_handler transform;
  123. /*! \brief Optional object set apply callback */
  124. sorcery_apply_handler apply;
  125. /*! \brief Optional object copy callback */
  126. sorcery_copy_handler copy;
  127. /*! \brief Optional object diff callback */
  128. sorcery_diff_handler diff;
  129. /*! \brief Wizard instances */
  130. struct ast_sorcery_object_wizards wizards;
  131. /*! \brief Object fields */
  132. struct ao2_container *fields;
  133. /*! \brief Configuration framework general information */
  134. struct aco_info *info;
  135. /*! \brief Configuration framework file information */
  136. struct aco_file *file;
  137. /*! \brief Type details */
  138. struct aco_type type;
  139. /*! \brief Observers */
  140. struct ao2_container *observers;
  141. /*! \brief Serializer for observers */
  142. struct ast_taskprocessor *serializer;
  143. /*! \brief Specifies if object type is reloadable or not */
  144. unsigned int reloadable:1;
  145. };
  146. /*! \brief Structure for registered object type observer */
  147. struct ast_sorcery_object_type_observer {
  148. /*! \brief Pointer to the observer implementation */
  149. const struct ast_sorcery_observer *callbacks;
  150. };
  151. /*! \brief Structure used for observer invocations */
  152. struct sorcery_observer_invocation {
  153. /*! \brief Pointer to the object type */
  154. struct ast_sorcery_object_type *object_type;
  155. /*! \brief Pointer to the object */
  156. void *object;
  157. };
  158. /*! \brief Structure for registered object field */
  159. struct ast_sorcery_object_field {
  160. /*! \brief Name of the field */
  161. char name[MAX_OBJECT_FIELD];
  162. /*! \brief The compiled name regex if name is a regex */
  163. regex_t *name_regex;
  164. /*! \brief Callback function for translation of a single value */
  165. sorcery_field_handler handler;
  166. /*! \brief Callback function for translation of multiple values */
  167. sorcery_fields_handler multiple_handler;
  168. /*! \brief Position of the field */
  169. intptr_t args[];
  170. };
  171. /*! \brief Proxy object for sorcery */
  172. struct sorcery_proxy {
  173. AO2_WEAKPROXY();
  174. /*! \brief The name of the module owning this sorcery instance */
  175. char module_name[0];
  176. };
  177. /*! \brief Full structure for sorcery */
  178. struct ast_sorcery {
  179. /*! \brief Container for known object types */
  180. struct ao2_container *types;
  181. /*! \brief Observers */
  182. struct ao2_container *observers;
  183. /*! \brief Pointer to module_name in the associated sorcery_proxy. */
  184. char *module_name;
  185. };
  186. /*! \brief Structure for passing load/reload details */
  187. struct sorcery_load_details {
  188. /*! \brief Sorcery structure in use */
  189. const struct ast_sorcery *sorcery;
  190. /*! \brief Type of object being loaded */
  191. const char *type;
  192. /*! \brief Whether this is a reload or not */
  193. unsigned int reload:1;
  194. /*! \brief Whether this is forced or not */
  195. unsigned int force:1;
  196. };
  197. /*! \brief Registered sorcery wizards */
  198. static struct ao2_container *wizards;
  199. /* The following 3 observer wrappers must name their
  200. * external observer 'callbacks' and it must be
  201. * the first member of the structure. Common macros
  202. * and container callbacks depend on it.
  203. */
  204. /*! \brief A global observer wrapper */
  205. struct sorcery_global_observer {
  206. const struct ast_sorcery_global_observer *callbacks;
  207. };
  208. /*! \brief An instance observer wrapper */
  209. struct sorcery_instance_observer {
  210. const struct ast_sorcery_instance_observer *callbacks;
  211. };
  212. /*! \brief A wizard observer wrapper */
  213. struct sorcery_wizard_observer {
  214. const struct ast_sorcery_wizard_observer *callbacks;
  215. };
  216. /*! \brief Registered global observers */
  217. struct ao2_container *observers;
  218. /*! \brief Registered sorcery instances */
  219. static struct ao2_container *instances;
  220. static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
  221. {
  222. int *field = (int *)(obj + args[0]);
  223. return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
  224. }
  225. static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
  226. {
  227. unsigned int *field = (unsigned int *)(obj + args[0]);
  228. return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
  229. }
  230. static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
  231. {
  232. double *field = (double *)(obj + args[0]);
  233. return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
  234. }
  235. static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
  236. {
  237. ast_string_field *field = (const char **)(obj + args[0]);
  238. return !(*buf = ast_strdup(*field)) ? -1 : 0;
  239. }
  240. static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
  241. {
  242. unsigned int *field = (unsigned int *)(obj + args[0]);
  243. return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
  244. }
  245. static int yesno_handler_fn(const void *obj, const intptr_t *args, char **buf)
  246. {
  247. unsigned int *field = (unsigned int *)(obj + args[0]);
  248. return !(*buf = ast_strdup(*field ? "yes" : "no")) ? -1 : 0;
  249. }
  250. static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
  251. {
  252. struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
  253. return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
  254. }
  255. static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
  256. {
  257. char *field = (char *)(obj + args[0]);
  258. return !(*buf = ast_strdup(field)) ? -1 : 0;
  259. }
  260. static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
  261. {
  262. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  263. struct ast_format_cap **cap = (struct ast_format_cap **)(obj + args[0]);
  264. return !(*buf = ast_strdup(ast_format_cap_get_names(*cap, &codec_buf)));
  265. }
  266. static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
  267. {
  268. switch(type) {
  269. case OPT_BOOL_T: return bool_handler_fn;
  270. case OPT_YESNO_T: return yesno_handler_fn;
  271. case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
  272. case OPT_CODEC_T: return codec_handler_fn;
  273. case OPT_DOUBLE_T: return double_handler_fn;
  274. case OPT_INT_T: return int_handler_fn;
  275. case OPT_SOCKADDR_T: return sockaddr_handler_fn;
  276. case OPT_STRINGFIELD_T: return stringfield_handler_fn;
  277. case OPT_UINT_T: return uint_handler_fn;
  278. default:
  279. case OPT_CUSTOM_T: return NULL;
  280. }
  281. return NULL;
  282. }
  283. /*! \brief Hashing and comparison functions for sorcery wizards */
  284. AO2_STRING_FIELD_HASH_FN(ast_sorcery_internal_wizard, callbacks.name)
  285. AO2_STRING_FIELD_CMP_FN(ast_sorcery_internal_wizard, callbacks.name)
  286. AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_field, name)
  287. AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_field, name)
  288. /*! \brief Cleanup function for graceful shutdowns */
  289. static void sorcery_cleanup(void)
  290. {
  291. ast_threadpool_shutdown(threadpool);
  292. threadpool = NULL;
  293. ao2_cleanup(wizards);
  294. wizards = NULL;
  295. ao2_cleanup(observers);
  296. observers = NULL;
  297. ao2_cleanup(instances);
  298. instances = NULL;
  299. }
  300. /*! \brief Compare function for sorcery instances */
  301. AO2_STRING_FIELD_CMP_FN(sorcery_proxy, module_name)
  302. /*! \brief Hashing function for sorcery instances */
  303. AO2_STRING_FIELD_HASH_FN(sorcery_proxy, module_name)
  304. int ast_sorcery_init(void)
  305. {
  306. struct ast_threadpool_options options = {
  307. .version = AST_THREADPOOL_OPTIONS_VERSION,
  308. .auto_increment = 1,
  309. .max_size = 0,
  310. .idle_timeout = 60,
  311. .initial_size = 0,
  312. };
  313. ast_assert(wizards == NULL);
  314. threadpool = ast_threadpool_create("sorcery", NULL, &options);
  315. if (!threadpool) {
  316. return -1;
  317. }
  318. wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
  319. ast_sorcery_internal_wizard_hash_fn, NULL, ast_sorcery_internal_wizard_cmp_fn);
  320. if (!wizards) {
  321. return -1;
  322. }
  323. observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
  324. if (!observers) {
  325. return -1;
  326. }
  327. instances = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, INSTANCE_BUCKETS,
  328. sorcery_proxy_hash_fn, NULL, sorcery_proxy_cmp_fn);
  329. if (!instances) {
  330. return -1;
  331. }
  332. ast_register_cleanup(sorcery_cleanup);
  333. return 0;
  334. }
  335. static void sorcery_internal_wizard_destructor(void *obj)
  336. {
  337. struct ast_sorcery_internal_wizard *wizard = obj;
  338. ao2_cleanup(wizard->observers);
  339. }
  340. int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
  341. {
  342. struct ast_sorcery_internal_wizard *wizard;
  343. int res = -1;
  344. ast_assert(!ast_strlen_zero(interface->name));
  345. ao2_lock(wizards);
  346. if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
  347. ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
  348. interface->name);
  349. goto done;
  350. }
  351. if (!(wizard = ao2_alloc(sizeof(*wizard), sorcery_internal_wizard_destructor))) {
  352. goto done;
  353. }
  354. wizard->callbacks = *interface;
  355. wizard->callbacks.module = module;
  356. wizard->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
  357. if (!wizard->observers) {
  358. goto done;
  359. }
  360. ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
  361. res = 0;
  362. ast_verb(5, "Sorcery registered wizard '%s'\n", interface->name);
  363. NOTIFY_GLOBAL_OBSERVERS(observers, wizard_registered,
  364. interface->name, interface);
  365. done:
  366. ao2_cleanup(wizard);
  367. ao2_unlock(wizards);
  368. return res;
  369. }
  370. int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
  371. {
  372. struct ast_sorcery_internal_wizard *wizard =
  373. interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL;
  374. if (wizard) {
  375. NOTIFY_GLOBAL_OBSERVERS(observers, wizard_unregistering, wizard->callbacks.name, &wizard->callbacks);
  376. ao2_unlink(wizards, wizard);
  377. ao2_ref(wizard, -1);
  378. ast_verb(5, "Sorcery unregistered wizard '%s'\n", interface->name);
  379. return 0;
  380. } else {
  381. return -1;
  382. }
  383. }
  384. /*! \brief Internal callback function for removing a generic observer */
  385. static int sorcery_generic_observer_remove(void *obj, void *arg, int flags)
  386. {
  387. const struct sorcery_global_observer *observer = obj;
  388. return (observer->callbacks == arg) ? CMP_MATCH : 0;
  389. }
  390. int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks)
  391. {
  392. struct sorcery_global_observer *cb;
  393. cb = ao2_alloc(sizeof(*cb), NULL);
  394. if (!cb) {
  395. return -1;
  396. }
  397. cb->callbacks = callbacks;
  398. ao2_link(observers, cb);
  399. ao2_ref(cb, -1);
  400. return 0;
  401. }
  402. void ast_sorcery_global_observer_remove(
  403. const struct ast_sorcery_global_observer *callbacks)
  404. {
  405. ao2_callback(observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
  406. }
  407. int ast_sorcery_instance_observer_add(struct ast_sorcery *sorcery,
  408. const struct ast_sorcery_instance_observer *callbacks)
  409. {
  410. struct sorcery_instance_observer *cb;
  411. cb = ao2_alloc(sizeof(*cb), NULL);
  412. if (!cb) {
  413. return -1;
  414. }
  415. cb->callbacks = callbacks;
  416. ao2_link(sorcery->observers, cb);
  417. ao2_ref(cb, -1);
  418. return 0;
  419. }
  420. void ast_sorcery_instance_observer_remove(struct ast_sorcery *sorcery,
  421. const struct ast_sorcery_instance_observer *callbacks)
  422. {
  423. ao2_callback(sorcery->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
  424. }
  425. int ast_sorcery_wizard_observer_add(struct ast_sorcery_wizard *interface,
  426. const struct ast_sorcery_wizard_observer *callbacks)
  427. {
  428. RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
  429. interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
  430. ao2_cleanup);
  431. if (wizard) {
  432. struct sorcery_wizard_observer *cb;
  433. cb = ao2_alloc(sizeof(*cb), NULL);
  434. if (!cb) {
  435. return -1;
  436. }
  437. cb->callbacks = callbacks;
  438. ao2_link(wizard->observers, cb);
  439. ao2_ref(cb, -1);
  440. return 0;
  441. }
  442. return -1;
  443. }
  444. void ast_sorcery_wizard_observer_remove(struct ast_sorcery_wizard *interface,
  445. const struct ast_sorcery_wizard_observer *callbacks)
  446. {
  447. RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
  448. interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
  449. ao2_cleanup);
  450. if (wizard) {
  451. ao2_callback(wizard->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
  452. }
  453. }
  454. /*! \brief Destructor called when sorcery structure is destroyed */
  455. static void sorcery_destructor(void *obj)
  456. {
  457. struct ast_sorcery *sorcery = obj;
  458. if (sorcery->observers) {
  459. NOTIFY_GLOBAL_OBSERVERS(observers, instance_destroying, sorcery->module_name, sorcery);
  460. }
  461. ao2_cleanup(sorcery->observers);
  462. ao2_cleanup(sorcery->types);
  463. }
  464. /*! \brief Hashing function for sorcery types */
  465. AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_type, name)
  466. AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_type, name)
  467. static void sorcery_proxy_cb(void *weakproxy, void *data)
  468. {
  469. ao2_unlink(instances, weakproxy);
  470. }
  471. struct ast_sorcery *__ast_sorcery_open(const char *module_name, const char *file, int line, const char *func)
  472. {
  473. struct sorcery_proxy *proxy;
  474. struct ast_sorcery *sorcery;
  475. ast_assert(module_name != NULL);
  476. ao2_wrlock(instances);
  477. sorcery = __ao2_weakproxy_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK,
  478. __PRETTY_FUNCTION__, file, line, func);
  479. if (sorcery) {
  480. ao2_unlock(instances);
  481. return sorcery;
  482. }
  483. proxy = ao2_t_weakproxy_alloc(sizeof(*proxy) + strlen(module_name) + 1, NULL, module_name);
  484. if (!proxy) {
  485. goto failure_cleanup;
  486. }
  487. strcpy(proxy->module_name, module_name); /* Safe */
  488. sorcery = __ao2_alloc(sizeof(*sorcery), sorcery_destructor, AO2_ALLOC_OPT_LOCK_MUTEX, module_name, file, line, func);
  489. if (!sorcery) {
  490. goto failure_cleanup;
  491. }
  492. sorcery->module_name = proxy->module_name;
  493. /* We have exclusive access to proxy and sorcery, no need for locking here. */
  494. if (ao2_t_weakproxy_set_object(proxy, sorcery, OBJ_NOLOCK, "weakproxy link")) {
  495. goto failure_cleanup;
  496. }
  497. if (ao2_weakproxy_subscribe(proxy, sorcery_proxy_cb, NULL, OBJ_NOLOCK)) {
  498. goto failure_cleanup;
  499. }
  500. sorcery->types = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, TYPE_BUCKETS,
  501. ast_sorcery_object_type_hash_fn, NULL, ast_sorcery_object_type_cmp_fn);
  502. if (!sorcery->types) {
  503. goto failure_cleanup;
  504. }
  505. if (!(sorcery->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL))) {
  506. goto failure_cleanup;
  507. }
  508. if (__ast_sorcery_apply_config(sorcery, module_name, module_name) == AST_SORCERY_APPLY_FAIL) {
  509. ast_log(LOG_ERROR, "Error attempting to apply configuration %s to sorcery.\n", module_name);
  510. goto failure_cleanup;
  511. }
  512. ao2_link_flags(instances, proxy, OBJ_NOLOCK);
  513. ao2_ref(proxy, -1);
  514. NOTIFY_GLOBAL_OBSERVERS(observers, instance_created, module_name, sorcery);
  515. ao2_unlock(instances);
  516. return sorcery;
  517. failure_cleanup:
  518. /* cleanup of sorcery may result in locking instances, so make sure we unlock first. */
  519. ao2_unlock(instances);
  520. ao2_cleanup(sorcery);
  521. ao2_cleanup(proxy);
  522. return NULL;
  523. }
  524. /*! \brief Search function for sorcery instances */
  525. struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
  526. {
  527. return ao2_weakproxy_find(instances, module_name, OBJ_SEARCH_KEY, "");
  528. }
  529. /*! \brief Destructor function for object types */
  530. static void sorcery_object_type_destructor(void *obj)
  531. {
  532. struct ast_sorcery_object_type *object_type = obj;
  533. AST_VECTOR_RW_WRLOCK(&object_type->wizards);
  534. AST_VECTOR_CALLBACK_VOID(&object_type->wizards, ao2_cleanup);
  535. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  536. AST_VECTOR_RW_FREE(&object_type->wizards);
  537. ao2_cleanup(object_type->fields);
  538. ao2_cleanup(object_type->observers);
  539. if (object_type->info) {
  540. aco_info_destroy(object_type->info);
  541. ast_free(object_type->info);
  542. }
  543. ast_free(object_type->file);
  544. ast_taskprocessor_unreference(object_type->serializer);
  545. }
  546. /*! \brief Internal function which allocates an object type structure */
  547. static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
  548. {
  549. #define INITIAL_WIZARD_VECTOR_SIZE 5
  550. struct ast_sorcery_object_type *object_type;
  551. char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
  552. if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
  553. return NULL;
  554. }
  555. /* Order matters for object wizards */
  556. if (AST_VECTOR_RW_INIT(&object_type->wizards, INITIAL_WIZARD_VECTOR_SIZE) != 0) {
  557. ao2_ref(object_type, -1);
  558. return NULL;
  559. }
  560. object_type->fields = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  561. OBJECT_FIELD_BUCKETS, ast_sorcery_object_field_hash_fn, NULL, ast_sorcery_object_field_cmp_fn);
  562. if (!object_type->fields) {
  563. ao2_ref(object_type, -1);
  564. return NULL;
  565. }
  566. object_type->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
  567. NULL, NULL);
  568. if (!object_type->observers) {
  569. ao2_ref(object_type, -1);
  570. return NULL;
  571. }
  572. object_type->info = ast_calloc(1,
  573. sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0]));
  574. if (!object_type->info) {
  575. ao2_ref(object_type, -1);
  576. return NULL;
  577. }
  578. object_type->file = ast_calloc(1,
  579. sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0]));
  580. if (!object_type->file) {
  581. ao2_ref(object_type, -1);
  582. return NULL;
  583. }
  584. /* Create name with seq number appended. */
  585. ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "sorcery/%s", type);
  586. if (!(object_type->serializer = ast_threadpool_serializer(tps_name, threadpool))) {
  587. ao2_ref(object_type, -1);
  588. return NULL;
  589. }
  590. object_type->info->files[0] = object_type->file;
  591. object_type->info->files[1] = NULL;
  592. object_type->info->module = module;
  593. ast_copy_string(object_type->name, type, sizeof(object_type->name));
  594. return object_type;
  595. }
  596. /*! \brief Object wizard destructor */
  597. static void sorcery_object_wizard_destructor(void *obj)
  598. {
  599. struct ast_sorcery_object_wizard *object_wizard = obj;
  600. if (object_wizard->data && object_wizard->wizard->callbacks.close) {
  601. object_wizard->wizard->callbacks.close(object_wizard->data);
  602. }
  603. if (object_wizard->wizard) {
  604. ast_module_unref(object_wizard->wizard->callbacks.module);
  605. }
  606. ao2_cleanup(object_wizard->wizard);
  607. }
  608. /*! \brief Return the number of wizards mapped to an object type */
  609. int ast_sorcery_get_wizard_mapping_count(struct ast_sorcery *sorcery,
  610. const char *type)
  611. {
  612. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  613. if (!object_type) {
  614. return -1;
  615. }
  616. return AST_VECTOR_SIZE(&object_type->wizards);
  617. }
  618. int ast_sorcery_get_wizard_mapping(struct ast_sorcery *sorcery,
  619. const char *type, int index, struct ast_sorcery_wizard **wizard, void **data)
  620. {
  621. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  622. struct ast_sorcery_object_wizard *owizard;
  623. if (!object_type) {
  624. return -1;
  625. }
  626. if (index < 0 || index >= AST_VECTOR_SIZE(&object_type->wizards)) {
  627. return -1;
  628. }
  629. owizard = AST_VECTOR_GET(&object_type->wizards, index);
  630. if (wizard != NULL) {
  631. *wizard = &(owizard->wizard->callbacks);
  632. ao2_bump(owizard->wizard);
  633. } else {
  634. return -1;
  635. }
  636. if (data != NULL) {
  637. *data = owizard->data;
  638. }
  639. return 0;
  640. }
  641. int __ast_sorcery_object_type_remove_wizard(struct ast_sorcery *sorcery,
  642. const char *object_type_name, const char *module, const char *wizard_type_name,
  643. const char *wizard_args)
  644. {
  645. RAII_VAR(struct ast_sorcery_object_type *, object_type,
  646. ao2_find(sorcery->types, object_type_name, OBJ_SEARCH_KEY), ao2_cleanup);
  647. int res = -1;
  648. int i;
  649. if (!object_type) {
  650. return res;
  651. }
  652. AST_VECTOR_RW_WRLOCK(&object_type->wizards);
  653. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  654. struct ast_sorcery_object_wizard *wizard = AST_VECTOR_GET(&object_type->wizards, i);
  655. if (strcmp(wizard->wizard->callbacks.name, wizard_type_name) == 0
  656. && strcmp(S_OR(wizard->wizard_args, ""), S_OR(wizard_args, "")) == 0) {
  657. ao2_cleanup(AST_VECTOR_REMOVE_ORDERED(&object_type->wizards, i));
  658. res = 0;
  659. break;
  660. }
  661. }
  662. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  663. return res;
  664. }
  665. /*! \brief Internal function removes a wizard mapping */
  666. int __ast_sorcery_remove_wizard_mapping(struct ast_sorcery *sorcery,
  667. const char *type, const char *module, const char *name)
  668. {
  669. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  670. int res;
  671. if (!object_type) {
  672. return -1;
  673. }
  674. AST_VECTOR_RW_WRLOCK(&object_type->wizards);
  675. #define WIZARD_NAME_COMPARE(a, b) (strcmp((a)->wizard->callbacks.name, (b)) == 0)
  676. res = AST_VECTOR_REMOVE_CMP_ORDERED(&object_type->wizards, name, WIZARD_NAME_COMPARE, ao2_cleanup);
  677. #undef WIZARD_NAME_COMPARE
  678. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  679. return res;
  680. }
  681. enum ast_sorcery_apply_result __ast_sorcery_object_type_insert_wizard(struct ast_sorcery *sorcery,
  682. const char *object_type_name, const char *module, const char *wizard_type_name,
  683. const char *wizard_args, enum ast_sorcery_wizard_apply_flags flags, int position,
  684. struct ast_sorcery_wizard **wizard, void **wizard_data)
  685. {
  686. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, object_type_name, OBJ_KEY), ao2_cleanup);
  687. RAII_VAR(struct ast_sorcery_internal_wizard *, internal_wizard, ao2_find(wizards, wizard_type_name, OBJ_KEY), ao2_cleanup);
  688. RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
  689. int created = 0;
  690. object_wizard = ao2_alloc(sizeof(*object_wizard)
  691. + (ast_strlen_zero(wizard_args) ? 0 : strlen(wizard_args) + 1),
  692. sorcery_object_wizard_destructor);
  693. if (!object_wizard) {
  694. return AST_SORCERY_APPLY_FAIL;
  695. }
  696. if (!internal_wizard
  697. || internal_wizard->callbacks.module != ast_module_running_ref(internal_wizard->callbacks.module)) {
  698. ast_log(LOG_ERROR,
  699. "Wizard '%s' could not be applied to object type '%s' as it was not found\n",
  700. wizard_type_name, object_type_name);
  701. return AST_SORCERY_APPLY_FAIL;
  702. }
  703. if (!object_type) {
  704. if (!(object_type = sorcery_object_type_alloc(object_type_name, module))) {
  705. ast_module_unref(internal_wizard->callbacks.module);
  706. return AST_SORCERY_APPLY_FAIL;
  707. }
  708. created = 1;
  709. }
  710. AST_VECTOR_RW_WRLOCK(&object_type->wizards);
  711. if (!created) {
  712. struct ast_sorcery_object_wizard *found;
  713. #define WIZARD_COMPARE(a, b) ((a)->wizard == (b))
  714. found = AST_VECTOR_GET_CMP(&object_type->wizards, internal_wizard, WIZARD_COMPARE);
  715. #undef WIZARD_COMPARE
  716. if (found
  717. && !((flags & AST_SORCERY_WIZARD_APPLY_ALLOW_DUPLICATE) || found->allow_duplicates)) {
  718. ast_debug(1, "Wizard %s already applied to object type %s\n",
  719. internal_wizard->callbacks.name, object_type->name);
  720. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  721. ast_module_unref(internal_wizard->callbacks.module);
  722. return AST_SORCERY_APPLY_DUPLICATE;
  723. }
  724. }
  725. ast_debug(5, "Calling wizard %s open callback on object type %s\n",
  726. wizard_type_name, object_type->name);
  727. if (internal_wizard->callbacks.open && !(object_wizard->data = internal_wizard->callbacks.open(wizard_args))) {
  728. ast_log(LOG_WARNING, "Wizard '%s' failed to open mapping for object type '%s' with data: %s\n",
  729. wizard_type_name, object_type->name, S_OR(wizard_args, ""));
  730. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  731. ast_module_unref(internal_wizard->callbacks.module);
  732. return AST_SORCERY_APPLY_FAIL;
  733. }
  734. object_wizard->wizard = ao2_bump(internal_wizard);
  735. object_wizard->caching = !!(flags & AST_SORCERY_WIZARD_APPLY_CACHING);
  736. object_wizard->read_only = !!(flags & AST_SORCERY_WIZARD_APPLY_READONLY);
  737. if (wizard_args) {
  738. strcpy(object_wizard->wizard_args, wizard_args); /* Safe */
  739. }
  740. if (position == AST_SORCERY_WIZARD_POSITION_LAST) {
  741. position = AST_VECTOR_SIZE(&object_type->wizards);
  742. }
  743. if (AST_VECTOR_INSERT_AT(&object_type->wizards, position, object_wizard) != 0) {
  744. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  745. return AST_SORCERY_APPLY_FAIL;
  746. }
  747. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  748. ao2_bump(object_wizard);
  749. if (created) {
  750. ao2_link(sorcery->types, object_type);
  751. }
  752. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, wizard_mapped,
  753. sorcery->module_name, sorcery, object_type_name, &internal_wizard->callbacks, wizard_args,
  754. object_wizard->data);
  755. if (wizard) {
  756. *wizard = &internal_wizard->callbacks;
  757. }
  758. if (wizard_data) {
  759. *wizard_data = object_wizard->data;
  760. }
  761. return AST_SORCERY_APPLY_SUCCESS;
  762. }
  763. /*! \brief Internal function which creates an object type and inserts a wizard mapping */
  764. enum ast_sorcery_apply_result __ast_sorcery_insert_wizard_mapping(struct ast_sorcery *sorcery,
  765. const char *type, const char *module, const char *name,
  766. const char *data, unsigned int caching, int position)
  767. {
  768. return __ast_sorcery_object_type_insert_wizard(sorcery, type, module, name,
  769. data, caching ? AST_SORCERY_WIZARD_APPLY_CACHING : AST_SORCERY_WIZARD_APPLY_NONE,
  770. position, NULL, NULL);
  771. }
  772. /*! \brief Internal function which creates an object type and adds a wizard mapping */
  773. enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
  774. const char *type, const char *module, const char *name,
  775. const char *data, unsigned int caching)
  776. {
  777. return __ast_sorcery_insert_wizard_mapping(sorcery, type, module, name,
  778. data, caching, AST_SORCERY_WIZARD_POSITION_LAST);
  779. }
  780. enum ast_sorcery_apply_result __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
  781. {
  782. struct ast_flags flags = { 0 };
  783. struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
  784. struct ast_variable *mapping;
  785. int res = AST_SORCERY_APPLY_SUCCESS;
  786. if (!config) {
  787. return AST_SORCERY_APPLY_NO_CONFIGURATION;
  788. }
  789. if (config == CONFIG_STATUS_FILEINVALID) {
  790. return AST_SORCERY_APPLY_FAIL;
  791. }
  792. for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
  793. RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
  794. RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
  795. char *options = mapping_name;
  796. char *type = strsep(&options, "/");
  797. char *data = mapping_value;
  798. char *wizard = strsep(&data, ",");
  799. unsigned int caching = 0;
  800. /* If no object type or wizard exists just skip, nothing we can do */
  801. if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
  802. continue;
  803. }
  804. /* If the wizard is configured as a cache treat it as such */
  805. if (!ast_strlen_zero(options) && strstr(options, "cache")) {
  806. caching = 1;
  807. }
  808. /* Any error immediately causes us to stop */
  809. if (__ast_sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
  810. res = AST_SORCERY_APPLY_FAIL;
  811. break;
  812. }
  813. }
  814. ast_config_destroy(config);
  815. return res;
  816. }
  817. enum ast_sorcery_apply_result __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
  818. {
  819. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  820. /* Defaults can not be added if any existing mapping exists */
  821. if (object_type) {
  822. return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
  823. }
  824. return __ast_sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
  825. }
  826. static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  827. {
  828. return ast_sorcery_object_set_extended(obj, var->name, var->value);
  829. }
  830. static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
  831. {
  832. const struct ast_sorcery_object_details *details = obj;
  833. if (details->object->extended) {
  834. *fields = ast_variables_dup(details->object->extended);
  835. } else {
  836. *fields = NULL;
  837. }
  838. return 0;
  839. }
  840. int ast_sorcery_object_unregister(struct ast_sorcery *sorcery, const char *type)
  841. {
  842. struct ast_sorcery_object_type *object_type;
  843. int res = -1;
  844. ao2_wrlock(sorcery->types);
  845. object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  846. if (object_type && object_type->type.type == ACO_ITEM) {
  847. ao2_unlink_flags(sorcery->types, object_type, OBJ_NOLOCK);
  848. res = 0;
  849. }
  850. ao2_unlock(sorcery->types);
  851. /* XXX may need to add an instance unregister observer callback on success. */
  852. ao2_cleanup(object_type);
  853. return res;
  854. }
  855. 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)
  856. {
  857. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  858. if (!object_type || object_type->type.item_alloc) {
  859. return -1;
  860. }
  861. object_type->type.name = object_type->name;
  862. object_type->type.type = ACO_ITEM;
  863. object_type->type.category = ".?";
  864. object_type->type.item_alloc = alloc;
  865. object_type->type.hidden = hidden;
  866. object_type->reloadable = reloadable;
  867. object_type->transform = transform;
  868. object_type->apply = apply;
  869. object_type->file->types[0] = &object_type->type;
  870. object_type->file->types[1] = NULL;
  871. if (aco_info_init(object_type->info)) {
  872. return -1;
  873. }
  874. if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
  875. return -1;
  876. }
  877. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, object_type_registered,
  878. sorcery->module_name, sorcery, type);
  879. return 0;
  880. }
  881. int ast_sorcery_object_set_congestion_levels(struct ast_sorcery *sorcery, const char *type, long low_water, long high_water)
  882. {
  883. struct ast_sorcery_object_type *object_type;
  884. int res = -1;
  885. object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
  886. if (object_type) {
  887. res = ast_taskprocessor_alert_set_levels(object_type->serializer,
  888. low_water, high_water);
  889. ao2_ref(object_type, -1);
  890. }
  891. return res;
  892. }
  893. void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
  894. {
  895. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  896. if (!object_type) {
  897. return;
  898. }
  899. object_type->copy = copy;
  900. }
  901. void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
  902. {
  903. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  904. if (!object_type) {
  905. return;
  906. }
  907. object_type->diff = diff;
  908. }
  909. static void sorcery_object_field_destructor(void *obj)
  910. {
  911. struct ast_sorcery_object_field *object_field = obj;
  912. if (object_field->name_regex) {
  913. regfree(object_field->name_regex);
  914. ast_free(object_field->name_regex);
  915. }
  916. }
  917. 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)
  918. {
  919. #define MAX_REGEX_ERROR_LEN 128
  920. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  921. RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
  922. int rc;
  923. if (!object_type || !object_type->type.item_alloc || !config_handler
  924. || !(object_field = ao2_alloc(sizeof(*object_field), sorcery_object_field_destructor))) {
  925. return -1;
  926. }
  927. ast_copy_string(object_field->name, regex, sizeof(object_field->name));
  928. object_field->multiple_handler = sorcery_handler;
  929. if (!(object_field->name_regex = ast_calloc(1, sizeof(regex_t)))) {
  930. return -1;
  931. }
  932. if ((rc = regcomp(object_field->name_regex, regex, REG_EXTENDED | REG_NOSUB))) {
  933. char *regerr = ast_alloca(MAX_REGEX_ERROR_LEN);
  934. regerror(rc, object_field->name_regex, regerr, MAX_REGEX_ERROR_LEN);
  935. ast_log(LOG_ERROR, "Regular expression '%s' failed to compile: %s\n", regex, regerr);
  936. return -1;
  937. }
  938. ao2_link(object_type->fields, object_field);
  939. __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
  940. return 0;
  941. }
  942. 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,
  943. aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, unsigned int alias, size_t argc, ...)
  944. {
  945. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  946. RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
  947. int pos;
  948. va_list args;
  949. if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
  950. return -1;
  951. }
  952. if (!sorcery_handler) {
  953. sorcery_handler = sorcery_field_default_handler(opt_type);
  954. }
  955. if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
  956. return -1;
  957. }
  958. ast_copy_string(object_field->name, name, sizeof(object_field->name));
  959. object_field->handler = sorcery_handler;
  960. object_field->multiple_handler = multiple_handler;
  961. va_start(args, argc);
  962. for (pos = 0; pos < argc; pos++) {
  963. object_field->args[pos] = va_arg(args, size_t);
  964. }
  965. va_end(args);
  966. if (!alias) {
  967. ao2_link(object_type->fields, object_field);
  968. }
  969. /* TODO: Improve this hack */
  970. if (!argc) {
  971. __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
  972. } else if (argc == 1) {
  973. __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
  974. object_field->args[0]);
  975. } else if (argc == 2) {
  976. __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
  977. object_field->args[0], object_field->args[1]);
  978. } else if (argc == 3) {
  979. __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
  980. object_field->args[0], object_field->args[1], object_field->args[2]);
  981. } else {
  982. ast_assert(0); /* The hack... she does us no good for this */
  983. }
  984. return 0;
  985. }
  986. /*! \brief Retrieves whether or not the type is reloadable */
  987. static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
  988. {
  989. RAII_VAR(struct ast_sorcery_object_type *, object_type,
  990. ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  991. return object_type && object_type->reloadable;
  992. }
  993. static int sorcery_wizard_load(void *obj, void *arg, int flags)
  994. {
  995. struct ast_sorcery_object_wizard *wizard = obj;
  996. struct sorcery_load_details *details = arg;
  997. void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
  998. if (details->reload) {
  999. if (details->force && wizard->wizard->callbacks.force_reload) {
  1000. load = wizard->wizard->callbacks.force_reload;
  1001. } else {
  1002. load = wizard->wizard->callbacks.reload;
  1003. }
  1004. } else {
  1005. load = wizard->wizard->callbacks.load;
  1006. }
  1007. if (load) {
  1008. NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loading,
  1009. wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
  1010. load(wizard->data, details->sorcery, details->type);
  1011. NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loaded,
  1012. wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
  1013. }
  1014. return 0;
  1015. }
  1016. /*! \brief Destructor for observer invocation */
  1017. static void sorcery_observer_invocation_destroy(void *obj)
  1018. {
  1019. struct sorcery_observer_invocation *invocation = obj;
  1020. ao2_cleanup(invocation->object_type);
  1021. ao2_cleanup(invocation->object);
  1022. }
  1023. /*! \brief Allocator function for observer invocation */
  1024. static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
  1025. {
  1026. struct sorcery_observer_invocation *invocation;
  1027. invocation = ao2_alloc_options(sizeof(*invocation),
  1028. sorcery_observer_invocation_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  1029. if (!invocation) {
  1030. return NULL;
  1031. }
  1032. ao2_ref(object_type, +1);
  1033. invocation->object_type = object_type;
  1034. if (object) {
  1035. ao2_ref(object, +1);
  1036. invocation->object = object;
  1037. }
  1038. return invocation;
  1039. }
  1040. /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
  1041. static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
  1042. {
  1043. const struct ast_sorcery_object_type_observer *observer = obj;
  1044. if (observer->callbacks->loaded) {
  1045. observer->callbacks->loaded(arg);
  1046. }
  1047. return 0;
  1048. }
  1049. /*! \brief Internal callback function which notifies observers that an object type has been loaded */
  1050. static int sorcery_observers_notify_loaded(void *data)
  1051. {
  1052. struct sorcery_observer_invocation *invocation = data;
  1053. ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
  1054. ao2_cleanup(invocation);
  1055. return 0;
  1056. }
  1057. static int sorcery_object_load(void *obj, void *arg, int flags)
  1058. {
  1059. struct ast_sorcery_object_type *type = obj;
  1060. struct sorcery_load_details *details = arg;
  1061. if (!type->type.item_alloc) {
  1062. return 0;
  1063. }
  1064. details->type = type->name;
  1065. if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
  1066. ast_log(LOG_NOTICE, "Type '%s' is not reloadable, maintaining previous values\n",
  1067. details->type);
  1068. return 0;
  1069. }
  1070. NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loading,
  1071. details->sorcery->module_name, details->sorcery, type->name, details->reload);
  1072. AST_VECTOR_RW_RDLOCK(&type->wizards);
  1073. AST_VECTOR_CALLBACK(&type->wizards, sorcery_wizard_load, NULL, details, 0);
  1074. AST_VECTOR_RW_UNLOCK(&type->wizards);
  1075. NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loaded,
  1076. details->sorcery->module_name, details->sorcery, type->name, details->reload);
  1077. if (ao2_container_count(type->observers)) {
  1078. struct sorcery_observer_invocation *invocation;
  1079. invocation = sorcery_observer_invocation_alloc(type, NULL);
  1080. if (invocation
  1081. && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded,
  1082. invocation)) {
  1083. ao2_cleanup(invocation);
  1084. }
  1085. }
  1086. return 0;
  1087. }
  1088. void ast_sorcery_load(const struct ast_sorcery *sorcery)
  1089. {
  1090. struct sorcery_load_details details = {
  1091. .sorcery = sorcery,
  1092. .reload = 0,
  1093. };
  1094. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
  1095. sorcery->module_name, sorcery, 0);
  1096. ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
  1097. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
  1098. sorcery->module_name, sorcery, 0);
  1099. }
  1100. void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
  1101. {
  1102. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1103. struct sorcery_load_details details = {
  1104. .sorcery = sorcery,
  1105. .reload = 0,
  1106. };
  1107. if (!object_type) {
  1108. return;
  1109. }
  1110. sorcery_object_load(object_type, &details, 0);
  1111. }
  1112. void ast_sorcery_reload(const struct ast_sorcery *sorcery)
  1113. {
  1114. struct sorcery_load_details details = {
  1115. .sorcery = sorcery,
  1116. .reload = 1,
  1117. };
  1118. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
  1119. sorcery->module_name, sorcery, 1);
  1120. ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
  1121. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
  1122. sorcery->module_name, sorcery, 1);
  1123. }
  1124. void ast_sorcery_force_reload(const struct ast_sorcery *sorcery)
  1125. {
  1126. struct sorcery_load_details details = {
  1127. .sorcery = sorcery,
  1128. .reload = 1,
  1129. .force = 1,
  1130. };
  1131. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
  1132. sorcery->module_name, sorcery, 1);
  1133. ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
  1134. NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
  1135. sorcery->module_name, sorcery, 1);
  1136. }
  1137. void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
  1138. {
  1139. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1140. struct sorcery_load_details details = {
  1141. .sorcery = sorcery,
  1142. .reload = 1,
  1143. };
  1144. if (!object_type) {
  1145. return;
  1146. }
  1147. sorcery_object_load(object_type, &details, 0);
  1148. }
  1149. void ast_sorcery_force_reload_object(const struct ast_sorcery *sorcery, const char *type)
  1150. {
  1151. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1152. struct sorcery_load_details details = {
  1153. .sorcery = sorcery,
  1154. .reload = 1,
  1155. .force = 1,
  1156. };
  1157. if (!object_type) {
  1158. return;
  1159. }
  1160. sorcery_object_load(object_type, &details, 0);
  1161. }
  1162. void ast_sorcery_ref(struct ast_sorcery *sorcery)
  1163. {
  1164. ao2_ref(sorcery, +1);
  1165. }
  1166. static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
  1167. {
  1168. struct ast_variable *tmp = NULL;
  1169. char *buf = NULL;
  1170. if (!object_field->handler) {
  1171. return NULL;
  1172. }
  1173. if (!(object_field->handler(object, object_field->args, &buf))) {
  1174. tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
  1175. }
  1176. ast_free(buf);
  1177. return tmp;
  1178. }
  1179. static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
  1180. {
  1181. struct ast_variable *tmp = NULL;
  1182. if (!object_field->multiple_handler) {
  1183. return NULL;
  1184. }
  1185. if (object_field->multiple_handler(object, &tmp)) {
  1186. ast_variables_destroy(tmp);
  1187. tmp = NULL;
  1188. }
  1189. return tmp;
  1190. }
  1191. struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
  1192. const void *object, enum ast_sorcery_field_handler_flags flags)
  1193. {
  1194. const struct ast_sorcery_object_details *details = object;
  1195. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1196. struct ao2_iterator i;
  1197. struct ast_sorcery_object_field *object_field;
  1198. struct ast_variable *head = NULL;
  1199. struct ast_variable *tail = NULL;
  1200. if (!object_type) {
  1201. return NULL;
  1202. }
  1203. i = ao2_iterator_init(object_type->fields, 0);
  1204. for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
  1205. struct ast_variable *tmp;
  1206. switch (flags) {
  1207. case AST_HANDLER_PREFER_LIST:
  1208. if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
  1209. (tmp = get_single_field_as_var_list(object, object_field))) {
  1210. break;
  1211. }
  1212. continue;
  1213. case AST_HANDLER_PREFER_STRING:
  1214. if ((tmp = get_single_field_as_var_list(object, object_field)) ||
  1215. (tmp = get_multiple_fields_as_var_list(object, object_field))) {
  1216. break;
  1217. }
  1218. continue;
  1219. case AST_HANDLER_ONLY_LIST:
  1220. if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
  1221. break;
  1222. }
  1223. continue;
  1224. case AST_HANDLER_ONLY_STRING:
  1225. if ((tmp = get_single_field_as_var_list(object, object_field))) {
  1226. break;
  1227. }
  1228. continue;
  1229. default:
  1230. continue;
  1231. }
  1232. tail = ast_variable_list_append_hint(&head, tail, tmp);
  1233. }
  1234. ao2_iterator_destroy(&i);
  1235. return head;
  1236. }
  1237. struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
  1238. {
  1239. const struct ast_sorcery_object_details *details = object;
  1240. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1241. struct ao2_iterator i;
  1242. struct ast_sorcery_object_field *object_field;
  1243. struct ast_json *json = ast_json_object_create();
  1244. int res = 0;
  1245. if (!object_type || !json) {
  1246. ast_json_unref(json);
  1247. return NULL;
  1248. }
  1249. i = ao2_iterator_init(object_type->fields, 0);
  1250. for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
  1251. if (object_field->multiple_handler) {
  1252. struct ast_variable *tmp = NULL;
  1253. struct ast_variable *field;
  1254. if ((res = object_field->multiple_handler(object, &tmp))) {
  1255. ast_variables_destroy(tmp);
  1256. ao2_ref(object_field, -1);
  1257. break;
  1258. }
  1259. for (field = tmp; field; field = field->next) {
  1260. struct ast_json *value = ast_json_string_create(field->value);
  1261. if (!value || ast_json_object_set(json, field->name, value)) {
  1262. res = -1;
  1263. break;
  1264. }
  1265. }
  1266. ast_variables_destroy(tmp);
  1267. } else if (object_field->handler) {
  1268. char *buf = NULL;
  1269. struct ast_json *value = NULL;
  1270. if (object_field->handler(object, object_field->args, &buf)
  1271. || !(value = ast_json_string_create(buf))
  1272. || ast_json_object_set(json, object_field->name, value)) {
  1273. ast_free(buf);
  1274. ast_debug(5, "Skipping field '%s' for object type '%s'\n",
  1275. object_field->name, object_type->name);
  1276. continue;
  1277. }
  1278. ast_free(buf);
  1279. } else {
  1280. continue;
  1281. }
  1282. }
  1283. ao2_iterator_destroy(&i);
  1284. /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
  1285. if (res) {
  1286. ast_json_unref(json);
  1287. json = NULL;
  1288. }
  1289. return json;
  1290. }
  1291. int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
  1292. {
  1293. const struct ast_sorcery_object_details *details = object;
  1294. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1295. RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
  1296. struct ast_variable *field;
  1297. int res = 0;
  1298. if (!object_type) {
  1299. return -1;
  1300. }
  1301. if (object_type->transform && (transformed = object_type->transform(objectset))) {
  1302. field = transformed;
  1303. } else {
  1304. field = objectset;
  1305. }
  1306. for (; field; field = field->next) {
  1307. if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
  1308. break;
  1309. }
  1310. }
  1311. if (!res && object_type->apply) {
  1312. res = object_type->apply(sorcery, object);
  1313. }
  1314. return res;
  1315. }
  1316. int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
  1317. {
  1318. const struct ast_variable *field;
  1319. int res = 0;
  1320. *changes = NULL;
  1321. /* Unless the ast_variable list changes when examined... it can't differ from itself */
  1322. if (original == modified) {
  1323. return 0;
  1324. }
  1325. for (field = modified; field; field = field->next) {
  1326. const char *old_value = ast_variable_find_in_list(original, field->name);
  1327. if (!old_value || strcmp(old_value, field->value)) {
  1328. struct ast_variable *tmp;
  1329. if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
  1330. res = -1;
  1331. break;
  1332. }
  1333. tmp->next = *changes;
  1334. *changes = tmp;
  1335. }
  1336. }
  1337. /* If an error occurred do not return a partial changeset */
  1338. if (res) {
  1339. ast_variables_destroy(*changes);
  1340. *changes = NULL;
  1341. }
  1342. return res;
  1343. }
  1344. static void sorcery_object_destructor(void *object)
  1345. {
  1346. struct ast_sorcery_object_details *details = object;
  1347. if (details->object->destructor) {
  1348. details->object->destructor(object);
  1349. }
  1350. ast_variables_destroy(details->object->extended);
  1351. ast_free(details->object->id);
  1352. }
  1353. void *ast_sorcery_lockable_alloc(size_t size, ao2_destructor_fn destructor, void *lockobj)
  1354. {
  1355. void *object = ao2_alloc_with_lockobj(size + sizeof(struct ast_sorcery_object),
  1356. sorcery_object_destructor, lockobj, "");
  1357. struct ast_sorcery_object_details *details = object;
  1358. if (!object) {
  1359. return NULL;
  1360. }
  1361. details->object = object + size;
  1362. details->object->destructor = destructor;
  1363. return object;
  1364. }
  1365. void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
  1366. {
  1367. void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object),
  1368. sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  1369. struct ast_sorcery_object_details *details = object;
  1370. if (!object) {
  1371. return NULL;
  1372. }
  1373. details->object = object + size;
  1374. details->object->destructor = destructor;
  1375. return object;
  1376. }
  1377. void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
  1378. {
  1379. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1380. struct ast_sorcery_object_details *details;
  1381. if (!object_type || !object_type->type.item_alloc ||
  1382. !(details = object_type->type.item_alloc(id))) {
  1383. return NULL;
  1384. }
  1385. if (ast_strlen_zero(id)) {
  1386. char uuid[AST_UUID_STR_LEN];
  1387. ast_uuid_generate_str(uuid, sizeof(uuid));
  1388. details->object->id = ast_strdup(uuid);
  1389. } else {
  1390. details->object->id = ast_strdup(id);
  1391. }
  1392. if (!details->object->id) {
  1393. ao2_ref(details, -1);
  1394. return NULL;
  1395. }
  1396. details->object->created = ast_tvnow();
  1397. ast_copy_string(details->object->type, type, sizeof(details->object->type));
  1398. if (aco_set_defaults(&object_type->type, id, details)) {
  1399. ao2_ref(details, -1);
  1400. return NULL;
  1401. }
  1402. return details;
  1403. }
  1404. void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
  1405. {
  1406. const struct ast_sorcery_object_details *details = object;
  1407. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1408. struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
  1409. RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
  1410. int res = 0;
  1411. if (!copy) {
  1412. return NULL;
  1413. } else if (object_type->copy) {
  1414. res = object_type->copy(object, copy);
  1415. } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
  1416. res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
  1417. } else {
  1418. /* No native copy available and could not create an objectset, this copy has failed */
  1419. res = -1;
  1420. }
  1421. if (res) {
  1422. ao2_cleanup(copy);
  1423. copy = NULL;
  1424. }
  1425. return copy;
  1426. }
  1427. int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
  1428. {
  1429. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
  1430. *changes = NULL;
  1431. if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
  1432. return -1;
  1433. }
  1434. if (original == modified) {
  1435. return 0;
  1436. } else if (!object_type->diff) {
  1437. RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
  1438. RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
  1439. objectset1 = ast_sorcery_objectset_create(sorcery, original);
  1440. objectset2 = ast_sorcery_objectset_create(sorcery, modified);
  1441. return ast_sorcery_changeset_create(objectset1, objectset2, changes);
  1442. } else {
  1443. return object_type->diff(original, modified, changes);
  1444. }
  1445. }
  1446. /*! \brief Structure used when calling create, update, or delete */
  1447. struct sorcery_details {
  1448. /*! \brief Pointer to the sorcery instance */
  1449. const struct ast_sorcery *sorcery;
  1450. /*! \brief Pointer to the object itself */
  1451. void *obj;
  1452. };
  1453. /*! \brief Internal function used to create an object in caching wizards */
  1454. static int sorcery_cache_create(void *obj, void *arg, int flags)
  1455. {
  1456. const struct ast_sorcery_object_wizard *object_wizard = obj;
  1457. const struct sorcery_details *details = arg;
  1458. if (!object_wizard->caching || !object_wizard->wizard->callbacks.create) {
  1459. return 0;
  1460. }
  1461. object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj);
  1462. return 0;
  1463. }
  1464. void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
  1465. {
  1466. struct ast_sorcery_object_type *object_type;
  1467. void *object = NULL;
  1468. int i;
  1469. unsigned int cached = 0;
  1470. if (ast_strlen_zero(id)) {
  1471. return NULL;
  1472. }
  1473. object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
  1474. if (!object_type) {
  1475. return NULL;
  1476. }
  1477. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1478. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1479. struct ast_sorcery_object_wizard *wizard =
  1480. AST_VECTOR_GET(&object_type->wizards, i);
  1481. if (wizard->wizard->callbacks.retrieve_id &&
  1482. !(object = wizard->wizard->callbacks.retrieve_id(sorcery, wizard->data, object_type->name, id))) {
  1483. continue;
  1484. }
  1485. cached = wizard->caching;
  1486. break;
  1487. }
  1488. if (!cached && object) {
  1489. struct sorcery_details sdetails = {
  1490. .sorcery = sorcery,
  1491. .obj = object,
  1492. };
  1493. AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, &sdetails, 0);
  1494. }
  1495. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1496. ao2_ref(object_type, -1);
  1497. return object;
  1498. }
  1499. void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
  1500. {
  1501. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1502. void *object = NULL;
  1503. int i;
  1504. unsigned int cached = 0;
  1505. if (!object_type) {
  1506. return NULL;
  1507. }
  1508. /* If returning multiple objects create a container to store them in */
  1509. if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
  1510. object = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  1511. if (!object) {
  1512. return NULL;
  1513. }
  1514. }
  1515. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1516. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1517. struct ast_sorcery_object_wizard *wizard =
  1518. AST_VECTOR_GET(&object_type->wizards, i);
  1519. if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
  1520. if (wizard->wizard->callbacks.retrieve_multiple) {
  1521. wizard->wizard->callbacks.retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
  1522. }
  1523. } else if (fields && wizard->wizard->callbacks.retrieve_fields) {
  1524. if (wizard->wizard->callbacks.retrieve_fields) {
  1525. object = wizard->wizard->callbacks.retrieve_fields(sorcery, wizard->data, object_type->name, fields);
  1526. }
  1527. }
  1528. if (((flags & AST_RETRIEVE_FLAG_MULTIPLE) && (!ao2_container_count(object) || !wizard->caching)) || !object) {
  1529. continue;
  1530. }
  1531. cached = wizard->caching;
  1532. break;
  1533. }
  1534. /* If we are returning a single object and it came from a non-cache source create it in any caches */
  1535. if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
  1536. struct sorcery_details sdetails = {
  1537. .sorcery = sorcery,
  1538. .obj = object,
  1539. };
  1540. AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, &sdetails, 0);
  1541. }
  1542. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1543. return object;
  1544. }
  1545. struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
  1546. {
  1547. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1548. struct ao2_container *objects;
  1549. int i;
  1550. if (!object_type) {
  1551. return NULL;
  1552. }
  1553. objects = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  1554. if (!objects) {
  1555. return NULL;
  1556. }
  1557. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1558. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1559. struct ast_sorcery_object_wizard *wizard =
  1560. AST_VECTOR_GET(&object_type->wizards, i);
  1561. if (!wizard->wizard->callbacks.retrieve_regex) {
  1562. continue;
  1563. }
  1564. wizard->wizard->callbacks.retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
  1565. if (wizard->caching && ao2_container_count(objects)) {
  1566. break;
  1567. }
  1568. }
  1569. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1570. return objects;
  1571. }
  1572. struct ao2_container *ast_sorcery_retrieve_by_prefix(const struct ast_sorcery *sorcery, const char *type, const char *prefix, const size_t prefix_len)
  1573. {
  1574. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1575. struct ao2_container *objects;
  1576. int i;
  1577. if (!object_type) {
  1578. return NULL;
  1579. }
  1580. objects = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  1581. if (!objects) {
  1582. return NULL;
  1583. }
  1584. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1585. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1586. struct ast_sorcery_object_wizard *wizard =
  1587. AST_VECTOR_GET(&object_type->wizards, i);
  1588. if (!wizard->wizard->callbacks.retrieve_prefix) {
  1589. continue;
  1590. }
  1591. wizard->wizard->callbacks.retrieve_prefix(sorcery, wizard->data, object_type->name, objects, prefix, prefix_len);
  1592. if (wizard->caching && ao2_container_count(objects)) {
  1593. break;
  1594. }
  1595. }
  1596. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1597. return objects;
  1598. }
  1599. /*! \brief Internal function which returns if the wizard has created the object */
  1600. static int sorcery_wizard_create(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
  1601. {
  1602. if (!object_wizard->wizard->callbacks.create || object_wizard->read_only) {
  1603. ast_debug(5, "Sorcery wizard '%s' does not support creation\n", object_wizard->wizard->callbacks.name);
  1604. return 0;
  1605. }
  1606. if (object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) {
  1607. return 0;
  1608. }
  1609. return CMP_MATCH;
  1610. }
  1611. /*! \brief Internal callback function which notifies an individual observer that an object has been created */
  1612. static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
  1613. {
  1614. const struct ast_sorcery_object_type_observer *observer = obj;
  1615. if (observer->callbacks->created) {
  1616. observer->callbacks->created(arg);
  1617. }
  1618. return 0;
  1619. }
  1620. /*! \brief Internal callback function which notifies observers that an object has been created */
  1621. static int sorcery_observers_notify_create(void *data)
  1622. {
  1623. struct sorcery_observer_invocation *invocation = data;
  1624. ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
  1625. ao2_cleanup(invocation);
  1626. return 0;
  1627. }
  1628. int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
  1629. {
  1630. const struct ast_sorcery_object_details *details = object;
  1631. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1632. struct ast_sorcery_object_wizard *object_wizard = NULL;
  1633. struct ast_sorcery_object_wizard *found_wizard;
  1634. int i;
  1635. struct sorcery_details sdetails = {
  1636. .sorcery = sorcery,
  1637. .obj = object,
  1638. };
  1639. if (!object_type) {
  1640. return -1;
  1641. }
  1642. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1643. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1644. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1645. if (!found_wizard->caching
  1646. && sorcery_wizard_create(found_wizard, &sdetails) == CMP_MATCH) {
  1647. object_wizard = found_wizard;
  1648. }
  1649. }
  1650. if (object_wizard) {
  1651. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1652. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1653. if (found_wizard->caching) {
  1654. sorcery_wizard_create(found_wizard, &sdetails);
  1655. }
  1656. }
  1657. if (ao2_container_count(object_type->observers)) {
  1658. struct sorcery_observer_invocation *invocation;
  1659. invocation = sorcery_observer_invocation_alloc(object_type, object);
  1660. if (invocation
  1661. && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create,
  1662. invocation)) {
  1663. ao2_cleanup(invocation);
  1664. }
  1665. }
  1666. }
  1667. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1668. return object_wizard ? 0 : -1;
  1669. }
  1670. /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
  1671. static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
  1672. {
  1673. const struct ast_sorcery_object_type_observer *observer = obj;
  1674. if (observer->callbacks->updated) {
  1675. observer->callbacks->updated(arg);
  1676. }
  1677. return 0;
  1678. }
  1679. /*! \brief Internal callback function which notifies observers that an object has been updated */
  1680. static int sorcery_observers_notify_update(void *data)
  1681. {
  1682. struct sorcery_observer_invocation *invocation = data;
  1683. ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
  1684. ao2_cleanup(invocation);
  1685. return 0;
  1686. }
  1687. /*! \brief Internal function which returns if a wizard has updated the object */
  1688. static int sorcery_wizard_update(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
  1689. {
  1690. if (!object_wizard->wizard->callbacks.update || object_wizard->read_only) {
  1691. ast_debug(5, "Sorcery wizard '%s' does not support updating\n", object_wizard->wizard->callbacks.name);
  1692. return 0;
  1693. }
  1694. if (object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj)) {
  1695. return 0;
  1696. }
  1697. return CMP_MATCH;
  1698. }
  1699. int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
  1700. {
  1701. const struct ast_sorcery_object_details *details = object;
  1702. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1703. struct ast_sorcery_object_wizard *object_wizard = NULL;
  1704. struct ast_sorcery_object_wizard *found_wizard;
  1705. int i;
  1706. struct sorcery_details sdetails = {
  1707. .sorcery = sorcery,
  1708. .obj = object,
  1709. };
  1710. if (!object_type) {
  1711. return -1;
  1712. }
  1713. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1714. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1715. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1716. if (!found_wizard->caching
  1717. && sorcery_wizard_update(found_wizard, &sdetails) == CMP_MATCH) {
  1718. object_wizard = found_wizard;
  1719. }
  1720. }
  1721. if (object_wizard) {
  1722. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1723. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1724. if (found_wizard->caching) {
  1725. sorcery_wizard_update(found_wizard, &sdetails);
  1726. }
  1727. }
  1728. if (ao2_container_count(object_type->observers)) {
  1729. struct sorcery_observer_invocation *invocation;
  1730. invocation = sorcery_observer_invocation_alloc(object_type, object);
  1731. if (invocation
  1732. && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update,
  1733. invocation)) {
  1734. ao2_cleanup(invocation);
  1735. }
  1736. }
  1737. }
  1738. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1739. return object_wizard ? 0 : -1;
  1740. }
  1741. /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
  1742. static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
  1743. {
  1744. const struct ast_sorcery_object_type_observer *observer = obj;
  1745. if (observer->callbacks->deleted) {
  1746. observer->callbacks->deleted(arg);
  1747. }
  1748. return 0;
  1749. }
  1750. /*! \brief Internal callback function which notifies observers that an object has been deleted */
  1751. static int sorcery_observers_notify_delete(void *data)
  1752. {
  1753. struct sorcery_observer_invocation *invocation = data;
  1754. ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
  1755. ao2_cleanup(invocation);
  1756. return 0;
  1757. }
  1758. /*! \brief Internal function which returns if a wizard has deleted the object */
  1759. static int sorcery_wizard_delete(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
  1760. {
  1761. if (!object_wizard->wizard->callbacks.delete || object_wizard->read_only) {
  1762. ast_debug(5, "Sorcery wizard '%s' does not support deletion\n", object_wizard->wizard->callbacks.name);
  1763. return 0;
  1764. }
  1765. if (object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj)) {
  1766. return 0;
  1767. }
  1768. return CMP_MATCH;
  1769. }
  1770. int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
  1771. {
  1772. const struct ast_sorcery_object_details *details = object;
  1773. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1774. struct ast_sorcery_object_wizard *object_wizard = NULL;
  1775. struct ast_sorcery_object_wizard *found_wizard;
  1776. int i;
  1777. struct sorcery_details sdetails = {
  1778. .sorcery = sorcery,
  1779. .obj = object,
  1780. };
  1781. if (!object_type) {
  1782. return -1;
  1783. }
  1784. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1785. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1786. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1787. if (!found_wizard->caching
  1788. && sorcery_wizard_delete(found_wizard, &sdetails) == CMP_MATCH) {
  1789. object_wizard = found_wizard;
  1790. }
  1791. }
  1792. if (object_wizard) {
  1793. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1794. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1795. if (found_wizard->caching) {
  1796. sorcery_wizard_delete(found_wizard, &sdetails);
  1797. }
  1798. }
  1799. if (ao2_container_count(object_type->observers)) {
  1800. struct sorcery_observer_invocation *invocation;
  1801. invocation = sorcery_observer_invocation_alloc(object_type, object);
  1802. if (invocation
  1803. && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete,
  1804. invocation)) {
  1805. ao2_cleanup(invocation);
  1806. }
  1807. }
  1808. }
  1809. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1810. return object_wizard ? 0 : -1;
  1811. }
  1812. int ast_sorcery_is_stale(const struct ast_sorcery *sorcery, void *object)
  1813. {
  1814. const struct ast_sorcery_object_details *details = object;
  1815. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
  1816. struct ast_sorcery_object_wizard *found_wizard;
  1817. int res = 0;
  1818. int i;
  1819. if (!object_type) {
  1820. return -1;
  1821. }
  1822. AST_VECTOR_RW_RDLOCK(&object_type->wizards);
  1823. for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
  1824. found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
  1825. if (found_wizard->wizard->callbacks.is_stale) {
  1826. res |= found_wizard->wizard->callbacks.is_stale(sorcery, found_wizard->data, object);
  1827. ast_debug(5, "After calling wizard '%s', object '%s' is %s\n",
  1828. found_wizard->wizard->callbacks.name,
  1829. ast_sorcery_object_get_id(object),
  1830. res ? "stale" : "not stale");
  1831. }
  1832. }
  1833. AST_VECTOR_RW_UNLOCK(&object_type->wizards);
  1834. return res;
  1835. }
  1836. const char *ast_sorcery_object_get_id(const void *object)
  1837. {
  1838. const struct ast_sorcery_object_details *details = object;
  1839. return details->object->id;
  1840. }
  1841. const struct timeval ast_sorcery_object_get_created(const void *object)
  1842. {
  1843. const struct ast_sorcery_object_details *details = object;
  1844. return details->object->created;
  1845. }
  1846. const char *ast_sorcery_object_get_type(const void *object)
  1847. {
  1848. const struct ast_sorcery_object_details *details = object;
  1849. return details->object->type;
  1850. }
  1851. const char *ast_sorcery_object_get_extended(const void *object, const char *name)
  1852. {
  1853. const struct ast_sorcery_object_details *details = object;
  1854. struct ast_variable *field;
  1855. for (field = details->object->extended; field; field = field->next) {
  1856. if (!strcmp(field->name + 1, name)) {
  1857. return field->value;
  1858. }
  1859. }
  1860. return NULL;
  1861. }
  1862. int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
  1863. {
  1864. RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
  1865. struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
  1866. const struct ast_sorcery_object_details *details = object;
  1867. if (!extended) {
  1868. return -1;
  1869. }
  1870. for (field = details->object->extended; field; previous = field, field = field->next) {
  1871. if (!strcmp(field->name, name)) {
  1872. if (previous) {
  1873. previous->next = field->next;
  1874. } else {
  1875. details->object->extended = field->next;
  1876. }
  1877. field->next = NULL;
  1878. break;
  1879. }
  1880. }
  1881. extended->next = details->object->extended;
  1882. details->object->extended = extended;
  1883. return 0;
  1884. }
  1885. unsigned int ast_sorcery_object_has_dynamic_contents(const void *object)
  1886. {
  1887. const struct ast_sorcery_object_details *details = object;
  1888. return details->object->has_dynamic_contents;
  1889. }
  1890. void ast_sorcery_object_set_has_dynamic_contents(const void *object)
  1891. {
  1892. const struct ast_sorcery_object_details *details = object;
  1893. details->object->has_dynamic_contents = 1;
  1894. }
  1895. int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
  1896. {
  1897. RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
  1898. struct ast_sorcery_object_type_observer *observer;
  1899. int res;
  1900. if (!object_type || !callbacks) {
  1901. return -1;
  1902. }
  1903. if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
  1904. return -1;
  1905. }
  1906. observer->callbacks = callbacks;
  1907. res = 0;
  1908. if (!ao2_link(object_type->observers, observer)) {
  1909. res = -1;
  1910. }
  1911. ao2_ref(observer, -1);
  1912. return res;
  1913. }
  1914. /*! \brief Internal callback function for removing an observer */
  1915. static int sorcery_observer_remove(void *obj, void *arg, int flags)
  1916. {
  1917. const struct ast_sorcery_object_type_observer *observer = obj;
  1918. return (observer->callbacks == arg) ? CMP_MATCH : 0;
  1919. }
  1920. void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
  1921. {
  1922. RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
  1923. struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
  1924. if (!sorcery) {
  1925. return;
  1926. }
  1927. object_type = ao2_find(sorcery->types, type, OBJ_KEY);
  1928. if (!object_type) {
  1929. return;
  1930. }
  1931. ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
  1932. sorcery_observer_remove, cbs);
  1933. }
  1934. int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
  1935. {
  1936. const void *object_left = obj;
  1937. const void *object_right = arg;
  1938. const char *right_key = arg;
  1939. int cmp;
  1940. switch (flags & OBJ_SEARCH_MASK) {
  1941. case OBJ_SEARCH_OBJECT:
  1942. right_key = ast_sorcery_object_get_id(object_right);
  1943. /* Fall through */
  1944. case OBJ_SEARCH_KEY:
  1945. cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
  1946. break;
  1947. case OBJ_SEARCH_PARTIAL_KEY:
  1948. cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
  1949. break;
  1950. default:
  1951. cmp = 0;
  1952. break;
  1953. }
  1954. return cmp;
  1955. }
  1956. int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
  1957. {
  1958. int cmp;
  1959. cmp = ast_sorcery_object_id_sort(obj, arg, flags);
  1960. if (cmp) {
  1961. return 0;
  1962. }
  1963. return CMP_MATCH;
  1964. }
  1965. int ast_sorcery_object_id_hash(const void *obj, int flags)
  1966. {
  1967. const char *key;
  1968. switch (flags & OBJ_SEARCH_MASK) {
  1969. case OBJ_SEARCH_KEY:
  1970. key = obj;
  1971. break;
  1972. case OBJ_SEARCH_OBJECT:
  1973. key = ast_sorcery_object_get_id(obj);
  1974. break;
  1975. default:
  1976. /* Hash can only work on something with a full key. */
  1977. ast_assert(0);
  1978. return 0;
  1979. }
  1980. return ast_str_hash(key);
  1981. }
  1982. struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
  1983. const char *type)
  1984. {
  1985. return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
  1986. }
  1987. static int is_registered_cb(void *obj, void *arg, int flags)
  1988. {
  1989. struct ast_sorcery_object_field *object_field = obj;
  1990. char *name = arg;
  1991. int rc = 0;
  1992. if (object_field->name_regex
  1993. && !regexec(object_field->name_regex, name, 0, NULL, 0)) {
  1994. rc = CMP_MATCH;
  1995. }
  1996. return rc;
  1997. }
  1998. int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
  1999. const char *field_name)
  2000. {
  2001. struct ast_sorcery_object_field *object_field;
  2002. int res = 1;
  2003. ast_assert(object_type != NULL);
  2004. object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
  2005. if (!object_field) {
  2006. object_field = ao2_callback(object_type->fields, 0, is_registered_cb, (char *)field_name);
  2007. }
  2008. if (!object_field) {
  2009. res = 0;
  2010. }
  2011. ao2_cleanup(object_field);
  2012. return res;
  2013. }
  2014. const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery)
  2015. {
  2016. return sorcery->module_name;
  2017. }