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 static apr_status_t cleanup_mx_hash(void *dummy)
144 mxcfg_by_type = NULL;
148 AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p)
156 mxcfg_by_type = apr_hash_make(p);
157 apr_pool_cleanup_register(p, NULL, cleanup_mx_hash, apr_pool_cleanup_null);
159 /* initialize default mutex configuration */
160 def = apr_pcalloc(p, sizeof *def);
161 def->mech = APR_LOCK_DEFAULT;
162 #ifdef DEFAULT_REL_RUNTIMEDIR
163 def->dir = DEFAULT_REL_RUNTIMEDIR;
167 apr_hash_set(mxcfg_by_type, "default", APR_HASH_KEY_STRING, def);
170 AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy,
173 apr_pool_t *p = cmd->pool;
176 int no_mutex = 0, omit_pid = 0;
177 apr_array_header_t *type_list;
180 const char *mutexdir;
182 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
188 mechdir = ap_getword_conf(cmd->pool, &arg);
189 if (*mechdir == '\0') {
190 return "Mutex requires at least a mechanism argument ("
191 AP_ALL_AVAILABLE_MUTEXES_STRING ")";
194 rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir);
195 if (rv == APR_ENOTIMPL) {
196 return apr_pstrcat(p, "Invalid Mutex argument ", mechdir,
197 " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL);
199 else if (rv == APR_BADARG
200 || (mutexdir && !ap_is_directory(p, mutexdir))) {
201 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
204 else if (rv == APR_ENOLOCK) { /* "none" */
208 /* "OmitPID" can appear at the end of the list, so build a list of
209 * mutex type names while looking for "OmitPID" (anywhere) or the end
211 type_list = apr_array_make(cmd->pool, 4, sizeof(const char *));
213 const char *s = ap_getword_conf(cmd->pool, &arg);
215 if (!strcasecmp(s, "omitpid")) {
219 const char **new_type = (const char **)apr_array_push(type_list);
224 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */
225 const char **new_type = (const char **)apr_array_push(type_list);
226 *new_type = "default";
229 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
230 const char *type = *elt;
231 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
233 return apr_psprintf(p, "Mutex type %s is not valid", type);
236 mxcfg->none = 0; /* in case that was the default */
237 mxcfg->omit_pid = omit_pid;
241 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
242 return apr_psprintf(p,
243 "None is not allowed for mutex type %s",
250 if (mutexdir) { /* retain mutex default if not configured */
251 mxcfg->dir = mutexdir;
259 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
261 const char *default_dir,
262 apr_lockmech_e default_mech,
265 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
267 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
271 ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */
273 mxcfg->options = options;
274 if (options & AP_MUTEX_DEFAULT_NONE) {
277 mxcfg->dir = default_dir; /* usually NULL */
278 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
279 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
284 static int mutex_needs_file(apr_lockmech_e mech)
286 if (mech != APR_LOCK_FLOCK
287 && mech != APR_LOCK_FCNTL
288 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
289 && mech != APR_LOCK_DEFAULT
297 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
299 const char *instance_id)
301 const char *pid_suffix = "";
303 if (!mutex_needs_file(mxcfg->mech)) {
308 if (!mxcfg->omit_pid) {
309 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
313 return ap_server_root_relative(p,
318 instance_id ? "-" : "",
319 instance_id ? instance_id : "",
324 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
326 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
328 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
330 /* MUST exist in table, or wasn't registered */
331 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
336 /* order of precedence:
337 * 1. Mutex directive for this mutex
338 * 2. Mutex directive for "default"
339 * 3. Defaults for this mutex from ap_mutex_register()
346 else if (defcfg->set) {
349 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
356 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
357 /* a file-based mutex mechanism was configured, but
358 * without a mutex file directory; go back through
359 * the chain to find the directory, store in new
360 * mutex cfg structure
362 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
364 /* !true if dir not already set: mxcfg->set && defcfg->dir */
365 if (defcfg->set && defcfg->dir) {
366 newcfg->dir = defcfg->dir;
368 else if (mxcfg->dir) {
369 newcfg->dir = mxcfg->dir;
372 newcfg->dir = defcfg->dir;
379 static void log_bad_create_options(server_rec *s, const char *type)
381 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
382 "Invalid options were specified when creating the %s mutex",
386 static void log_unknown_type(server_rec *s, const char *type)
388 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
389 "Can't create mutex of unknown type %s", type);
392 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
395 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
396 "Couldn't create the %s mutex %s%s%s", type,
397 fname ? "(file " : "",
402 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
404 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
405 "Couldn't set permissions on the %s mutex; "
406 "check User and Group directives",
410 AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
413 const char *instance_id,
414 server_rec *s, apr_pool_t *p,
419 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
422 log_bad_create_options(s, type);
427 log_unknown_type(s, type);
436 fname = get_mutex_filename(p, mxcfg, type, instance_id);
438 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p);
439 if (rv != APR_SUCCESS) {
440 log_create_failure(rv, s, type, fname);
447 #ifdef AP_NEED_SET_MUTEX_PERMS
448 rv = ap_unixd_set_global_mutex_perms(*mutex);
449 if (rv != APR_SUCCESS) {
450 log_perms_failure(rv, s, type);
457 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
460 const char *instance_id,
461 server_rec *s, apr_pool_t *p,
466 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
469 log_bad_create_options(s, type);
474 log_unknown_type(s, type);
483 fname = get_mutex_filename(p, mxcfg, type, instance_id);
485 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
486 if (rv != APR_SUCCESS) {
487 log_create_failure(rv, s, type, fname);
494 #ifdef AP_NEED_SET_MUTEX_PERMS
495 rv = ap_unixd_set_proc_mutex_perms(*mutex);
496 if (rv != APR_SUCCESS) {
497 log_perms_failure(rv, s, type);