10 bool set_config_value(const char *conf_dir_name,
11 const char *conf_file_name,
12 const char *section_type,
13 const char *section_name,
16 struct uci_context *ctx;
17 struct uci_package *pkg;
18 struct uci_section *sct;
19 struct uci_section *tmp_sct;
20 struct uci_option *opt;
25 struct uci_element *elem;
32 if ((conf_dir_name != NULL) &&
33 (conf_file_name != NULL) &&
34 (section_type != NULL) &&
35 (section_name != NULL) &&
38 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
39 fname = (char *)calloc(1, b_count);
41 strncpy(fname, conf_dir_name, b_count);
42 strncat(fname, "/", 1);
43 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
44 ctx = uci_alloc_context();
47 uci_set_confdir(ctx, conf_dir_name);
48 if (access(fname, W_OK) != 0) {
49 if (access(fname, F_OK) != 0) {
50 fp = fopen(fname, "w+");
54 if (access(fname, W_OK) == 0) {
55 err_flg = uci_load(ctx, fname, &pkg);
56 uci_foreach_element(&pkg->sections, elem) {
57 tmp_sct = uci_to_section(elem);
58 if (strcmp(tmp_sct->type, section_type) == 0) {
63 //sct = uci_lookup_section(ctx, pkg, "service");
65 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
66 err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
69 opt = uci_lookup_option(ctx, sct, key);
71 memset(&ptr, 0, sizeof(ptr));
72 ptr.package = pkg->e.name;
73 ptr.section = sct->e.name;
76 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
77 ptr.value = strdup(value);
83 opt = uci_alloc_option(sct, key, value);
89 uci_free_context(ctx);
93 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
97 log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
103 log_error("Could not change config value, out of memory");
107 log_error("Could not change config value, invalid parameters");