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);
133 /* hash is created the first time a module calls ap_mutex_register(),
134 * rather than attempting to be the REALLY_REALLY_FIRST pre-config
135 * hook; it is cleaned up when the associated pool goes away; assume
136 * pconf is the pool passed to ap_mutex_register()
138 static apr_hash_t *mxcfg_by_type;
140 static apr_status_t cleanup_mx_hash(void *dummy)
142 mxcfg_by_type = NULL;
146 static void mx_hash_init(apr_pool_t *p)
154 mxcfg_by_type = apr_hash_make(p);
155 apr_pool_cleanup_register(p, NULL, cleanup_mx_hash, apr_pool_cleanup_null);
157 /* initialize default mutex configuration */
158 def = apr_pcalloc(p, sizeof *def);
159 def->mech = APR_LOCK_DEFAULT;
160 #ifdef DEFAULT_REL_RUNTIMEDIR
161 def->dir = DEFAULT_REL_RUNTIMEDIR;
165 apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
168 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
171 apr_pool_t *p = cmd->pool;
174 int no_mutex = 0, omit_pid = 0;
175 apr_array_header_t *type_list;
178 const char *mutexdir;
180 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
186 mechdir = ap_getword_conf(cmd->pool, &arg);
187 if (*mechdir == '\0') {
188 return "Mutex requires at least a mechanism argument ("
189 AP_ALL_AVAILABLE_MUTEXES_STRING ")";
192 rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
193 if (rv == APR_ENOTIMPL) {
194 return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
195 " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
197 else if (rv == APR_BADARG
198 || (mutexdir && !ap_is_directory(p, mutexdir))) {
199 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
202 else if (rv == APR_ENOLOCK) { /* "none" */
206 /* "OmitPID" can appear at the end of the list, so build a list of
207 * mutex type names while looking for "OmitPID" (anywhere) or the end
209 type_list = apr_array_make(cmd->pool, 4, sizeof(char *));
211 char *s = ap_getword_conf(cmd->pool, &arg);
213 if (!strcasecmp(s, "omitpid")) {
217 char **new_type = (char **)apr_array_push(type_list);
222 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */
223 char **new_type = (char **)apr_array_push(type_list);
224 *new_type = "default";
227 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
228 const char *type = *elt;
229 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
231 return apr_psprintf(p, "Mutex type %s is not valid", type);
234 mxcfg->none = 0; /* in case that was the default */
235 mxcfg->omit_pid = omit_pid;
239 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
240 return apr_psprintf(p,
241 "None is not allowed for mutex type %s",
248 if (mutexdir) { /* retain mutex default if not configured */
249 mxcfg->dir = mutexdir;
257 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
259 const char *default_dir,
260 apr_lockmech_e default_mech,
263 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
265 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
271 mxcfg->options = options;
272 if (options & AP_MUTEX_DEFAULT_NONE) {
275 mxcfg->dir = default_dir; /* usually NULL */
276 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
277 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
282 static int mutex_needs_file(apr_lockmech_e mech)
284 if (mech != APR_LOCK_FLOCK
285 && mech != APR_LOCK_FCNTL
286 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
287 && mech != APR_LOCK_DEFAULT
295 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
297 const char *instance_id)
299 const char *pid_suffix = "";
301 if (!mutex_needs_file(mxcfg->mech)) {
306 if (!mxcfg->omit_pid) {
307 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
311 return ap_server_root_relative(p,
316 instance_id ? "-" : "",
317 instance_id ? instance_id : "",
322 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
324 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
326 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
328 /* MUST exist in table, or wasn't registered */
329 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
334 /* order of precedence:
335 * 1. Mutex directive for this mutex
336 * 2. Mutex directive for "default"
337 * 3. Defaults for this mutex from ap_mutex_register()
344 else if (defcfg->set) {
347 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
354 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
355 /* a file-based mutex mechanism was configured, but
356 * without a mutex file directory; go back through
357 * the chain to find the directory, store in new
358 * mutex cfg structure
360 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
362 /* !true if dir not already set: mxcfg->set && defcfg->dir */
363 if (defcfg->set && defcfg->dir) {
364 newcfg->dir = defcfg->dir;
366 else if (mxcfg->dir) {
367 newcfg->dir = mxcfg->dir;
370 newcfg->dir = defcfg->dir;
377 static void log_bad_create_options(server_rec *s, const char *type)
379 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
380 "Invalid options were specified when creating the %s mutex",
384 static void log_unknown_type(server_rec *s, const char *type)
386 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
387 "Can't create mutex of unknown type %s", type);
390 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
393 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
394 "Couldn't create the %s mutex %s%s%s", type,
395 fname ? "(file " : "",
400 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
402 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
403 "Couldn't set permissions on the %s mutex; "
404 "check User and Group directives",
408 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
410 const char *instance_id,
411 server_rec *s, apr_pool_t *p,
416 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
419 log_bad_create_options(s, type);
424 log_unknown_type(s, type);
433 fname = get_mutex_filename(p, mxcfg, type, instance_id);
435 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
436 if (rv != APR_SUCCESS) {
437 log_create_failure(rv, s, type, fname);
441 #ifdef AP_NEED_SET_MUTEX_PERMS
442 rv = ap_unixd_set_global_mutex_perms(*mutex);
443 if (rv != APR_SUCCESS) {
444 log_perms_failure(rv, s, type);
452 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
454 const char *instance_id,
455 server_rec *s, apr_pool_t *p,
460 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
463 log_bad_create_options(s, type);
468 log_unknown_type(s, type);
477 fname = get_mutex_filename(p, mxcfg, type, instance_id);
479 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
480 if (rv != APR_SUCCESS) {
481 log_create_failure(rv, s, type, fname);
485 #ifdef AP_NEED_SET_MUTEX_PERMS
486 rv = ap_unixd_set_proc_mutex_perms(*mutex);
487 if (rv != APR_SUCCESS) {
488 log_perms_failure(rv, s, type);