示例#1
0
static void net_async_poll_timeout(void *arg) {
    static const char *thisfunc = "net_async_poll_timeout():";
    am_net_t *n = (am_net_t *) arg;
    AM_LOG_WARNING(n->instance_id,
            "%s timeout waiting for a response from a server", thisfunc);
    n->error = AM_ETIMEDOUT;
    if (n->on_close) n->on_close(n->data, 0);
    am_net_diconnect(n);
}
示例#2
0
/**
 * Ensure that at warning log level we DO log something via AM_LOG_WARNING.
 */
void test_log_warning_at_warning_level(void** state) {
    int result;
    const char* message = "Message written at WARNING level.";

    logging_setup(AM_LOG_LEVEL_WARNING);
    AM_LOG_WARNING(getpid(), message);
    sleep(5);
    result = validate_contains(log_file_name, message);
    logging_teardown();
    assert_int_equal(result, 1);
}
static void am_url_validator_tick(void *arg) {
    static const char *thisfunc = "am_url_validator_tick():";
    int i;
    struct url_validator_worker_data *list;

    list = (struct url_validator_worker_data *) calloc(1,
            sizeof (struct url_validator_worker_data) * AM_MAX_INSTANCES);
    if (list == NULL || get_valid_url_all(list) != AM_SUCCESS) {
        am_free(list);
        return;
    }

    for (i = 0; i < AM_MAX_INSTANCES; i++) {
        /* schedule a new url_validator_worker only when instance is registered and validator is not running already */
        if (list[i].instance_id > 0 && !list[i].running) {
            struct url_validator_worker_data *worker_data =
                    (struct url_validator_worker_data *) malloc(sizeof (struct url_validator_worker_data));
            if (worker_data != NULL) {
                worker_data->instance_id = list[i].instance_id;
                worker_data->url_index = list[i].url_index;
                worker_data->last = list[i].last;
                worker_data->config_path = strdup(list[i].config_path);
                if (am_worker_dispatch(url_validator_worker, worker_data) != AM_SUCCESS) {
                    AM_LOG_WARNING(list[i].instance_id, "%s failed to dispatch url validator worker", thisfunc);
                    AM_FREE(worker_data->config_path, worker_data);
                }
            } else {
                AM_LOG_ERROR(list[i].instance_id, "%s memory allocation error", thisfunc);
            }
        } else if (list[i].instance_id > 0 && list[i].running) {
            AM_LOG_WARNING(list[i].instance_id, "%s url validator worker already running", thisfunc);
        }
        am_free(list[i].config_path);
    }
    free(list);
}
示例#4
0
/**
 * This is the simplest of tests to check we can log things without crashing.
 *
 * In fact, because of the way logging works (differently) in test mode than it does in "agent mode"
 * all we're really doing here is to test that logging in test mode isn't broken.  This may or may not
 * bear any relation to whether logging works for the rest of the time.
 */
