void validate_setup(void) { cfg_opt_t *opt = 0; static cfg_opt_t action_opts[] = { CFG_INT("speed", 0, CFGF_NONE), CFG_STR("name", 0, CFGF_NONE), CFG_INT("xspeed", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t multi_opts[] = { CFG_INT_LIST("speeds", 0, CFGF_NONE), CFG_SEC("options", action_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR_LIST("ip-address", 0, CFGF_NONE), CFG_INT_CB("action", ACTION_NONE, CFGF_NONE, parse_action), CFG_SEC("options", action_opts, CFGF_NONE), CFG_SEC("multi_options", multi_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, 0); cfg_set_validate_func(cfg, "ip-address", validate_ip); fail_unless(cfg_set_validate_func(cfg, "ip-address", validate_ip) == validate_ip); opt = cfg_getopt(cfg, "ip-address"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_ip); cfg_set_validate_func(cfg, "options", validate_action); fail_unless(cfg_set_validate_func(cfg, "options", validate_action) == validate_action); opt = cfg_getopt(cfg, "options"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_action); cfg_set_validate_func(cfg, "options|speed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "options|speed", validate_speed) == validate_speed); opt = cfg_getopt(cfg, "options|speed"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_speed); cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed) == validate_speed); cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed) == validate_speed); /* Validate callbacks for *set*() functions, i.e. not when parsing file content */ cfg_set_validate_func2(cfg, "multi_options|speed", validate_speed2); cfg_set_validate_func2(cfg, "multi_options|options|name", validate_name2); }
cfg_t *parse_conf(const char *filename) { cfg_opt_t bookmark_opts[] = { CFG_STR("host", 0, CFGF_NODEFAULT), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", "anonymous", CFGF_NONE), CFG_STR("password", "anonymous@", CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_BOOL("reverse-dns", cfg_true, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_BOOL("remote-completion", cfg_true, CFGF_NONE), CFG_FUNC("alias", conf_alias), CFG_STR_LIST("xterm-terminals", "{xterm, rxvt}", CFGF_NONE), CFG_INT_CB("auto-create-bookmark", ACB_YES, CFGF_NONE, conf_parse_acb), CFG_FUNC("include-file", cfg_include), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port); cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno)); printf("continuing with default values...\n\n"); case CFG_SUCCESS: break; case CFG_PARSE_ERROR: return 0; } return cfg; }
int main(void) { cfg_opt_t greet_opts[] = { CFG_STR_LIST("targets", "{World}", CFGF_NONE), CFG_INT("repeat", 1, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("greeting", greet_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_t *cfg, *cfg_greet; int repeat; int i, j; cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "greeting|repeat", validate_unsigned_int); if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) return 1; for(j = 0; j < cfg_size(cfg, "greeting"); j++) { cfg_greet = cfg_getnsec(cfg, "greeting", j); repeat = cfg_getint(cfg_greet, "repeat"); while(repeat--) { printf("%s", cfg_title(cfg_greet)); for(i = 0; i < cfg_size(cfg_greet, "targets"); i++) printf(", %s", cfg_getnstr(cfg_greet, "targets", i)); printf("!\n"); } } cfg_free(cfg); return 0; }
/** * @brief 根据 conf 文件加载业务名称与gameid的对应关系; * @load_count 本次被加载的条数; * @return -1: failed, 0: succ */ int load_svc_gameid_map(const char *conf_path, uint32_t *load_count) { uint32_t n, last; int i, ret; cfg_t *si; struct svc_gameid_map_t *sg; static cfg_opt_t svcinfo_opts[] = { CFG_STR("name", 0, CFGF_NONE), CFG_INT("gameid", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("svcinfo", svcinfo_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); /* set a validating callback function for svcinfo sections */ cfg_set_validate_func(cfg, "svcinfo", &cb_validate_svcinfo); ret = cfg_parse(cfg, conf_path ? conf_path : DEF_SVC_CFG_PATH); if(ret == CFG_FILE_ERROR) { fprintf(stderr, "Access error, conf: %s, err: %s\n", conf_path ? conf_path : DEF_SVC_CFG_PATH, strerror(errno)); return -1; } else if(ret == CFG_PARSE_ERROR) { fprintf(stderr, "Parse error, conf: %s\n", conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } memset(sg_map_shadow, 0, sizeof(sg_map_shadow)); n = cfg_size(cfg, "svcinfo"); for(i = 0; i < n; i++) { if (i == MAX_SVC_NUM) break; si = cfg_getnsec(cfg, "svcinfo", i); sg = &(sg_map_shadow[i]); snprintf(sg->name, sizeof(sg->name), "%s", cfg_getstr(si, "name")); sg->gameid = cfg_getint(si, "gameid"); cur_svcinfo_count_shadow++; } cfg_free(cfg); /* sort sg */ qsort(sg_map_shadow, cur_svcinfo_count_shadow, sizeof(sg_map_shadow[0]), svcinfocmp); last = 0; for (i = 0; i < cur_svcinfo_count_shadow; i++) { sg = &(sg_map_shadow[i]); if (sg->gameid == last) { fprintf(stderr, "dup gameid: %u in conf: %s\n", last, conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } if (sg->gameid > MAX_SVC_NUM) { fprintf(stderr, "gameid: %u > max(%u) in conf: %s\n", sg->gameid, MAX_SVC_NUM, conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } last = sg->gameid; } svcinfo_enable = 0; memcpy(sg_map, sg_map_shadow, sizeof(sg_map)); cur_svcinfo_count = cur_svcinfo_count_shadow; if (load_count) { *load_count = cur_svcinfo_count; } svcinfo_enable = 1; return 0; }
/* * read configuration file */ int read_config(struct program_params *pp) { static cfg_opt_t pv_opts[] = { CFG_INT("tier", 0, CFGF_NONE), CFG_FLOAT("pinningScore", 0, CFGF_NONE), CFG_STR("path", NULL, CFGF_NONE), CFG_INT_CB("maxUsedSpace", -1, CFGF_NONE, parse_size_value), CFG_END() }; static cfg_opt_t volume_opts[] = { CFG_STR("LogicalVolume", NULL, CFGF_NONE), CFG_STR("VolumeGroup", NULL, CFGF_NONE), CFG_FLOAT("timeExponent", 1.0/(2<<14), CFGF_NONE), CFG_FLOAT("hitScore", 16, CFGF_NONE), CFG_FLOAT("readMultiplier", 1, CFGF_NONE), CFG_FLOAT("writeMultiplier", 4, CFGF_NONE), CFG_INT_CB("pvmoveWait", 5*60, CFGF_NONE, parse_time_value), CFG_INT_CB("checkWait", 15*60, CFGF_NONE, parse_time_value), CFG_SEC("pv", pv_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_t *cfg; cfg = cfg_init(opts, CFGF_NONE); assert(cfg); // add verification functions cfg_set_validate_func(cfg, "volume|timeExponent", validate_require_positive); cfg_set_validate_func(cfg, "volume|hitScore", validate_require_positive); cfg_set_validate_func(cfg, "volume|readMultiplier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|writeMultiplier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|pinningScore", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|tier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|maxUsedSpace", validate_require_nonnegative); // TODO cfg_set_validate_func(cfg, "volume", validate_pv); // do they belong to volume, is there enough space switch(cfg_parse(cfg, pp->conf_file_path)) { case CFG_FILE_ERROR: fprintf(stderr, "Configuration file \"%s\" could not be read: %s\n", pp->conf_file_path, strerror(errno)); return 1; case CFG_SUCCESS: break; case CFG_PARSE_ERROR: fprintf(stderr, "Configuration file errors, aborting\n"); return 1; default: fprintf(stderr, "Internal error"); assert(0); } pp->cfg = cfg; return 0; }
int main(int argc, char *argv[]) { try { cfg_opt_t opts_sensor[] = { CFG_INT(const_cast<char *>("crate"), 0xff, CFGF_NONE), CFG_INT(const_cast<char *>("fru"), 0xff, CFGF_NONE), CFG_STR(const_cast<char *>("card"), const_cast<char *>(""), CFGF_NONE), CFG_STR(const_cast<char *>("sensor"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC(const_cast<char *>("sensor"), opts_sensor, CFGF_MULTI), CFG_STR(const_cast<char *>("outfile"), const_cast<char *>("data.csv"), CFGF_NONE), CFG_STR(const_cast<char *>("host"), const_cast<char *>("localhost"), CFGF_NONE), CFG_STR(const_cast<char *>("password"), const_cast<char *>(""), CFGF_NONE), CFG_INT(const_cast<char *>("port"), 4681, CFGF_NONE), CFG_INT(const_cast<char *>("interval"), 5, CFGF_NONE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "host", &cfg_validate_hostname); cfg_set_validate_func(cfg, "port", &cfg_validate_port); if (argc < 2) { printf("%s sensor-spec.conf\n", argv[0]); exit(1); } if (cfg_parse(cfg, argv[1]) == CFG_PARSE_ERROR) exit(1); const char *host = cfg_getstr(cfg, "host"); const char *pass = cfg_getstr(cfg, "password"); const char *outfile = cfg_getstr(cfg, "outfile"); uint16_t port = cfg_getint(cfg, "port"); uint32_t interval = cfg_getint(cfg, "interval"); sysmgr::sysmgr sm(host, pass, port); try { sm.connect(); } catch (sysmgr::sysmgr_exception &e) { printf("Unable to connect to system manager: %s\n", e.message.c_str()); exit(2); } std::vector<Sensor> sensors; std::vector<sysmgr::crate_info> sm_crates = sm.list_crates(); for (std::vector<sysmgr::crate_info>::iterator it = sm_crates.begin(); it != sm_crates.end(); it++) { if (!it->connected) printf("Warning: Crate %hhu is not connected at this time. Discarding all data related to it.\n", it->crateno); } for(unsigned int i = 0; i < cfg_size(cfg, "sensor"); i++) { cfg_t *cfgsensor = cfg_getnsec(cfg, "sensor", i); uint8_t crate = cfg_getint(cfgsensor, "crate"); uint8_t fru = cfg_getint(cfgsensor, "fru"); const char *card = cfg_getstr(cfgsensor, "card"); const char *sensor = cfg_getstr(cfgsensor, "sensor"); bool found = false; for (std::vector<sysmgr::crate_info>::iterator c_it = sm_crates.begin(); c_it != sm_crates.end(); c_it++) { if (!c_it->connected) continue; if (crate != 0xff && c_it->crateno != crate) continue; std::vector<sysmgr::card_info> sm_frus = sm.list_cards(c_it->crateno); for (std::vector<sysmgr::card_info>::iterator f_it = sm_frus.begin(); f_it != sm_frus.end(); f_it++) { if (fru != 0xff && f_it->fru != fru) continue; if (card[0] != '\0' && f_it->name != card) continue; std::vector<sysmgr::sensor_info> sm_sensors = sm.list_sensors(c_it->crateno, f_it->fru); for (std::vector<sysmgr::sensor_info>::iterator s_it = sm_sensors.begin(); s_it != sm_sensors.end(); s_it++) { if (s_it->type == 'E' || s_it->type == 'O') continue; // We don't know how to read these. if (sensor[0] != '\0' && s_it->name != sensor) continue; sensors.push_back(Sensor(c_it->crateno, f_it->fru, s_it->name, stdsprintf("C%hhu %s (%s) %s", c_it->crateno, sysmgr::sysmgr::get_slotstring(f_it->fru).c_str(), f_it->name.c_str(), s_it->name.c_str()))); found = true; } } } if (!found) printf("Warning: Unable to find a match for %hhu %hhu \"%s\" \"%s\"\n", crate, fru, card, sensor); } FILE *fd = fopen(outfile, "w"); if (!fd) { printf("Unable to open %s for writing\n", outfile); exit(1); } cfg_free(cfg); printf("Sensors indexed. Now polling %d sensors.\n", (int)sensors.size()); fprintf(fd, "Time"); for (std::vector<Sensor>::iterator it = sensors.begin(); it != sensors.end(); it++) { fprintf(fd, CSV_COMMA "\"%s\"", it->name.c_str()); } fprintf(fd, "\n"); fflush(fd); time_t now; char timestamp[32]; while (1) { time(&now); strftime(timestamp, 32, "%Y-%m-%d %H:%M:%S", localtime(&now)); fprintf(fd, "%s", timestamp); for (std::vector<Sensor>::iterator it = sensors.begin(); it != sensors.end(); it++) { fprintf(fd, CSV_COMMA); try { sysmgr::sensor_reading reading = sm.sensor_read(it->crate, it->fru, it->sensor); if (reading.threshold_set) fprintf(fd, "%f", reading.threshold); else fprintf(fd, "0x%04hx", reading.eventmask); } catch (sysmgr::sysmgr_exception &e) { //printf("Error reading sensor \"%s\": %s\n", it->name.c_str(), e.message.c_str()); } //usleep(100000); } fprintf(fd, "\n"); fflush(fd); sleep(interval); if (!sm.connected()) break; } } catch (sysmgr::sysmgr_exception &e) { printf("Caught fatal exception: %s\n", e.message.c_str()); printf("Goodbye.\n"); } }
/** * Parse config file options */ static gboolean parse_config_file(void) { cfg_t *cfg, *sec_as, *sec_mpd; cfg_opt_t mpd_opts[] = { CFG_STR("host", "localhost", CFGF_NONE), CFG_INT("port", 6600, CFGF_NONE), CFG_INT("timeout", 5, CFGF_NONE), CFG_INT("interval", 10, CFGF_NONE), CFG_STR("password", "", CFGF_NONE), CFG_END() }; cfg_opt_t as_opts[] = { CFG_STR("username", "", CFGF_NONE), CFG_STR("password", "", CFGF_NONE), CFG_STR("password_hash", "", CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT_CB("log_level", G_LOG_LEVEL_ERROR, CFGF_NONE, &cf_log_level), CFG_STR("log_file", "/var/log/scmpc.log", CFGF_NONE), CFG_STR("pid_file", "/var/run/scmpc.pid", CFGF_NONE), CFG_STR("cache_file", "/var/lib/scmpc/scmpc.cache", CFGF_NONE), CFG_INT("queue_length", 500, CFGF_NONE), CFG_INT("cache_interval", 10, CFGF_NONE), CFG_SEC("mpd", mpd_opts, CFGF_NONE), CFG_SEC("audioscrobbler", as_opts, CFGF_NONE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "queue_length", &cf_validate_num); cfg_set_validate_func(cfg, "cache_interval", &cf_validate_num); cfg_set_validate_func(cfg, "mpd|port", &cf_validate_num); cfg_set_validate_func(cfg, "mpd|timeout", &cf_validate_num); if (parse_files(cfg) == FALSE) { cfg_free(cfg); return FALSE; } g_free(prefs.log_file); g_free(prefs.pid_file); g_free(prefs.cache_file); g_free(prefs.mpd_hostname); g_free(prefs.mpd_password); g_free(prefs.as_username); g_free(prefs.as_password); g_free(prefs.as_password_hash); prefs.log_level = cfg_getint(cfg, "log_level"); prefs.log_file = expand_tilde(cfg_getstr(cfg, "log_file")); prefs.pid_file = expand_tilde(cfg_getstr(cfg, "pid_file")); prefs.cache_file = expand_tilde(cfg_getstr(cfg, "cache_file")); prefs.queue_length = cfg_getint(cfg, "queue_length"); prefs.cache_interval = cfg_getint(cfg, "cache_interval"); sec_mpd = cfg_getsec(cfg, "mpd"); prefs.mpd_hostname = g_strdup(cfg_getstr(sec_mpd, "host")); prefs.mpd_port = cfg_getint(sec_mpd, "port"); prefs.mpd_timeout = cfg_getint(sec_mpd, "timeout"); prefs.mpd_password = g_strdup(cfg_getstr(sec_mpd, "password")); sec_as = cfg_getsec(cfg, "audioscrobbler"); prefs.as_username = g_strdup(cfg_getstr(sec_as, "username")); prefs.as_password = g_strdup(cfg_getstr(sec_as, "password")); prefs.as_password_hash = g_strdup(cfg_getstr(sec_as, "password_hash")); prefs.fork = TRUE; cfg_free(cfg); return TRUE; }
int main(int argc, char *argv[]) { uint8_t threadid_main = 0; pthread_key_create(&threadid_key, NULL); pthread_setspecific(threadid_key, &threadid_main); mprintf("University of Wisconsin IPMI MicroTCA System Manager\n"); if (argc > 1 && strcmp(argv[1], "--version") == 0) { mprintf("\nCompiled from %s@%s\n", (GIT_BRANCH[0] ? GIT_BRANCH : "git-archive"), (GIT_COMMIT[0] ? GIT_COMMIT : "27868b9b800d107fbb53b68c2fce207144f97a98")); if (strlen(GIT_DIRTY) > 1) mprintf("%s", GIT_DIRTY); mprintf("\n"); return 0; } /* * Parse Configuration */ cfg_opt_t opts_auth[] = { CFG_STR_LIST(const_cast<char *>("raw"), const_cast<char *>("{}"), CFGF_NONE), CFG_STR_LIST(const_cast<char *>("manage"), const_cast<char *>("{}"), CFGF_NONE), CFG_STR_LIST(const_cast<char *>("read"), const_cast<char *>("{}"), CFGF_NONE), CFG_END() }; cfg_opt_t opts_crate[] = { CFG_STR(const_cast<char *>("host"), const_cast<char *>(""), CFGF_NONE), CFG_STR(const_cast<char *>("description"), const_cast<char *>(""), CFGF_NONE), CFG_STR(const_cast<char *>("username"), const_cast<char *>(""), CFGF_NONE), CFG_STR(const_cast<char *>("password"), const_cast<char *>(""), CFGF_NONE), CFG_INT_CB(const_cast<char *>("authtype"), 0, CFGF_NONE, cfg_parse_authtype), CFG_INT_CB(const_cast<char *>("mch"), 0, CFGF_NONE, cfg_parse_MCH), CFG_BOOL(const_cast<char *>("enabled"), cfg_true, CFGF_NONE), CFG_END() }; cfg_opt_t opts_cardmodule[] = { CFG_STR(const_cast<char *>("module"), const_cast<char *>(""), CFGF_NONE), CFG_STR_LIST(const_cast<char *>("config"), const_cast<char *>("{}"), CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC(const_cast<char *>("authentication"), opts_auth, CFGF_NONE), CFG_SEC(const_cast<char *>("crate"), opts_crate, CFGF_MULTI), CFG_SEC(const_cast<char *>("cardmodule"), opts_cardmodule, CFGF_MULTI), CFG_INT(const_cast<char *>("socket_port"), 4681, CFGF_NONE), CFG_INT(const_cast<char *>("ratelimit_delay"), 0, CFGF_NONE), CFG_BOOL(const_cast<char *>("daemonize"), cfg_false, CFGF_NONE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "crate|host", &cfg_validate_hostname); cfg_set_validate_func(cfg, "socket_port", &cfg_validate_port); if (argc >= 2 && access(argv[1], R_OK) == 0) { if(cfg_parse(cfg, argv[1]) == CFG_PARSE_ERROR) exit(1); } else if (access(CONFIG_PATH "/" CONFIG_FILE, R_OK) == 0) { if(cfg_parse(cfg, CONFIG_PATH "/" CONFIG_FILE) == CFG_PARSE_ERROR) exit(1); } else { printf("Config file %s not found, and no argument supplied.\n", CONFIG_PATH "/" CONFIG_FILE); printf("Try: %s sysmgr.conf\n", argv[0]); exit(1); } bool crate_found = false; bool crate_enabled = false; cfg_t *cfgauth = cfg_getsec(cfg, "authentication"); for(unsigned int i = 0; i < cfg_size(cfgauth, "raw"); i++) config_authdata.raw.push_back(std::string(cfg_getnstr(cfgauth, "raw", i))); for(unsigned int i = 0; i < cfg_size(cfgauth, "manage"); i++) config_authdata.manage.push_back(std::string(cfg_getnstr(cfgauth, "manage", i))); for(unsigned int i = 0; i < cfg_size(cfgauth, "read"); i++) config_authdata.read.push_back(std::string(cfg_getnstr(cfgauth, "read", i))); for(unsigned int i = 0; i < cfg_size(cfg, "crate"); i++) { cfg_t *cfgcrate = cfg_getnsec(cfg, "crate", i); crate_found = true; enum Crate::Mfgr MCH; switch (cfg_getint(cfgcrate, "mch")) { case Crate::VADATECH: MCH = Crate::VADATECH; break; case Crate::NAT: MCH = Crate::NAT; break; } const char *user = cfg_getstr(cfgcrate, "username"); const char *pass = cfg_getstr(cfgcrate, "password"); Crate *crate = new Crate(i+1, MCH, cfg_getstr(cfgcrate, "host"), (user[0] ? user : NULL), (pass[0] ? pass : NULL), cfg_getint(cfgcrate, "authtype"), cfg_getstr(cfgcrate, "description")); bool enabled = (cfg_getbool(cfgcrate, "enabled") == cfg_true); if (enabled) crate_enabled = true; threadlocal.push_back(threadlocaldata_t(crate, enabled)); } for(unsigned int i = 0; i < cfg_size(cfg, "cardmodule"); i++) { cfg_t *cfgmodule = cfg_getnsec(cfg, "cardmodule", i); const char *module = cfg_getstr(cfgmodule, "module"); std::vector<std::string> configdata; for(unsigned int i = 0; i < cfg_size(cfgmodule, "config"); i++) configdata.push_back(std::string(cfg_getnstr(cfgmodule, "config", i))); std::string default_module_path = DEFAULT_MODULE_PATH; if (getenv("SYSMGR_MODULE_PATH") != NULL) default_module_path = getenv("SYSMGR_MODULE_PATH"); std::string modulepath = module; if (modulepath.find("/") == std::string::npos) modulepath = default_module_path +"/"+ modulepath; cardmodule_t cm; cm.dl_addr = dlopen(modulepath.c_str(), RTLD_NOW|RTLD_GLOBAL); if (cm.dl_addr == NULL) { printf("Error loading module %s:\n\t%s\n", module, dlerror()); exit(2); } void *sym; #define LOAD_SYM(name, type) \ sym = dlsym(cm.dl_addr, #name); \ if (sym == NULL) { \ mprintf("Error loading module %s " type " " #name ":\n\t%s\n", module, dlerror()); \ exit(2); \ } LOAD_SYM(APIVER, "variable"); cm.APIVER = *reinterpret_cast<uint32_t*>(sym); LOAD_SYM(MIN_APIVER, "variable"); cm.MIN_APIVER = *reinterpret_cast<uint32_t*>(sym); if (cm.APIVER < 2 || cm.MIN_APIVER > 2) { mprintf("Error loading module %s: Incompatible API version %u\n", module, cm.APIVER); } LOAD_SYM(initialize_module, "function"); cm.initialize_module = reinterpret_cast<bool (*)(std::vector<std::string>)>(sym); LOAD_SYM(instantiate_card, "function"); cm.instantiate_card = reinterpret_cast<Card* (*)(Crate*, std::string, void*, uint8_t)>(sym); #undef LOAD_SYM if (!cm.initialize_module(configdata)) { printf("Error loading module %s: initialize_module() returned false\n", module); exit(2); } card_modules.insert(card_modules.begin(), cm); } uint16_t port = cfg_getint(cfg, "socket_port"); config_ratelimit_delay = cfg_getint(cfg, "ratelimit_delay"); bool daemonize = (cfg_getbool(cfg, "daemonize") == cfg_true); cfg_free(cfg); if (!crate_found) { printf("No crate specified in the configuration file.\n"); exit(1); } if (!crate_enabled) { printf("No crates are enabled in the configuration file.\n"); printf("No crates to service.\n"); exit(1); } if (daemonize) { do_fork(); stdout_use_syslog = true; mprintf("University of Wisconsin IPMI MicroTCA System Manager\n"); } /* * Initialize library crypto routines before spawning threads. * This connect will fail due to hostname too long, after running the crypt init functions. * * Max Hostname Limit: 64 */ ipmi_ctx_t dummy_ipmi_ctx = ipmi_ctx_create(); if (ipmi_ctx_open_outofband_2_0(dummy_ipmi_ctx, ".................................................................", // hostname NULL, // username NULL, // password NULL, // k_g 0, // k_g_len, 4, // privilege_level 0, // cipher_suite_id 0, // session_timeout 5, // retransmission_timeout IPMI_WORKAROUND_FLAGS_OUTOFBAND_2_0_OPEN_SESSION_PRIVILEGE, // workaround_flags IPMI_FLAGS_DEFAULT // flags ) == 0) { ipmi_ctx_close(dummy_ipmi_ctx); } ipmi_ctx_destroy(dummy_ipmi_ctx); /* * Instantiate Worker Threads */ for (std::vector<threadlocaldata_t>::iterator it = threadlocal.begin(); it != threadlocal.end(); it++) if (it->enabled) pthread_create(&it->thread, NULL, crate_monitor, (void *)it->crate->get_number()); #ifndef DEBUG_ONESHOT protocol_server(port); #endif for (std::vector<threadlocaldata_t>::iterator it = threadlocal.begin(); it != threadlocal.end(); it++) if (it->enabled) pthread_join(it->thread, NULL); }
int xbps_init(struct xbps_handle *xhp) { cfg_opt_t vpkg_opts[] = { CFG_STR_LIST(__UNCONST("targets"), NULL, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { /* Defaults if not set in configuration file */ CFG_STR(__UNCONST("rootdir"), __UNCONST("/"), CFGF_NONE), CFG_STR(__UNCONST("cachedir"), __UNCONST(XBPS_CACHE_PATH), CFGF_NONE), CFG_INT(__UNCONST("FetchCacheConnections"), XBPS_FETCH_CACHECONN, CFGF_NONE), CFG_INT(__UNCONST("FetchCacheConnectionsPerHost"), XBPS_FETCH_CACHECONN_HOST, CFGF_NONE), CFG_INT(__UNCONST("FetchTimeoutConnection"), XBPS_FETCH_TIMEOUT, CFGF_NONE), CFG_INT(__UNCONST("TransactionFrequencyFlush"), XBPS_TRANS_FLUSH, CFGF_NONE), CFG_BOOL(__UNCONST("syslog"), true, CFGF_NONE), CFG_STR_LIST(__UNCONST("repositories"), NULL, CFGF_MULTI), CFG_STR_LIST(__UNCONST("PackagesOnHold"), NULL, CFGF_MULTI), CFG_SEC(__UNCONST("virtual-package"), vpkg_opts, CFGF_MULTI|CFGF_TITLE), CFG_FUNC(__UNCONST("include"), &cfg_include), CFG_END() }; struct utsname un; int rv, cc, cch; bool syslog_enabled = false; assert(xhp != NULL); if (xhp->initialized) return 0; if (xhp->conffile == NULL) xhp->conffile = XBPS_CONF_DEF; /* parse configuration file */ xhp->cfg = cfg_init(opts, CFGF_NOCASE); cfg_set_validate_func(xhp->cfg, "virtual-package", &cb_validate_virtual); if ((rv = cfg_parse(xhp->cfg, xhp->conffile)) != CFG_SUCCESS) { if (rv == CFG_FILE_ERROR) { /* * Don't error out if config file not found. * If a default repository is set, use it; otherwise * use defaults (no repos and no virtual packages). */ if (errno != ENOENT) return rv; xhp->conffile = NULL; if (xhp->repository) { char *buf; buf = xbps_xasprintf("repositories = { %s }", xhp->repository); assert(buf); if ((rv = cfg_parse_buf(xhp->cfg, buf)) != 0) return rv; free(buf); } } else if (rv == CFG_PARSE_ERROR) { /* * Parser error from configuration file. */ return ENOTSUP; } } xbps_dbg_printf(xhp, "Configuration file: %s\n", xhp->conffile ? xhp->conffile : "not found"); /* * Respect client setting in struct xbps_handle for {root,cache}dir; * otherwise use values from configuration file or defaults if unset. */ if (xhp->rootdir == NULL) { if (xhp->cfg == NULL) xhp->rootdir = "/"; else xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir"); } if (xhp->cachedir == NULL) { if (xhp->cfg == NULL) xhp->cachedir = XBPS_CACHE_PATH; else xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir"); } if ((xhp->cachedir_priv = set_cachedir(xhp)) == NULL) return ENOMEM; xhp->cachedir = xhp->cachedir_priv; if ((xhp->metadir_priv = set_metadir(xhp)) == NULL) return ENOMEM; xhp->metadir = xhp->metadir_priv; uname(&un); xhp->un_machine = strdup(un.machine); assert(xhp->un_machine); if (xhp->cfg == NULL) { xhp->flags |= XBPS_FLAG_SYSLOG; xhp->fetch_timeout = XBPS_FETCH_TIMEOUT; xhp->transaction_frequency_flush = XBPS_TRANS_FLUSH; cc = XBPS_FETCH_CACHECONN; cch = XBPS_FETCH_CACHECONN_HOST; } else { if (cfg_getbool(xhp->cfg, "syslog")) xhp->flags |= XBPS_FLAG_SYSLOG; xhp->fetch_timeout = cfg_getint(xhp->cfg, "FetchTimeoutConnection"); cc = cfg_getint(xhp->cfg, "FetchCacheConnections"); cch = cfg_getint(xhp->cfg, "FetchCacheConnectionsPerHost"); xhp->transaction_frequency_flush = cfg_getint(xhp->cfg, "TransactionFrequencyFlush"); } if (xhp->flags & XBPS_FLAG_SYSLOG) syslog_enabled = true; xbps_fetch_set_cache_connection(cc, cch); xbps_dbg_printf(xhp, "Rootdir=%s\n", xhp->rootdir); xbps_dbg_printf(xhp, "Metadir=%s\n", xhp->metadir); xbps_dbg_printf(xhp, "Cachedir=%s\n", xhp->cachedir); xbps_dbg_printf(xhp, "FetchTimeout=%u\n", xhp->fetch_timeout); xbps_dbg_printf(xhp, "FetchCacheconn=%u\n", cc); xbps_dbg_printf(xhp, "FetchCacheconnHost=%u\n", cch); xbps_dbg_printf(xhp, "Syslog=%u\n", syslog_enabled); xbps_dbg_printf(xhp, "TransactionFrequencyFlush=%u\n", xhp->transaction_frequency_flush); xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->un_machine); xhp->initialized = true; return 0; }
int main(int argc, char **argv) { unsigned int i; cfg_t *cfg; unsigned n; int ret; cfg_opt_t proxy_opts[] = { CFG_INT("type", 0, CFGF_NONE), CFG_STR("host", 0, CFGF_NONE), CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_END() }; cfg_opt_t bookmark_opts[] = { CFG_STR("machine", 0, CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", 0, CFGF_NONE), CFG_STR("password", 0, CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_SEC("proxy", proxy_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT("backlog", 42, CFGF_NONE), CFG_STR("probe-device", "eth2", CFGF_NONE), CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE), CFG_FUNC("func", &cb_func), CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask), CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}", CFGF_NONE, &cb_verify_ask), CFG_FUNC("include", &cfg_include), CFG_END() }; #ifndef _WIN32 /* for some reason, MS Visual C++ chokes on this (?) */ printf("Using %s\n\n", confuse_copyright); #endif cfg = cfg_init(opts, CFGF_NOCASE); /* set a validating callback function for bookmark sections */ cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark); ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf"); printf("ret == %d\n", ret); if(ret == CFG_FILE_ERROR) { perror("test.conf"); return 1; } else if(ret == CFG_PARSE_ERROR) { fprintf(stderr, "parse error\n"); return 2; } printf("backlog == %ld\n", cfg_getint(cfg, "backlog")); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); cfg_setstr(cfg, "probe-device", "lo"); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); n = cfg_size(cfg, "bookmark"); printf("%d configured bookmarks:\n", n); for(i = 0; i < n; i++) { cfg_t *pxy; cfg_t *bm = cfg_getnsec(cfg, "bookmark", i); printf(" bookmark #%u (%s):\n", i+1, cfg_title(bm)); printf(" machine = %s\n", cfg_getstr(bm, "machine")); printf(" port = %d\n", (int)cfg_getint(bm, "port")); printf(" login = %s\n", cfg_getstr(bm, "login")); printf(" passive-mode = %s\n", cfg_getbool(bm, "passive-mode") ? "true" : "false"); printf(" directory = %s\n", cfg_getstr(bm, "directory")); printf(" password = %s\n", cfg_getstr(bm, "password")); pxy = cfg_getsec(bm, "proxy"); if(pxy) { int j, m; if(cfg_getstr(pxy, "host") == 0) { printf(" no proxy host is set, setting it to 'localhost'...\n"); /* For sections without CFGF_MULTI flag set, there is * also an extended syntax to get an option in a * subsection: */ cfg_setstr(bm, "proxy|host", "localhost"); } printf(" proxy host is %s\n", cfg_getstr(pxy, "host")); printf(" proxy type is %ld\n", cfg_getint(pxy, "type")); printf(" proxy port is %ld\n", cfg_getint(pxy, "port")); m = cfg_size(pxy, "exclude"); printf(" got %d hosts to exclude from proxying:\n", m); for(j = 0; j < m; j++) { printf(" exclude %s\n", cfg_getnstr(pxy, "exclude", j)); } } else printf(" no proxy settings configured\n"); } printf("delays are (%d):\n", cfg_size(cfg, "delays")); for(i = 0; i < cfg_size(cfg, "delays"); i++) printf(" %G\n", cfg_getnfloat(cfg, "delays", i)); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); /* Using cfg_setint(), the integer value for the option ask-quit * is not verified by the value parsing callback. * * cfg_setint(cfg, "ask-quit", 4); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); */ /* The following commented line will generate a failed assertion * and abort, since the option "foo" is not declared * * printf("foo == %ld\n", cfg_getint(cfg, "foo")); */ cfg_addlist(cfg, "ask-quit-array", 2, 1, 2); for(i = 0; i < cfg_size(cfg, "ask-quit-array"); i++) printf("ask-quit-array[%d] == %ld\n", i, cfg_getnint(cfg, "ask-quit-array", i)); /* print the parsed values to another file */ { FILE *fp = fopen("test.conf.out", "w"); cfg_set_print_func(cfg, "func", print_func); cfg_set_print_func(cfg, "ask-quit", print_ask); cfg_set_print_func(cfg, "ask-quit-array", print_ask); cfg_print(cfg, fp); fclose(fp); } cfg_free(cfg); return 0; }