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