]> pilppa.com Git - libplp.git/commitdiff
initial config file support
authorMika Laitio <lamikr@pilppa.org>
Tue, 21 Dec 2010 22:18:50 +0000 (00:18 +0200)
committerMika Laitio <lamikr@pilppa.org>
Tue, 21 Dec 2010 22:18:50 +0000 (00:18 +0200)
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 <lamikr@pilppa.org>
README
configure.ac
src/Makefile.am
src/config.c [new file with mode: 0644]
src/config.h [new file with mode: 0644]
src_test/Makefile.am
src_test/test_config.c [new file with mode: 0644]

diff --git a/README b/README
index 1afd599f404663135b62008cd4aaa29f6c63427c..de94f3500b0faeaa215d1b5542fb3be081c5fabc 100644 (file)
--- 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
 ----
index 1f08a474ed404e0ff29c61794cf092a0cbc7d467..bb2745032392d7fa235ece8ed9c0609572855681 100644 (file)
@@ -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)
 
index e0ee76846bf22938933e5b3c397cd6537a3c47ec..9697c8bacc2f1939a1e2090873fdabcbf2eb29aa 100644 (file)
@@ -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 (file)
index 0000000..0b09944
--- /dev/null
@@ -0,0 +1,102 @@
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <uci.h>
+
+#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 (file)
index 0000000..4b1fc3f
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * config.h
+ *
+ *  Created on: Dec 21, 2010
+ *      Author: lamikr
+ */
+
+#ifndef CONFIG_H_
+#define CONFIG_H_
+
+#include <stdbool.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);
+
+#endif /* CONFIG_H_ */
index 858dabd32d585d5b58cbc87d9be909364112bb34..e331d77c58e3a7a8df8fbc7cdfb0884fbc55e993 100644 (file)
@@ -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 (file)
index 0000000..13a1d53
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * test_w1.cc
+ *
+ *  Created on: Oct 20, 2010
+ *      Author: lamikr
+ */
+#include <unistd.h>
+#include <stdio.h>
+#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;
+}