static int init_jk()

in native/iis/jk_isapi_plugin.c [2582:2802]


static int init_jk(char *serverName)
{
    char *p, shm_name[MAX_PATH];
    int i, rc = JK_FALSE;
    jk_log_context_t log_ctx;
    jk_log_context_t *l = &log_ctx;

    init_logger(JK_FALSE);

    l->logger = logger;
    l->id = "INIT_JK";

    /* TODO: Use System logging to notify the user that
     *       we cannot open the configured log file.
     */
    StringCbCopy(shm_name, MAX_PATH, SHM_DEF_PREFIX);
    jk_log(l, JK_LOG_INFO, "Starting " FULL_VERSION_STRING);
    StringCbCat(shm_name, MAX_PATH, serverName);
    StringCbCat(shm_name, MAX_PATH, "_");
    StringCbCat(shm_name, MAX_PATH, extension_uri + 1);
    if ((p = strrchr(shm_name, '.')))
        *p = '\0';
    jk_set_worker_def_cache_size(DEFAULT_WORKER_THREADS);

    /* Logging the initialization type: registry or properties file in virtual dir
     */
    if (JK_IS_DEBUG_LEVEL(l)) {
        jk_log(l, JK_LOG_DEBUG, "Detected IIS version %d.%d", iis_info.major, iis_info.minor);
        if (using_ini_file) {
            jk_log(l, JK_LOG_DEBUG, "Using ini file %s.", ini_file_name);
        }
        else {
            jk_log(l, JK_LOG_DEBUG, "Using registry.");
        }

        jk_log(l, JK_LOG_DEBUG, "Using log file %s.", log_file);
        jk_log(l, JK_LOG_DEBUG, "Using log level %d.", log_level);
        jk_log(l, JK_LOG_DEBUG, "Using log rotation time %d seconds.", log_rotationtime);
        jk_log(l, JK_LOG_DEBUG, "Using log file size %d bytes.", log_filesize);

        jk_log(l, JK_LOG_DEBUG, "Using extension uri %s.", extension_uri);
        jk_log(l, JK_LOG_DEBUG, "Using worker file %s.", worker_file);
        jk_log(l, JK_LOG_DEBUG, "Using worker mount file %s.",
               worker_mount_file);
        jk_log(l, JK_LOG_DEBUG, "Using rewrite rule file %s.",
               rewrite_rule_file);
        jk_log(l, JK_LOG_DEBUG, "Using uri select %d.", uri_select_option);
        jk_log(l, JK_LOG_DEBUG, "Using%s chunked encoding.", (chunked_encoding_enabled ? "" : " no"));

        jk_log(l, JK_LOG_DEBUG, "Using notification event %s (0x%08x)",
               (iis_info.filter_notify_event == SF_NOTIFY_AUTH_COMPLETE) ?
               "SF_NOTIFY_AUTH_COMPLETE" :
               ((iis_info.filter_notify_event == SF_NOTIFY_PREPROC_HEADERS) ?
               "SF_NOTIFY_PREPROC_HEADERS" : "UNKNOWN"),
               iis_info.filter_notify_event);

        if (error_page) {
            jk_log(l, JK_LOG_DEBUG, "Using error page '%s'.", error_page);
        }
        jk_log(l, JK_LOG_DEBUG, "Using uri header %s.", URI_HEADER_NAME);
        jk_log(l, JK_LOG_DEBUG, "Using query header %s.", QUERY_HEADER_NAME);
        jk_log(l, JK_LOG_DEBUG, "Using request id header %s.", REQUEST_ID_HEADER_NAME);
        jk_log(l, JK_LOG_DEBUG, "Using worker header %s.", WORKER_HEADER_NAME);
        jk_log(l, JK_LOG_DEBUG, "Using worker index %s.", WORKER_HEADER_INDEX);
        jk_log(l, JK_LOG_DEBUG, "Using translate header %s.", TOMCAT_TRANSLATE_HEADER_NAME);
        jk_log(l, JK_LOG_DEBUG, "Using a default of %d connections per pool.",
               DEFAULT_WORKER_THREADS);
        if (request_id_header) {
            jk_log(l, JK_LOG_DEBUG, "Using incoming request id header '%s'.", request_id_header);
        }
    }

    if ((log_rotationtime > 0) && (log_filesize > 0)) {
        jk_log(l, JK_LOG_WARNING,
               "%s is defined in configuration, but will be ignored because %s is set. ",
               LOG_FILESIZE_TAG, LOG_ROTATION_TIME_TAG);
    }

    if (rewrite_rule_file[0] && jk_map_alloc(&rewrite_map)) {
        if (jk_map_read_properties(rewrite_map, NULL, rewrite_rule_file,
                                   NULL, JK_MAP_HANDLE_RAW, l)) {
            if (JK_IS_DEBUG_LEVEL(l)) {
                jk_log(l, JK_LOG_DEBUG, "Loaded rewrite rule file %s.",
                       rewrite_rule_file);

            }
            jk_map_alloc(&rregexp_map);
            for (i = 0; i < jk_map_size(rewrite_map); i++) {
                const char *src = jk_map_name_at(rewrite_map, i);
                if (*src == '~') {
                    ap_regex_t *regexp = malloc(sizeof(ap_regex_t));
                    const char *val = jk_map_value_at(rewrite_map, i);
                    /* Skip leading tilde */
                    src++;
                    regexp->fake = val;
                    if (ap_regcomp(regexp, src) == JK_TRUE) {
                        jk_map_add(rregexp_map, src, regexp);
                        if (JK_IS_DEBUG_LEVEL(l)) {
                            jk_log(l, JK_LOG_DEBUG,
                                   "Added regular expression rule %s -> %s",
                                   src, regexp->fake);
                        }
                    }
                    else {
                        jk_log(l, JK_LOG_ERROR,
                               "Unable to compile regular expression %s",
                               src);
                        free(regexp);
                    }
                }
            }
        }
        else {
            jk_map_free(&rewrite_map);
            rewrite_map = NULL;
        }
    }

    if (uri_worker_map_alloc(&uw_map, NULL, l)) {
        rc = JK_FALSE;
        if (reject_unsafe)
            uw_map->reject_unsafe = 1;
        else
            uw_map->reject_unsafe = 0;
        uw_map->reload = worker_mount_reload;
        if (worker_mount_file[0]) {
            uw_map->fname = worker_mount_file;
            rc = uri_worker_map_load(uw_map, l);
        }
    }
    if (rc) {
        rc = JK_FALSE;
        if (jk_map_alloc(&workers_map)) {
            if (jk_map_read_properties(workers_map, NULL, worker_file, NULL,
                                       JK_MAP_HANDLE_DUPLICATES, l)) {
                int rv = -1;

                /* we add the URI->WORKER MAP since workers using AJP14 will feed it */

                if (jk_map_resolve_references(workers_map, "worker.", 1, 1, l) == JK_FALSE) {
                    jk_log(l, JK_LOG_ERROR, "Error in resolving configuration references");
                }
                /*
                 * Create named shared memory for each server
                 */
                if (shm_config_size == -1) {
                    shm_config_size = jk_shm_calculate_size(workers_map, l);
                }
                else if (shm_config_size > 0) {
                    jk_log(l, JK_LOG_WARNING,
                           "The optimal shared memory size can now be determined automatically.");
                    jk_log(l, JK_LOG_WARNING,
                           "You can remove the shm_size attribute if you want to use the optimal size.");
                }
                if ((shm_config_size > 0) &&
                    (rv = jk_shm_open(shm_name, shm_config_size, l)) != 0) {
                    jk_log(l, JK_LOG_ERROR,
                           "Initializing shm:%s errno=%d. Load balancing workers will not function properly",
                           jk_shm_name(), rv);
                }
                if (rv != 0 && (rv = jk_shm_open(NULL, shm_config_size, l)) != 0) {
                    /* Do not try to open the worker if we cannot create
                     * the shared memory segment or heap memory.
                     */
                    jk_log(l, JK_LOG_EMERG,
                           "Initializing shm:%s errno=%d. Cannot continue",
                           jk_shm_name(), rv);
                    jk_map_free(&workers_map);
                    workers_map = NULL;
                    return JK_FALSE;
                }
                else {
                    if (shm_loaded_name[0]) {
                        if (strcmp(shm_loaded_name, shm_name)) {
                            jk_log(l, JK_LOG_WARNING,
                                   "Loading different shared memory %s. Already loaded %s",
                                   shm_name, shm_loaded_name);
                        }
                    }
                    else {
                        StringCbCopy(shm_loaded_name, MAX_PATH, shm_name);
                    }
                }
                worker_env.uri_to_worker = uw_map;
                worker_env.server_name = serverName;
                worker_env.pool = NULL;

                if (wc_open(workers_map, &worker_env, l)) {
                    rc = JK_TRUE;
                    uri_worker_map_ext(uw_map, l);
                    uri_worker_map_switch(uw_map, l);
                }
                else {
                    jk_shm_close(l);
                }
            }
            else {
                jk_log(l, JK_LOG_EMERG,
                       "Unable to read worker file %s. (errno=%d, err=%s)", worker_file, errno, strerror(errno));
            }
            if (rc != JK_TRUE) {
                jk_map_free(&workers_map);
                workers_map = NULL;
            }
        }
    }
    if (rc) {
        if (watchdog_interval) {
            DWORD  wi;
            watchdog_handle = CreateThread(NULL, 0, watchdog_thread,
                                           NULL, 0, &wi);
            if (!watchdog_handle) {
                jk_log(l, JK_LOG_EMERG, "Error %d (0x%08x) creating Watchdog thread",
                       GetLastError(), GetLastError());
                watchdog_interval = 0;
            }
        }
        jk_log(l, JK_LOG_INFO, "%s initialized", (FULL_VERSION_STRING));
    }
    return rc;
}