Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
config_options.c File Reference

Configuration Option-handling. More...

#include "asterisk.h"
#include <regex.h>
#include "asterisk/_private.h"
#include "asterisk/config.h"
#include "asterisk/config_options.h"
#include "asterisk/stringfields.h"
#include "asterisk/acl.h"
#include "asterisk/app.h"
#include "asterisk/frame.h"
#include "asterisk/xmldoc.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
#include "asterisk/format_cap.h"

Go to the source code of this file.

Data Structures

struct  aco_info_internal
 Bits of aco_info that shouldn't be assigned outside this file. More...
 
struct  aco_option
 
struct  aco_type_internal
 

Macros

#define CONFIG_OPT_BUCKETS   53
 
#define XMLDOC_STRICT   1
 

Functions

int __aco_option_register (struct aco_info *info, const char *name, enum aco_matchtype matchtype, struct aco_type **types, const char *default_val, enum aco_option_type kind, aco_option_handler handler, unsigned int flags, unsigned int no_doc, size_t argc,...)
 register a config option More...
 
static int acl_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default handler for ACLs. More...
 
static void aco_deinit (void)
 
void aco_info_destroy (struct aco_info *info)
 Destroy an initialized aco_info struct. More...
 
int aco_info_init (struct aco_info *info)
 Initialize an aco_info structure. More...
 
int aco_init (void)
 
struct ao2_containeraco_option_container_alloc (void)
 Allocate a container to hold config options.
 
static struct aco_optionaco_option_find (struct aco_type *type, const char *name)
 
intptr_t aco_option_get_argument (const struct aco_option *option, unsigned int position)
 Get the offset position for an argument within a config option. More...
 
unsigned int aco_option_get_flags (const struct aco_option *option)
 Read the flags of a config option - useful when using a custom callback for a config option. More...
 
int aco_option_register_deprecated (struct aco_info *info, const char *name, struct aco_type **types, const char *aliased_to)
 Register a deprecated (and aliased) config option. More...
 
void * aco_pending_config (struct aco_info *info)
 Get pending config changes. More...
 
enum aco_process_status aco_process_ast_config (struct aco_info *info, struct aco_file *file, struct ast_config *cfg)
 Process config info from an ast_config via options registered with an aco_info. More...
 
int aco_process_category_options (struct aco_type *type, struct ast_config *cfg, const char *cat, void *obj)
 Parse each option defined in a config category. More...
 
enum aco_process_status aco_process_config (struct aco_info *info, int reload)
 Process a config info via the options registered with an aco_info. More...
 
int aco_process_var (struct aco_type *type, const char *cat, struct ast_variable *var, void *obj)
 Parse a single ast_variable and apply it to an object. More...
 
int aco_set_defaults (struct aco_type *type, const char *category, void *obj)
 Set all default options of obj. More...
 
static int apply_config (struct aco_info *info)
 
static aco_option_handler ast_config_option_default_handler (enum aco_option_type type)
 
static int bool_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for bools (ast_true/ast_false) More...
 
static int boolflag_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for bools (ast_true/ast_false) that are stored as flags. More...
 
static regex_t * build_regex (const char *text)
 
static int chararray_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default handler for character arrays. More...
 
