2 * BIRD Internet Routing Daemon -- Configuration File Handling
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 #include "sysdep/config.h"
15 #include "lib/resource.h"
16 #include "lib/timer.h"
18 /* Configuration structure */
20 pool
*pool
; /* Pool the configuration is stored in */
21 linpool
*mem
; /* Linear pool containing configuration data */
22 list protos
; /* Configured protocol instances (struct proto_config) */
23 list tables
; /* Configured routing tables (struct rtable_config) */
24 list mpls_domains
; /* Configured MPLS domains (struct mpls_domain_config) */
25 list logfiles
; /* Configured log files (sysdep) */
26 list tests
; /* Configured unit tests (f_bt_test_suite) */
27 list symbols
; /* Configured symbols in config order */
29 int mrtdump_file
; /* Configured MRTDump file (sysdep, fd in unix) */
30 const char *syslog_name
; /* Name used for syslog (NULL -> no syslog) */
31 struct rtable_config
*def_tables
[NET_MAX
]; /* Default routing tables for each network */
32 struct iface_patt
*router_id_from
; /* Configured list of router ID iface patterns */
34 u32 router_id
; /* Our Router ID */
35 u32 proto_default_debug
; /* Default protocol debug mask */
36 u32 proto_default_mrtdump
; /* Default protocol mrtdump mask */
37 u32 channel_default_debug
; /* Default channel debug mask */
38 u32 table_default_debug
; /* Default table debug mask */
39 struct timeformat tf_route
; /* Time format for 'show route' */
40 struct timeformat tf_proto
; /* Time format for 'show protocol' */
41 struct timeformat tf_log
; /* Time format for the logfile */
42 struct timeformat tf_base
; /* Time format for other purposes */
43 u32 gr_wait
; /* Graceful restart wait timeout (sec) */
44 const char *hostname
; /* Hostname */
46 int cli_debug
; /* Tracing of CLI connections and commands */
47 int latency_debug
; /* I/O loop tracks duration of each event */
48 u32 latency_limit
; /* Events with longer duration are logged (us) */
49 u32 watchdog_warning
; /* I/O loop watchdog limit for warning (us) */
50 u32 watchdog_timeout
; /* Watchdog timeout (in seconds, 0 = disabled) */
51 char *err_msg
; /* Parser error message */
52 int err_lino
; /* Line containing error */
53 int err_chno
; /* Character where the parser stopped */
54 char *err_file_name
; /* File name containing error */
55 char *file_name
; /* Name of main configuration file */
56 int file_fd
; /* File descriptor of main configuration file */
58 struct sym_scope
*root_scope
; /* Scope for root symbols */
59 struct sym_scope
*current_scope
; /* Current scope where we are actually in while parsing */
60 int obstacle_count
; /* Number of items blocking freeing of this config */
61 int shutdown
; /* This is a pseudo-config for daemon shutdown */
62 int gr_down
; /* This is a pseudo-config for graceful restart */
63 btime load_time
; /* When we've got this configuration */
66 /* Please don't use these variables in protocols. Use proto_config->global instead. */
67 extern struct config
*config
; /* Currently active configuration */
68 extern struct config
*new_config
; /* Configuration being parsed */
70 struct config
*config_alloc(const char *name
);
71 int config_parse(struct config
*);
72 int cli_parse(struct config
*);
73 void config_free(struct config
*);
74 void config_free_old(void);
75 int config_commit(struct config
*, int type
, uint timeout
);
76 int config_confirm(void);
77 int config_undo(void);
78 int config_status(void);
79 btime
config_timer_status(void);
80 void config_init(void);
81 void cf_error(const char *msg
, ...) NORET
;
82 #define cf_warn(msg, args...) log(L_WARN "%s:%d:%d: " msg, ifs->file_name, ifs->lino, ifs->chno - ifs->toklen + 1, ##args)
83 void config_add_obstacle(struct config
*);
84 void config_del_obstacle(struct config
*);
85 void order_shutdown(int gr
);
87 #define RECONFIG_NONE 0
88 #define RECONFIG_HARD 1
89 #define RECONFIG_SOFT 2
90 #define RECONFIG_UNDO 3
93 #define CONF_PROGRESS 1
95 #define CONF_UNQUEUED 3
96 #define CONF_CONFIRM 4
97 #define CONF_SHUTDOWN -1
98 #define CONF_NOTHING -2
102 extern pool
*config_pool
;
103 extern linpool
*cfg_mem
;
105 #define cfg_alloc(size) lp_alloc(cfg_mem, size)
106 #define cfg_allocu(size) lp_allocu(cfg_mem, size)
107 #define cfg_allocz(size) lp_allocz(cfg_mem, size)
108 char *cfg_strdup(const char *c
);
109 void cfg_copy_list(list
*dest
, list
*src
, unsigned node_size
);
113 extern int (*cf_read_hook
)(byte
*buf
, uint max
, int fd
);
121 node n
; /* In list of symbols in config */
123 struct sym_scope
*scope
;
124 int class; /* SYM_* */
125 uint flags
; /* SYM_FLAG_* */
128 struct proto_config
*proto
; /* For SYM_PROTO and SYM_TEMPLATE */
129 const struct f_line
*function
; /* For SYM_FUNCTION */
130 const struct filter
*filter
; /* For SYM_FILTER */
131 struct rtable_config
*table
; /* For SYM_TABLE */
132 struct f_dynamic_attr
*attribute
; /* For SYM_ATTRIBUTE */
133 struct mpls_domain_config
*mpls_domain
; /* For SYM_MPLS_DOMAIN */
134 struct mpls_range_config
*mpls_range
; /* For SYM_MPLS_RANGE */
135 struct f_val
*val
; /* For SYM_CONSTANT */
136 uint offset
; /* For SYM_VARIABLE */
137 const struct keyword
*keyword
; /* For SYM_KEYWORD */
138 const struct f_method
*method
; /* For SYM_METHOD */
145 struct sym_scope
*next
; /* Next on scope stack */
146 struct symbol
*name
; /* Name of this scope */
148 HASH(struct symbol
) hash
; /* Local symbol hash */
150 uint slots
; /* Variable slots */
151 byte soft_scopes
; /* Number of soft scopes above */
152 byte active
:1; /* Currently entered */
153 byte block
:1; /* No independent stack frame */
154 byte readonly
:1; /* Do not add new symbols */
157 extern struct sym_scope
*global_root_scope
;
158 extern pool
*global_root_scope_pool
;
159 extern linpool
*global_root_scope_linpool
;
162 #define SYM_MAX_LEN 64
164 /* Remember to update cf_symbol_class_name() */
167 #define SYM_TEMPLATE 2
168 #define SYM_FUNCTION 3
171 #define SYM_ATTRIBUTE 6
172 #define SYM_KEYWORD 7
174 #define SYM_MPLS_DOMAIN 9
175 #define SYM_MPLS_RANGE 10
177 #define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */
178 #define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff)
179 #define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */
180 #define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff)
182 #define SYM_TYPE(s) ((s)->val->type)
183 #define SYM_VAL(s) ((s)->val->val)
186 #define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */
188 struct include_file_stack
{
189 void *buffer
; /* Internal lexer state */
190 char *file_name
; /* File name */
191 int fd
; /* File descriptor */
192 int lino
; /* Current line num */
193 int chno
; /* Current char num (on current line) */
194 int toklen
; /* Current token length */
195 int depth
; /* Include depth, 0 = cannot include */
197 struct include_file_stack
*prev
; /* Previous record in stack */
198 struct include_file_stack
*up
; /* Parent (who included this file) */
201 extern struct include_file_stack
*ifs
;
204 void cf_lex_init(int is_cli
, struct config
*c
);
205 void cf_lex_unwind(void);
207 struct symbol
*cf_find_symbol_scope(const struct sym_scope
*scope
, const byte
*c
);
208 static inline struct symbol
*cf_find_symbol_cfg(const struct config
*cfg
, const byte
*c
)
209 { return cf_find_symbol_scope(cfg
->root_scope
, c
); }
211 #define cf_find_symbol(where, what) _Generic(*(where), \
212 struct config: cf_find_symbol_cfg, \
213 struct sym_scope: cf_find_symbol_scope \
216 struct symbol
*cf_get_symbol(struct config
*conf
, const byte
*c
);
217 struct symbol
*cf_default_name(struct config
*conf
, char *template, int *counter
);
218 struct symbol
*cf_localize_symbol(struct config
*conf
, struct symbol
*sym
);
220 static inline int cf_symbol_is_local(struct config
*conf
, struct symbol
*sym
)
221 { return (sym
->scope
== conf
->current_scope
) && !conf
->current_scope
->soft_scopes
; }
224 struct symbol
*cf_new_symbol(struct sym_scope
*scope
, pool
*p
, struct linpool
*lp
, const byte
*c
);
227 * cf_define_symbol - define meaning of a symbol
228 * @sym: symbol to be defined
229 * @type: symbol class to assign
230 * @def: class dependent data
232 * Defines new meaning of a symbol. If the symbol is an undefined
233 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
234 * in different scope, a new symbol in current scope is created and the
235 * meaning is assigned to it. If it's already defined in the current scope,
236 * an error is reported via cf_error().
238 * Result: Pointer to the newly defined symbol. If we are in the top-level
239 * scope, it's the same @sym as passed to the function.
241 #define cf_define_symbol(conf_, osym_, type_, var_, def_) ({ \
242 struct symbol *sym_ = cf_localize_symbol(conf_, osym_); \
243 sym_->class = type_; \
247 #define cf_create_symbol(conf_, name_, type_, var_, def_) \
248 cf_define_symbol(conf_, cf_get_symbol(conf_, name_), type_, var_, def_)
250 void cf_push_scope(struct config
*, struct symbol
*);
251 void cf_pop_scope(struct config
*);
252 void cf_push_soft_scope(struct config
*);
253 void cf_pop_soft_scope(struct config
*);
255 static inline void cf_push_block_scope(struct config
*conf
)
256 { cf_push_scope(conf
, NULL
); conf
->current_scope
->block
= 1; }
258 static inline void cf_pop_block_scope(struct config
*conf
)
259 { ASSERT(conf
->current_scope
->block
); cf_pop_scope(conf
); }
261 char *cf_symbol_class_name(struct symbol
*sym
);
265 extern char *cf_text
;
270 void sysdep_preconfig(struct config
*);
271 int sysdep_commit(struct config
*, struct config
*);
272 void sysdep_shutdown_done(void);