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