Blame modules/ssl/ssl_engine_mutex.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*                      _             _
Packit 90a5c9
 *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
Packit 90a5c9
 * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
Packit 90a5c9
 * | | | | | | (_) | (_| |   \__ \__ \ |
Packit 90a5c9
 * |_| |_| |_|\___/ \__,_|___|___/___/_|
Packit 90a5c9
 *                      |_____|
Packit 90a5c9
 *  ssl_engine_mutex.c
Packit 90a5c9
 *  Semaphore for Mutual Exclusion
Packit 90a5c9
 */
Packit 90a5c9
                             /* ``Real programmers confuse
Packit 90a5c9
                                  Christmas and Halloween
Packit 90a5c9
                                  because DEC 25 = OCT 31.''
Packit 90a5c9
                                             -- Unknown     */
Packit 90a5c9
Packit 90a5c9
#include "ssl_private.h"
Packit 90a5c9
Packit 90a5c9
int ssl_mutex_init(server_rec *s, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    /* A mutex is only needed if a session cache is configured, and
Packit 90a5c9
     * the provider used is not internally multi-process/thread
Packit 90a5c9
     * safe. */
Packit 90a5c9
    if (!mc->sesscache
Packit 90a5c9
        || (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) == 0) {
Packit 90a5c9
        return TRUE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (mc->pMutex) {
Packit 90a5c9
        return TRUE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((rv = ap_global_mutex_create(&mc->pMutex, NULL, SSL_CACHE_MUTEX_TYPE,
Packit 90a5c9
                                     NULL, s, s->process->pool, 0))
Packit 90a5c9
            != APR_SUCCESS) {
Packit 90a5c9
        return FALSE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return TRUE;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
int ssl_mutex_reinit(server_rec *s, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *lockfile;
Packit 90a5c9
Packit 90a5c9
    if (mc->pMutex == NULL || !mc->sesscache
Packit 90a5c9
        || (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) == 0) {
Packit 90a5c9
        return TRUE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    lockfile = apr_global_mutex_lockfile(mc->pMutex);
Packit 90a5c9
    if ((rv = apr_global_mutex_child_init(&mc->pMutex,
Packit 90a5c9
                                          lockfile,
Packit 90a5c9
                                          p)) != APR_SUCCESS) {
Packit 90a5c9
        if (lockfile)
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(02024)
Packit 90a5c9
                         "Cannot reinit %s mutex with file `%s'",
Packit 90a5c9
                         SSL_CACHE_MUTEX_TYPE, lockfile);
Packit 90a5c9
        else
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s, APLOGNO(02025)
Packit 90a5c9
                         "Cannot reinit %s mutex", SSL_CACHE_MUTEX_TYPE);
Packit 90a5c9
        return FALSE;
Packit 90a5c9
    }
Packit 90a5c9
    return TRUE;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
int ssl_mutex_on(server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_global_mutex_lock(mc->pMutex)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s, APLOGNO(02026)
Packit 90a5c9
                     "Failed to acquire SSL session cache lock");
Packit 90a5c9
        return FALSE;
Packit 90a5c9
    }
Packit 90a5c9
    return TRUE;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
int ssl_mutex_off(server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_global_mutex_unlock(mc->pMutex)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s, APLOGNO(02027)
Packit 90a5c9
                     "Failed to release SSL session cache lock");
Packit 90a5c9
        return FALSE;
Packit 90a5c9
    }
Packit 90a5c9
    return TRUE;
Packit 90a5c9
}
Packit 90a5c9