Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
func_global.c File Reference

Global variable dialplan functions. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/stasis_channels.h"
Include dependency graph for func_global.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int global_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int global_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int load_module (void)
 
static int shared_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static void shared_variable_free (void *data)
 
static int shared_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Variable dialplan functions" , .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 global_function
 
static struct ast_custom_function shared_function
 
static const struct ast_datastore_info shared_variable_info
 

Detailed Description

Global variable dialplan functions.

Author
Tilghman Lesher func_.nosp@m.glob.nosp@m.al__2.nosp@m.0060.nosp@m.5@the.nosp@m.-til.nosp@m.ghman.nosp@m..com

Definition in file func_global.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 331 of file func_global.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 331 of file func_global.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 331 of file func_global.c.

◆ global_read()

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

Definition at line 123 of file func_global.c.

References ast_copy_string(), NULL, pbx_builtin_getvar_helper(), and var.

124 {
125  const char *var = pbx_builtin_getvar_helper(NULL, data);
126 
127  *buf = '\0';
128 
129  if (var)
130  ast_copy_string(buf, var, len);
131 
132  return 0;
133 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define var
Definition: ast_expr2f.c:614
#define NULL
Definition: resample.c:96
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401

◆ global_write()

static int global_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 135 of file func_global.c.

References NULL, and pbx_builtin_setvar_helper().

136 {
138 
139  return 0;
140 }
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...

◆ load_module()

static int load_module ( void  )
static

Definition at line 321 of file func_global.c.

References ast_custom_function_register.

322 {
323  int res = 0;
324 
327 
328  return res;
329 }
static struct ast_custom_function shared_function
Definition: func_global.c:305
static struct ast_custom_function global_function
Definition: func_global.c:142
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ shared_read()

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

Definition at line 148 of file func_global.c.

References args, ast_alloca, AST_APP_ARG, ast_channel_datastore_find(), ast_channel_get_by_name(), ast_channel_get_by_name_prefix(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_LIST_TRAVERSE, ast_log, AST_STANDARD_APP_ARGS, ast_strlen_zero, ast_var_name(), ast_var_value(), ast_datastore::data, LOG_ERROR, LOG_WARNING, NULL, prefix, and var.

149 {
150  struct ast_datastore *varstore;
151  struct varshead *varshead;
152  struct ast_var_t *var;
154  AST_APP_ARG(var);
155  AST_APP_ARG(chan);
156  );
157  struct ast_channel *c_ref = NULL;
158 
159  if (ast_strlen_zero(data)) {
160  ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
161  return -1;
162  }
163 
165 
166  if (!ast_strlen_zero(args.chan)) {
167  char *prefix = ast_alloca(strlen(args.chan) + 2);
168  sprintf(prefix, "%s-", args.chan);
169  if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
170  ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
171  return -1;
172  }
173  chan = c_ref;
174  } else if (!chan) {
175  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
176  return -1;
177  }
178 
179  ast_channel_lock(chan);
180 
181  if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
182  ast_channel_unlock(chan);
183  if (c_ref) {
184  c_ref = ast_channel_unref(c_ref);
185  }
186  return -1;
187  }
188 
189  varshead = varstore->data;
190  *buf = '\0';
191 
192  /* Protected by the channel lock */
193  AST_LIST_TRAVERSE(varshead, var, entries) {
194  if (!strcmp(args.var, ast_var_name(var))) {
196  break;
197  }
198  }
199 
200  ast_channel_unlock(chan);
201 
202  if (c_ref) {
203  c_ref = ast_channel_unref(c_ref);
204  }
205 
206  return 0;
207 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:80
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
#define var
Definition: ast_expr2f.c:614
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
const char * args
#define NULL
Definition: resample.c:96
const char * data
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1434
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
static const struct ast_datastore_info shared_variable_info
Definition: func_global.c:107
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
static char prefix[MAX_PREFIX]
Definition: http.c:141
#define AST_APP_ARG(name)
Define an application argument.

◆ shared_variable_free()

static void shared_variable_free ( void *  data)
static

Definition at line 112 of file func_global.c.

References ast_free, AST_LIST_REMOVE_HEAD, ast_var_delete(), ast_var_t::entries, and var.

113 {
114  struct varshead *varshead = data;
115  struct ast_var_t *var;
116 
117  while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
118  ast_var_delete(var);
119  }
120  ast_free(varshead);
121 }
#define var
Definition: ast_expr2f.c:614
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:832
void ast_var_delete(struct ast_var_t *var)
Definition: extconf.c:2473
#define ast_free(a)
Definition: astmm.h:182
struct ast_var_t::@249 entries

◆ shared_write()

static int shared_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 209 of file func_global.c.

