Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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_delete_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int global_exists_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
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 = AST_BUILDOPT_SUM, .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_delete_function
 
static struct ast_custom_function global_exists_function
 
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 415 of file func_global.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 415 of file func_global.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 415 of file func_global.c.

◆ global_delete_write()

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

Definition at line 201 of file func_global.c.

202{
204
205 return 0;
206}
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.
#define NULL
Definition: resample.c:96

References NULL, and pbx_builtin_setvar_helper().

◆ global_exists_read()

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

Definition at line 213 of file func_global.c.

215{
216 const char *var = pbx_builtin_getvar_helper(NULL, data);
217
218 strcpy(buf, var ? "1" : "0");
219
220 return 0;
221}
#define var
Definition: ast_expr2f.c:605
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.

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

◆ global_read()

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

Definition at line 176 of file func_global.c.

177{
178 const char *var = pbx_builtin_getvar_helper(NULL, data);
179
180 *buf = '\0';
181
182 if (var)
184
185 return 0;
186}
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:425

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

◆ global_write()

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

Definition at line 188 of file func_global.c.

189{
191
192 return 0;
193}
int value
Definition: syslog.c:37

References NULL, pbx_builtin_setvar_helper(), and value.

◆ load_module()

static int load_module ( void  )
static

Definition at line 403 of file func_global.c.

404{
405 int res = 0;
406
411
412 return res;
413}
static struct ast_custom_function shared_function
Definition: func_global.c:385
static struct ast_custom_function global_exists_function
Definition: func_global.c:223
static struct ast_custom_function global_function
Definition: func_global.c:195
static struct ast_custom_function global_delete_function
Definition: func_global.c:208
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559

References ast_custom_function_register, global_delete_function, global_exists_function, global_function, and shared_function.

◆ shared_read()

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

Definition at line 228 of file func_global.c.

229{
230 struct ast_datastore *varstore;
231 struct varshead *varshead;
232 struct ast_var_t *var;
235 AST_APP_ARG(chan);
236 );
237 struct ast_channel *c_ref = NULL;
238
239 if (ast_strlen_zero(data)) {
240 ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
241 return -1;
242 }
243
245
246 if (!ast_strlen_zero(args.chan)) {
247 char *prefix = ast_alloca(strlen(args.chan) + 2);
248 sprintf(prefix, "%s-", args.chan);
249 if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
250 ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
251 return -1;
252 }
253 chan = c_ref;
254 } else if (!chan) {
255 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
256 return -1;
257 }
258
259 ast_channel_lock(chan);
260
261 if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
262 ast_channel_unlock(chan);
263 if (c_ref) {
264 c_ref = ast_channel_unref(c_ref);
265 }
266 return -1;
267 }
268
269 varshead = varstore->data;
270 *buf = '\0';
271
272 /* Protected by the channel lock */
273 AST_LIST_TRAVERSE(varshead, var, entries) {
274 if (!strcmp(args.var, ast_var_name(var))) {
276 break;
277 }
278 }
279
280 ast_channel_unlock(chan);
281
282 if (c_ref) {
283 c_ref = ast_channel_unref(c_ref);
284 }
285
286 return 0;
287}
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_log
Definition: astobj2.c:42
#define ast_channel_lock(chan)
Definition: channel.h:2970
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:1461
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1481
#define ast_channel_unlock(chan)
Definition: channel.h:2971
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:2428
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:80
static const struct ast_datastore_info shared_variable_info
Definition: func_global.c:160
static char prefix[MAX_PREFIX]
Definition: http.c:144
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
#define LOG_WARNING
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
const char * data
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
const char * args

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(), buf, ast_datastore::data, ast_channel::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, shared_variable_info, and var.

◆ shared_variable_free()

static void shared_variable_free ( void *  data)
static

Definition at line 165 of file func_global.c.

166{
167 struct varshead *varshead = data;
168 struct ast_var_t *var;
169
172 }
174}
#define ast_free(a)
Definition: astmm.h:180
void ast_var_delete(struct ast_var_t *var)
Definition: extconf.c:2471
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
struct ast_var_t::@213 entries

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

◆ shared_write()

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

Definition at line 289 of file func_global.c.

290{
291 struct ast_datastore *varstore;
292 struct varshead *varshead;
293 struct ast_var_t *var;
296 AST_APP_ARG(chan);
297 );
298 struct ast_channel *c_ref = NULL;
299 int len;
300 RAII_VAR(char *, shared_buffer, NULL, ast_free);
301
302 if (ast_strlen_zero(data)) {
303 ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
304 return -1;
305 }
306
308
309 if (!ast_strlen_zero(args.chan)) {
310 char *prefix = ast_alloca(strlen(args.chan) + 2);
311 sprintf(prefix, "%s-", args.chan);
312 if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
313 ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
314 return -1;
315 }
316 chan = c_ref;
317 } else if (!chan) {
318 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
319 return -1;
320 }
321
322 len = 9 + strlen(args.var); /* SHARED() + var */
323 shared_buffer = ast_malloc(len);
324 if (!shared_buffer) {
325 if (c_ref) {
326 ast_channel_unref(c_ref);
327 }
328 return -1;
329 }
330
331 ast_channel_lock(chan);
332
333 if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
334 if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
335 ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
336 ast_channel_unlock(chan);
337 if (c_ref) {
338 c_ref = ast_channel_unref(c_ref);
339 }
340 return -1;
341 }
342
343 if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
344 ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
345 ast_datastore_free(varstore);
346 ast_channel_unlock(chan);
347 if (c_ref) {
348 c_ref = ast_channel_unref(c_ref);
349 }
350 return -1;
351 }
352
353 varstore->data = varshead;
354 ast_channel_datastore_add(chan, varstore);
355 }
356 varshead = varstore->data;
357
358 /* Protected by the channel lock */
360 /* If there's a previous value, remove it */
361 if (!strcmp(args.var, ast_var_name(var))) {
364 break;
365 }
366 }
368
369 if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
371
372 sprintf(shared_buffer, "SHARED(%s)", args.var);
373 ast_channel_publish_varset(chan, shared_buffer, value);
374 }
375
376 ast_channel_unlock(chan);
377
378 if (c_ref) {
379 c_ref = ast_channel_unref(c_ref);
380 }
381
382 return 0;
383}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
#define ast_var_assign(name, value)
Definition: chanvars.h:40
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
void ast_channel_publish_varset(struct ast_channel *chan, const char *variable, const char *value)
Publish a ast_channel_publish_varset for a channel.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
struct varshead varshead
#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:941

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, ast_channel::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, RAII_VAR, S_OR, shared_variable_info, value, var, and ast_channel::varshead.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 391 of file func_global.c.

392{
393 int res = 0;
394
399
400 return res;
401}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), global_delete_function, global_exists_function, global_function, and shared_function.

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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 415 of file func_global.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 415 of file func_global.c.

◆ global_delete_function

struct ast_custom_function global_delete_function
static
Initial value:
= {
.name = "GLOBAL_DELETE",
}
static int global_delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:201

Definition at line 208 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ global_exists_function

struct ast_custom_function global_exists_function
static
Initial value:
= {
.name = "GLOBAL_EXISTS",
}
static int global_exists_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:213

Definition at line 223 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ global_function

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

Definition at line 195 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ shared_function

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

Definition at line 385 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ 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:165

Definition at line 160 of file func_global.c.

Referenced by shared_read(), and shared_write().