int hostnameEdit(const char *option2) { struct uci_context *c; struct uci_ptr p; int length2= strlen("system.@system[0].hostname=")+strlen(option2)+1; char *hostname = safe_malloc(length2); strcpy(hostname,"system.@system[0].hostname="); strcat(hostname,option2); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, hostname, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } uci_set(c,&p); uci_save(c, p.p); uci_commit(c, &p.p, false); uci_free_context (c); free(hostname); return(0); }
/********************************************* * 载入配置文件,并遍历Section. */ static int uci_do_add(struct uci_context *ctx,struct uci_package *pkg,char *section_type,Meter *meter) { struct uci_section *s = NULL; int ret; struct uci_ptr ptr; struct uci_element *e = NULL; ret = uci_add_section(ctx, pkg, section_type, &s); if (ret != UCI_OK){ fprintf(stderr,"add section failed.\n"); return ret; } char ptr_str[64] = {0}; sprintf(ptr_str,"meter_lastvalue.%s.modbus_id=%d",s->e.name,meter->modbus_id); printf("ptr_str is %s.\n",ptr_str); if (uci_lookup_ptr(ctx, &ptr, ptr_str, true) != UCI_OK) { printf("lookup_ptr failed.\n"); return 1; } ret = uci_set(ctx, &ptr); if (ret != UCI_OK){ fprintf(stderr,"ret is not ok.\n"); return ret; } int i; Meter_Attribute *attribute = meter->attribute; for(i = 0; i < meter->attr_num; i++,attribute++) { sprintf(ptr_str,"meter_lastvalue.%s.%s=%d",s->e.name,attribute->value_unit,777); printf("ptr_str is %s.\n",ptr_str); if ((ret = uci_lookup_ptr(ctx, &ptr, ptr_str, true)) != UCI_OK) { printf("lookup_ptr failed.\n"); return ret; } ret = uci_set(ctx, &ptr); if (ret != UCI_OK){ fprintf(stderr,"ret is not ok.\n"); return ret; } } if ( UCI_OK != (ret = uci_commit(ctx, &pkg, false))) { printf("uci commit failed.\n"); return ret; } return ret; }
static void toggle_rule(const char *hook_name, const char *state, struct uci_option *option) { nakd_assert(hook_name != NULL && state != NULL && option != NULL); struct uci_context *ctx = option->section->package->ctx; struct uci_section *section = option->section; struct uci_option *opt_enabled = uci_lookup_option(ctx, section, "enabled"); struct uci_ptr new_opt_enabled_ptr = { .target = UCI_TYPE_OPTION, .p = option->section->package, .s = option->section }; const char *cvalue = strcmp(hook_name, "nak_hooks_enable") ? "option enabled 1" : "option enabled 0"; char *value = strdup(cvalue); uci_parse_ptr(ctx, &new_opt_enabled_ptr, value); uci_set(ctx, &new_opt_enabled_ptr); } struct nakd_uci_hook rule_hooks[] = { {"nak_hooks_enable", toggle_rule}, {"nak_hooks_disable", toggle_rule}, {NULL, NULL} }; void nakd_call_stage_hooks(const char *stage) { nakd_call_uci_hooks("firewall", rule_hooks, stage); }
int ssidEdit(const char *option1) { struct uci_context *c; struct uci_ptr p; int length1= strlen("wireless.@wifi-iface[0].ssid=")+strlen(option1)+1; char *ssid = safe_malloc(length1); strcpy(ssid,"wireless.@wifi-iface[0].ssid="); strcat(ssid,option1); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } uci_set(c,&p); uci_save(c, p.p); uci_commit(c, &p.p, false); uci_free_context (c); free(ssid); return(0); }
static void shift_uci_commit(void) { int i; struct uci_ptr ptr; char str[CONFIG_STR_MAX]; for (i = 0; i < CONFIG_MAX_ANALOG_PORTS; i++) { snprintf(str, CONFIG_STR_MAX, SHIFT_UCI_SET_TPL, i + 1, conf.port[i].shift); if (uci_lookup_ptr(conf.uci_ctx, &ptr, str, true) != UCI_OK) { uci_perror(conf.uci_ctx, str); exit(9); } uci_set(conf.uci_ctx, &ptr); uci_save(conf.uci_ctx, ptr.p); } if (uci_lookup_ptr(conf.uci_ctx, &ptr, SHIFT_UCI, true) != UCI_OK) { uci_perror(conf.uci_ctx, SHIFT_UCI); exit(10); } uci_commit(conf.uci_ctx, &ptr.p, false); }