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 APLOG_USE_MODULE(core);
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)
50 /* Split arg into meth and file */
51 char *meth = apr_pstrdup(pool, arg);
52 char *file = strchr(meth, ':');
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
66 if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) {
70 /* NOTE: previously, 'yes' implied 'sem' */
71 if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) {
72 *mutexmech = APR_LOCK_DEFAULT;
74 #if APR_HAS_FCNTL_SERIALIZE
75 else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) {
76 *mutexmech = APR_LOCK_FCNTL;
79 #if APR_HAS_FLOCK_SERIALIZE
80 else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) {
81 *mutexmech = APR_LOCK_FLOCK;
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.
92 *mutexfile = apr_pstrdup(pool, file);
98 #if APR_HAS_SYSVSEM_SERIALIZE
99 else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) {
100 *mutexmech = APR_LOCK_SYSVSEM;
103 #if APR_HAS_PROC_PTHREAD_SERIALIZE
104 else if (!strcasecmp(meth, "pthread")) {
105 *mutexmech = APR_LOCK_PROC_PTHREAD;
112 /* Unless the method above assumed responsibility for setting up
113 * mutexfile and NULLing out file, presume it is a file we
117 *mutexfile = ap_server_root_relative(pool, file);
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()
140 static apr_hash_t *mxcfg_by_type;
142 AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p)
150 mxcfg_by_type = apr_hash_make(p);
151 apr_pool_cleanup_register(p, &mxcfg_by_type, ap_pool_cleanup_set_null,
152 apr_pool_cleanup_null);
154 /* initialize default mutex configuration */
155 def = apr_pcalloc(p, sizeof *def);
156 def->mech = APR_LOCK_DEFAULT;
157 def->dir = DEFAULT_REL_RUNTIMEDIR;
158 apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
161 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
164 apr_pool_t *p = cmd->pool;
167 int no_mutex = 0, omit_pid = 0;
168 apr_array_header_t *type_list;
171 const char *mutexdir;
173 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
179 mechdir = ap_getword_conf(cmd->pool, &arg);
180 if (*mechdir == '\0') {
181 return "Mutex requires at least a mechanism argument ("
182 AP_ALL_AVAILABLE_MUTEXES_STRING ")";
185 rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
186 if (rv == APR_ENOTIMPL) {
187 return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
188 " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
190 else if (rv == APR_BADARG
191 || (mutexdir && !ap_is_directory(p, mutexdir))) {
192 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
195 else if (rv == APR_ENOLOCK) { /* "none" */
199 /* "OmitPID" can appear at the end of the list, so build a list of
200 * mutex type names while looking for "OmitPID" (anywhere) or the end
202 type_list = apr_array_make(cmd->pool, 4, sizeof(const char *));
204 const char *s = ap_getword_conf(cmd->pool, &arg);
206 if (!strcasecmp(s, "omitpid")) {
210 const char **new_type = (const char **)apr_array_push(type_list);
215 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */
216 const char **new_type = (const char **)apr_array_push(type_list);
217 *new_type = "default";
220 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
221 const char *type = *elt;
222 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
224 return apr_psprintf(p, "Mutex type %s is not valid", type);
227 mxcfg->none = 0; /* in case that was the default */
228 mxcfg->omit_pid = omit_pid;
232 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
233 return apr_psprintf(p,
234 "None is not allowed for mutex type %s",
241 if (mutexdir) { /* retain mutex default if not configured */
242 mxcfg->dir = mutexdir;
250 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
252 const char *default_dir,
253 apr_lockmech_e default_mech,
256 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
258 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
262 ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */
264 mxcfg->options = options;
265 if (options & AP_MUTEX_DEFAULT_NONE) {
268 mxcfg->dir = default_dir; /* usually NULL */
269 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
270 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
275 static int mutex_needs_file(apr_lockmech_e mech)
277 if (mech != APR_LOCK_FLOCK
278 && mech != APR_LOCK_FCNTL
279 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
280 && mech != APR_LOCK_DEFAULT
288 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
290 const char *instance_id)
292 const char *pid_suffix = "";
294 if (!mutex_needs_file(mxcfg->mech)) {
299 if (!mxcfg->omit_pid) {
300 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
304 return ap_server_root_relative(p,
309 instance_id ? "-" : "",
310 instance_id ? instance_id : "",
315 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
317 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
319 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
321 /* MUST exist in table, or wasn't registered */
322 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
327 /* order of precedence:
328 * 1. Mutex directive for this mutex
329 * 2. Mutex directive for "default"
330 * 3. Defaults for this mutex from ap_mutex_register()
337 else if (defcfg->set) {
340 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
347 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
348 /* a file-based mutex mechanism was configured, but
349 * without a mutex file directory; go back through
350 * the chain to find the directory, store in new
351 * mutex cfg structure
353 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
355 /* !true if dir not already set: mxcfg->set && defcfg->dir */
356 if (defcfg->set && defcfg->dir) {
357 newcfg->dir = defcfg->dir;
359 else if (mxcfg->dir) {
360 newcfg->dir = mxcfg->dir;
363 newcfg->dir = defcfg->dir;
370 static void log_bad_create_options(server_rec *s, const char *type)
372 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
373 "Invalid options were specified when creating the %s mutex",
377 static void log_unknown_type(server_rec *s, const char *type)
379 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
380 "Can't create mutex of unknown type %s", type);
383 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
386 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
387 "Couldn't create the %s mutex %s%s%s", type,
388 fname ? "(file " : "",
393 #ifdef AP_NEED_SET_MUTEX_PERMS
394 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
396 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
397 "Couldn't set permissions on the %s mutex; "
398 "check User and Group directives",
403 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
406 const char *instance_id,
407 server_rec *s, apr_pool_t *p,
412 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
415 log_bad_create_options(s, type);
420 log_unknown_type(s, type);
429 fname = get_mutex_filename(p, mxcfg, type, instance_id);
431 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
432 if (rv != APR_SUCCESS) {
433 log_create_failure(rv, s, type, fname);
440 #ifdef AP_NEED_SET_MUTEX_PERMS
441 rv = ap_unixd_set_global_mutex_perms(*mutex);
442 if (rv != APR_SUCCESS) {
443 log_perms_failure(rv, s, type);
450 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
453 const char *instance_id,
454 server_rec *s, apr_pool_t *p,
459 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
462 log_bad_create_options(s, type);
467 log_unknown_type(s, type);
476 fname = get_mutex_filename(p, mxcfg, type, instance_id);
478 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
479 if (rv != APR_SUCCESS) {
480 log_create_failure(rv, s, type, fname);
487 #ifdef AP_NEED_SET_MUTEX_PERMS
488 rv = ap_unixd_set_proc_mutex_perms(*mutex);
489 if (rv != APR_SUCCESS) {
490 log_perms_failure(rv, s, type);