]> granicus.if.org Git - apache/blob - server/util_mutex.c
Add an END flag to RewriteRule that acts like L=LAST but also prevents
[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 static apr_status_t cleanup_mx_hash(void *dummy)
143 {
144     mxcfg_by_type = NULL;
145     return APR_SUCCESS;
146 }
147
148 AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p)
149 {
150     mutex_cfg_t *def;
151
152     if (mxcfg_by_type) {
153         return;
154     }
155
156     mxcfg_by_type = apr_hash_make(p);
157     apr_pool_cleanup_register(p, NULL, cleanup_mx_hash, apr_pool_cleanup_null);
158
159     /* initialize default mutex configuration */
160     def = apr_pcalloc(p, sizeof *def);
161     def->mech = APR_LOCK_DEFAULT;
162 #ifdef DEFAULT_REL_RUNTIMEDIR
163     def->dir = DEFAULT_REL_RUNTIMEDIR;
164 #else
165     def->dir = "logs";
166 #endif
167     apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
168 }
169
170 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
171                                             const char *arg)
172 {
173     apr_pool_t *p = cmd->pool;
174     const char **elt;
175     const char *mechdir;
176     int no_mutex = 0, omit_pid = 0;
177     apr_array_header_t *type_list;
178     apr_lockmech_e mech;
179     apr_status_t rv;
180     const char *mutexdir;
181     mutex_cfg_t *mxcfg;
182     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
183
184     if (err != NULL) {
185         return err;
186     }
187
188     mechdir = ap_getword_conf(cmd->pool, &arg);
189     if (*mechdir == '\0') {
190         return "Mutex requires at least a mechanism argument (" 
191                AP_ALL_AVAILABLE_MUTEXES_STRING ")";
192     }
193
194     rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
195     if (rv == APR_ENOTIMPL) {
196         return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
197                            " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
198     }
199     else if (rv == APR_BADARG
200              || (mutexdir && !ap_is_directory(p, mutexdir))) {
201         return apr_pstrcat(p, "Invalid Mutex directory in argument ",
202                            mechdir, NULL);
203     }
204     else if (rv == APR_ENOLOCK) { /* "none" */
205         no_mutex = 1;
206     }
207
208     /* "OmitPID" can appear at the end of the list, so build a list of
209      * mutex type names while looking for "OmitPID" (anywhere) or the end
210      */
211     type_list = apr_array_make(cmd->pool, 4, sizeof(const char *));
212     while (*arg) {
213         const char *s = ap_getword_conf(cmd->pool, &arg);
214
215         if (!strcasecmp(s, "omitpid")) {
216             omit_pid = 1;
217         }
218         else {
219             const char **new_type = (const char **)apr_array_push(type_list);
220             *new_type = s;
221         }
222     }
223
224     if (apr_is_empty_array(type_list)) { /* no mutex type?  assume "default" */
225         const char **new_type = (const char **)apr_array_push(type_list);
226         *new_type = "default";
227     }
228
229     while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
230         const char *type = *elt;
231         mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
232         if (!mxcfg) {
233             return apr_psprintf(p, "Mutex type %s is not valid", type);
234         }
235
236         mxcfg->none = 0; /* in case that was the default */
237         mxcfg->omit_pid = omit_pid;
238
239         mxcfg->set = 1;
240         if (no_mutex) {
241             if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
242                 return apr_psprintf(p,
243                                     "None is not allowed for mutex type %s",
244                                     type);
245             }
246             mxcfg->none = 1;
247         }
248         else {
249             mxcfg->mech = mech;
250             if (mutexdir) { /* retain mutex default if not configured */
251                 mxcfg->dir = mutexdir;
252             }
253         }
254     }
255
256     return NULL;
257 }
258
259 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
260                                            const char *type,
261                                            const char *default_dir,
262                                            apr_lockmech_e default_mech,
263                                            apr_int32_t options)
264 {
265     mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
266
267     if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
268         return APR_EINVAL;
269     }
270
271     ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */
272
273     mxcfg->options = options;
274     if (options & AP_MUTEX_DEFAULT_NONE) {
275         mxcfg->none = 1;
276     }
277     mxcfg->dir = default_dir; /* usually NULL */
278     mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
279     apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
280
281     return APR_SUCCESS;
282 }
283
284 static int mutex_needs_file(apr_lockmech_e mech)
285 {
286     if (mech != APR_LOCK_FLOCK
287         && mech != APR_LOCK_FCNTL
288 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
289         && mech != APR_LOCK_DEFAULT
290 #endif
291         ) {
292         return 0;
293     }
294     return 1;
295 }
296
297 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
298                                       const char *type,
299                                       const char *instance_id)
300 {
301     const char *pid_suffix = "";
302
303     if (!mutex_needs_file(mxcfg->mech)) {
304         return NULL;
305     }
306
307 #if HAVE_UNISTD_H
308     if (!mxcfg->omit_pid) {
309         pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
310     }
311 #endif
312
313     return ap_server_root_relative(p,
314                                    apr_pstrcat(p,
315                                                mxcfg->dir,
316                                                "/",
317                                                type,
318                                                instance_id ? "-" : "",
319                                                instance_id ? instance_id : "",
320                                                pid_suffix,
321                                                NULL));
322 }
323
324 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
325 {
326     mutex_cfg_t *defcfg, *mxcfg, *newcfg;
327
328     defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
329
330     /* MUST exist in table, or wasn't registered */
331     mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
332     if (!mxcfg) {
333         return NULL;
334     }
335
336     /* order of precedence:
337      * 1. Mutex directive for this mutex
338      * 2. Mutex directive for "default"
339      * 3. Defaults for this mutex from ap_mutex_register()
340      * 4. Global defaults
341      */
342
343     if (mxcfg->set) {
344         newcfg = mxcfg;
345     }
346     else if (defcfg->set) {
347         newcfg = defcfg;
348     }
349     else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
350         newcfg = mxcfg;
351     }
352     else {
353         newcfg = defcfg;
354     }
355
356     if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
357         /* a file-based mutex mechanism was configured, but
358          * without a mutex file directory; go back through
359          * the chain to find the directory, store in new
360          * mutex cfg structure
361          */
362         newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
363
364         /* !true if dir not already set: mxcfg->set && defcfg->dir */
365         if (defcfg->set && defcfg->dir) {
366             newcfg->dir = defcfg->dir;
367         }
368         else if (mxcfg->dir) {
369             newcfg->dir = mxcfg->dir;
370         }
371         else {
372             newcfg->dir = defcfg->dir;
373         }
374     }
375
376     return newcfg;
377 }
378
379 static void log_bad_create_options(server_rec *s, const char *type)
380 {
381     ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
382                  "Invalid options were specified when creating the %s mutex",
383                  type);
384 }
385
386 static void log_unknown_type(server_rec *s, const char *type)
387 {
388     ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
389                  "Can't create mutex of unknown type %s", type);
390 }
391
392 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
393                                const char *fname)
394 {
395     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
396                  "Couldn't create the %s mutex %s%s%s", type,
397                  fname ? "(file " : "",
398                  fname ? fname : "",
399                  fname ? ")" : "");
400 }
401
402 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
403 {
404     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
405                  "Couldn't set permissions on the %s mutex; "
406                  "check User and Group directives",
407                  type);
408 }
409
410 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
411                                                 const char **name,
412                                                 const char *type,
413                                                 const char *instance_id,
414                                                 server_rec *s, apr_pool_t *p,
415                                                 apr_int32_t options)
416 {
417     apr_status_t rv;
418     const char *fname;
419     mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
420
421     if (options) {
422         log_bad_create_options(s, type);
423         return APR_EINVAL;
424     }
425
426     if (!mxcfg) {
427         log_unknown_type(s, type);
428         return APR_EINVAL;
429     }
430
431     if (mxcfg->none) {
432         *mutex = NULL;
433         return APR_SUCCESS;
434     }
435
436     fname = get_mutex_filename(p, mxcfg, type, instance_id);
437
438     rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
439     if (rv != APR_SUCCESS) {
440         log_create_failure(rv, s, type, fname);
441         return rv;
442     }
443
444     if (name)
445         *name = fname;
446
447 #ifdef AP_NEED_SET_MUTEX_PERMS
448     rv = ap_unixd_set_global_mutex_perms(*mutex);
449     if (rv != APR_SUCCESS) {
450         log_perms_failure(rv, s, type);
451     }
452 #endif
453
454     return rv;
455 }
456
457 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
458                                               const char **name,
459                                               const char *type,
460                                               const char *instance_id,
461                                               server_rec *s, apr_pool_t *p,
462                                               apr_int32_t options)
463 {
464     apr_status_t rv;
465     const char *fname;
466     mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
467
468     if (options) {
469         log_bad_create_options(s, type);
470         return APR_EINVAL;
471     }
472
473     if (!mxcfg) {
474         log_unknown_type(s, type);
475         return APR_EINVAL;
476     }
477
478     if (mxcfg->none) {
479         *mutex = NULL;
480         return APR_SUCCESS;
481     }
482
483     fname = get_mutex_filename(p, mxcfg, type, instance_id);
484
485     rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
486     if (rv != APR_SUCCESS) {
487         log_create_failure(rv, s, type, fname);
488         return rv;
489     }
490
491     if (name)
492         *name = fname;
493
494 #ifdef AP_NEED_SET_MUTEX_PERMS
495     rv = ap_unixd_set_proc_mutex_perms(*mutex);
496     if (rv != APR_SUCCESS) {
497         log_perms_failure(rv, s, type);
498     }
499 #endif
500
501     return rv;
502 }