2 Configuration file handling on top of tini
4 Copyright (C) Amitay Isaacs 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __CTDB_CONF_H__
21 #define __CTDB_CONF_H__
30 * @brief Configuration file handling with sections and key-value pairs
32 * CTDB settings can be written in a configuration file ctdb.conf (similar to
33 * samba's smb.conf). Various daemons and tools will consult the configuration
34 * file for runtime settings.
36 * The configuration will be organized in sections depending on various
37 * components. Each section will have various configuration options in the form
53 * @brief Abstract data structure holding the configuration options
58 * @brief configuration option update mode
60 * When a value of configuration option is changed, update mode is set
63 * CONF_MODE_API - value modified using set functions
64 * CONF_MODE_LOAD - value modified via conf_load
65 * CONF_MODE_RELOAD - value modified via conf_reload
67 enum conf_update_mode
{
74 * @brief configuration option type
83 * @brief Configuration section validation function
85 * Check if all the configuration options are consistent with each-other
87 typedef bool (*conf_validate_section_fn
)(struct conf_context
*conf
,
89 enum conf_update_mode mode
);
92 * @brief Configuration option validation function for string
94 * Check if a configuration option value is valid
96 typedef bool (*conf_validate_string_option_fn
)(const char *key
,
97 const char *old_value
,
98 const char *new_value
,
99 enum conf_update_mode mode
);
102 * @brief Configuration option validation function for integer
104 * Check if a configuration option value is valid
106 typedef bool (*conf_validate_integer_option_fn
)(const char *key
,
109 enum conf_update_mode mode
);
112 * @brief Configuration option validation function for boolean
114 * Check if a configuration option value is valid
116 typedef bool (*conf_validate_boolean_option_fn
)(const char *key
,
119 enum conf_update_mode mode
);
122 * @brief Initialize configuration option database
124 * This return a new configuration options context. Freeing this context will
125 * free up all the memory associated with the configuration options.
127 * @param[in] mem_ctx Talloc memory context
128 * @param[in] result The new configuration options context
129 * @return 0 on success, errno on failure
131 int conf_init(TALLOC_CTX
*mem_ctx
, struct conf_context
**result
);
134 * @brief Define a section for organizing configuration options
136 * This functions creates a section to organize configuration option. The
137 * section names are case-insensitive and are always stored in lower case.
139 * @param[in] conf The configuration options context
140 * @param[in] section The name of the section
141 * @param[in] validate The validation function for configuration options
143 void conf_define_section(struct conf_context
*conf
,
145 conf_validate_section_fn validate
);
148 * @brief Define a configuration option which has a string value
150 * This functions adds a new configuration option organized under a given
151 * section. Configuration options are case-insensitive and are always stored
154 * @param[in] conf The configuration options context
155 * @param[in] section The name of the section
156 * @param[in] key The name of the configuration option
157 * @param[in] default_value The default value for the configuration option
158 * @param[in] validate The validation function for the configuration option
160 void conf_define_string(struct conf_context
*conf
,
163 const char *default_value
,
164 conf_validate_string_option_fn validate
);
167 * @brief Define a configuration option which has an integer value
169 * This functions adds a new configuration option organized under a given
170 * section. Configuration options are case-insensitive and are always stored
173 * @param[in] conf The configuration options context
174 * @param[in] section The name of the section
175 * @param[in] key The name of the configuration option
176 * @param[in] default_value The default value for the configuration option
177 * @param[in] validate The validation function for the configuration option
179 void conf_define_integer(struct conf_context
*conf
,
182 const int default_value
,
183 conf_validate_integer_option_fn validate
);
186 * @brief Define a configuration option which has an boolean value
188 * This functions adds a new configuration option organized under a given
189 * section. Configuration options are case-insensitive and are always stored
192 * @param[in] conf The configuration options context
193 * @param[in] section The name of the section
194 * @param[in] key The name of the configuration option
195 * @param[in] default_value The default value for the configuration option
196 * @param[in] validate The validation function for the configuration option
198 void conf_define_boolean(struct conf_context
*conf
,
201 const bool default_value
,
202 conf_validate_boolean_option_fn validate
);
205 * @brief Assign user-accessible pointer for string option
207 * This pointer can be used for accessing the value of configuration option
208 * directly without requiring a function call.
210 * @param[in] conf The configuration options context
211 * @param[in] section The name of the section
212 * @param[in] key The name of the configuration option
213 * @param[in] ptr User-accessible pointer to the value
215 void conf_assign_string_pointer(struct conf_context
*conf
,
221 * @brief Assign user-accessible pointer for integer option
223 * This pointer can be used for accessing the value of configuration option
224 * directly without requiring a function call.
226 * @param[in] conf The configuration options context
227 * @param[in] section The name of the section
228 * @param[in] key The name of the configuration option
229 * @param[in] ptr User-accessible pointer to the value
231 void conf_assign_integer_pointer(struct conf_context
*conf
,
237 * @brief Assign user-accessible pointer for boolean option
239 * This pointer can be used for accessing the value of configuration option
240 * directly without requiring a function call.
242 * @param[in] conf The configuration options context
243 * @param[in] section The name of the section
244 * @param[in] key The name of the configuration option
245 * @param[in] ptr User-accessible pointer to the value
246 * @return true on success, false on failure
248 void conf_assign_boolean_pointer(struct conf_context
*conf
,
254 * @brief Query a configuration option
256 * This function checks if a configuration option is defined or not.
258 * @param[in] conf The configuration options context
259 * @param[in] section The name of the section
260 * @param[in] key The name of the configuration option
261 * @param[out] type The type of the configuration option
262 * @return true on success, false if section/option is not defined
264 bool conf_query(struct conf_context
*conf
,
267 enum conf_type
*type
);
270 * @brief Check if the defined configuration options are valid
272 * This function must be called after creating configuration options
273 * to confirm that all the option definitions are valid.
275 * @param[in] conf The configuration options context
276 * @return true on success, false on failure
278 bool conf_valid(struct conf_context
*conf
);
281 * @brief Set the default values for all configuration options
283 * This function resets all the configuration options to their default values.
285 * @param[in] conf The connfiguration options context
287 void conf_set_defaults(struct conf_context
*conf
);
290 * @brief Load the values for configuration option values from a file
292 * This function will update the values of the configuration options from those
293 * specified in a file. This function will fail in case it encounters an
294 * undefined option. Any sections which are not defined, will be ignored.
296 * This function will call validation function (if specified) before updating
297 * the value of a configuration option. After updating all the values for a
298 * section, the validation for section (if specified) will be called. If any
299 * of the validation functions return error, then all the configuration
300 * options will be reset to their previous values.
302 * @param[in] conf The configuration options context
303 * @param[in] filename The configuration file
304 * @param[in] skip_unknown Whether unknown config options should be ignored
305 * @return 0 on success, errno on failure
307 int conf_load(struct conf_context
*conf
,
308 const char *filename
,
313 * @brief Reload the values for configuration options
315 * This function will re-load the values of the configuration options. This
316 * function can be called only after successful call to conf_load().
320 * @param[in] conf The configuration options context
321 * @return 0 on success, errno on failure.
323 int conf_reload(struct conf_context
*conf
);
326 * @brief Set the string value of a configuration option
328 * This function can be used to update the value of a configuration option.
329 * This will call the validation function for that option (if defined) and
330 * the section validation function (if defined).
332 * If a user-defined storage pointer is provided, then the value of a
333 * configuration option should not be changed via that pointer.
335 * @param[in] conf The configuration options context
336 * @param[in] section The name of a section
337 * @param[in] key The name of a configuration option
338 * @param[in] str_val The string value
339 * @return 0 on success, errno in case of failure
341 int conf_set_string(struct conf_context
*conf
,
344 const char *str_val
);
347 * @brief Set the integer value of a configuration option
349 * This function can be used to update the value of a configuration option.
350 * This will call the validation function for that option (if defined) and
351 * the section validation function (if defined).
353 * If a user-defined storage pointer is provided, then the value of a
354 * configuration option should not be changed via that pointer.
356 * @param[in] conf The configuration options context
357 * @param[in] section The name of a section
358 * @param[in] key The name of a configuration option
359 * @param[in] int_val The integer value
360 * @return 0 on success, errno in case of failure
362 int conf_set_integer(struct conf_context
*conf
,
368 * @brief Set the boolean value of a configuration option
370 * This function can be used to update the value of a configuration option.
371 * This will call the validation function for that option (if defined) and
372 * the section validation function (if defined).
374 * If a user-defined storage pointer is provided, then the value of a
375 * configuration option should not be changed via that pointer.
377 * @param[in] conf The configuration options context
378 * @param[in] section The name of a section
379 * @param[in] key The name of a configuration option
380 * @param[in] bool_val The boolean value
381 * @return 0 on success, errno in case of failure
383 int conf_set_boolean(struct conf_context
*conf
,
389 * @brief Get the string value of a configuration option
391 * This function can be used to fetch the current value of a configuration
394 * If a user-defined storage pointer is provided, then the value of a
395 * configuration option can be accessed directly via that pointer.
397 * @param[in] conf The configuration options context
398 * @param[in] section The name of a section
399 * @param[in] key The name of a configuration option
400 * @param[out] str_val The string value of the configuration option
401 * @param[out] is_default True if the value is default value
402 * @return 0 on success, errno in case of failure
404 int conf_get_string(struct conf_context
*conf
,
407 const char **str_val
,
411 * @brief Get the integer value of a configuration option
413 * This function can be used to fetch the current value of a configuration
416 * If a user-defined storage pointer is provided, then the value of a
417 * configuration option can be accessed directly via that pointer.
419 * @param[in] conf The configuration options context
420 * @param[in] section The name of a section
421 * @param[in] key The name of a configuration option
422 * @param[out] int_val The integer value of the configuration option
423 * @param[out] is_default True if the value is default value
424 * @return 0 on success, errno in case of failure
426 int conf_get_integer(struct conf_context
*conf
,
433 * @brief Get the boolean value of a configuration option
435 * This function can be used to fetch the current value of a configuration
438 * If a user-defined storage pointer is provided, then the value of a
439 * configuration option can be accessed directly via that pointer.
441 * @param[in] conf The configuration options context
442 * @param[in] section The name of a section
443 * @param[in] key The name of a configuration option
444 * @param[out] bool_val The boolean value of the configuration option
445 * @param[out] is_default True if the value is default value
446 * @return 0 on success, errno in case of failure
448 int conf_get_boolean(struct conf_context
*conf
,
455 * @brief Dump the configuration in a file
457 * All the configuration options are dumped with their current values.
458 * If an option has a default value, then it is commented.
460 * Here is a sample output:
465 * # key3 = default_value3
469 * @param[in] conf The configuration options context
470 * @param[in] fp File pointer
472 void conf_dump(struct conf_context
*conf
, FILE *fp
);
474 #endif /* __CTDB_CONF_H__ */