func_config.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. * Tilghman Lesher <func_config__200803@the-tilghman.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief A function to retrieve variables from an Asterisk configuration file
  22. *
  23. * \author Russell Bryant <russell@digium.com>
  24. * \author Tilghman Lesher <func_config__200803@the-tilghman.com>
  25. *
  26. * \ingroup functions
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/module.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/app.h"
  37. /*** DOCUMENTATION
  38. <function name="AST_CONFIG" language="en_US">
  39. <synopsis>
  40. Retrieve a variable from a configuration file.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="config_file" required="true" />
  44. <parameter name="category" required="true" />
  45. <parameter name="variable_name" required="true" />
  46. <parameter name="index" required="false">
  47. <para>If there are multiple variables with the same name, you can specify
  48. <literal>0</literal> for the first item (default), <literal>-1</literal> for the last
  49. item, or any other number for that specific item. <literal>-1</literal> is useful
  50. when the variable is derived from a template and you want the effective value (the last
  51. occurrence), not the value from the template (the first occurrence).</para>
  52. </parameter>
  53. </syntax>
  54. <description>
  55. <para>This function reads a variable from an Asterisk configuration file.</para>
  56. </description>
  57. </function>
  58. ***/
  59. struct config_item {
  60. AST_RWLIST_ENTRY(config_item) entry;
  61. struct ast_config *cfg;
  62. char filename[0];
  63. };
  64. static AST_RWLIST_HEAD_STATIC(configs, config_item);
  65. static int config_function_read(struct ast_channel *chan, const char *cmd, char *data,
  66. char *buf, size_t len)
  67. {
  68. struct ast_config *cfg;
  69. struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
  70. char *parse;
  71. struct config_item *cur;
  72. int index = 0;
  73. struct ast_variable *var;
  74. struct ast_variable *found = NULL;
  75. int ix = 0;
  76. AST_DECLARE_APP_ARGS(args,
  77. AST_APP_ARG(filename);
  78. AST_APP_ARG(category);
  79. AST_APP_ARG(variable);
  80. AST_APP_ARG(index);
  81. );
  82. if (ast_strlen_zero(data)) {
  83. ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
  84. return -1;
  85. }
  86. parse = ast_strdupa(data);
  87. AST_STANDARD_APP_ARGS(args, parse);
  88. if (ast_strlen_zero(args.filename)) {
  89. ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
  90. return -1;
  91. }
  92. if (ast_strlen_zero(args.category)) {
  93. ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
  94. return -1;
  95. }
  96. if (ast_strlen_zero(args.variable)) {
  97. ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
  98. return -1;
  99. }
  100. if (!ast_strlen_zero(args.index)) {
  101. if (!sscanf(args.index, "%d", &index)) {
  102. ast_log(LOG_ERROR, "AST_CONFIG() index must be an integer\n");
  103. return -1;
  104. }
  105. }
  106. if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  107. return -1;
  108. }
  109. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  110. /* Retrieve cfg from list */
  111. AST_RWLIST_RDLOCK(&configs);
  112. AST_RWLIST_TRAVERSE(&configs, cur, entry) {
  113. if (!strcmp(cur->filename, args.filename)) {
  114. break;
  115. }
  116. }
  117. if (!cur) {
  118. /* At worst, we might leak an entry while upgrading locks */
  119. AST_RWLIST_UNLOCK(&configs);
  120. AST_RWLIST_WRLOCK(&configs);
  121. if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
  122. AST_RWLIST_UNLOCK(&configs);
  123. return -1;
  124. }
  125. strcpy(cur->filename, args.filename);
  126. ast_clear_flag(&cfg_flags, CONFIG_FLAG_FILEUNCHANGED);
  127. if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  128. ast_free(cur);
  129. AST_RWLIST_UNLOCK(&configs);
  130. return -1;
  131. }
  132. cur->cfg = cfg;
  133. AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
  134. }
  135. cfg = cur->cfg;
  136. } else {
  137. /* Replace cfg in list */
  138. AST_RWLIST_WRLOCK(&configs);
  139. AST_RWLIST_TRAVERSE(&configs, cur, entry) {
  140. if (!strcmp(cur->filename, args.filename)) {
  141. break;
  142. }
  143. }
  144. if (!cur) {
  145. if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
  146. AST_RWLIST_UNLOCK(&configs);
  147. return -1;
  148. }
  149. strcpy(cur->filename, args.filename);
  150. cur->cfg = cfg;
  151. AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
  152. } else {
  153. ast_config_destroy(cur->cfg);
  154. cur->cfg = cfg;
  155. }
  156. }
  157. for (var = ast_category_root(cfg, args.category); var; var = var->next) {
  158. if (strcasecmp(args.variable, var->name)) {
  159. continue;
  160. }
  161. found = var;
  162. if (index == -1) {
  163. continue;
  164. }
  165. if (ix == index) {
  166. break;
  167. }
  168. found = NULL;
  169. ix++;
  170. }
  171. if (!found) {
  172. ast_debug(1, "'%s' not found at index %d in [%s] of '%s'. Maximum index found: %d\n",
  173. args.variable, index, args.category, args.filename, ix);
  174. AST_RWLIST_UNLOCK(&configs);
  175. return -1;
  176. }
  177. ast_copy_string(buf, found->value, len);
  178. /* Unlock down here, so there's no chance the struct goes away while we're using it. */
  179. AST_RWLIST_UNLOCK(&configs);
  180. return 0;
  181. }
  182. static struct ast_custom_function config_function = {
  183. .name = "AST_CONFIG",
  184. .read = config_function_read,
  185. };
  186. static int unload_module(void)
  187. {
  188. struct config_item *current;
  189. int res = ast_custom_function_unregister(&config_function);
  190. AST_RWLIST_WRLOCK(&configs);
  191. while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) {
  192. ast_config_destroy(current->cfg);
  193. ast_free(current);
  194. }
  195. AST_RWLIST_UNLOCK(&configs);
  196. return res;
  197. }
  198. static int load_module(void)
  199. {
  200. return ast_custom_function_register(&config_function);
  201. }
  202. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access");