Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


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"

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)
 
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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static 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 } , }
 

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

static void __reg_module ( void  )
static

Definition at line 207 of file func_config.c.

static void __unreg_module ( void  )
static

Definition at line 207 of file func_config.c.

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

Definition at line 68 of file func_config.c.

References args, AST_APP_ARG, ast_calloc, 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(), ast_variable_retrieve(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, and parse().

70 {
71  struct ast_config *cfg;
72  struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
73  const char *val;
74  char *parse;
75  struct config_item *cur;
77  AST_APP_ARG(filename);
78  AST_APP_ARG(category);
79  AST_APP_ARG(variable);
80  AST_APP_ARG(index);
81  );
82 
83  if (ast_strlen_zero(data)) {
84  ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
85  return -1;
86  }
87 
88  parse = ast_strdupa(data);
90 
91  if (ast_strlen_zero(args.filename)) {
92  ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
93  return -1;
94  }
95 
96  if (ast_strlen_zero(args.category)) {
97  ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
98  return -1;
99  }
100 
101  if (ast_strlen_zero(args.variable)) {
102  ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\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 
110  if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
111  /* Retrieve cfg from list */
113  AST_RWLIST_TRAVERSE(&configs, cur, entry) {
114  if (!strcmp(cur->filename, args.filename)) {
115  break;
116  }
117  }
118 
119  if (!cur) {
120  /* At worst, we might leak an entry while upgrading locks */
123  if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
125  return -1;
126  }
127 
128  strcpy(cur->filename, args.filename);
129 
131  if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
132  ast_free(cur);
134  return -1;
135  }
136 
137  cur->cfg = cfg;
138  AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
139  }
140 
141  cfg = cur->cfg;
142  } else {
143  /* Replace cfg in list */
145  AST_RWLIST_TRAVERSE(&configs, cur, entry) {
146  if (!strcmp(cur->filename, args.filename)) {
147  break;
148  }
149  }
150 
151  if (!cur) {
152  if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
154  return -1;
155  }
156 
157  strcpy(cur->filename, args.filename);
158  cur->cfg = cfg;
159 
160  AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
161  } else {
162  ast_config_destroy(cur->cfg);
163  cur->cfg = cfg;
164  }
165  }
166 
167  if (!(val = ast_variable_retrieve(cfg, args.category, args.variable))) {
168  ast_debug(1, "'%s' not found in [%s] of '%s'\n", args.variable,
169  args.category, args.filename);
171  return -1;
172  }
173 
174  ast_copy_string(buf, val, len);
175 
176  /* Unlock down here, so there's no chance the struct goes away while we're using it. */
178 
179  return 0;
180 }
const char * ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
Gets a variable.
Definition: config.c:625
Definition: ast_expr2.c:325
#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
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Definition: app.h:572
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
#define ast_free(a)
Definition: astmm.h:97
Structure used to handle boolean flags.
Definition: utils.h:200
#define ast_clear_flag(p, flag)
Definition: utils.h:77
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
#define ast_calloc(a, b)
Definition: astmm.h:82
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Definition: app.h:604
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
#define CONFIG_STATUS_FILEUNCHANGED
Definition: config.h:51
static int load_module ( void  )
static

Definition at line 202 of file func_config.c.

References ast_custom_function_register.

203 {
205 }
static struct ast_custom_function config_function
Definition: func_config.c:182
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static int unload_module ( void  )
static

Definition at line 187 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.

188 {
189  struct config_item *current;
191 
193  while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) {
194  ast_config_destroy(current->cfg);
195  ast_free(current);
196  }
198 
199  return res;
200 }
static struct ast_custom_function config_function
Definition: func_config.c:182
#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
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
#define ast_free(a)
Definition: astmm.h:97
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:829

Variable Documentation

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 207 of file func_config.c.

Definition at line 207 of file func_config.c.

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:68

Definition at line 182 of file func_config.c.

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