Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
func_config.c File Reference

A function to retrieve variables from an Asterisk configuration file. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
Include dependency graph for func_config.c:

Go to the source code of this file.

Data Structures

struct  config_item
 
struct  configs
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int config_function_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk configuration file variable access" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function config_function
 
static struct configs configs = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 

Detailed Description

A function to retrieve variables from an Asterisk configuration file.

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com
Tilghman Lesher func_.nosp@m.conf.nosp@m.ig__2.nosp@m.0080.nosp@m.3@the.nosp@m.-til.nosp@m.ghman.nosp@m..com

Definition in file func_config.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 237 of file func_config.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 237 of file func_config.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 237 of file func_config.c.

◆ config_function_read()

static int config_function_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 73 of file func_config.c.

References args, AST_APP_ARG, ast_calloc, ast_category_root(), ast_clear_flag, ast_config_destroy(), ast_config_load, ast_copy_string(), ast_debug, AST_DECLARE_APP_ARGS, ast_free, ast_log, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, ast_variable::name, ast_variable::next, NULL, parse(), ast_variable::value, and var.

75 {
76  struct ast_config *cfg;
77  struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
78  char *parse;
79  struct config_item *cur;
80  int index = 0;
81  struct ast_variable *var;
82  struct ast_variable *found = NULL;
83  int ix = 0;
85  AST_APP_ARG(filename);
86  AST_APP_ARG(category);
87  AST_APP_ARG(variable);
88  AST_APP_ARG(index);
89  );
90 
91  if (ast_strlen_zero(data)) {
92  ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
93  return -1;
94  }
95 
96  parse = ast_strdupa(data);
98 
99  if (ast_strlen_zero(args.filename)) {
100  ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
101  return -1;
102  }
103 
104  if (ast_strlen_zero(args.category)) {
105  ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
106  return -1;
107  }
108 
109  if (ast_strlen_zero(args.variable)) {
110  ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
111  return -1;
112  }
113 
114  if (!ast_strlen_zero(args.index)) {
115  if (!sscanf(args.index, "%d", &index)) {
116  ast_log(LOG_ERROR, "AST_CONFIG() index must be an integer\n");
117  return -1;
118  }
119  }
120 
121  if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
122  return -1;
123  }
124 
125  if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
126  /* Retrieve cfg from list */
129  if (!strcmp(cur->filename, args.filename)) {
130  break;
131  }
132  }
133 
134  if (!cur) {
135  /* At worst, we might leak an entry while upgrading locks */
138  if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
140  return -1;
141  }
142 
143  strcpy(cur->filename, args.filename);
144 
146  if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
147  ast_free(cur);
149  return -1;
150  }
151 
152  cur->cfg = cfg;
154  }
155 
156  cfg = cur->cfg;
157  } else {
158  /* Replace cfg in list */
161  if (!strcmp(cur->filename, args.filename)) {
162  break;
163  }
164  }
165 
166  if (!cur) {
167  if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
169  return -1;
170  }
171 
172  strcpy(cur->filename, args.filename);
173  cur->cfg = cfg;
174 
176  } else {
177  ast_config_destroy(cur->cfg);
178  cur->cfg = cfg;
179  }
180  }
181 
182  for (var = ast_category_root(cfg, args.category); var; var = var->next) {
183  if (strcasecmp(args.variable, var->name)) {
184  continue;
185  }
186  found = var;
187  if (index == -1) {
188  continue;
189  }
190  if (ix == index) {
191  break;
192  }
193  found = NULL;
194  ix++;
195  }
196 
197  if (!found) {
198  ast_debug(1, "'%s' not found at index %d in [%s] of '%s'. Maximum index found: %d\n",
199  args.variable, index, args.category, args.filename, ix);
201  return -1;
202  }
203 
204  ast_copy_string(buf, found->value, len);
205 
206  /* Unlock down here, so there's no chance the struct goes away while we're using it. */
208 
209  return 0;
210 }
struct ast_variable * next
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define CONFIG_STATUS_FILEINVALID
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
const char * args
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
struct ast_variable * ast_category_root(struct ast_config *config, char *cat)
returns the root ast_variable of a config
Definition: main/config.c:1162
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define CONFIG_STATUS_FILEUNCHANGED
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
Structure used to handle boolean flags.
Definition: utils.h:199
#define ast_clear_flag(p, flag)
Definition: utils.h:77
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:740
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Definition: search.h:40
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 232 of file func_config.c.

References ast_custom_function_register.

233 {
235 }
static struct ast_custom_function config_function
Definition: func_config.c:212
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 217 of file func_config.c.

References ast_config_destroy(), ast_custom_function_unregister(), ast_free, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

218 {
219  struct config_item *current;
221 
223  while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) {
224  ast_config_destroy(current->cfg);
225  ast_free(current);
226  }
228 
229  return res;
230 }
static struct ast_custom_function config_function
Definition: func_config.c:212
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define ast_free(a)
Definition: astmm.h:182
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843
Definition: search.h:40

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk configuration file variable access" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 237 of file func_config.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 237 of file func_config.c.

◆ config_function

struct ast_custom_function config_function
static
Initial value:
= {
.name = "AST_CONFIG",
}
static int config_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_config.c:73

Definition at line 212 of file func_config.c.

◆ configs

struct configs configs = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static

Referenced by AST_TEST_DEFINE().