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"
36 #include "util_mutex.h"
37 #if AP_NEED_SET_MUTEX_PERMS
41 #include <unistd.h> /* getpid() */
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)
48 /* Split arg into meth and file */
49 char *meth = apr_pstrdup(pool, arg);
50 char *file = strchr(meth, ':');
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
64 if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) {
68 /* NOTE: previously, 'yes' implied 'sem' */
69 if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) {
70 *mutexmech = APR_LOCK_DEFAULT;
72 #if APR_HAS_FCNTL_SERIALIZE
73 else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) {
74 *mutexmech = APR_LOCK_FCNTL;
77 #if APR_HAS_FLOCK_SERIALIZE
78 else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) {
79 *mutexmech = APR_LOCK_FLOCK;
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.
90 *mutexfile = apr_pstrdup(pool, file);
96 #if APR_HAS_SYSVSEM_SERIALIZE
97 else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) {
98 *mutexmech = APR_LOCK_SYSVSEM;
101 #if APR_HAS_PROC_PTHREAD_SERIALIZE
102 else if (!strcasecmp(meth, "pthread")) {
103 *mutexmech = APR_LOCK_PROC_PTHREAD;
110 /* Unless the method above assumed responsibility for setting up
111 * mutexfile and NULLing out file, presume it is a file we
115 *mutexfile = ap_server_root_relative(pool, file);
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()
137 static apr_hash_t *mxcfg_by_type;
139 static apr_status_t cleanup_mx_hash(void *dummy)
141 mxcfg_by_type = NULL;
145 static void mx_hash_init(apr_pool_t *p)
153 mxcfg_by_type = apr_hash_make(p);
154 apr_pool_cleanup_register(p, NULL, cleanup_mx_hash, apr_pool_cleanup_null);
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;
164 apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
167 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
171 apr_pool_t *p = cmd->pool;
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);
184 return apr_psprintf(p, "Mutex type %s is not valid", type);
187 mxcfg->none = 0; /* in case that was the default */
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);
194 else if (rv == APR_BADARG
195 || (mutexdir && !ap_is_directory(p, mutexdir))) {
196 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
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",
211 if (mutexdir) { /* retain mutex default if not configured */
212 mxcfg->dir = mutexdir;
219 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
221 const char *default_dir,
222 apr_lockmech_e default_mech,
225 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
227 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
233 mxcfg->options = options;
234 if (options & AP_MUTEX_DEFAULT_NONE) {
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);
244 static int mutex_needs_file(apr_lockmech_e mech)
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
257 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
259 const char *instance_id)
261 const char *pid_suffix = "";
263 if (!mutex_needs_file(mxcfg->mech)) {
268 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
271 return ap_server_root_relative(p,
276 instance_id ? "-" : "",
277 instance_id ? instance_id : "",
282 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
284 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
286 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
288 /* MUST exist in table, or wasn't registered */
289 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
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()
304 else if (defcfg->set) {
307 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
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
320 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
322 /* !true if dir not already set: mxcfg->set && defcfg->dir */
323 if (defcfg->set && defcfg->dir) {
324 newcfg->dir = defcfg->dir;
326 else if (mxcfg->dir) {
327 newcfg->dir = mxcfg->dir;
330 newcfg->dir = defcfg->dir;
337 static void log_bad_create_options(server_rec *s, const char *type)
339 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
340 "Invalid options were specified when creating the %s mutex",
344 static void log_unknown_type(server_rec *s, const char *type)
346 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
347 "Can't create mutex of unknown type %s", type);
350 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
353 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
354 "Couldn't create the %s mutex %s%s%s", type,
355 fname ? "(file " : "",
360 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
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",
368 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
370 const char *instance_id,
371 server_rec *s, apr_pool_t *p,
376 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
379 log_bad_create_options(s, type);
384 log_unknown_type(s, type);
393 fname = get_mutex_filename(p, mxcfg, type, instance_id);
395 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
396 if (rv != APR_SUCCESS) {
397 log_create_failure(rv, s, type, fname);
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);
412 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
414 const char *instance_id,
415 server_rec *s, apr_pool_t *p,
420 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
423 log_bad_create_options(s, type);
428 log_unknown_type(s, type);
437 fname = get_mutex_filename(p, mxcfg, type, instance_id);
439 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
440 if (rv != APR_SUCCESS) {
441 log_create_failure(rv, s, type, fname);
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);