]> granicus.if.org Git - apache/blob - server/util_mutex.c
Elaborate on low hanging XXX fruit
[apache] / server / util_mutex.c
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 /*
18  * util_mutex.c: Useful functions for determining allowable
19  *               mutexes and mutex settings
20  */
21
22
23 #include "apr.h"
24 #include "apr_hash.h"
25 #include "apr_strings.h"
26 #include "apr_lib.h"
27
28 #define APR_WANT_STRFUNC
29 #include "apr_want.h"
30
31 #include "ap_config.h"
32 #include "httpd.h"
33 #include "http_main.h"
34 #include "http_config.h"
35 #include "http_log.h"
36 #include "util_mutex.h"
37 #if AP_NEED_SET_MUTEX_PERMS
38 #include "unixd.h"
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h> /* getpid() */
42 #endif
43
44 AP_DECLARE(apr_status_t) ap_parse_mutex(const char *arg, apr_pool_t *pool,
45                                         apr_lockmech_e *mutexmech,
46                                         const char **mutexfile)
47 {
48     /* Split arg into meth and file */
49     char *meth = apr_pstrdup(pool, arg);
50     char *file = strchr(meth, ':');
51     if (file) {
52         *(file++) = '\0';
53         if (!*file) {
54             file = NULL;
55         }
56     }
57
58     /* APR determines temporary filename unless overridden below,
59      * we presume file indicates an mutexfile is a file path
60      * unless the method sets mutexfile=file and NULLs file
61      */
62     *mutexfile = NULL;
63
64     if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) {
65         return APR_ENOLOCK;
66     }
67
68     /* NOTE: previously, 'yes' implied 'sem' */
69     if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) {
70         *mutexmech = APR_LOCK_DEFAULT;
71     }
72 #if APR_HAS_FCNTL_SERIALIZE
73     else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) {
74         *mutexmech = APR_LOCK_FCNTL;
75     }
76 #endif
77 #if APR_HAS_FLOCK_SERIALIZE
78     else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) {
79         *mutexmech = APR_LOCK_FLOCK;
80     }
81 #endif
82 #if APR_HAS_POSIXSEM_SERIALIZE
83     else if (!strcasecmp(meth, "posixsem") || !strcasecmp(meth, "sem")) {
84         *mutexmech = APR_LOCK_POSIXSEM;
85         /* Posix/SysV semaphores aren't file based, use the literal name
86          * if provided and fall back on APR's default if not.  Today, APR
87          * will ignore it, but once supported it has an absurdly short limit.
88          */
89         if (file) {
90             *mutexfile = apr_pstrdup(pool, file);
91
92             file = NULL;
93         }
94     }
95 #endif
96 #if APR_HAS_SYSVSEM_SERIALIZE
97     else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) {
98         *mutexmech = APR_LOCK_SYSVSEM;
99     }
100 #endif
101 #if APR_HAS_PROC_PTHREAD_SERIALIZE
102     else if (!strcasecmp(meth, "pthread")) {
103         *mutexmech = APR_LOCK_PROC_PTHREAD;
104     }
105 #endif
106     else {
107         return APR_ENOTIMPL;
108     }
109
110     /* Unless the method above assumed responsibility for setting up
111      * mutexfile and NULLing out file, presume it is a file we
112      * are looking to use
113      */
114     if (file) {
115         *mutexfile = ap_server_root_relative(pool, file);
116         if (!*mutexfile) {
117             return APR_BADARG;
118         }
119     }
120
121     return APR_SUCCESS;
122 }
123
124 typedef struct {
125     apr_int32_t options;
126     int set;
127     int none;
128     apr_lockmech_e mech;
129     const char *dir;
130 } mutex_cfg_t;
131
132 /* hash is created the first time a module calls ap_mutex_register(),
133  * rather than attempting to be the REALLY_REALLY_FIRST pre-config
134  * hook; it is cleaned up when the associated pool goes away; assume
135  * pconf is the pool passed to ap_mutex_register()
136  */
137 static apr_hash_t *mxcfg_by_type;
138
139 static apr_status_t cleanup_mx_hash(void *dummy)
140 {
141     mxcfg_by_type = NULL;
142     return APR_SUCCESS;
143 }
144
145 static void mx_hash_init(apr_pool_t *p)
146 {
147     mutex_cfg_t *def;
148
149     if (mxcfg_by_type) {
150         return;
151     }
152
153     mxcfg_by_type = apr_hash_make(p);
154     apr_pool_cleanup_register(p, NULL, cleanup_mx_hash, apr_pool_cleanup_null);
155
156     /* initialize default mutex configuration */
157     def = apr_pcalloc(p, sizeof *def);
158     def->mech = APR_LOCK_DEFAULT;
159 #ifdef DEFAULT_REL_RUNTIMEDIR
160     def->dir = DEFAULT_REL_RUNTIMEDIR;
161 #else
162     def->dir = "logs";
163 #endif
164     apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
165 }
166
167 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
168                                             const char *type,
169                                             const char *mechdir)
170 {
171     apr_pool_t *p = cmd->pool;
172     apr_lockmech_e mech;
173     apr_status_t rv;
174     const char *mutexdir;
175     mutex_cfg_t *mxcfg = apr_hash_get(mxcfg_by_type, type,
176                                       APR_HASH_KEY_STRING);
177     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
178
179     if (err != NULL) {
180         return err;
181     }
182
183     if (!mxcfg) {
184         return apr_psprintf(p, "Mutex type %s is not valid", type);
185     }
186
187     mxcfg->none = 0; /* in case that was the default */
188
189     rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
190     if (rv == APR_ENOTIMPL) {
191         return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
192                            " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
193     }
194     else if (rv == APR_BADARG
195              || (mutexdir && !ap_is_directory(p, mutexdir))) {
196         return apr_pstrcat(p, "Invalid Mutex directory in argument ",
197                            mechdir, NULL);
198     }
199
200     mxcfg->set = 1;
201     if (rv == APR_ENOLOCK) { /* "none" */
202         if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
203             return apr_psprintf(p,
204                                 "None is not allowed for mutex type %s",
205                                 type);
206         }
207         mxcfg->none = 1;
208     }
209     else {
210         mxcfg->mech = mech;
211         if (mutexdir) { /* retain mutex default if not configured */
212             mxcfg->dir = mutexdir;
213         }
214     }
215
216     return NULL;
217 }
218
219 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
220                                            const char *type,
221                                            const char *default_dir,
222                                            apr_lockmech_e default_mech,
223                                            apr_int32_t options)
224 {
225     mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
226
227     if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
228         return APR_EINVAL;
229     }
230
231     mx_hash_init(pconf);
232
233     mxcfg->options = options;
234     if (options & AP_MUTEX_DEFAULT_NONE) {
235         mxcfg->none = 1;
236     }
237     mxcfg->dir = default_dir; /* usually NULL */
238     mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
239     apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
240
241     return APR_SUCCESS;
242 }
243
244 static int mutex_needs_file(apr_lockmech_e mech)
245 {
246     if (mech != APR_LOCK_FLOCK
247         && mech != APR_LOCK_FCNTL
248 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
249         && mech != APR_LOCK_DEFAULT
250 #endif
251         ) {
252         return 0;
253     }
254     return 1;
255 }
256
257 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
258                                       const char *type,
259                                       const char *instance_id)
260 {
261     const char *pid_suffix = "";
262
263     if (!mutex_needs_file(mxcfg->mech)) {
264         return NULL;
265     }
266
267 #if HAVE_UNISTD_H
268     pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
269 #endif
270
271     return ap_server_root_relative(p,
272                                    apr_pstrcat(p,
273                                                mxcfg->dir,
274                                                "/",
275                                                type,
276                                                instance_id ? "-" : "",
277                                                instance_id ? instance_id : "",
278                                                pid_suffix,
279                                                NULL));
280 }
281
282 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
283 {
284     mutex_cfg_t *defcfg, *mxcfg, *newcfg;
285
286     defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
287
288     /* MUST exist in table, or wasn't registered */
289     mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
290     if (!mxcfg) {
291         return NULL;
292     }
293
294     /* order of precedence:
295      * 1. Mutex directive for this mutex
296      * 2. Mutex directive for "default"
297      * 3. Defaults for this mutex from ap_mutex_register()
298      * 4. Global defaults
299      */
300
301     if (mxcfg->set) {
302         newcfg = mxcfg;
303     }
304     else if (defcfg->set) {
305         newcfg = defcfg;
306     }
307     else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
308         newcfg = mxcfg;
309     }
310     else {
311         newcfg = defcfg;
312     }
313
314     if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
315         /* a file-based mutex mechanism was configured, but
316          * without a mutex file directory; go back through
317          * the chain to find the directory, store in new
318          * mutex cfg structure
319          */
320         newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
321
322         /* !true if dir not already set: mxcfg->set && defcfg->dir */
323         if (defcfg->set && defcfg->dir) {
324             newcfg->dir = defcfg->dir;
325         }
326         else if (mxcfg->dir) {
327             newcfg->dir = mxcfg->dir;
328         }
329         else {
330             newcfg->dir = defcfg->dir;
331         }
332     }
333
334     return newcfg;
335 }
336
337 static void log_bad_create_options(server_rec *s, const char *type)
338 {
339     ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
340                  "Invalid options were specified when creating the %s mutex",
341                  type);
342 }
343
344 static void log_unknown_type(server_rec *s, const char *type)
345 {
346     ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
347                  "Can't create mutex of unknown type %s", type);
348 }
349
350 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
351                                const char *fname)
352 {
353     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
354                  "Couldn't create the %s mutex %s%s%s", type,
355                  fname ? "(file " : "",
356                  fname ? fname : "",
357                  fname ? ")" : "");
358 }
359
360 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
361 {
362     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
363                  "Couldn't set permissions on the %s mutex; "
364                  "check User and Group directives",
365                  type);
366 }
367
368 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
369                                                 const char *type,
370                                                 const char *instance_id,
371                                                 server_rec *s, apr_pool_t *p,
372                                                 apr_int32_t options)
373 {
374     apr_status_t rv;
375     const char *fname;
376     mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
377
378     if (options) {
379         log_bad_create_options(s, type);
380         return APR_EINVAL;
381     }
382
383     if (!mxcfg) {
384         log_unknown_type(s, type);
385         return APR_EINVAL;
386     }
387
388     if (mxcfg->none) {
389         *mutex = NULL;
390         return APR_SUCCESS;
391     }
392
393     fname = get_mutex_filename(p, mxcfg, type, instance_id);
394
395     rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
396     if (rv != APR_SUCCESS) {
397         log_create_failure(rv, s, type, fname);
398         return rv;
399     }
400
401 #ifdef AP_NEED_SET_MUTEX_PERMS
402     rv = ap_unixd_set_global_mutex_perms(*mutex);
403     if (rv != APR_SUCCESS) {
404         log_perms_failure(rv, s, type);
405         return rv;
406     }
407 #endif
408
409     return APR_SUCCESS;
410 }
411
412 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
413                                               const char *type,
414                                               const char *instance_id,
415                                               server_rec *s, apr_pool_t *p,
416                                               apr_int32_t options)
417 {
418     apr_status_t rv;
419     const char *fname;
420     mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
421
422     if (options) {
423         log_bad_create_options(s, type);
424         return APR_EINVAL;
425     }
426
427     if (!mxcfg) {
428         log_unknown_type(s, type);
429         return APR_EINVAL;
430     }
431
432     if (mxcfg->none) {
433         *mutex = NULL;
434         return APR_SUCCESS;
435     }
436
437     fname = get_mutex_filename(p, mxcfg, type, instance_id);
438
439     rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
440     if (rv != APR_SUCCESS) {
441         log_create_failure(rv, s, type, fname);
442         return rv;
443     }
444
445 #ifdef AP_NEED_SET_MUTEX_PERMS
446     rv = ap_unixd_set_proc_mutex_perms(*mutex);
447     if (rv != APR_SUCCESS) {
448         log_perms_failure(rv, s, type);
449         return rv;
450     }
451 #endif
452
453     return APR_SUCCESS;
454 }