int map_get_int(jk_map_t *m, const char *name, int def) { char buf[100]; char *rc; int len; int int_res; int multit = 1; sprintf(buf, "%d", def); rc = map_get_string(m, name, buf); len = strlen(rc); if(len) { char *lastchar = rc + len - 1; if('m' == *lastchar || 'M' == *lastchar) { *lastchar = '\0'; multit = 1024 * 1024; } else if('k' == *lastchar || 'K' == *lastchar) { *lastchar = '\0'; multit = 1024; } } int_res = atoi(rc); return int_res * multit; }
double map_get_double(jk_map_t *m, const char *name, double def) { char buf[100]; char *rc; sprintf(buf, "%f", def); rc = map_get_string(m, name, buf); return atof(rc); }
static int corosync_main_config_log_destination_set ( const char *path, const char *key, const char *subsys, const char **error_string, unsigned int mode_mask, char deprecated, const char *replacement) { static char formatted_error_reason[128]; char *value = NULL; unsigned int mode; char key_name[MAP_KEYNAME_MAXLEN]; snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, key); if (map_get_string(key_name, &value) == CS_OK) { if (deprecated) { log_printf(LOGSYS_LEVEL_WARNING, "Warning: the %s config paramater has been obsoleted." " See corosync.conf man page %s directive.", key, replacement); } mode = logsys_config_mode_get (subsys); if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) { mode |= mode_mask; if (logsys_config_mode_set(subsys, mode) < 0) { sprintf (formatted_error_reason, "unable to set mode %s", key); goto parse_error; } } else if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) { mode &= ~mode_mask; if (logsys_config_mode_set(subsys, mode) < 0) { sprintf (formatted_error_reason, "unable to unset mode %s", key); goto parse_error; } } else { sprintf (formatted_error_reason, "unknown value for %s", key); goto parse_error; } } free(value); return (0); parse_error: *error_string = formatted_error_reason; free(value); return (-1); }
/** * Replace $(property) in value. * */ char *map_replace_properties(const char *value, jk_map_t *m) { char *rc = (char *)value; char *env_start = rc; int rec = 0; while(env_start = strstr(env_start, "$(")) { char *env_end = strstr(env_start, ")"); if( rec++ > 20 ) return rc; if(env_end) { char env_name[LENGTH_OF_LINE + 1] = ""; char *env_value; *env_end = '\0'; strcpy(env_name, env_start + 2); *env_end = ')'; env_value = map_get_string(m, env_name, NULL); if(!env_value) { env_value=getenv( env_name ); } if(env_value) { int offset=0; char *new_value = jk_pool_alloc(&m->p, (sizeof(char) * (strlen(rc) + strlen(env_value)))); if(!new_value) { break; } *env_start = '\0'; strcpy(new_value, rc); strcat(new_value, env_value); strcat(new_value, env_end + 1); offset= env_start - rc + strlen( env_value ); rc = new_value; /* Avoid recursive subst */ env_start = rc + offset; } else { env_start = env_end; } } else { break; } } return rc; }
static int corosync_main_config_set ( const char *path, const char *subsys, const char **error_string) { const char *error_reason = error_string_response; char *value = NULL; int mode; char key_name[MAP_KEYNAME_MAXLEN]; /* * this bit abuses the internal logsys exported API * to guarantee that all configured subsystems are * initialized too. * * using this approach avoids some headaches caused * by IPC and TOTEM that have a special logging * handling requirements */ if (subsys != NULL) { if (_logsys_subsys_create(subsys, NULL) < 0) { error_reason = "unable to create new logging subsystem"; goto parse_error; } } mode = logsys_config_mode_get(subsys); if (mode < 0) { error_reason = "unable to get mode"; goto parse_error; } if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason, LOGSYS_MODE_OUTPUT_STDERR, 0, 1, NULL) != 0) goto parse_error; if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason, LOGSYS_MODE_OUTPUT_SYSLOG, 0, 1, NULL) != 0) goto parse_error; snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility"); if (map_get_string(key_name, &value) == CS_OK) { int syslog_facility; syslog_facility = qb_log_facility2int(value); if (syslog_facility < 0) { error_reason = "unknown syslog facility specified"; goto parse_error; } if (logsys_config_syslog_facility_set(subsys, syslog_facility) < 0) { error_reason = "unable to set syslog facility"; goto parse_error; } free(value); } else { /* Set default here in case of a reload */ if (logsys_config_syslog_facility_set(subsys, qb_log_facility2int("daemon")) < 0) { error_reason = "unable to set syslog facility"; goto parse_error; } } snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level"); if (map_get_string(key_name, &value) == CS_OK) { int syslog_priority; log_printf(LOGSYS_LEVEL_WARNING, "Warning: the syslog_level config parameter has been obsoleted." " See corosync.conf man page syslog_priority directive."); syslog_priority = logsys_priority_id_get(value); free(value); if (syslog_priority < 0) { error_reason = "unknown syslog level specified"; goto parse_error; } if (logsys_config_syslog_priority_set(subsys, syslog_priority) < 0) { error_reason = "unable to set syslog level"; goto parse_error; } } snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority"); if (map_get_string(key_name, &value) == CS_OK) { int syslog_priority; syslog_priority = logsys_priority_id_get(value); free(value); if (syslog_priority < 0) { error_reason = "unknown syslog priority specified"; goto parse_error; } if (logsys_config_syslog_priority_set(subsys, syslog_priority) < 0) { error_reason = "unable to set syslog priority"; goto parse_error; } } else { if (logsys_config_syslog_priority_set(subsys, logsys_priority_id_get("info")) < 0) { error_reason = "unable to set syslog level"; goto parse_error; } } #ifdef LOGCONFIG_USE_ICMAP snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile"); if (map_get_string(key_name, &value) == CS_OK) { if (logsys_config_file_set (subsys, &error_reason, value) < 0) { goto parse_error; } free(value); } #else if (!subsys) { if (logsys_config_file_set (subsys, &error_reason, main_logfile) < 0) { goto parse_error; } } #endif if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason, LOGSYS_MODE_OUTPUT_FILE, 1, 0, "to_logfile") != 0) goto parse_error; if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason, LOGSYS_MODE_OUTPUT_FILE, 0, 0, NULL) != 0) goto parse_error; snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority"); if (map_get_string(key_name, &value) == CS_OK) { int logfile_priority; logfile_priority = logsys_priority_id_get(value); free(value); if (logfile_priority < 0) { error_reason = "unknown logfile priority specified"; goto parse_error; } if (logsys_config_logfile_priority_set(subsys, logfile_priority) < 0) { error_reason = "unable to set logfile priority"; goto parse_error; } } else { if (logsys_config_logfile_priority_set(subsys, logsys_priority_id_get("info")) < 0) { error_reason = "unable to set syslog level"; goto parse_error; } } snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "debug"); if (map_get_string(key_name, &value) == CS_OK) { if (strcmp (value, "trace") == 0) { if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_TRACE) < 0) { error_reason = "unable to set debug trace"; free(value); goto parse_error; } } else if (strcmp (value, "on") == 0) { if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_ON) < 0) { error_reason = "unable to set debug on"; free(value); goto parse_error; } } else if (strcmp (value, "off") == 0) { if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) { error_reason = "unable to set debug off"; free(value); goto parse_error; } } else { error_reason = "unknown value for debug"; free(value); goto parse_error; } free(value); } else { if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) { error_reason = "unable to set debug off"; free(value); goto parse_error; } } return (0); parse_error: *error_string = error_reason; return (-1); }
/* * format set is the only global specific option that * doesn't apply at system/subsystem level. */ static int corosync_main_config_format_set ( const char **error_string) { const char *error_reason; char new_format_buffer[PATH_MAX]; char *value = NULL; int err = 0; if (map_get_string("logging.fileline", &value) == CS_OK) { if (strcmp (value, "on") == 0) { if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), " %f:%l", "g]")) { err = logsys_format_set(new_format_buffer); } else if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%f:%l", NULL)) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for fileline"; free(value); goto parse_error; } free(value); } if (err) { error_reason = "not enough memory to set logging format buffer"; goto parse_error; } if (map_get_string("logging.function_name", &value) == CS_OK) { if (strcmp (value, "on") == 0) { if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%n:", "f:")) { err = logsys_format_set(new_format_buffer); } else if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), " %n", "g]")) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for function_name"; free(value); goto parse_error; } free(value); } if (err) { error_reason = "not enough memory to set logging format buffer"; goto parse_error; } if (map_get_string("logging.timestamp", &value) == CS_OK) { if (strcmp (value, "on") == 0) { if(!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%t ", NULL)) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for timestamp"; free(value); goto parse_error; } free(value); } if (err) { error_reason = "not enough memory to set logging format buffer"; goto parse_error; } return (0); parse_error: *error_string = error_reason; return (-1); }
int map_read_properties(jk_map_t *m, const char *f) { int rc = JK_FALSE; if(m && f) { #ifdef AS400 FILE *fp = fopen(f, "r, o_ccsid=0"); #else FILE *fp = fopen(f, "r"); #endif if(fp) { char buf[LENGTH_OF_LINE + 1]; char *prp; rc = JK_TRUE; while(NULL != (prp = fgets(buf, LENGTH_OF_LINE, fp))) { trim_prp_comment(prp); if(trim(prp)) { char *v = strchr(prp, '='); if(v) { *v = '\0'; v++; if(strlen(v) && strlen(prp)) { char *oldv = map_get_string(m, prp, NULL); v = map_replace_properties(v, m); if(oldv) { char *tmpv = jk_pool_alloc(&m->p, strlen(v) + strlen(oldv) + 3); if(tmpv) { char sep = '*'; if(jk_is_path_poperty(prp)) { sep = PATH_SEPERATOR; } else if(jk_is_cmd_line_poperty(prp)) { sep = ' '; } sprintf(tmpv, "%s%c%s", oldv, sep, v); } v = tmpv; } else { v = jk_pool_strdup(&m->p, v); } if(v) { void *old = NULL; map_put(m, prp, v, &old); } else { rc = JK_FALSE; break; } } } } } fclose(fp); } } return rc; }