References args, ast_alloca, AST_APP_ARG, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_get_by_name(), ast_channel_get_by_name_prefix(), ast_channel_lock, ast_channel_publish_varset(), ast_channel_unlock, ast_channel_unref, ast_datastore_alloc, ast_datastore_free(), AST_DECLARE_APP_ARGS, ast_free, AST_LIST_INSERT_HEAD, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log, ast_malloc, AST_STANDARD_APP_ARGS, ast_strlen_zero, ast_var_assign, ast_var_delete(), ast_var_name(), ast_datastore::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, RAII_VAR, S_OR, var, and ast_channel::varshead.

210 {
211  struct ast_datastore *varstore;
212  struct varshead *varshead;
213  struct ast_var_t *var;
215  AST_APP_ARG(var);
216  AST_APP_ARG(chan);
217  );
218  struct ast_channel *c_ref = NULL;
219  int len;
220  RAII_VAR(char *, shared_buffer, NULL, ast_free);
221 
222  if (ast_strlen_zero(data)) {
223  ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
224  return -1;
225  }
226 
228 
229  if (!ast_strlen_zero(args.chan)) {
230  char *prefix = ast_alloca(strlen(args.chan) + 2);
231  sprintf(prefix, "%s-", args.chan);
232  if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
233  ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
234  return -1;
235  }
236  chan = c_ref;
237  } else if (!chan) {
238  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
239  return -1;
240  }
241 
242  len = 9 + strlen(args.var); /* SHARED() + var */
243  shared_buffer = ast_malloc(len);
244  if (!shared_buffer) {
245  if (c_ref) {
246  ast_channel_unref(c_ref);
247  }
248  return -1;
249  }
250 
251  ast_channel_lock(chan);
252 
253  if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
254  if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
255  ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
256  ast_channel_unlock(chan);
257  if (c_ref) {
258  c_ref = ast_channel_unref(c_ref);
259  }
260  return -1;
261  }
262 
263  if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
264  ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
265  ast_datastore_free(varstore);
266  ast_channel_unlock(chan);
267  if (c_ref) {
268  c_ref = ast_channel_unref(c_ref);
269  }
270  return -1;
271  }
272 
273  varstore->data = varshead;
274  ast_channel_datastore_add(chan, varstore);
275  }
276  varshead = varstore->data;
277 
278  /* Protected by the channel lock */
279  AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) {
280  /* If there's a previous value, remove it */
281  if (!strcmp(args.var, ast_var_name(var))) {
282  AST_LIST_REMOVE_CURRENT(entries);
283  ast_var_delete(var);
284  break;
285  }
286  }
288 
289  if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
290  AST_LIST_INSERT_HEAD(varshead, var, entries);
291 
292  sprintf(shared_buffer, "SHARED(%s)", args.var);
293  ast_channel_publish_varset(chan, shared_buffer, value);
294  }
295 
296  ast_channel_unlock(chan);
297 
298  if (c_ref) {
299  c_ref = ast_channel_unref(c_ref);
300  }
301 
302  return 0;
303 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
#define var
Definition: ast_expr2f.c:614
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
const char * args
#define NULL
Definition: resample.c:96
const char * data
int value
Definition: syslog.c:37
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1434
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
static const struct ast_datastore_info shared_variable_info
Definition: func_global.c:107
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
#define LOG_ERROR
Definition: logger.h:285
void ast_channel_publish_varset(struct ast_channel *chan, const char *variable, const char *value)
Publish a ast_channel_varset for a channel.
void ast_var_delete(struct ast_var_t *var)
Definition: extconf.c:2473
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define ast_var_assign(name, value)
Definition: chanvars.h:40
void * data
Definition: datastore.h:70
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
struct varshead varshead
static char prefix[MAX_PREFIX]
Definition: http.c:141
#define AST_APP_ARG(name)
Define an application argument.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 311 of file func_global.c.

References ast_custom_function_unregister().

312 {
313  int res = 0;
314 
317 
318  return res;
319 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function shared_function
Definition: func_global.c:305
static struct ast_custom_function global_function
Definition: func_global.c:142

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Variable dialplan functions" , .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 331 of file func_global.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 331 of file func_global.c.

◆ global_function

struct ast_custom_function global_function
static
Initial value:
= {
.name = "GLOBAL",
.read = global_read,
.write = global_write,
}
static int global_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:123
static int global_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:135

Definition at line 142 of file func_global.c.

◆ shared_function

struct ast_custom_function shared_function
static
Initial value:
= {
.name = "SHARED",
.read = shared_read,
.write = shared_write,
}
static int shared_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:209
static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:148

Definition at line 305 of file func_global.c.

◆ shared_variable_info

const struct ast_datastore_info shared_variable_info
static
Initial value:
= {
.type = "SHARED_VARIABLES",
}
static void shared_variable_free(void *data)
Definition: func_global.c:112

Definition at line 107 of file func_global.c.