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_server_root_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;
168 apr_pool_t *ptemp = cmd->temp_pool;
171 int no_mutex = 0, omit_pid = 0;
172 apr_array_header_t *type_list;
175 const char *mutexdir;
177 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
183 mechdir = ap_getword_conf(cmd->pool, &arg);
184 if (*mechdir == '\0') {
185 return "Mutex requires at least a mechanism argument ("
186 AP_ALL_AVAILABLE_MUTEXES_STRING ")";
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(ptemp, mutexdir))) {
196 return apr_pstrcat(p, "Invalid Mutex directory in argument ",
199 else if (rv == APR_ENOLOCK) { /* "none" */
203 /* "OmitPID" can appear at the end of the list, so build a list of
204 * mutex type names while looking for "OmitPID" (anywhere) or the end
206 type_list = apr_array_make(cmd->pool, 4, sizeof(const char *));
208 const char *s = ap_getword_conf(cmd->pool, &arg);
210 if (!strcasecmp(s, "omitpid")) {
214 const char **new_type = (const char **)apr_array_push(type_list);
219 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */
220 const char **new_type = (const char **)apr_array_push(type_list);
221 *new_type = "default";
224 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) {
225 const char *type = *elt;
226 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
228 return apr_psprintf(p, "Mutex type %s is not valid", type);
231 mxcfg->none = 0; /* in case that was the default */
232 mxcfg->omit_pid = omit_pid;
236 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) {
237 return apr_psprintf(p,
238 "None is not allowed for mutex type %s",
245 if (mutexdir) { /* retain mutex default if not configured */
246 mxcfg->dir = mutexdir;
254 AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
256 const char *default_dir,
257 apr_lockmech_e default_mech,
260 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg);
262 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) {
266 ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */
268 mxcfg->options = options;
269 if (options & AP_MUTEX_DEFAULT_NONE) {
272 mxcfg->dir = default_dir; /* usually NULL */
273 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */
274 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg);
279 static int mutex_needs_file(apr_lockmech_e mech)
281 if (mech != APR_LOCK_FLOCK
282 && mech != APR_LOCK_FCNTL
283 #if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE
284 && mech != APR_LOCK_DEFAULT
292 static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg,
294 const char *instance_id)
296 const char *pid_suffix = "";
298 if (!mutex_needs_file(mxcfg->mech)) {
303 if (!mxcfg->omit_pid) {
304 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid());
308 return ap_server_root_relative(p,
313 instance_id ? "-" : "",
314 instance_id ? instance_id : "",
319 static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type)
321 mutex_cfg_t *defcfg, *mxcfg, *newcfg;
323 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
325 /* MUST exist in table, or wasn't registered */
326 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING);
331 /* order of precedence:
332 * 1. Mutex directive for this mutex
333 * 2. Mutex directive for "default"
334 * 3. Defaults for this mutex from ap_mutex_register()
341 else if (defcfg->set) {
344 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) {
351 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) {
352 /* a file-based mutex mechanism was configured, but
353 * without a mutex file directory; go back through
354 * the chain to find the directory, store in new
355 * mutex cfg structure
357 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg);
359 /* !true if dir not already set: mxcfg->set && defcfg->dir */
360 if (defcfg->set && defcfg->dir) {
361 newcfg->dir = defcfg->dir;
363 else if (mxcfg->dir) {
364 newcfg->dir = mxcfg->dir;
367 newcfg->dir = defcfg->dir;
374 static void log_bad_create_options(server_rec *s, const char *type)
376 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00021)
377 "Invalid options were specified when creating the %s mutex",
381 static void log_unknown_type(server_rec *s, const char *type)
383 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00022)
384 "Can't create mutex of unknown type %s", type);
387 static void log_create_failure(apr_status_t rv, server_rec *s, const char *type,
390 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00023)
391 "Couldn't create the %s mutex %s%s%s", type,
392 fname ? "(file " : "",
397 #ifdef AP_NEED_SET_MUTEX_PERMS
398 static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type)
400 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00024)
401 "Couldn't set permissions on the %s mutex; "
402 "check User and Group directives",
407 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);
444 #ifdef AP_NEED_SET_MUTEX_PERMS
445 rv = ap_unixd_set_global_mutex_perms(*mutex);
446 if (rv != APR_SUCCESS) {
447 log_perms_failure(rv, s, type);
454 AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
457 const char *instance_id,
458 server_rec *s, apr_pool_t *p,
463 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type);
466 log_bad_create_options(s, type);
471 log_unknown_type(s, type);
480 fname = get_mutex_filename(p, mxcfg, type, instance_id);
482 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p);
483 if (rv != APR_SUCCESS) {
484 log_create_failure(rv, s, type, fname);
491 #ifdef AP_NEED_SET_MUTEX_PERMS
492 rv = ap_unixd_set_proc_mutex_perms(*mutex);
493 if (rv != APR_SUCCESS) {
494 log_perms_failure(rv, s, type);
501 AP_CORE_DECLARE(void) ap_dump_mutexes(apr_pool_t *p, server_rec *s, apr_file_t *out)
503 apr_hash_index_t *idx;
504 mutex_cfg_t *defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING);
505 for (idx = apr_hash_first(p, mxcfg_by_type); idx; idx = apr_hash_next(idx))
508 const char *name, *mech;
510 const char *dir = "";
511 apr_hash_this(idx, &name_, NULL, NULL);
513 mxcfg = mxcfg_lookup(p, name);
514 if (mxcfg == defcfg && strcmp(name, "default") != 0) {
515 apr_file_printf(out, "Mutex %s: using_defaults\n", name);
519 apr_file_printf(out, "Mutex %s: none\n", name);
522 switch (mxcfg->mech) {
523 case APR_LOCK_DEFAULT:
526 #if APR_HAS_FCNTL_SERIALIZE
531 #if APR_HAS_FLOCK_SERIALIZE
536 #if APR_HAS_POSIXSEM_SERIALIZE
537 case APR_LOCK_POSIXSEM:
541 #if APR_HAS_SYSVSEM_SERIALIZE
542 case APR_LOCK_SYSVSEM:
546 #if APR_HAS_PROC_PTHREAD_SERIALIZE
547 case APR_LOCK_PROC_PTHREAD:
556 dir = ap_server_root_relative(p, mxcfg->dir);
558 apr_file_printf(out, "Mutex %s: dir=\"%s\" mechanism=%s %s\n", name, dir, mech,
559 mxcfg->omit_pid ? "[OmitPid]" : "");