Coverage Report

Created: 2025-07-11 06:40

/src/httpd/srclib/apr/locks/unix/global_mutex.c
Line
Count
Source (jump to first uncovered line)
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include "apr.h"
18
19
#include "apr_strings.h"
20
#include "apr_arch_global_mutex.h"
21
#include "apr_proc_mutex.h"
22
#include "apr_thread_mutex.h"
23
#include "apr_portable.h"
24
25
static apr_status_t global_mutex_cleanup(void *data)
26
0
{
27
0
    apr_global_mutex_t *m = (apr_global_mutex_t *)data;
28
0
    apr_status_t rv;
29
30
0
    rv = apr_proc_mutex_destroy(m->proc_mutex);
31
32
0
#if APR_HAS_THREADS
33
0
    if (m->thread_mutex) {
34
0
        if (rv != APR_SUCCESS) {
35
0
            (void)apr_thread_mutex_destroy(m->thread_mutex);
36
0
        }
37
0
        else {
38
0
            rv = apr_thread_mutex_destroy(m->thread_mutex);
39
0
        }
40
0
    }
41
0
#endif /* APR_HAS_THREADS */
42
43
0
    return rv;
44
0
}
45
46
APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
47
                                                  const char *fname,
48
                                                  apr_lockmech_e mech,
49
                                                  apr_pool_t *pool)
50
0
{
51
0
    apr_status_t rv;
52
0
    apr_global_mutex_t *m;
53
54
0
    m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m));
55
0
    m->pool = pool;
56
57
0
    rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool);
58
0
    if (rv != APR_SUCCESS) {
59
0
        return rv;
60
0
    }
61
62
0
#if APR_HAS_THREADS
63
0
    if (m->proc_mutex->meth->flags & APR_PROCESS_LOCK_MECH_IS_GLOBAL) {
64
0
        m->thread_mutex = NULL; /* We don't need a thread lock. */
65
0
    }
66
0
    else {
67
0
        rv = apr_thread_mutex_create(&m->thread_mutex,
68
0
                                     APR_THREAD_MUTEX_DEFAULT, m->pool);
69
0
        if (rv != APR_SUCCESS) {
70
0
            rv = apr_proc_mutex_destroy(m->proc_mutex);
71
0
            return rv;
72
0
        }
73
0
    }
74
0
#endif /* APR_HAS_THREADS */
75
76
0
    apr_pool_cleanup_register(m->pool, (void *)m,
77
0
                              global_mutex_cleanup, apr_pool_cleanup_null);
78
0
    *mutex = m;
79
0
    return APR_SUCCESS;
80
0
}
81
82
APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
83
                              apr_global_mutex_t **mutex,
84
                              const char *fname,
85
                              apr_pool_t *pool)
86
0
{
87
0
    return apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
88
0
}
89
90
APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex)
91
0
{
92
0
    apr_status_t rv;
93
94
0
#if APR_HAS_THREADS
95
0
    if (mutex->thread_mutex) {
96
0
        rv = apr_thread_mutex_lock(mutex->thread_mutex);
97
0
        if (rv != APR_SUCCESS) {
98
0
            return rv;
99
0
        }
100
0
    }
101
0
#endif /* APR_HAS_THREADS */
102
103
0
    rv = apr_proc_mutex_lock(mutex->proc_mutex);
104
105
0
#if APR_HAS_THREADS
106
0
    if (rv != APR_SUCCESS) {
107
0
        if (mutex->thread_mutex) {
108
0
            (void)apr_thread_mutex_unlock(mutex->thread_mutex);
109
0
        }
110
0
    }
111
0
#endif /* APR_HAS_THREADS */
112
113
0
    return rv;
114
0
}
115
116
APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex)
117
0
{
118
0
    apr_status_t rv;
119
120
0
#if APR_HAS_THREADS
121
0
    if (mutex->thread_mutex) {
122
0
        rv = apr_thread_mutex_trylock(mutex->thread_mutex);
123
0
        if (rv != APR_SUCCESS) {
124
0
            return rv;
125
0
        }
126
0
    }
127
0
#endif /* APR_HAS_THREADS */
128
129
0
    rv = apr_proc_mutex_trylock(mutex->proc_mutex);
130
131
0
#if APR_HAS_THREADS
132
0
    if (rv != APR_SUCCESS) {
133
0
        if (mutex->thread_mutex) {
134
0
            (void)apr_thread_mutex_unlock(mutex->thread_mutex);
135
0
        }
136
0
    }
137
0
#endif /* APR_HAS_THREADS */
138
139
0
    return rv;
140
0
}
141
142
APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex,
143
                                                 apr_interval_time_t timeout)
