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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * util_mutex.c: Useful functions for determining allowable
19 * mutexes and mutex settings
25 #include "apr_strings.h"
28 #define APR_WANT_STRFUNC
31 #include "ap_config.h"
33 #include "http_main.h"
34 #include "http_config.h"
35 #include "http_core.h"
37 #include "util_mutex.h"
38 #if AP_NEED_SET_MUTEX_PERMS
42 #include <unistd.h> /* getpid() */
45 /* we know core's module_index is 0 */
46 #undef APLOG_MODULE_INDEX
47 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
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)
53 /* Split arg into meth and file */
54 char *meth = apr_pstrdup(pool, arg);
55 char *file = strchr(meth, ':');
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
69 if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) {
73 /* NOTE: previously, 'yes' implied 'sem' */
74 if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) {
75 *mutexmech = APR_LOCK_DEFAULT;
77 #if APR_HAS_FCNTL_SERIALIZE
78 else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) {
79 *mutexmech = APR_LOCK_FCNTL;
82 #if APR_HAS_FLOCK_SERIALIZE
83 else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) {
84 *mutexmech = APR_LOCK_FLOCK;
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.
95 *mutexfile = apr_pstrdup(pool, file);
101 #if APR_HAS_SYSVSEM_SERIALIZE
102 else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) {
103 *mutexmech = APR_LOCK_SYSVSEM;
106 #if APR_HAS_PROC_PTHREAD_SERIALIZE
107 else if (!strcasecmp(meth, "pthread")) {
108 *mutexmech = APR_LOCK_PROC_PTHREAD;
115 /* Unless the method above assumed responsibility for setting up
116 * mutexfile and NULLing out file, presume it is a file we
120 *mutexfile = ap_runtime_dir_relative(pool, file);
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()
143 static apr_hash_t *mxcfg_by_type;
145 AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p)
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);
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);
164 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
167 apr_pool_t *p = cmd->pool;
170 int no_mutex = 0, omit_pid = 0;
171 apr_array_header_t *type_list;
174 const char *mutexdir;
176 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
182 mechdir = ap_getword_conf(cmd->pool, &arg);
183 if (*mechdir == '\0') {
184 return "Mutex requires at least a mechanism argument ("
185 AP_ALL_AVAILABLE_MUTEXES_STRING ")";
188 rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
189 if (rv == APR_ENOTIMPL) {
190 return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
191 " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
193 else if (rv == APR_BADARG
194 || (mutexdir && !ap_is_directory(p, mutexdir))) {
195 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
198 else if (rv == APR_ENOLOCK) { /* "none" */
202 /* "OmitPID" can appear at the end of the list, so build a list of
203 * mutex type names while looking for "OmitPID" (anywhere) or the end
205 type_list = apr_array_make(cmd->pool, 4, sizeof(const char *));
207 const char *s = ap_getword_conf(cmd->pool, &arg);
209 if (!strcasecmp(s, "omitpid")) {
213 const char **new_type = (const char **)apr_array_push(type_list);
218 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */
219 const char **new_type = (const char **)apr_array_push(type_list);
220 *new_type = "default";
223 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
224 const char *type = *elt;
225 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
227 return apr_psprintf(p, "Mutex type %s is not valid", type);
230 mxcfg->none = 0; /* in case that was the default */
231 mxcfg->omit_pid = omit_pid;
235 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
236 return apr_psprintf(p,
237 "None is not allowed for mutex type %s",
244 if (mutexdir) { /* retain mutex default if not configured */
245 mxcfg->dir = mutexdir;
253 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
255 const char *default_dir,
256 apr_lockmech_e default_mech,
259 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
261 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
265 ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */
267 mxcfg->options = options;
268 if (options & AP_MUTEX_DEFAULT_NONE) {
271 mxcfg->dir = default_dir; /* usually NULL */
272 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
273 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
278 static int mutex_needs_file(apr_lockmech_e mech)
280 if (mech != APR_LOCK_FLOCK
281 && mech != APR_LOCK_FCNTL
282 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
283 && mech != APR_LOCK_DEFAULT
291 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
293 const char *instance_id)
295 const char *pid_suffix = "";
297 if (!mutex_needs_file(mxcfg->mech)) {
302 if (!mxcfg->omit_pid) {
303 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
307 return ap_runtime_dir_relative(p,
312 instance_id ? "-" : "",
313 instance_id ? instance_id : "",
318 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
320 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
322 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
324 /* MUST exist in table, or wasn't registered */
325 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
330 /* order of precedence:
331 * 1. Mutex directive for this mutex
332 * 2. Mutex directive for "default"
333 * 3. Defaults for this mutex from ap_mutex_register()
340 else if (defcfg->set) {
343 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
350 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
351 /* a file-based mutex mechanism was configured, but
352 * without a mutex file directory; go back through
353 * the chain to find the directory, store in new
354 * mutex cfg structure
356 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
358 /* !true if dir not already set: mxcfg->set && defcfg->dir */
359 if (defcfg->set && defcfg->dir) {
360 newcfg->dir = defcfg->dir;
362 else if (mxcfg->dir) {
363 newcfg->dir = mxcfg->dir;
366 newcfg->dir = defcfg->dir;
373 static void log_bad_create_options(server_rec *s, const char *type)
375 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00021)
376 "Invalid options were specified when creating the %s mutex",
380 static void log_unknown_type(server_rec *s, const char *type)
382 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00022)
383 "Can't create mutex of unknown type %s", type);
386 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
389 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00023)
390 "Couldn't create the %s mutex %s%s%s", type,
391 fname ? "(file " : "",
396 #ifdef AP_NEED_SET_MUTEX_PERMS
397 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
399 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00024)
400 "Couldn't set permissions on the %s mutex; "
401 "check User and Group directives",
406 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
409 const char *instance_id,
410 server_rec *s, apr_pool_t *p,
415 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
418 log_bad_create_options(s, type);
423 log_unknown_type(s, type);
432 fname = get_mutex_filename(p, mxcfg, type, instance_id);
434 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
435 if (rv != APR_SUCCESS) {
436 log_create_failure(rv, s, type, fname);
443 #ifdef AP_NEED_SET_MUTEX_PERMS
444 rv = ap_unixd_set_global_mutex_perms(*mutex);
445 if (rv != APR_SUCCESS) {
446 log_perms_failure(rv, s, type);
453 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
456 const char *instance_id,
457 server_rec *s, apr_pool_t *p,
462 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
465 log_bad_create_options(s, type);
470 log_unknown_type(s, type);
479 fname = get_mutex_filename(p, mxcfg, type, instance_id);
481 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
482 if (rv != APR_SUCCESS) {
483 log_create_failure(rv, s, type, fname);
490 #ifdef AP_NEED_SET_MUTEX_PERMS
491 rv = ap_unixd_set_proc_mutex_perms(*mutex);
492 if (rv != APR_SUCCESS) {
493 log_perms_failure(rv, s, type);
500 AP_CORE_DECLARE(void) ap_dump_mutexes(apr_pool_t *p, server_rec *s, apr_file_t *out)
502 apr_hash_index_t *idx;
503 mutex_cfg_t *defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
504 for (idx = apr_hash_first(p, mxcfg_by_type); idx; idx = apr_hash_next(idx))
507 const char *name, *mech;
509 const char *dir = "";
510 apr_hash_this(idx, &name_, NULL, NULL);
512 mxcfg = mxcfg_lookup(p, name);
513 if (mxcfg == defcfg && strcmp(name, "default") != 0) {
514 apr_file_printf(out, "Mutex %s: using_defaults\n", name);
518 apr_file_printf(out, "Mutex %s: none\n", name);
521 switch (mxcfg->mech) {
522 case APR_LOCK_DEFAULT:
525 #if APR_HAS_FCNTL_SERIALIZE
530 #if APR_HAS_FLOCK_SERIALIZE
535 #if APR_HAS_POSIXSEM_SERIALIZE
536 case APR_LOCK_POSIXSEM:
540 #if APR_HAS_SYSVSEM_SERIALIZE
541 case APR_LOCK_SYSVSEM:
545 #if APR_HAS_PROC_PTHREAD_SERIALIZE
546 case APR_LOCK_PROC_PTHREAD:
555 dir = ap_runtime_dir_relative(p, mxcfg->dir);
557 apr_file_printf(out, "Mutex %s: dir=\"%s\" mechanism=%s %s\n", name, dir, mech,
558 mxcfg->omit_pid ? "[OmitPid]" : "");