From c2adfd4e17d4fb0c1c16d3bcf96dbd6ccb798753 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Wed, 22 Dec 2010 00:18:50 +0200 Subject: [PATCH] initial config file support supports for now only the setting of config values, read must still be done directly by using libuci functions. Note config value setup requires a special version of libuci which contains some modifications for adding new sections. Signed-off-by: Mika Laitio --- README | 16 +++---- configure.ac | 2 +- src/Makefile.am | 6 ++- src/config.c | 102 +++++++++++++++++++++++++++++++++++++++++ src/config.h | 20 ++++++++ src_test/Makefile.am | 5 +- src_test/test_config.c | 30 ++++++++++++ 7 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 src/config.c create mode 100644 src/config.h create mode 100644 src_test/test_config.c diff --git a/README b/README index 1afd599..de94f35 100644 --- a/README +++ b/README @@ -4,20 +4,20 @@ This library provides a mechanism to read service files and launch and monitor t Dependencies to other libraries ------------------------------- -- libuci for reading configuration files +- modified version of is needed libuci for setting the configuration values to config files Build, install and clean ------------------------ - build with command: ./autobuild.sh (generates required autoconf files) - install with command: make install -- clean source code from generated build files: make distclean +- clean source code from generated build files: make maintainer-clean -Examples --------- -See the src_test/test_ha -"test_ha" is a example application which launches the processes defined in the -.service files available in the directory "src_test/etc/plp/ha" -(Note that these files needs to be copied to /etc/plp/ha directory and you must fix from them the ExecStart to executables) +Examples in src_test directory +------------------------------ +- "test_ha" is a example application which launches the processes defined in the + .service files available in the directory "src_test/etc/plp/ha" + (Note that these files needs to be copied to /etc/plp/ha directory and you must fix from them the ExecStart to executables) +- "test_config" shows how to set and change config file values. TODO ---- diff --git a/configure.ac b/configure.ac index 1f08a47..bb27450 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ AC_PROG_INSTALL PKG_PROG_PKG_CONFIG() -#PKG_CHECK_MODULES(SRC) +PKG_CHECK_MODULES(SRC, libuci) AC_SUBST(SRC_CFLAGS) AC_SUBST(SRC_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index e0ee768..9697c8b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,11 +2,13 @@ lib_LTLIBRARIES = libplp.la libplp_la_SOURCES = \ retval.h \ log.h \ - log_config.c + log_config.c \ + config.c config.h libplp_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined AM_CPPFLAGS = $(SRC_CFLAGS) DISTCLEANFILES = Makefile.in libplpincludedir=$(includedir)/plp libplpinclude_HEADERS = \ retval.h \ - log.h + log.h \ + config.h diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..0b09944 --- /dev/null +++ b/src/config.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include + +#include "log.h" +#include "config.h" + +bool set_config_value(const char *conf_dir_name, + const char *conf_file_name, + const char *section_type, + const char *section_name, + const char *key, + const char *value) { + struct uci_context *ctx; + struct uci_package *pkg; + struct uci_section *sct; + struct uci_section *tmp_sct; + struct uci_option *opt; + int err_flg; + char *fname; + int b_count; + bool save; + bool ret_val; + + ret_val = false; + save = false; + if ((conf_dir_name != NULL) && + (conf_file_name != NULL) && + (section_type != NULL) && + (section_name != NULL) && + (key != NULL) && + (value != NULL)) { + b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10; + fname = (char *)calloc(1, b_count); + if (fname != NULL) { + strncpy(fname, conf_dir_name, b_count); + strncat(fname, "/", 1); + strncat(fname, conf_file_name, strlen(conf_file_name) + 1); + ctx = uci_alloc_context(); + sct = NULL; + uci_set_confdir(ctx, conf_dir_name); + if (access(fname, F_OK) != 0) { + FILE *fp; + + fp = fopen(fname, "w+"); + fclose(fp); + } + err_flg = uci_load(ctx, fname, &pkg); + struct uci_element *e; + uci_foreach_element(&pkg->sections, e) { + tmp_sct = uci_to_section(e); + if (strcmp(tmp_sct->type, section_type) == 0) { + sct = tmp_sct; + break; + } + } + //sct = uci_lookup_section(ctx, pkg, "service"); + if (sct != NULL) { + log_debug("section found\n"); + } + else { + log_debug("adding new section\n"); + err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct); + log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name); + } + opt = uci_lookup_option(ctx, sct, key); + if (opt != NULL) { + struct uci_ptr ptr; + memset(&ptr, 0, sizeof(ptr)); + ptr.package = pkg->e.name; + ptr.section = sct->e.name; + ptr.option = key; + + if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) { + log_debug("trying to change option value\n"); + ptr.value = strdup(value); + uci_set(ctx, &ptr); + save = true; + } + } + else { + opt = uci_alloc_option(sct, key, value); + save = true; + } + if (save == true) { + log_debug("saving config file: %s\n", fname); + uci_save(ctx, pkg); + } + free(fname); + ret_val = true; + } + else { + log_error("Could not change config value, out of memory"); + } + } + else { + log_error("Could not change config value, invalid parameters"); + } + return ret_val; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..4b1fc3f --- /dev/null +++ b/src/config.h @@ -0,0 +1,20 @@ +/* + * config.h + * + * Created on: Dec 21, 2010 + * Author: lamikr + */ + +#ifndef CONFIG_H_ +#define CONFIG_H_ + +#include + +bool set_config_value(const char *conf_dir_name, + const char *conf_file_name, + const char *section_type, + const char *section_name, + const char *key, + const char *value); + +#endif /* CONFIG_H_ */ diff --git a/src_test/Makefile.am b/src_test/Makefile.am index 858dabd..e331d77 100644 --- a/src_test/Makefile.am +++ b/src_test/Makefile.am @@ -1,8 +1,11 @@ -noinst_PROGRAMS = test_logs +noinst_PROGRAMS = test_logs test_config test_logs_SOURCES = test_logs.c test_logs_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a +test_config_SOURCES = test_config.c +test_config_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a + AM_CPPFLAGS = $(SRC_CFLAGS) -I../src DISTCLEANFILES = Makefile.in \ No newline at end of file diff --git a/src_test/test_config.c b/src_test/test_config.c new file mode 100644 index 0000000..13a1d53 --- /dev/null +++ b/src_test/test_config.c @@ -0,0 +1,30 @@ +/* + * test_w1.cc + * + * Created on: Oct 20, 2010 + * Author: lamikr + */ +#include +#include +#include "../src/config.h" + +int main(int argc, char** argv) { + char work_dir[FILENAME_MAX]; + + getcwd(work_dir, sizeof(work_dir)); + printf("working directory: %s\n", work_dir); + + set_config_value(work_dir, + "dev_cfg.txt", + "mysection_type", + "mysection_name", + "myoption_name", + "my_option_value"); + set_config_value(work_dir, + "dev_cfg.txt", + "mysection_type", + "mysection_name", + "myoption_name", + "my_option_value2"); + return 0; +} -- 2.41.0