static char * cli_show_help (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static void cli_show_module_options (struct ast_cli_args *a)
 
static void cli_show_module_type (struct ast_cli_args *a)
 
static void cli_show_module_types (struct ast_cli_args *a)
 
static void cli_show_modules (struct ast_cli_args *a)
 
static int codec_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for codec preferences/capabilities. More...
 
static char * complete_config_module (const char *word)
 
static char * complete_config_option (const char *module, const char *option, const char *word)
 
static char * complete_config_type (const char *module, const char *word)
 
static int config_opt_cmp (void *obj, void *arg, int flags)
 
static int config_opt_hash (const void *obj, const int flags)
 
static void config_option_destroy (void *obj)
 
static struct ast_strderive_category_text (enum aco_category_op category_match, const char *category)
 
static int double_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for doubles. More...
 
static int find_option_cb (void *obj, void *arg, int flags)
 
static struct ast_xml_doc_itemfind_xmldoc_option (struct ast_xml_doc_item *config_info, struct aco_type **types, const char *name)
 
static struct ast_xml_doc_itemfind_xmldoc_type (struct ast_xml_doc_item *config_info, const char *name)
 
static int int_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for signed integers. More...
 
static int internal_aco_type_category_check (struct aco_type *match, const char *category)
 
static struct aco_typeinternal_aco_type_find (struct aco_file *file, struct ast_config *cfg, const char *category)
 
static void internal_file_types_destroy (struct aco_file *file)
 
static enum aco_process_status internal_process_ast_config (struct aco_info *info, struct aco_file *file, struct ast_config *cfg)
 
static void internal_type_destroy (struct aco_type *type)
 
static int internal_type_init (struct aco_type *type)
 
static int is_preload (struct aco_file *file, const char *cat)
 
static int link_option_to_types (struct aco_info *info, struct aco_type **types, struct aco_option *opt)
 
static int noop_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default handler for doing nothing.
 
static int process_category (struct ast_config *cfg, struct aco_info *info, struct aco_file *file, const char *cat, int preload)
 
static int sockaddr_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default handler for ast_sockaddrs. More...
 
static int stringfield_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for stringfields. More...
 
static int timelen_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for timelen signed integers. More...
 
static int uint_handler_fn (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Default option handler for unsigned integers. More...
 
static int xmldoc_update_config_option (struct aco_type **types, const char *module, const char *name, const char *object_name, const char *default_value, unsigned int regex, enum aco_option_type type)
 
static int xmldoc_update_config_type (const char *module, const char *name, const char *category, const char *matchfield, const char *matchvalue, enum aco_category_op category_match)
 

Variables

static char * aco_option_type_string []
 Value of the aco_option_type enum as strings.
 
static struct ast_cli_entry cli_aco []
 
static struct ao2_containerxmldocs
 

Detailed Description

Configuration Option-handling.

Author
Terry Wilson twils.nosp@m.on@d.nosp@m.igium.nosp@m..com

Definition in file config_options.c.

Function Documentation

int __aco_option_register ( struct aco_info info,
const char *  name,
enum aco_matchtype  match_type,
struct aco_type **  types,
const char *  default_val,
enum aco_option_type  type,
aco_option_handler  handler,
unsigned int  flags,
unsigned int  no_doc,
size_t  argc,
  ... 
)

register a config option

Note
this should probably only be called by one of the aco_option_register* macros
Parameters
infoThe aco_info holding this module's config information
nameThe name of the option
match_type
typesAn array of valid option types for matching categories to the correct struct type
default_valThe default value of the option in the same format as defined in a config file
typeThe option type (only for default handlers)
handlerThe handler function for the option (only for non-default types)
flagsa type specific flags, stored in the option and available to the handler
no_docif non-zero, this option should not have documentation
argcThe number for variadic arguments
...field offsets to store for default handlers
Return values
0success
-1failure

Definition at line 300 of file config_options.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_ref, and OPT_CUSTOM_T.

Referenced by __ast_sorcery_object_field_register(), and ast_sorcery_object_fields_register().

303 {
304  struct aco_option *opt;
305  va_list ap;
306  int tmp;
307 
308  /* Custom option types require a handler */
309  if (!handler && kind == OPT_CUSTOM_T) {
310  return -1;
311  }
312 
313  if (!(types && types[0])) {
314  return -1;
315  }
316 
317  opt = ao2_alloc_options(sizeof(*opt) + argc * sizeof(opt->args[0]),
318  config_option_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
319  if (!opt) {
320  return -1;
321  }
322 
323  if (matchtype == ACO_REGEX && !(opt->name_regex = build_regex(name))) {
324  ao2_ref(opt, -1);
325  return -1;
326  }
327 
328  va_start(ap, argc);
329  for (tmp = 0; tmp < argc; tmp++) {
330  opt->args[tmp] = va_arg(ap, size_t);
331  }
332  va_end(ap);
333 
334  opt->name = name;
335  opt->match_type = matchtype;
336  opt->default_val = default_val;
337  opt->type = kind;
338  opt->handler = handler;
339  opt->flags = flags;
340  opt->argc = argc;
341  opt->no_doc = no_doc;
342 
343  if (!opt->handler && !(opt->handler = ast_config_option_default_handler(opt->type))) {
344  /* This should never happen */
345  ast_log(LOG_ERROR, "No handler provided, and no default handler exists for type %u\n", opt->type);
346  ao2_ref(opt, -1);
347  return -1;
348  };
349 
350  if (link_option_to_types(info, types, opt)) {
351  ao2_ref(opt, -1);
352  return -1;
353  }
354 
355  return 0;
356 }
Type for a custom (user-defined) option handler.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static int acl_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default handler for ACLs.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1549 of file config_options.c.

References ast_append_ha(), and ast_variable::value.

1549  {
1550  struct ast_ha **ha = (struct ast_ha **)(obj + opt->args[0]);
1551  int error = 0;
1552  *ha = ast_append_ha(opt->flags ? "permit" : "deny", var->value, *ha, &error);
1553  return error;
1554 }
internal representation of ACL entries In principle user applications would have no need for this...
Definition: acl.h:51
struct ast_ha * ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
Add a new rule to a list of HAs.
Definition: acl.c:712
void aco_info_destroy ( struct aco_info info)

Destroy an initialized aco_info struct.

Parameters
infoThe address of the aco_info struct to destroy

Definition at line 910 of file config_options.c.

References aco_info::files.

Referenced by aco_info_init(), ast_ari_config_destroy(), ast_ari_config_init(), conf_destroy_config(), load_module(), sorcery_object_type_destructor(), stasis_cleanup(), and unload_module().

911 {
912  int x;
913  /* It shouldn't be possible for internal->pending to be in use when this is called because
914  * of the locks in loader.c around reloads and unloads and the fact that internal->pending
915  * only exists while those locks are held */
916  ast_free(info->internal);
917  info->internal = NULL;
918 
919  for (x = 0; info->files[x]; x++) {
920  internal_file_types_destroy(info->files[x]);
921  }
922 }
struct aco_file * files[]
int aco_info_init ( struct aco_info info)

Initialize an aco_info structure.

Note
aco_info_destroy must be called if this succeeds
Parameters
infoThe address of an aco_info struct to initialize
Return values
0Success
non-zeroFailure

Definition at line 877 of file config_options.c.

References aco_info_destroy(), ast_calloc, aco_type::category, aco_type::category_match, aco_info::files, aco_type::hidden, aco_info::hidden, aco_type::matchfield, aco_type::matchvalue, aco_info::module, aco_type::name, aco_type::type, and aco_file::types.

Referenced by __ast_sorcery_object_register(), ast_ari_config_init(), conf_load_config(), load_module(), and stasis_init().

878 {
879  size_t x = 0, y = 0;
880  struct aco_file *file;
881  struct aco_type *type;
882 
883  if (!(info->internal = ast_calloc(1, sizeof(*info->internal)))) {
884  return -1;
885  }
886 
887  while ((file = info->files[x++])) {
888  while ((type = file->types[y++])) {
889  if (internal_type_init(type)) {
890  goto error;
891  }
892 #ifdef AST_XML_DOCS
893  if (!info->hidden &&
894  !type->hidden &&
895  type->type != ACO_IGNORE &&
896  xmldoc_update_config_type(info->module, type->name, type->category, type->matchfield, type->matchvalue, type->category_match)) {
897  goto error;
898  }
899 #endif /* AST_XML_DOCS */
900  }
901  y = 0;
902  }
903 
904  return 0;
905 error:
906  aco_info_destroy(info);
907  return -1;
908 }
const char * matchfield
The representation of a single configuration file to be processed.
enum aco_type_t type
const char * matchvalue
unsigned int hidden
const char * category
enum aco_category_op category_match
struct aco_file * files[]
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
const char * name
Type information about a category-level configurable object.
const char * module
struct aco_type * types[]
unsigned int hidden
int aco_init ( void  )

Provided by config_options.c

Definition at line 1428 of file config_options.c.

References ast_cli_register_multiple, ast_register_cleanup(), and ast_xmldoc_build_documentation().

1429 {
1430 #ifdef AST_XML_DOCS
1431  ast_register_cleanup(aco_deinit);
1432  if (!(xmldocs = ast_xmldoc_build_documentation("configInfo"))) {
1433  ast_log(LOG_ERROR, "Couldn't build config documentation\n");
1434  return -1;
1435  }
1436  ast_cli_register_multiple(cli_aco, ARRAY_LEN(cli_aco));
1437 #endif /* AST_XML_DOCS */
1438  return 0;
1439 }
struct ao2_container * ast_xmldoc_build_documentation(const char *type)
Build the documentation for a particular source type.
Definition: xmldoc.c:2684
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
intptr_t aco_option_get_argument ( const struct aco_option option,
unsigned int  position 
)

Get the offset position for an argument within a config option.

Parameters
optionPointer to the aco_option struct
positionArgument number
Return values
positionof the argument

Definition at line 248 of file config_options.c.

Referenced by timeval_str2struct().

249 {
250  return option->args[position];
251 }
unsigned int aco_option_get_flags ( const struct aco_option option)

Read the flags of a config option - useful when using a custom callback for a config option.

Since
12
Parameters
optionPointer to the aco_option struct
Return values
valueof the flags on the config option

Definition at line 243 of file config_options.c.

Referenced by option_handler_parkedfeature().

244 {
245  return option->flags;
246 }
int aco_option_register_deprecated ( struct aco_info info,
const char *  name,
struct aco_type **  types,
const char *  aliased_to 
)

Register a deprecated (and aliased) config option.

Parameters
infoA pointer to the aco_info struct
nameThe name of the deprecated option
typesAn array of valid option types for matching categories to the correct struct type
aliased_toThe name of the option that this deprecated option matches to
Return values
0Success
-1Failure

Definition at line 217 of file config_options.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, and ao2_ref.

218 {
219  struct aco_option *opt;
220 
221  if (!info || ast_strlen_zero(name) || ast_strlen_zero(aliased_to)) {
222  return -1;
223  }
224 
225  opt = ao2_alloc_options(sizeof(*opt), config_option_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
226  if (!opt) {
227  return -1;
228  }
229 
230  opt->name = name;
231  opt->aliased_to = aliased_to;
232  opt->deprecated = 1;
233  opt->match_type = ACO_EXACT;
234 
235  if (link_option_to_types(info, types, opt)) {
236  ao2_ref(opt, -1);
237  return -1;
238  }
239 
240  return 0;
241 }
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void* aco_pending_config ( struct aco_info info)

Get pending config changes.

Note
This will most likely be called from the pre_apply_config callback function
Parameters
infoAn initialized aco_info
Return values
NULLerror
non-NULLA pointer to the user-defined config object with un-applied changes

Definition at line 103 of file config_options.c.

References aco_info_internal::pending.

Referenced by hepv3_config_pre_apply(), http_media_cache_config_pre_apply(), and prometheus_config_pre_apply().

104 {
105  if (!(info && info->internal)) {
106  ast_log(LOG_ERROR, "This may not be called without an initialized aco_info!\n");
107  return NULL;
108  }
109  return info->internal->pending;
110 }
enum aco_process_status aco_process_ast_config ( struct aco_info info,
struct aco_file file,
struct ast_config cfg 
)

Process config info from an ast_config via options registered with an aco_info.

Parameters
infoThe aco_info to be used for handling the config
fileThe file attached to aco_info that the config represents
cfgA pointer to a loaded ast_config to parse
Return values
ACO_PROCESS_OKSuccess
ACO_PROCESS_ERRORFailure

Definition at line 617 of file config_options.c.

References ACO_PROCESS_ERROR, ACO_PROCESS_OK, aco_file::filename, aco_info_internal::pending, aco_info::pre_apply_config, and aco_info::snapshot_alloc.

618 {
619  if (!info->internal) {
620  ast_log(LOG_ERROR, "Attempt to process %s with uninitialized aco_info\n", file->filename);
621  return ACO_PROCESS_ERROR;
622  }
623 
624  if (!(info->internal->pending = info->snapshot_alloc())) {
625  ast_log(LOG_ERROR, "In %s: Could not allocate temporary objects\n", file->filename);
626  goto error;
627  }
628 
629  if (internal_process_ast_config(info, file, cfg)) {
630  goto error;
631  }
632 
633  if (info->pre_apply_config && info->pre_apply_config()) {
634  goto error;
635  }
636 
637  if (apply_config(info)) {
638  goto error;
639  };
640 
641  ao2_cleanup(info->internal->pending);
642  info->internal->pending = NULL;
643  return ACO_PROCESS_OK;
644 
645 error:
646  ao2_cleanup(info->internal->pending);
647  info->internal->pending = NULL;
648 
649  return ACO_PROCESS_ERROR;
650 }
aco_pre_apply_config pre_apply_config
Their was an error and no changes were applied.
The config was processed and applied.
aco_snapshot_alloc snapshot_alloc
const char * filename
int aco_process_category_options ( struct aco_type type,
struct ast_config cfg,
const char *  cat,
void *  obj 
)

Parse each option defined in a config category.

Parameters
typeThe aco_type with the options for parsing
cfgThe ast_config being parsed
catThe config category being parsed
objThe user-defined config object that will store the parsed config items
Return values
0Success
-1Failure

Definition at line 805 of file config_options.c.

References aco_process_var(), and ast_variable::next.

806 {
807  struct ast_variable *var;
808 
809  for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
810  if (aco_process_var(type, cat, var, obj)) {
811  return -1;
812  }
813  }
814 
815  return 0;
816 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
int aco_process_var(struct aco_type *type, const char *cat, struct ast_variable *var, void *obj)
Parse a single ast_variable and apply it to an object.
enum aco_process_status aco_process_config ( struct aco_info info,
int  reload 
)

Process a config info via the options registered with an aco_info.

Parameters
infoThe config_options_info to be used for handling the config
reloadNon-zero if this is for a reload.
Return values
ACO_PROCESS_OKSuccess
ACO_PROCESS_ERRORFailure
ACO_PROCESS_UNCHANGEDNo change due to unedited config file

Definition at line 652 of file config_options.c.

References ACO_PROCESS_ERROR, ACO_PROCESS_OK, ACO_PROCESS_UNCHANGED, aco_set_defaults(), aco_file::alias, ast_config_destroy(), ast_config_load, ast_debug, aco_type::category, CONFIG_FLAG_FILEUNCHANGED, aco_file::filename, aco_info::files, aco_type::item_offset, aco_info::module, aco_info_internal::pending, aco_info::post_apply_config, aco_info::pre_apply_config, aco_info::snapshot_alloc, aco_type::type, and aco_file::types.

Referenced by conf_load_config(), conf_reload_config(), load_module(), process_config(), reload(), reload_module(), and stasis_init().

653 {
654  struct ast_config *cfg;
655  struct ast_flags cfg_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0, };
656  int res = ACO_PROCESS_OK;
657  int file_count = 0;
658  struct aco_file *file;
659 
660  if (!info->internal) {
661  ast_log(LOG_ERROR, "Attempting to process uninitialized aco_info\n");
662  return ACO_PROCESS_ERROR;
663  }
664 
665  if (!(info->files[0])) {
666  ast_log(LOG_ERROR, "No filename given, cannot proceed!\n");
667  return ACO_PROCESS_ERROR;
668  }
669 
670  if (!(info->internal->pending = info->snapshot_alloc())) {
671  ast_log(LOG_ERROR, "In %s: Could not allocate temporary objects\n", info->module);
672  return ACO_PROCESS_ERROR;
673  }
674 
675  while (res != ACO_PROCESS_ERROR && (file = info->files[file_count++])) {
676  const char *filename = file->filename;
677  struct aco_type *match;
678  int i;
679 
680  /* set defaults for global objects */
681  for (i = 0, match = file->types[i]; match; match = file->types[++i]) {
682  void **field = info->internal->pending + match->item_offset;
683 
684  if (match->type == ACO_IGNORE) {
685  continue;
686  }
687 
688  if (match->type != ACO_GLOBAL || !*field) {
689  continue;
690  }
691 
692  if (aco_set_defaults(match, match->category, *field)) {
693  ast_log(LOG_ERROR, "In %s: Setting defaults for %s failed\n", file->filename, match->category);
694  res = ACO_PROCESS_ERROR;
695  break;
696  }
697  }
698 
699  if (res == ACO_PROCESS_ERROR) {
700  break;
701  }
702 
703 try_alias:
704  cfg = ast_config_load(filename, cfg_flags);
705  if (!cfg || cfg == CONFIG_STATUS_FILEMISSING) {
706  if (file->alias && strcmp(file->alias, filename)) {
707  filename = file->alias;
708  goto try_alias;
709  }
710  ast_log(LOG_ERROR, "Unable to load config file '%s'\n", file->filename);
711  res = ACO_PROCESS_ERROR;
712  break;
713  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
714  ast_debug(1, "%s was unchanged\n", file->filename);
715  res = ACO_PROCESS_UNCHANGED;
716  continue;
717  } else if (cfg == CONFIG_STATUS_FILEINVALID) {
718  ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n",
719  file->filename);
720  res = ACO_PROCESS_ERROR;
721  break;
722  }
723 
724  /* A file got loaded. */
725  if (reload) {
726  /* Must do any subsequent file loads unconditionally. */
727  reload = 0;
728  ast_clear_flag(&cfg_flags, CONFIG_FLAG_FILEUNCHANGED);
729 
730  if (file_count != 1) {
731  /*
732  * Must restart loading to load all config files since a file
733  * after the first one changed.
734  */
735  file_count = 0;
736  } else {
737  res = internal_process_ast_config(info, file, cfg);
738  }
739  } else {
740  res = internal_process_ast_config(info, file, cfg);
741  }
742  ast_config_destroy(cfg);
743  }
744 
745  if (res != ACO_PROCESS_OK) {
746  goto end;
747  }
748 
749  if (info->pre_apply_config && info->pre_apply_config()) {
750  res = ACO_PROCESS_ERROR;
751  goto end;
752  }
753 
754  if (apply_config(info)) {
755  res = ACO_PROCESS_ERROR;
756  goto end;
757  }
758 
759  if (info->post_apply_config) {
760  info->post_apply_config();
761  }
762 
763 end:
764  ao2_cleanup(info->internal->pending);
765  info->internal->pending = NULL;
766 
767  return res;
768 }
aco_pre_apply_config pre_apply_config
The representation of a single configuration file to be processed.
enum aco_type_t type
const char * alias
#define ast_config_load(filename, flags)
Load a config file.
const char * category
#define ast_debug(level,...)
Log a DEBUG message.
The config had not been edited and no changes applied.
Their was an error and no changes were applied.
struct aco_file * files[]
aco_post_apply_config post_apply_config
The config was processed and applied.
Structure used to handle boolean flags.
Definition: utils.h:199
Type information about a category-level configurable object.
aco_snapshot_alloc snapshot_alloc
size_t item_offset
const char * filename
int aco_set_defaults(struct aco_type *type, const char *category, void *obj)
Set all default options of obj.
const char * module
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct aco_type * types[]
int aco_process_var ( struct aco_type type,
const char *  cat,
struct ast_variable var,
void *  obj 
)

Parse a single ast_variable and apply it to an object.

Note
This function can be used to build up an object by repeatedly passing in the config variable name and values that would be found in a config file. This can be useful if the object is to be populated by a dialplan function, for example.
Parameters
typeThe aco_type associated with the object
catThe category to use
varA variable to apply to the object
objA pointer to the object to be configured
Return values
0Success
-1Failure

Definition at line 769 of file config_options.c.

References ao2_ref, ast_strdupa, ast_variable::file, ast_variable::name, aco_type::name, RAII_VAR, and ast_variable::value.

Referenced by aco_process_category_options(), and ast_sorcery_objectset_apply().

770 {
771  RAII_VAR(struct aco_option *, opt, aco_option_find(type, var->name), ao2_cleanup);
772  if (opt && opt->deprecated && !ast_strlen_zero(opt->aliased_to)) {
773  const char *alias = ast_strdupa(opt->aliased_to);
774  ast_log(LOG_WARNING, "At line %d of %s option '%s' is deprecated. Use '%s' instead\n", var->lineno, var->file, var->name, alias);
775  ao2_ref(opt, -1);
776  opt = aco_option_find(type, alias);
777  }
778 
779  if (!opt) {
780  ast_log(LOG_ERROR, "Could not find option suitable for category '%s' named '%s' at line %d of %s\n", cat, var->name, var->lineno, var->file);
781  return -1;
782  }
783 
784  if (!opt->handler) {
785  /* It should be impossible for an option to not have a handler */
786  ast_log(LOG_ERROR, "BUG! Somehow a config option for %s/%s was created with no handler!\n", cat, var->name);
787  return -1;
788  }
789 
790 #ifdef AST_DEVMODE
791  if (opt->doc_unavailable) {
792  ast_log(LOG_ERROR, "Config option '%s' of type '%s' is not completely documented and can not be set\n", var->name, type->name);
793  return -1;
794  }
795 #endif
796 
797  if (opt->handler(opt, var, obj)) {
798  ast_log(LOG_ERROR, "Error parsing %s=%s at line %d of %s\n", var->name, var->value, var->lineno, var->file);
799  return -1;
800  }
801 
802  return 0;
803 }
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const char * name
#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
int aco_set_defaults ( struct aco_type type,
const char *  category,
void *  obj 
)

Set all default options of obj.

Parameters
typeThe aco_type with the options
categoryThe configuration category from which obj is being configured
objThe object being configured
Return values
0Success
-1Failure

Definition at line 924 of file config_options.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_ref, ast_variables_destroy(), aco_type_internal::opts, and RAII_VAR.

Referenced by aco_process_config(), ast_sorcery_alloc(), conf_alloc(), and stasis_init().

925 {
926  struct aco_option *opt;
927  struct ao2_iterator iter;
928 
929  if (!type->internal) {
930  return -1;
931  }
932 
933  iter = ao2_iterator_init(type->internal->opts, 0);
934 
935  while ((opt = ao2_iterator_next(&iter))) {
936  RAII_VAR(struct ast_variable *, var, NULL, ast_variables_destroy);
937 
938  if (ast_strlen_zero(opt->default_val)) {
939  ao2_ref(opt, -1);
940  continue;
941  }
942  if (!(var = ast_variable_new(opt->name, opt->default_val, ""))) {
943  ao2_ref(opt, -1);
944  ao2_iterator_destroy(&iter);
945  return -1;
946  }
947  if (opt->handler(opt, var, obj)) {
948  ast_log(LOG_ERROR, "Unable to set default for %s, %s=%s\n", category, var->name, var->value);
949  ao2_ref(opt, -1);
950  ao2_iterator_destroy(&iter);
951  return -1;
952  }
953  ao2_ref(opt, -1);
954  }
955  ao2_iterator_destroy(&iter);
956 
957  return 0;
958 }
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
Structure for variables, used for configurations and for channel variables.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
struct ao2_container * opts
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
#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
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
static int bool_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for bools (ast_true/ast_false)

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1586 of file config_options.c.

References ast_false(), ast_true(), and ast_variable::value.

1587 {
1588  unsigned int *field = (unsigned int *)(obj + opt->args[0]);
1589  *field = opt->flags ? ast_true(var->value) : ast_false(var->value);
1590  return 0;
1591 }
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"...
Definition: utils.c:2216
static int boolflag_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for bools (ast_true/ast_false) that are stored as flags.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1597 of file config_options.c.

References ast_false(), ast_true(), and ast_variable::value.

1598 {
1599  unsigned int *flags_field = (unsigned int *)(obj + opt->args[0]);
1600  unsigned int val = opt->flags ? ast_true(var->value) : ast_false(var->value);
1601  unsigned int flag = opt->args[1];
1602  if (val) {
1603  *flags_field |= flag;
1604  } else {
1605  *flags_field &= ~flag;
1606  }
1607  return 0;
1608 }
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"...
Definition: utils.c:2216
static int chararray_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default handler for character arrays.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1631 of file config_options.c.

References ast_copy_string(), and ast_variable::value.

1632 {
1633  char *field = (char *)(obj + opt->args[0]);
1634  size_t len = opt->args[1];
1635 
1636  if (opt->flags && ast_strlen_zero(var->value)) {
1637  return -1;
1638  }
1639  ast_copy_string(field, var->value, len);
1640  return 0;
1641 }
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
static int codec_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for codec preferences/capabilities.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1560 of file config_options.c.

References ast_format_cap_update_by_allow_disallow(), and ast_variable::value.

1560  {
1561  struct ast_format_cap **cap = (struct ast_format_cap **)(obj + opt->args[0]);
1562  return ast_format_cap_update_by_allow_disallow(*cap, var->value, opt->flags);
1563 }
int ast_format_cap_update_by_allow_disallow(struct ast_format_cap *cap, const char *list, int allowing)
Parse an "allow" or "deny" list and modify a format capabilities structure accordingly.
Definition: format_cap.c:320
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
static int double_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for doubles.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1540 of file config_options.c.

References ast_parse_arg(), and ast_variable::value.

1540  {
1541  double *field = (double *)(obj + opt->args[0]);
1542  return ast_parse_arg(var->value, PARSE_DOUBLE | opt->flags, field);
1543 }
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
static int int_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for signed integers.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1447 of file config_options.c.

References ast_parse_arg(), ast_variable::name, and ast_variable::value.

1447  {
1448  int *field = (int *)(obj + opt->args[0]);
1449  unsigned int flags = PARSE_INT32 | opt->flags;
1450  int res = 0;
1451  if (opt->flags & PARSE_IN_RANGE) {
1452  res = opt->flags & PARSE_DEFAULT ?
1453  ast_parse_arg(var->value, flags, field, (int) opt->args[1], (int) opt->args[2], opt->args[3]) :
1454  ast_parse_arg(var->value, flags, field, (int) opt->args[1], (int) opt->args[2]);
1455  if (res) {
1456  if (opt->flags & PARSE_RANGE_DEFAULTS) {
1457  ast_log(LOG_WARNING, "Failed to set %s=%s. Set to %d instead due to range limit (%d, %d)\n", var->name, var->value, *field, (int) opt->args[1], (int) opt->args[2]);
1458  res = 0;
1459  } else if (opt->flags & PARSE_DEFAULT) {
1460  ast_log(LOG_WARNING, "Failed to set %s=%s, Set to default value %d instead.\n", var->name, var->value, *field);
1461  res = 0;
1462  }
1463  }
1464  } else if ((opt->flags & PARSE_DEFAULT) && ast_parse_arg(var->value, flags, field, (int) opt->args[1])) {
1465  ast_log(LOG_WARNING, "Attempted to set %s=%s, but set it to %d instead due to default)\n", var->name, var->value, *field);
1466  } else {
1467  res = ast_parse_arg(var->value, flags, field);
1468  }
1469 
1470  return res;
1471 }
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
static int sockaddr_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default handler for ast_sockaddrs.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1614 of file config_options.c.

References ast_parse_arg(), and ast_variable::value.

1615 {
1616  struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + opt->args[0]);
1617  return ast_parse_arg(var->value, PARSE_ADDR | opt->flags, field);
1618 }
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
Socket address structure.
Definition: netsock2.h:97
static int stringfield_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for stringfields.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1569 of file config_options.c.

References pool, and ast_variable::value.

1570 {
1571  ast_string_field *field = (const char **)(obj + opt->args[0]);
1572  struct ast_string_field_pool **pool = (struct ast_string_field_pool **)(obj + opt->args[1]);
1573  struct ast_string_field_mgr *mgr = (struct ast_string_field_mgr *)(obj + opt->args[2]);
1574 
1575  if (opt->flags && ast_strlen_zero(var->value)) {
1576  return -1;
1577  }
1578  ast_string_field_ptr_set_by_fields(*pool, *mgr, field, var->value);
1579  return 0;
1580 }
static pj_pool_t * pool
Global memory pool for configuration and timers.
static int timelen_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for timelen signed integers.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1507 of file config_options.c.

References ast_parse_arg(), ast_variable::name, and ast_variable::value.

1508 {
1509  int *field = (int *)(obj + opt->args[0]);
1510  unsigned int flags = PARSE_TIMELEN | opt->flags;
1511  int res = 0;
1512  if (opt->flags & PARSE_IN_RANGE) {
1513  if (opt->flags & PARSE_DEFAULT) {
1514  res = ast_parse_arg(var->value, flags, field, (enum ast_timelen) opt->args[1], (int) opt->args[2], (int) opt->args[3], opt->args[4]);
1515  } else {
1516  res = ast_parse_arg(var->value, flags, field, (enum ast_timelen) opt->args[1], (int) opt->args[2], (int) opt->args[3]);
1517  }
1518  if (res) {
1519  if (opt->flags & PARSE_RANGE_DEFAULTS) {
1520  ast_log(LOG_WARNING, "Failed to set %s=%s. Set to %d instead due to range limit (%d, %d)\n", var->name, var->value, *field, (int) opt->args[2], (int) opt->args[3]);
1521  res = 0;
1522  } else if (opt->flags & PARSE_DEFAULT) {
1523  ast_log(LOG_WARNING, "Failed to set %s=%s, Set to default value %d instead.\n", var->name, var->value, *field);
1524  res = 0;
1525  }
1526  }
1527  } else if ((opt->flags & PARSE_DEFAULT) && ast_parse_arg(var->value, flags, field, (enum ast_timelen) opt->args[1], (int) opt->args[2])) {
1528  ast_log(LOG_WARNING, "Attempted to set %s=%s, but set it to %d instead due to default)\n", var->name, var->value, *field);
1529  } else {
1530  res = ast_parse_arg(var->value, flags, field, (enum ast_timelen) opt->args[1]);
1531  }
1532 
1533  return res;
1534 }
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
static int uint_handler_fn ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Default option handler for unsigned integers.

Note
For a description of the opt->flags and opt->args values, see the documentation for enum aco_option_type in config_options.h

Definition at line 1477 of file config_options.c.

References ast_parse_arg(), ast_variable::name, and ast_variable::value.

1477  {
1478  unsigned int *field = (unsigned int *)(obj + opt->args[0]);
1479  unsigned int flags = PARSE_UINT32 | opt->flags;
1480  int res = 0;
1481  if (opt->flags & PARSE_IN_RANGE) {
1482  res = opt->flags & PARSE_DEFAULT ?
1483  ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1], (unsigned int) opt->args[2], opt->args[3]) :
1484  ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1], (unsigned int) opt->args[2]);
1485  if (res) {
1486  if (opt->flags & PARSE_RANGE_DEFAULTS) {
1487  ast_log(LOG_WARNING, "Failed to set %s=%s. Set to %u instead due to range limit (%d, %d)\n", var->name, var->value, *field, (int) opt->args[1], (int) opt->args[2]);
1488  res = 0;
1489  } else if (opt->flags & PARSE_DEFAULT) {
1490  ast_log(LOG_WARNING, "Failed to set %s=%s, Set to default value %u instead.\n", var->name, var->value, *field);
1491  res = 0;
1492  }
1493  }
1494  } else if ((opt->flags & PARSE_DEFAULT) && ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1])) {
1495  ast_log(LOG_WARNING, "Attempted to set %s=%s, but set it to %u instead due to default)\n", var->name, var->value, *field);
1496  } else {
1497  res = ast_parse_arg(var->value, flags, field);
1498  }
1499 
1500  return res;
1501 }
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827

Variable Documentation

struct ast_cli_entry cli_aco[]
static
Initial value:
= {
{ .handler = cli_show_help , .summary = "Show configuration help for a module" ,},
}

Definition at line 1417 of file config_options.c.