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 *e;
30 if ((conf_dir_name != NULL) &&
31 (conf_file_name != NULL) &&
32 (section_type != NULL) &&
33 (section_name != NULL) &&
36 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
37 fname = (char *)calloc(1, b_count);
39 strncpy(fname, conf_dir_name, b_count);
40 strncat(fname, "/", 1);
41 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
42 ctx = uci_alloc_context();
45 uci_set_confdir(ctx, conf_dir_name);
46 if (access(fname, W_OK) != 0) {
47 if (access(fname, F_OK) != 0) {
49 fp = fopen(fname, "w+");
53 err_flg = uci_load(ctx, fname, &pkg);
54 uci_foreach_element(&pkg->sections, e) {
55 tmp_sct = uci_to_section(e);
56 if (strcmp(tmp_sct->type, section_type) == 0) {
61 //sct = uci_lookup_section(ctx, pkg, "service");
63 log_debug("section found\n");
66 log_debug("adding new section\n");
67 err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
68 log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name);
70 opt = uci_lookup_option(ctx, sct, key);
73 memset(&ptr, 0, sizeof(ptr));
74 ptr.package = pkg->e.name;
75 ptr.section = sct->e.name;
78 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
79 log_debug("trying to change option value\n");
80 ptr.value = strdup(value);
86 opt = uci_alloc_option(sct, key, value);
90 log_debug("saving config file: %s\n", fname);
93 uci_free_context(ctx);
99 log_error("Could not change config value, out of memory");
103 log_error("Could not change config value, invalid parameters");