void test_logging_in_unit_test_mode(void** state) {
    
    static const char* text1 = "Now is the winter of our discontent,";
    static const char* text2 = "Made glorious summer by this son of York";
        
    AM_LOG_INFO(0, "instance id is zero and no args");
    AM_LOG_INFO(0, "instance id is zero and incorrect args", text1);
    AM_LOG_INFO(0, "instance id is zero and more incorrect args", text1, text2);

    /* we're testing this will not crash */
    AM_LOG_INFO(0, NULL, text1, text2);

    /* this will not appear, since the instance is greater than zero, but it should not crash either */
    AM_LOG_ERROR(10, "%s %s", text1, text2);

    AM_LOG_INFO(0, "%s %s", text1, text2);
    AM_LOG_WARNING(0, "%s %s", text1, text2);
    AM_LOG_ERROR(0, "%s %s", text1, text2);
    AM_LOG_DEBUG(0, "%s %s", text1, text2);
    AM_LOG_AUDIT(0, "%s %s", text1, text2);

    AM_LOG_ALWAYS(0, "%s %s", text1, text2);
    AM_LOG_ALWAYS(0, "Now %s the %s of our %s, %s summ%s of York",
                  "is",
                  "winter",
                  "discontent",
                  "Made glorious",
                  "er by this son");

    /* attempt to overflow the buffer, although this will be ultimately unsuccessful because the
     * logging works differently in unit test mode than it does in "real life" mode.
     */
    AM_LOG_ALWAYS(0, "\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
                     "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ");
}
static void parse_config_value(unsigned long instance_id, const char *line, const char *prm, int type,
        int *itm_sz, void *itm, const char *sep) {

    if (itm == NULL || compare_property(line, prm) != AM_SUCCESS) {
        return;
    }

    switch (type) {
        case CONF_STRING:
        {
            char **value = (char **) itm;
            *value = (char *) parse_value(line, prm, type, NULL);
            if (strstr(prm, "password") != NULL) {
                AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to '%s'",
                        prm, *value == NULL ? "NULL" : "********");
                break;
            }
            AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to '%s'",
                    prm, *value == NULL ? "NULL" : *value);
        }
            break;
        case CONF_NUMBER:
        case CONF_DEBUG_LEVEL:
        case CONF_ATTR_MODE:
        case CONF_AUDIT_LEVEL:
        {
            int *value = (int *) itm;
            int *value_tmp = (int *) parse_value(line, prm, type, NULL);
            if (value_tmp != NULL) {
                *value = *value_tmp;
                free(value_tmp);
            }
            AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to '%d'", prm, *value);
        }
            break;
        case CONF_STRING_LIST:
        {
            char ***value = (char ***) itm;
            struct val_string_list *value_tmp = (struct val_string_list *) parse_value(line, prm, type, sep);
            if (value_tmp != NULL) {
                *value = value_tmp->list;
                *itm_sz = value_tmp->size;
                free(value_tmp);
            }
            AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to %d value(s)", prm, *itm_sz);
        }
            break;
        case CONF_NUMBER_LIST:
        {
            int **value = (int **) itm;
            struct val_number_list *value_tmp = (struct val_number_list *) parse_value(line, prm, type, sep);
            if (value_tmp != NULL) {
                *value = value_tmp->list;
                *itm_sz = value_tmp->size;
                free(value_tmp);
            }
            AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to %d value(s)", prm, *itm_sz);
        }
            break;
        case CONF_STRING_MAP:
        {
            int old_sz = *itm_sz;
            am_config_map_t **value = (am_config_map_t **) itm;
            char *value_tmp = (char *) parse_value(line, prm, CONF_STRING, NULL);
            if (value_tmp == NULL) {
                break;
            }
            *value = (am_config_map_t *) realloc(*value, sizeof (am_config_map_t) *(++(*itm_sz)));
            if (*value == NULL) {
                if (--(*itm_sz) < 0) {
                    *itm_sz = 0;
                }
                free(value_tmp);
                break;
            }
            (&(*value)[old_sz])->name = value_tmp;
            (&(*value)[old_sz])->value = value_tmp + strlen(value_tmp) + 1;
            AM_LOG_DEBUG(instance_id, "am_get_config_file() %s is set to %d value(s)", prm, *itm_sz);
        }
            break;
        default:
            AM_LOG_WARNING(instance_id, "am_get_config_file() unknown type value %d setting %s", type, prm);
            break;
    }
}
示例#6
0
static void *net_async_connect(void *arg) {
    am_net_t *n = (am_net_t *) arg;
    static const char *thisfunc = "net_async_connect():";
    struct in6_addr serveraddr;
    struct addrinfo *rp, hints;
    int err = 0, on = 1;
    char port[7];
    am_timer_t tmr;

    memset(&hints, 0, sizeof (struct addrinfo));
    hints.ai_flags = AI_NUMERICSERV;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    err = INETPTON(AF_INET, n->uv.host, &serveraddr);
    if (err == 1) {
        hints.ai_family = AF_INET;
        hints.ai_flags |= AI_NUMERICHOST;
    } else {
        err = INETPTON(AF_INET6, n->uv.host, &serveraddr);
        if (err == 1) {
            hints.ai_family = AF_INET6;
            hints.ai_flags |= AI_NUMERICHOST;
        }
    }

    snprintf(port, sizeof (port), "%d", n->uv.port);

    am_timer_start(&tmr);
    if ((err = getaddrinfo(n->uv.host, port, &hints, &n->ra)) != 0) {
        n->error = AM_EHOSTUNREACH;
        am_timer_stop(&tmr);
        am_timer_report(n->instance_id, &tmr, "getaddrinfo");
        set_event(n->ce);
        return NULL;
    }

    am_timer_stop(&tmr);
    am_timer_report(n->instance_id, &tmr, "getaddrinfo");

    n->error = 0;

    for (rp = n->ra; rp != NULL; rp = rp->ai_next) {

        if (rp->ai_family != AF_INET && rp->ai_family != AF_INET6 &&
                rp->ai_socktype != SOCK_STREAM && rp->ai_protocol != IPPROTO_TCP) continue;

        if ((n->sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == INVALID_SOCKET) {
            AM_LOG_ERROR(n->instance_id,
                    "%s: cannot create socket while connecting to %s:%d",
                    thisfunc, n->uv.host, n->uv.port);
            net_log_error(n->instance_id, net_error());
            continue;
        }

        if (setsockopt(n->sock, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
        if (setsockopt(n->sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
#ifdef SO_NOSIGPIPE
        if (setsockopt(n->sock, SOL_SOCKET, SO_NOSIGPIPE, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
#endif
        if (set_nonblocking(n, 1) != 0) {
            n->error = AM_EPERM;
            continue;
        }

        err = connect(n->sock, rp->ai_addr, (SOCKLEN_T) rp->ai_addrlen);
        if (err == 0) {
            AM_LOG_DEBUG(n->instance_id, "%s: connected to %s:%d (%s)",
                    thisfunc, n->uv.host, n->uv.port,
                    rp->ai_family == AF_INET ? "IPv4" : "IPv6");
            n->error = 0;
            if (n->uv.ssl) {
                net_connect_ssl(n);
                if (n->ssl.error != AM_SUCCESS) {
                    AM_LOG_ERROR(n->instance_id,
                            "%s: SSL/TLS connection to %s:%d (%s) failed (%s)",
                            thisfunc, n->uv.host, n->uv.port,
                            rp->ai_family == AF_INET ? "IPv4" : "IPv6",
                            am_strerror(n->ssl.error));
                    net_close_socket(n->sock);
                    n->sock = INVALID_SOCKET;
                    n->error = n->ssl.error;
                    break;
                }
            }
            net_async_poll(n);
            break;
        }

        if (err == INVALID_SOCKET && net_in_progress(net_error())) {
#ifdef _WIN32
            WSAPOLLFD fds[1];
#else
            struct pollfd fds[1];
#endif
            memset(fds, 0, sizeof (fds));
            fds[0].fd = n->sock;
            fds[0].events = connect_ev;
            fds[0].revents = 0;

            err = sockpoll(fds, 1, n->timeout > 0 ? n->timeout * 1000 : -1);
            if (err > 0 && fds[0].revents & connected_ev) {
                int pe = 0;
                SOCKLEN_T pe_sz = sizeof (pe);
                err = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (char *) &pe, &pe_sz);
                if (err == 0 && pe == 0) {
                    AM_LOG_DEBUG(n->instance_id, "%s: connected to %s:%d (%s)",
                            thisfunc, n->uv.host, n->uv.port,
                            rp->ai_family == AF_INET ? "IPv4" : "IPv6");

                    n->error = 0;
                    if (n->uv.ssl) {
                        net_connect_ssl(n);
                        if (n->ssl.error != AM_SUCCESS) {
                            AM_LOG_ERROR(n->instance_id,
                                    "%s: SSL/TLS connection to %s:%d (%s) failed (%s)",
                                    thisfunc, n->uv.host, n->uv.port,
                                    rp->ai_family == AF_INET ? "IPv4" : "IPv6",
                                    am_strerror(n->ssl.error));
                            net_close_socket(n->sock);
                            n->sock = INVALID_SOCKET;
                            n->error = n->ssl.error;
                            break;
                        }
                    }
                    net_async_poll(n);
                    break;
                }
                net_log_error(n->instance_id, pe);
                n->error = AM_ECONNREFUSED;
            } else if (err == 0) {
                AM_LOG_WARNING(n->instance_id,
                        "%s: timeout connecting to %s:%d (%s)",
                        thisfunc, n->uv.host, n->uv.port,
                        rp->ai_family == AF_INET ? "IPv4" : "IPv6");
                n->error = AM_ETIMEDOUT;
            } else {
                int pe = 0;
                SOCKLEN_T pe_sz = sizeof (pe);
                err = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (char *) &pe, &pe_sz);
                n->error = AM_ETIMEDOUT;
                break;
            }
        }

        net_close_socket(n->sock);
        n->sock = INVALID_SOCKET;
    }

    if (n->error != 0) {
        set_event(n->ce);
    }
    return NULL;
}
示例#7
0
static void parse_config_value(am_xml_parser_ctx_t *x, const char *prm, int type,
        int *itm_sz, void *itm, const char *val, int len) {

    if (itm == NULL || val == NULL || len <= 0 || strcmp(x->current_name, prm) != 0) {
        return;
    }

    switch (type) {
        case CONF_STRING:
        {
            char **value = (char **) itm;
            *value = strndup(val, len);
            if (x->log_enable) {
                if (strstr(prm, "password") != NULL) {
                    AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '********'",
                            prm);
                    break;
                }
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%s'",
                        prm, LOGEMPTY(*value));
            }
        }
            break;
        case CONF_NUMBER:
        {
            int *value = (int *) itm;
            if (strncasecmp(val, "on", len) == 0 || strncasecmp(val, "true", len) == 0 ||
                    strncasecmp(val, "local", len) == 0) {
                *value = 1;
            } else if (strncasecmp(val, "off", len) == 0 || strncasecmp(val, "false", len) == 0 ||
                    strncasecmp(val, "centralized", len) == 0) {
                *value = 0;
            } else {
                char *t = strndup(val, len);
                if (t != NULL) {
                    *value = strtol(t, NULL, AM_BASE_TEN);
                    free(t);
                }
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'", prm, *value);
            }
        }
            break;
        case CONF_DEBUG_LEVEL:
        {
            int *value = (int *) itm;
            *value = AM_LOG_LEVEL_NONE;
            if (strncasecmp(val, "all", 3) == 0) {
                *value = AM_LOG_LEVEL_DEBUG;
            } else if (strncasecmp(val, "error", len) == 0) {
                *value = AM_LOG_LEVEL_ERROR;
            } else if (strncasecmp(val, "info", len) == 0) {
                *value = AM_LOG_LEVEL_INFO;
            } else if (strncasecmp(val, "message", len) == 0) {
                *value = AM_LOG_LEVEL_WARNING;
            } else if (strncasecmp(val, "warning", len) == 0) {
                *value = AM_LOG_LEVEL_WARNING;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_ATTR_MODE:
        {
            int *value = (int *) itm;
            *value = AM_SET_ATTRS_NONE;
            if (strncasecmp(val, "HTTP_HEADER", len) == 0) {
                *value = AM_SET_ATTRS_AS_HEADER;
            } else if (strncasecmp(val, "HTTP_COOKIE", len) == 0) {
                *value = AM_SET_ATTRS_AS_COOKIE;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_AUDIT_LEVEL:
        {
            int *value = (int *) itm;
            *value = 0;
            if (strncasecmp(val, "LOG_ALLOW", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_ALLOW;
            } else if (strncasecmp(val, "LOG_BOTH", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_ALLOW;
                *value |= AM_LOG_LEVEL_AUDIT_DENY;
            } else if (strncasecmp(val, "LOG_DENY", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_DENY;
            } else if (strncasecmp(val, "ALL", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT;
                *value |= AM_LOG_LEVEL_AUDIT_REMOTE;
            } else if (strncasecmp(val, "LOCAL", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT;
            } else if (strncasecmp(val, "REMOTE", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_REMOTE;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_STRING_LIST:
        {
            int old_sz = *itm_sz;
            char ***value = (char ***) itm;
            char **value_tmp;
            char *v = strndup(val, len);
            if (v == NULL) break;
            value_tmp = (char **) realloc(*value, sizeof (char *) *(++(*itm_sz)));
            if (value_tmp == NULL) {//TODO: realloc failure?
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(v);
                break;
            }
            *value = value_tmp;
            (*value)[old_sz] = v;
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        case CONF_NUMBER_LIST:
        {
            int old_sz = *itm_sz;
            int **value = (int **) itm;
            int *value_tmp;
            char *v = strndup(val, len);
            if (v == NULL) break;
            value_tmp = (int *) realloc(*value, sizeof (int) *(++(*itm_sz)));
            if (value_tmp == NULL) {
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(v);
                break;
            }
            *value = value_tmp;
            (*value)[old_sz] = strtol(v, NULL, AM_BASE_TEN);
            free(v);
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        case CONF_STRING_MAP:
        {
            int old_sz = *itm_sz;
            am_config_map_t **value = (am_config_map_t **) itm;
            am_config_map_t *value_tmp;
            size_t slen = (size_t) len;
            char *v, *mv;

            if (val[0] != '[') break;
            v = strndup(val, len);
            if (v == NULL) break;

            mv = match_group(x->rgx, 2 /* groups in [key]=value */, v, &slen);
            free(v);
            if (mv == NULL) break;

            value_tmp = (am_config_map_t *) realloc(*value, sizeof (am_config_map_t) *(++(*itm_sz)));
            if (value_tmp == NULL) {
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(mv);
                break;
            }
            *value = value_tmp;
            (&(*value)[old_sz])->name = mv;
            (&(*value)[old_sz])->value = mv + strlen(mv) + 1;
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        default:
            AM_LOG_WARNING(x->conf->instance_id, "am_parse_config_xml() unknown type value %d setting %s", type, prm);
            break;
    }
}
void url_validator_worker(void *arg) {
    static const char *thisfunc = "url_validator_worker():";
    struct url_validator_worker_data *w = (struct url_validator_worker_data *) arg;
    am_net_options_t *net_options;
    int i, validate_status;
    am_config_t *conf = NULL;
    time_t current_ts;
    struct url_valid_table *e, *t;
    int current_index, current_ok, current_fail, default_ok, next_ok;
    char **url_list;
    int url_list_sz = 0;
    int ping_diff;

    set_valid_url_instance_running(w->instance_id, AM_TRUE);

    conf = am_get_config_file(w->instance_id, w->config_path);
    if (conf == NULL) {
        AM_LOG_WARNING(w->instance_id, "%s failed to get agent configuration (%s)",
                thisfunc, LOGEMPTY(w->config_path));
        set_valid_url_instance_running(w->instance_id, AM_FALSE);
        AM_FREE(w->config_path, w);
        return;
    }

    ping_diff = MAX(MIN_URL_VALIDATOR_TICK, conf->valid_ping);
    current_ts = time(NULL);
    /* this index corresponds to the naming.url multi-value index */
    current_index = w->url_index;

    if (conf->valid_level > 1 || conf->naming_url_sz < 2 || conf->naming_url_sz != conf->valid_default_url_sz ||
            (current_ts - w->last) < ping_diff) {
        /* a) validation is disabled; b) there is nothing to validate;
         * c) naming.url list and default.url list sizes do not match or
         * d) its not time yet to do any validation
         */
        am_config_free(&conf);
        set_valid_url_instance_running(w->instance_id, AM_FALSE);
        AM_FREE(w->config_path, w);
        return;
    }

    if (current_index < 0 || current_index >= conf->naming_url_sz) {
        AM_LOG_WARNING(w->instance_id,
                "%s invalid current index value, defaulting to %s", thisfunc, conf->naming_url[0]);
        set_valid_url_index(w->instance_id, 0);
        am_config_free(&conf);
        set_valid_url_instance_running(w->instance_id, AM_FALSE);
        AM_FREE(w->config_path, w);
        return;
    }

#define URL_LIST_FREE(l, s) do { \
        int k; \
        if (l == NULL) break; \
        for (k = 0; k < s; k++) { \
            am_free(l[k]); \
        }\
        free(l); \
    } while (0)

    net_options = calloc(1, sizeof (am_net_options_t));
    if (net_options == NULL) {
        AM_LOG_ERROR(w->instance_id, "%s memory allocation error", thisfunc);
        am_config_free(&conf);
        set_valid_url_instance_running(w->instance_id, AM_FALSE);
        AM_FREE(w->config_path, w);
        return;
    }

    url_list = (char **) calloc(1, conf->naming_url_sz * sizeof (char *));
    if (url_list == NULL) {
        AM_LOG_ERROR(w->instance_id, "%s memory allocation error", thisfunc);
        am_config_free(&conf);
        set_valid_url_instance_running(w->instance_id, AM_FALSE);
        AM_FREE(net_options, w->config_path, w);
        return;
    }
    url_list_sz = conf->naming_url_sz;

    for (i = 0; i < url_list_sz; i++) {
        /* default.url.set contains fail-over order;
         * will keep internal value list index-ordered 
         **/
        int j = conf->valid_default_url[i];
        url_list[i] = strdup(conf->naming_url[j]);
        if (url_list[i] == NULL) {
            URL_LIST_FREE(url_list, url_list_sz);
            am_config_free(&conf);
            set_valid_url_instance_running(w->instance_id, AM_FALSE);
            AM_FREE(net_options, w->config_path, w);
            return;
        }
    }

    am_net_options_create(conf, net_options, NULL);
    net_options->keepalive = AM_FALSE;
    net_options->local = net_options->cert_trust = AM_TRUE;
    net_options->net_timeout = 2; /* fixed for url validator; in sec */

    /* do the actual url validation */

    for (i = 0; i < url_list_sz; i++) {
        const char *url = url_list[i];
        int ok = 0, fail = 0, httpcode = 0;

        get_validation_table_entry(w->instance_id, i, &ok, &fail, NULL);

        AM_LOG_DEBUG(w->instance_id, "%s validating %s", thisfunc, url);

        if (conf->valid_level == 1) {
            /* simple HEAD request */
            validate_status = am_url_validate(w->instance_id, url, net_options, &httpcode);
        } else {
            /* full scale agent login-logout request */
            char *agent_token = NULL;
            validate_status = am_agent_login(w->instance_id, url, conf->user, conf->pass, conf->realm,
                    net_options, &agent_token, NULL, NULL, NULL);
            if (agent_token != NULL) {
                am_agent_logout(0, url, agent_token, net_options);
                free(agent_token);
                httpcode = 200;
            }
        }

        if (validate_status == AM_SUCCESS && httpcode != 0) {
            if (ok++ > conf->valid_ping_ok) {
                ok = conf->valid_ping_ok;
            }
            fail = 0;
        } else {
            if (fail++ > conf->valid_ping_miss) {
                fail = conf->valid_ping_miss;
            }
            ok = 0;
        }

        set_validation_table_entry(w->instance_id, i, ok, fail);
    }

    /* map stored index value to our ordered list index */
    for (i = 0; i < conf->valid_default_url_sz; i++) {
        if (current_index == conf->valid_default_url[i]) {
            current_index = i;
            break;
        }
    }

    default_ok = current_ok = current_fail = 0;
    /* fetch validation table entry for the current_index 
     * (which now corresponds to the default.url.set value/index) */
    get_validation_table_entry(w->instance_id, current_index, &current_ok, &current_fail, &default_ok);

    /* do the fail-over logic */
    do {
        if (current_ok > 0) {
            if (current_index > 0 && default_ok >= conf->valid_ping_ok) {
                set_valid_url_index(w->instance_id, conf->valid_default_url[0]);
                AM_LOG_INFO(w->instance_id, "%s fail-back to %s", thisfunc, url_list[0]);
            } else {
                set_valid_url_index(w->instance_id, conf->valid_default_url[current_index]);
                AM_LOG_INFO(w->instance_id, "%s continue with %s", thisfunc, url_list[current_index]);
            }
            break;
        }

        /* current index is not valid; check ping.miss.count */
        if (current_ok == 0 && current_fail <= conf->valid_ping_miss) {
            set_valid_url_index(w->instance_id, conf->valid_default_url[current_index]);
            AM_LOG_INFO(w->instance_id, "%s still staying with %s", thisfunc, url_list[current_index]);
            break;
        }

        /* find next valid index value to fail-over to */
        next_ok = 0;
        AM_MUTEX_LOCK(&table_mutex);

        AM_LIST_FOR_EACH(table, e, t) {
            if (e->instance_id == w->instance_id && e->index == 0) {
                default_ok = e->ok;
            }
            if (e->instance_id == w->instance_id && e->ok > 0) {
                next_ok = e->ok;
                i = e->index;
                break;
            }
        }

        AM_MUTEX_UNLOCK(&table_mutex);

        if (next_ok == 0) {
            AM_LOG_WARNING(w->instance_id,
                    "%s none of the values are valid, defaulting to %s", thisfunc, url_list[0]);
            set_valid_url_index(w->instance_id, conf->valid_default_url[0]);
            break;
        }

        if (current_index > 0 && default_ok >= conf->valid_ping_ok) {
            AM_LOG_INFO(w->instance_id, "%s fail-back to %s", thisfunc, url_list[0]);
            set_valid_url_index(w->instance_id, conf->valid_default_url[0]);
            break;
        }

        AM_LOG_INFO(w->instance_id, "%s fail-over to %s", thisfunc, url_list[i]);
        set_valid_url_index(w->instance_id, conf->valid_default_url[i]);

    } while (0);

    am_net_options_delete(net_options);
    free(net_options);
    am_config_free(&conf);
    set_valid_url_instance_running(w->instance_id, AM_FALSE);
    AM_FREE(w->config_path, w);
    URL_LIST_FREE(url_list, url_list_sz);
}