lagopus_result_t lagopus_snmp_get_agentx_sock(char **agentx_sock) { if (agentx_sock != NULL && *agentx_sock == NULL) { if (IS_VALID_STRING(conf.agentx_sock) == true) { char *str = NULL; s_rlock_snmp_conf(&conf); { str = strdup(conf.agentx_sock); } s_unlock_snmp_conf(&conf); if (IS_VALID_STRING(str) == true) { *agentx_sock = str; return LAGOPUS_RESULT_OK; } else { free((void *)str); return LAGOPUS_RESULT_NO_MEMORY; } } else { return LAGOPUS_RESULT_NOT_DEFINED; } } return LAGOPUS_RESULT_INVALID_ARGS; }
mccp_result_t mccp_str_trim_right(const char *org, const char *trimchars, char **retptr) { mccp_result_t n = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(org) == true && IS_VALID_STRING(trimchars) == true && retptr != NULL) { char *buf = strdup(org); char *ed = buf + strlen(buf) - 1; *retptr = NULL; while (ed >= buf) { if (strchr(trimchars, (int)*ed) != NULL) { *ed = '\0'; ed--; } else { break; } } *retptr = buf; n = (mccp_result_t)strlen(buf); } else { n = MCCP_RESULT_INVALID_ARGS; } return n; }
static inline bool s_log_init(mccp_log_destination_t dst, const char *arg, bool multi_process, bool emit_date, uint64_t debug_level, uint64_t trace_flags) { bool ret = false; if (s_is_fork_initialized == false) { (void)pthread_atfork(NULL, NULL, s_child_at_fork); s_is_fork_initialized = true; } if (dst == MCCP_LOG_EMIT_TO_FILE || dst == MCCP_LOG_EMIT_TO_UNKNOWN) { int ofd = -INT_MAX; s_log_final(); if (IS_VALID_STRING(arg) == true && dst == MCCP_LOG_EMIT_TO_FILE && s_is_file(arg) == true) { ofd = open(arg, O_RDWR | O_CREAT | O_APPEND, 0600); if (ofd >= 0) { s_log_fd = fdopen(ofd, "a+"); if (s_log_fd != NULL) { ret = true; } } } else { s_log_fd = NULL; /* use stderr. */ ret = true; } } else if (dst == MCCP_LOG_EMIT_TO_SYSLOG) { if (IS_VALID_STRING(arg) == true) { s_log_final(); openlog(arg, 0, LOG_USER); ret = true; } } if (ret == true) { s_log_dst = dst; free((void *)s_log_arg); s_log_arg = (IS_VALID_STRING(arg) == true) ? strdup(arg) : NULL; s_do_multi_process = multi_process; s_do_date = emit_date; s_dbg_level = debug_level; s_trace_flags = trace_flags; } return ret; }
static inline bool s_parse_bigint_by_base(const char *str, MP_INT *valptr, unsigned int base) { bool ret = false; bool is_neg = false; skip_spaces(str); if (IS_VALID_STRING(str) == true) { switch ((int)str[0]) { case '-': { is_neg = true; str++; break; } case '+': { str++; break; } } } skip_spaces(str); if (IS_VALID_STRING(str) == true) { char *buf = strdup(str); if (buf != NULL) { size_t len; if ((len = strlen(buf)) > 0) { const MP_INT *mul = NULL; char *endp = &(buf[len]); trim_spaces(buf, endp); mul = s_get_prefix_multiplier(buf); if (mul != NULL) { MP_INT tmp; if (mpz_init_set_str(&tmp, buf, (int)base) == 0) { mpz_mul(valptr, &tmp, mul); if (is_neg == true) { mpz_neg(valptr, valptr); } ret = true; } mpz_clear(&tmp); } } free((void *)buf); } } return ret; }
mccp_result_t mccp_str_tokenize(char *buf, char **tokens, size_t max, const char *delm) { mccp_result_t n = 0; size_t no_delm = 0; if (IS_VALID_STRING(buf) == true && IS_VALID_STRING(delm) == true) { while (*buf != '\0' && (size_t)n < max) { while (strchr(delm, (int)*buf) != NULL && *buf != '\0') { /* * Increent the pointer while *buf is a delimiter. */ buf++; } if (*buf == '\0') { break; } tokens[n] = buf; no_delm = 0; while (strchr(delm, (int)*buf) == NULL && *buf != '\0') { no_delm++; buf++; } if (*buf == '\0') { if (no_delm > 0) { n++; } break; } *buf = '\0'; n++; if (*(buf + 1) == '\0') { break; } else { buf++; } } } else { n = MCCP_RESULT_INVALID_ARGS; } return n; }
static lagopus_result_t tmp_dir_opt_parse(const char *const *argv[], void *c, void *out_configs, lagopus_dstring_t *result) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; char *path = NULL; (void) out_configs; (void) c; if (argv != NULL && out_configs != NULL && result != NULL) { if (*(*argv + 1) != NULL) { (*argv)++; if (IS_VALID_STRING(*(*argv)) == true) { ret = flow_cmd_dump_tmp_dir_set(*(*argv)); if (ret != LAGOPUS_RESULT_OK) { ret = datastore_json_result_string_setf(result, ret, "Bad opt value = %s.", *(*argv)); } } else { ret = datastore_json_result_string_setf(result, ret, "Bad opt value = %s.", *(*argv)); } } else { if ((ret = flow_cmd_dump_tmp_dir_get(&path)) == LAGOPUS_RESULT_OK) { if ((ret = lagopus_dstring_appendf(result, "{")) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); goto done; } if ((ret = datastore_json_string_append( result, ATTR_NAME_GET(opt_strs, OPT_TMP_DIR), path, false)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); goto done; } if ((ret = lagopus_dstring_appendf(result, "}")) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); goto done; } } else { ret = datastore_json_result_string_setf(result, ret, "Can't get tmp dir."); } } } else { ret = datastore_json_result_set(result, LAGOPUS_RESULT_INVALID_ARGS, NULL); } done: free(path); return ret; }
static void s_once_proc(void) { char *dbg_lvl_str = getenv("LAGOPUS_LOG_DEBUGLEVEL"); char *logfile = getenv("LAGOPUS_LOG_FILE"); uint16_t d = 0; lagopus_log_destination_t log_dst = LAGOPUS_LOG_EMIT_TO_UNKNOWN; if (IS_VALID_STRING(dbg_lvl_str) == true) { uint16_t tmp = 0; if (lagopus_str_parse_uint16(dbg_lvl_str, &tmp) == LAGOPUS_RESULT_OK) { d = tmp; } } if ((logfile = s_validate_path(logfile, s_is_valid_path)) != NULL) { log_dst = LAGOPUS_LOG_EMIT_TO_FILE; } if (lagopus_log_initialize(log_dst, logfile, false, true, d) != LAGOPUS_RESULT_OK) { lagopus_exit_error(1, "logger initialization error.\n"); } if (d > 0) { lagopus_msg_debug(d, "Logger debug level is set to: %d.\n", d); } }
mccp_result_t mccp_str_parse_double(const char *buf, double *val) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL) { size_t len = strlen(buf); if (len > 0) { char *eptr = NULL; double tmp = strtod(buf, &eptr); if (eptr == (buf + len)) { ret = MCCP_RESULT_OK; *val = tmp; } else { ret = MCCP_RESULT_NAN; } } else { ret = MCCP_RESULT_INVALID_ARGS; } } else { ret = MCCP_RESULT_INVALID_ARGS; } return ret; }
mccp_result_t mccp_str_parse_uint16(const char *buf, uint16_t *val) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL) { MP_INT v; mpz_init(&v); if ((ret = s_parse_bigint_in_range(buf, &v, &s_0, &s_USHRT_MAX)) == MCCP_RESULT_OK) { #ifdef MCCP_ARCH_64_BITS uint64_t v64 = (uint64_t)mpz_get_ui(&v); *val = (uint16_t)v64; #else uint64_t v64 = s_bignum_2_uint64_t(&v); *val = (uint16_t)v64; #endif /* MCCP_ARCH_64_BITS */ } mpz_clear(&v); } return ret; }
static void s_once_proc(void) { char *dbg_lvl_str = getenv("LAGOPUS_LOG_DEBUGLEVEL"); char *logfile = getenv("LAGOPUS_LOG_FILE"); uint16_t d = 0; lagopus_log_destination_t log_dst = LAGOPUS_LOG_EMIT_TO_UNKNOWN; if (IS_VALID_STRING(dbg_lvl_str) == true) { uint16_t tmp = 0; if (lagopus_str_parse_uint16(dbg_lvl_str, &tmp) == LAGOPUS_RESULT_OK) { d = tmp; } } if ((logfile = s_validate_path(logfile, s_is_valid_path)) != NULL) { log_dst = LAGOPUS_LOG_EMIT_TO_FILE; } if (lagopus_log_initialize(log_dst, logfile, false, true, d) != LAGOPUS_RESULT_OK) { lagopus_exit_error(1, "logger initialization error.\n"); } if (d > 0) { lagopus_msg_debug(d, "Logger debug level is set to: %d.\n", d); } #ifdef HAVE_PROCFS_SELF_EXE if (readlink("/proc/self/exe", s_exefile, PATH_MAX) != -1) { (void)lagopus_set_command_name(s_exefile); lagopus_msg_debug(10, "set the command name '%s'.\n", lagopus_get_command_name()); } #endif /* HAVE_PROCFS_SELF_EXE */ }
mccp_result_t mccp_str_parse_bool(const char *buf, bool *val) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL) { if (strcasecmp(buf, "true") == 0 || strcasecmp(buf, "yes") == 0 || strcasecmp(buf, "on") == 0 || strcmp(buf, "1") == 0) { *val = true; ret = MCCP_RESULT_OK; } else if (strcasecmp(buf, "false") == 0 || strcasecmp(buf, "no") == 0 || strcasecmp(buf, "off") == 0 || strcmp(buf, "0") == 0) { *val = false; ret = MCCP_RESULT_OK; } else { ret = MCCP_RESULT_INVALID_ARGS; } } else { ret = MCCP_RESULT_INVALID_ARGS; } return ret; }
static inline lagopus_result_t s_opt_parse_port(datastore_interp_state_t state, const char *const argv[], lagopus_dstring_t *result) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; uint16_t tmp = 0; if (IS_VALID_STRING(*argv) == true) { if ((ret = lagopus_str_parse_uint16(*argv, &tmp)) == LAGOPUS_RESULT_OK) { if (state != DATASTORE_INTERP_STATE_DRYRUN) { s_port = tmp; } ret = LAGOPUS_RESULT_OK; } else { ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_INVALID_ARGS, "\"can't parse '%s' as a " "uint16_t integer.\"", *argv); } } else { ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_INVALID_ARGS, "Bad opt value = %s", *argv); } return ret; }
static inline lagopus_result_t s_opt_parse_tls(datastore_interp_state_t state, const char *const argv[], lagopus_dstring_t *result) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; bool tls = false; if (IS_VALID_STRING(*argv) == true) { if (state == DATASTORE_INTERP_STATE_DRYRUN) { ret = LAGOPUS_RESULT_OK; } else { ret = lagopus_str_parse_bool(*argv, &tls); if (ret == LAGOPUS_RESULT_OK) { s_set_tls(tls); } else { ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_INVALID_ARGS, "Bad opt value = %s", *argv); } } } else { ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_INVALID_ARGS, "Bad opt value = %s", *argv); } return ret; }
static inline gallus_result_t s_find_module(const char *name, a_module **retmptr) { gallus_result_t ret = GALLUS_RESULT_ANY_FAILURES; if (IS_VALID_STRING(name) == true) { ret = GALLUS_RESULT_NOT_FOUND; if (retmptr != NULL) { *retmptr = NULL; } if (s_n_modules > 0) { a_module *mptr; size_t i; for (i = 0; i < s_n_modules; i++) { mptr = &(s_modules[i]); if (strcmp(mptr->m_name, name) == 0) { if (retmptr != NULL) { *retmptr = mptr; return (gallus_result_t)i; } } } } } else { ret = GALLUS_RESULT_INVALID_ARGS; } return ret; }
static void s_once_proc(void) { char *dbg_lvl_str = getenv("MCCP_LOG_DEBUGLEVEL"); char *logfile = getenv("MCCP_LOG_FILE"); uint64_t d = 0; mccp_log_destination_t log_dst = MCCP_LOG_EMIT_TO_UNKNOWN; if (IS_VALID_STRING(dbg_lvl_str) == true) { uint64_t tmp = 0; if (mccp_str_parse_uint64(dbg_lvl_str, &tmp) == MCCP_RESULT_OK) { d = tmp; } } if ((logfile = s_validate_path(logfile, s_is_valid_path)) != NULL) { log_dst = MCCP_LOG_EMIT_TO_FILE; } if (mccp_log_initialize(log_dst, logfile, false, true, d, 0LL) != MCCP_RESULT_OK) { mccp_exit_error(1, "logger initialization error.\n"); } if (d > 0) { mccp_msg_debug(d, "Logger debug level is set to: " PF64(u) ".\n", d); } }
mccp_result_t mccp_str_parse_int64(const char *buf, int64_t *val) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL) { MP_INT v; mpz_init(&v); if ((ret = s_parse_bigint_in_range(buf, &v, &s_LLONG_MIN, &s_LLONG_MAX)) == MCCP_RESULT_OK) { #ifdef MCCP_ARCH_64_BITS *val = (int64_t)mpz_get_si(&v); #else *val = (int64_t)s_bignum_2_uint64_t(&v); #endif /* MCCP_ARCH_64_BITS */ } mpz_clear(&v); } return ret; }
static lagopus_result_t s_parse_snmp(datastore_interp_t *iptr, datastore_interp_state_t state, size_t argc, const char *const argv[], lagopus_hashmap_t *hptr, datastore_update_proc_t u_proc, datastore_enable_proc_t e_proc, datastore_serialize_proc_t s_proc, datastore_destroy_proc_t d_proc, lagopus_dstring_t *result) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; size_t i; (void)iptr; (void)state; (void)argc; (void)hptr; (void)u_proc; (void)e_proc; (void)s_proc; (void)d_proc; for (i = 0; i < argc; i++) { lagopus_msg_debug(1, "argv[" PFSZS(4, u) "]:\t'%s'\n", i, argv[i]); } argv++; if (IS_VALID_STRING(*argv) == false) { ret = s_parse_snmp_show(result); } else { ret = s_parse_snmp_internal(argv, result); } return ret; }
mccp_result_t mccp_str_parse_int16_by_base(const char *buf, int16_t *val, unsigned int base) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL && base > 1) { MP_INT v; mpz_init(&v); if ((ret = s_parse_bigint_by_base_in_range(buf, &v, base, &s_SHRT_MIN, &s_SHRT_MAX)) == MCCP_RESULT_OK) { #ifdef MCCP_ARCH_64_BITS int64_t v64 = (int64_t)mpz_get_si(&v); *val = (int16_t)v64; #else int64_t v64 = (int64_t)s_bignum_2_uint64_t(&v); *val = (int16_t)v64; #endif /* MCCP_ARCH_64_BITS */ } mpz_clear(&v); } return ret; }
static void parse_args(int argc, const char *const argv[]) { (void)argc; while (*argv != NULL) { if (strcmp("--help", *argv) == 0 || strcmp("-?", *argv) == 0) { usage(stderr); exit(0); } else if (strcmp("-to", *argv) == 0) { if (IS_VALID_STRING(*(argv + 1)) == true) { int64_t tmp; argv++; if (lagopus_str_parse_int64(*argv, &tmp) == LAGOPUS_RESULT_OK) { if (tmp >= 0) { s_to = ONE_SEC * tmp; } } else { fprintf(stderr, "can't parse \"%s\" as a number.\n", *argv); exit(1); } } else { fprintf(stderr, "A timeout # is not specified.\n"); exit(1); } } argv++; } }
static inline bool s_log_reinit(void) { const char *arg = (IS_VALID_STRING(s_log_arg) == true) ? s_log_arg : NULL; bool ret = s_log_init(s_log_dst, arg, s_do_multi_process, s_do_date, s_dbg_level); return ret; }
int main(int argc, const char *const argv[]) { lagopus_result_t r = LAGOPUS_RESULT_ANY_FAILURES; const char *file = argv[1]; (void)argc; if ((r = datastore_register_configurator(MY_CONF_NAME)) == LAGOPUS_RESULT_OK) { lagopus_dstring_t ds; char *str = NULL; (void)lagopus_dstring_create(&ds); if (IS_VALID_STRING(file) == true) { r = datastore_interp_eval_file(&s_interp, MY_CONF_NAME, file, &ds); } else { while ((r = datastore_interp_eval_fd(&s_interp, MY_CONF_NAME, stdin, stdout, false, &ds)) == LAGOPUS_RESULT_OK) { lagopus_dstring_clear(&ds); } } if (r != LAGOPUS_RESULT_OK) { lagopus_perror(r); } (void)lagopus_dstring_str_get(&ds, &str); if (IS_VALID_STRING(str) == true) { fprintf(stdout, "%s", str); } free((void *)str); } else { lagopus_perror(r); } datastore_unregister_configurator(MY_CONF_NAME); return (r == LAGOPUS_RESULT_OK) ? 0 : 1; }
static inline testobj_config_t * s_config_create(const char *name) { testobj_config_t *ret = NULL; if (IS_VALID_STRING(name) == true) { char *cname = strdup(name); if (IS_VALID_STRING(cname) == true) { ret = (testobj_config_t *)malloc(sizeof(testobj_config_t)); if (ret != NULL) { (void)memset((void *)ret, 0, sizeof(testobj_config_t)); ret->m_name = cname; } else { free((void *)cname); } } } return ret; }
static inline void s_attr_dump(lagopus_dstring_t *ds, testobj_attr_t *o) { if (o != NULL) { (void)lagopus_dstring_appendf(ds, "{\"intval\": %u, \"strval\": \"%s\"}", o->m_intval, (IS_VALID_STRING(o->m_strval) == true) ? o->m_strval : ""); } }
static inline void s_delete_stage(lagopus_pipeline_stage_t ps) { if (ps != NULL) { (void)lagopus_hashmap_delete(&s_ps_obj_tbl, (void *)ps, NULL, true); if (IS_VALID_STRING(ps->m_name) == true) { (void)lagopus_hashmap_delete(&s_ps_name_tbl, (void *)(ps->m_name), NULL, true); } } }
static inline bool s_log_reinit(void) { char *arg = (IS_VALID_STRING(s_log_arg) == true) ? strdup(s_log_arg) : NULL; bool ret = s_log_init(s_log_dst, arg, s_do_multi_process, s_do_date, s_dbg_level, s_trace_flags); free((void *)arg); return ret; }
static inline lagopus_result_t s_attr_set_strval(testobj_attr_t *o, const char *val) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if (o != NULL && IS_VALID_STRING(val) == true) { char *c = strdup(val); if (IS_VALID_STRING(c) == true) { if (o->m_strval != NULL) { free((void *)(o->m_strval)); } o->m_strval = c; ret = LAGOPUS_RESULT_OK; } else { ret = LAGOPUS_RESULT_NO_MEMORY; } } else { ret = LAGOPUS_RESULT_INVALID_ARGS; } return ret; }
static inline void s_del_pidfile(void) { if (IS_VALID_STRING(s_pidfile_buf) == true) { struct stat st; if (stat(s_pidfile_buf, &st) == 0) { if (S_ISREG(st.st_mode)) { (void)unlink(s_pidfile_buf); } } } }
static inline lagopus_result_t queue_conf_delete(queue_conf_t *conf) { if (queue_table != NULL) { if (conf != NULL && IS_VALID_STRING(conf->name) == true) { return lagopus_hashmap_delete(&queue_table, (void *) conf->name, NULL, true); } else { return LAGOPUS_RESULT_INVALID_ARGS; } } else { return LAGOPUS_RESULT_NOT_STARTED; } }
static inline lagopus_result_t s_config_add(testobj_config_t *o) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if (o != NULL && IS_VALID_STRING(o->m_name) == true) { void *val = (void *)o; ret = lagopus_hashmap_add(&s_tbl, (void *)(o->m_name), &val, false); } else { ret = LAGOPUS_RESULT_INVALID_ARGS; } return ret; }
static inline lagopus_pipeline_stage_t s_find_stage(const char *name) { lagopus_pipeline_stage_t ret = NULL; if (IS_VALID_STRING(name) == true) { void *val; if (lagopus_hashmap_find(&s_ps_name_tbl, (void *)name, &val) == LAGOPUS_RESULT_OK) { ret = (lagopus_pipeline_stage_t)val; } } return ret; }