sorcery.c 66 KB

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