144
0
{
145
0
    apr_status_t rv;
146
147
0
#if APR_HAS_THREADS
148
0
    if (mutex->thread_mutex) {
149
0
        apr_time_t expiry = 0;
150
0
        if (timeout > 0) {
151
0
            expiry = apr_time_now() + timeout;
152
0
        }
153
0
        rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout);
154
0
        if (rv != APR_SUCCESS) {
155
0
            return rv;
156
0
        }
157
0
        if (expiry) {
158
0
            timeout = expiry - apr_time_now();
159
0
            if (timeout < 0) {
160
0
                timeout = 0;
161
0
            }
162
0
        }
163
0
    }
164
0
#endif /* APR_HAS_THREADS */
165
166
0
    rv = apr_proc_mutex_timedlock(mutex->proc_mutex, timeout);
167
168
0
#if APR_HAS_THREADS
169
0
    if (rv != APR_SUCCESS) {
170
0
        if (mutex->thread_mutex) {
171
0
            (void)apr_thread_mutex_unlock(mutex->thread_mutex);
172
0
        }
173
0
    }
174
0
#endif /* APR_HAS_THREADS */
175
176
0
    return rv;
177
0
}
178
179
APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex)
180
0
{
181
0
    apr_status_t rv;
182
183
0
    rv = apr_proc_mutex_unlock(mutex->proc_mutex);
184
0
#if APR_HAS_THREADS
185
0
    if (mutex->thread_mutex) {
186
0
        if (rv != APR_SUCCESS) {
187
0
            (void)apr_thread_mutex_unlock(mutex->thread_mutex);
188
0
        }
189
0
        else {
190
0
            rv = apr_thread_mutex_unlock(mutex->thread_mutex);
191
0
        }
192
0
    }
193
0
#endif /* APR_HAS_THREADS */
194
0
    return rv;
195
0
}
196
197
APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex,
198
                                                apr_global_mutex_t *pmutex)
199
0
{
200
0
    ospmutex->pool = pmutex->pool;
201
0
    ospmutex->proc_mutex = pmutex->proc_mutex;
202
0
#if APR_HAS_THREADS
203
0
    ospmutex->thread_mutex = pmutex->thread_mutex;
204
0
#endif
205
0
    return APR_SUCCESS;
206
0
}
207
208
APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex)
209
0
{
210
0
    return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
211
0
}
212
213
APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex)
214
0
{
215
0
    return apr_proc_mutex_lockfile(mutex->proc_mutex);
216
0
}
217
218
APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex)
219
0
{
220
0
    return apr_proc_mutex_mech(mutex->proc_mutex);
221
0
}
222
223
APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex)
224
0
{
225
0
    return apr_proc_mutex_name(mutex->proc_mutex);
226
0
}
227
228
APR_PERMS_SET_IMPLEMENT(global_mutex)
229
0
{
230
0
    apr_global_mutex_t *mutex = (apr_global_mutex_t *)theglobal_mutex;
231
232
0
    return APR_PERMS_SET_FN(proc_mutex)(mutex->proc_mutex, perms, uid, gid);
233
0
}
234
235
APR_POOL_IMPLEMENT_ACCESSOR(global_mutex)
236