]> granicus.if.org Git - apache/blob - modules/ssl/ssl_engine_mutex.c
Move mod_ssl-internal interfaces into ssl_private.h; allow mod_ssl.h
[apache] / modules / ssl / ssl_engine_mutex.c
1 /* Copyright 2001-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*                      _             _
17  *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
18  * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
19  * | | | | | | (_) | (_| |   \__ \__ \ |
20  * |_| |_| |_|\___/ \__,_|___|___/___/_|
21  *                      |_____|
22  *  ssl_engine_mutex.c
23  *  Semaphore for Mutual Exclusion
24  */
25                              /* ``Real programmers confuse
26                                   Christmas and Halloween
27                                   because DEC 25 = OCT 31.''
28                                              -- Unknown     */
29
30 #include "ssl_private.h"
31 #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
32 #include "unixd.h"
33 #define MOD_SSL_SET_MUTEX_PERMS /* XXX Apache should define something */
34 #endif
35
36 int ssl_mutex_init(server_rec *s, apr_pool_t *p)
37 {
38     SSLModConfigRec *mc = myModConfig(s);
39     apr_status_t rv;
40
41     if (mc->nMutexMode == SSL_MUTEXMODE_NONE) 
42         return TRUE;
43
44     if (mc->pMutex) {
45         return TRUE;
46     }
47     if ((rv = apr_global_mutex_create(&mc->pMutex, mc->szMutexFile,
48                                       mc->nMutexMech, s->process->pool))
49             != APR_SUCCESS) {
50         if (mc->szMutexFile)
51             ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
52                          "Cannot create SSLMutex with file `%s'",
53                          mc->szMutexFile);
54         else
55             ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
56                          "Cannot create SSLMutex");
57         return FALSE;
58     }
59
60 #ifdef MOD_SSL_SET_MUTEX_PERMS
61     rv = unixd_set_global_mutex_perms(mc->pMutex);
62     if (rv != APR_SUCCESS) {
63         ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
64                      "Could not set permissions on ssl_mutex; check User "
65                      "and Group directives");
66         return FALSE;
67     }
68 #endif
69     return TRUE;
70 }
71
72 int ssl_mutex_reinit(server_rec *s, apr_pool_t *p)
73 {
74     SSLModConfigRec *mc = myModConfig(s);
75     apr_status_t rv;
76
77     if (mc->nMutexMode == SSL_MUTEXMODE_NONE)
78         return TRUE;
79
80     if ((rv = apr_global_mutex_child_init(&mc->pMutex,
81                                     mc->szMutexFile, p)) != APR_SUCCESS) {
82         if (mc->szMutexFile)
83             ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
84                          "Cannot reinit SSLMutex with file `%s'",
85                          mc->szMutexFile);
86         else
87             ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s,
88                          "Cannot reinit SSLMutex");
89         return FALSE;
90     }
91     return TRUE;
92 }
93
94 int ssl_mutex_on(server_rec *s)
95 {
96     SSLModConfigRec *mc = myModConfig(s);
97     apr_status_t rv;
98
99     if (mc->nMutexMode == SSL_MUTEXMODE_NONE)
100         return TRUE;
101     if ((rv = apr_global_mutex_lock(mc->pMutex)) != APR_SUCCESS) {
102         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s,
103                      "Failed to acquire global mutex lock");
104         return FALSE;
105     }
106     return TRUE;
107 }
108
109 int ssl_mutex_off(server_rec *s)
110 {
111     SSLModConfigRec *mc = myModConfig(s);
112     apr_status_t rv;
113
114     if (mc->nMutexMode == SSL_MUTEXMODE_NONE)
115         return TRUE;
116     if ((rv = apr_global_mutex_unlock(mc->pMutex)) != APR_SUCCESS) {
117         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, s,
118                      "Failed to release global mutex lock");
119         return FALSE;
120     }
121     return TRUE;
122 }
123