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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17/* 18 * util_mutex.c: Useful functions for determining allowable 19 * mutexes and mutex settings 20 */ 21 22 23#include "apr.h" 24#include "apr_hash.h" 25#include "apr_strings.h" 26#include "apr_lib.h" 27 28#define APR_WANT_STRFUNC 29#include "apr_want.h" 30 31#include "ap_config.h" 32#include "httpd.h" 33#include "http_main.h" 34#include "http_config.h" 35#include "http_core.h" 36#include "http_log.h" 37#include "util_mutex.h" 38#if AP_NEED_SET_MUTEX_PERMS 39#include "unixd.h" 40#endif 41#ifdef HAVE_UNISTD_H 42#include <unistd.h> /* getpid() */ 43#endif 44 45/* we know core's module_index is 0 */ 46#undef APLOG_MODULE_INDEX 47#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX 48 49AP_DECLARE(apr_status_t) ap_parse_mutex(const char *arg, apr_pool_t *pool, 50 apr_lockmech_e *mutexmech, 51 const char **mutexfile) 52{ 53 /* Split arg into meth and file */ 54 char *meth = apr_pstrdup(pool, arg); 55 char *file = strchr(meth, ':'); 56 if (file) { 57 *(file++) = '\0'; 58 if (!*file) { 59 file = NULL; 60 } 61 } 62 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 66 */ 67 *mutexfile = NULL; 68 69 if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) { 70 return APR_ENOLOCK; 71 } 72 73 /* NOTE: previously, 'yes' implied 'sem' */ 74 if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) { 75 *mutexmech = APR_LOCK_DEFAULT; 76 } 77#if APR_HAS_FCNTL_SERIALIZE 78 else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) { 79 *mutexmech = APR_LOCK_FCNTL; 80 } 81#endif 82#if APR_HAS_FLOCK_SERIALIZE 83 else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) { 84 *mutexmech = APR_LOCK_FLOCK; 85 } 86#endif 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. 93 */ 94 if (file) { 95 *mutexfile = apr_pstrdup(pool, file); 96 97 file = NULL; 98 } 99 } 100#endif 101#if APR_HAS_SYSVSEM_SERIALIZE 102 else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) { 103 *mutexmech = APR_LOCK_SYSVSEM; 104 } 105#endif 106#if APR_HAS_PROC_PTHREAD_SERIALIZE 107 else if (!strcasecmp(meth, "pthread")) { 108 *mutexmech = APR_LOCK_PROC_PTHREAD; 109 } 110#endif 111 else { 112 return APR_ENOTIMPL; 113 } 114 115 /* Unless the method above assumed responsibility for setting up 116 * mutexfile and NULLing out file, presume it is a file we 117 * are looking to use 118 */ 119 if (file) { 120 *mutexfile = ap_server_root_relative(pool, file); 121 if (!*mutexfile) { 122 return APR_BADARG; 123 } 124 } 125 126 return APR_SUCCESS; 127} 128 129typedef struct { 130 apr_int32_t options; 131 int set; 132 int none; 133 int omit_pid; 134 apr_lockmech_e mech; 135 const char *dir; 136} mutex_cfg_t; 137 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() 142 */ 143static apr_hash_t *mxcfg_by_type; 144 145AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p) 146{ 147 mutex_cfg_t *def; 148 149 if (mxcfg_by_type) { 150 return; 151 } 152 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); 156 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); 162} 163 164AP_DECLARE_NONSTD(const char *)ap_set_mutex(cmd_parms *cmd, void *dummy, 165 const char *arg) 166{ 167 apr_pool_t *p = cmd->pool; 168 const char **elt; 169 const char *mechdir; 170 int no_mutex = 0, omit_pid = 0; 171 apr_array_header_t *type_list; 172 apr_lockmech_e mech; 173 apr_status_t rv; 174 const char *mutexdir; 175 mutex_cfg_t *mxcfg; 176 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); 177 178 if (err != NULL) { 179 return err; 180 } 181 182 mechdir = ap_getword_conf(cmd->pool, &arg); 183 if (*mechdir == '\0') { 184 return "Mutex requires at least a mechanism argument (" 185 AP_ALL_AVAILABLE_MUTEXES_STRING ")"; 186 } 187 188 rv = ap_parse_mutex(mechdir, p, &mech, &mutexdir); 189 if (rv == APR_ENOTIMPL) { 190 return apr_pstrcat(p, "Invalid Mutex argument ", mechdir, 191 " (" AP_ALL_AVAILABLE_MUTEXES_STRING ")", NULL); 192 } 193 else if (rv == APR_BADARG 194 || (mutexdir && !ap_is_directory(p, mutexdir))) { 195 return apr_pstrcat(p, "Invalid Mutex directory in argument ", 196 mechdir, NULL); 197 } 198 else if (rv == APR_ENOLOCK) { /* "none" */ 199 no_mutex = 1; 200 } 201 202 /* "OmitPID" can appear at the end of the list, so build a list of 203 * mutex type names while looking for "OmitPID" (anywhere) or the end 204 */ 205 type_list = apr_array_make(cmd->pool, 4, sizeof(const char *)); 206 while (*arg) { 207 const char *s = ap_getword_conf(cmd->pool, &arg); 208 209 if (!strcasecmp(s, "omitpid")) { 210 omit_pid = 1; 211 } 212 else { 213 const char **new_type = (const char **)apr_array_push(type_list); 214 *new_type = s; 215 } 216 } 217 218 if (apr_is_empty_array(type_list)) { /* no mutex type? assume "default" */ 219 const char **new_type = (const char **)apr_array_push(type_list); 220 *new_type = "default"; 221 } 222 223 while ((elt = (const char **)apr_array_pop(type_list)) != NULL) { 224 const char *type = *elt; 225 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING); 226 if (!mxcfg) { 227 return apr_psprintf(p, "Mutex type %s is not valid", type); 228 } 229 230 mxcfg->none = 0; /* in case that was the default */ 231 mxcfg->omit_pid = omit_pid; 232 233 mxcfg->set = 1; 234 if (no_mutex) { 235 if (!(mxcfg->options & AP_MUTEX_ALLOW_NONE)) { 236 return apr_psprintf(p, 237 "None is not allowed for mutex type %s", 238 type); 239 } 240 mxcfg->none = 1; 241 } 242 else { 243 mxcfg->mech = mech; 244 if (mutexdir) { /* retain mutex default if not configured */ 245 mxcfg->dir = mutexdir; 246 } 247 } 248 } 249 250 return NULL; 251} 252 253AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf, 254 const char *type, 255 const char *default_dir, 256 apr_lockmech_e default_mech, 257 apr_int32_t options) 258{ 259 mutex_cfg_t *mxcfg = apr_pcalloc(pconf, sizeof *mxcfg); 260 261 if ((options & ~(AP_MUTEX_ALLOW_NONE | AP_MUTEX_DEFAULT_NONE))) { 262 return APR_EINVAL; 263 } 264 265 ap_mutex_init(pconf); /* in case this mod's pre-config ran before core's */ 266 267 mxcfg->options = options; 268 if (options & AP_MUTEX_DEFAULT_NONE) { 269 mxcfg->none = 1; 270 } 271 mxcfg->dir = default_dir; /* usually NULL */ 272 mxcfg->mech = default_mech; /* usually APR_LOCK_DEFAULT */ 273 apr_hash_set(mxcfg_by_type, type, APR_HASH_KEY_STRING, mxcfg); 274 275 return APR_SUCCESS; 276} 277 278static int mutex_needs_file(apr_lockmech_e mech) 279{ 280 if (mech != APR_LOCK_FLOCK 281 && mech != APR_LOCK_FCNTL 282#if APR_USE_FLOCK_SERIALIZE || APR_USE_FCNTL_SERIALIZE 283 && mech != APR_LOCK_DEFAULT 284#endif 285 ) { 286 return 0; 287 } 288 return 1; 289} 290 291static const char *get_mutex_filename(apr_pool_t *p, mutex_cfg_t *mxcfg, 292 const char *type, 293 const char *instance_id) 294{ 295 const char *pid_suffix = ""; 296 297 if (!mutex_needs_file(mxcfg->mech)) { 298 return NULL; 299 } 300 301#if HAVE_UNISTD_H 302 if (!mxcfg->omit_pid) { 303 pid_suffix = apr_psprintf(p, ".%" APR_PID_T_FMT, getpid()); 304 } 305#endif 306 307 return ap_server_root_relative(p, 308 apr_pstrcat(p, 309 mxcfg->dir, 310 "/", 311 type, 312 instance_id ? "-" : "", 313 instance_id ? instance_id : "", 314 pid_suffix, 315 NULL)); 316} 317 318static mutex_cfg_t *mxcfg_lookup(apr_pool_t *p, const char *type) 319{ 320 mutex_cfg_t *defcfg, *mxcfg, *newcfg; 321 322 defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING); 323 324 /* MUST exist in table, or wasn't registered */ 325 mxcfg = apr_hash_get(mxcfg_by_type, type, APR_HASH_KEY_STRING); 326 if (!mxcfg) { 327 return NULL; 328 } 329 330 /* order of precedence: 331 * 1. Mutex directive for this mutex 332 * 2. Mutex directive for "default" 333 * 3. Defaults for this mutex from ap_mutex_register() 334 * 4. Global defaults 335 */ 336 337 if (mxcfg->set) { 338 newcfg = mxcfg; 339 } 340 else if (defcfg->set) { 341 newcfg = defcfg; 342 } 343 else if (mxcfg->none || mxcfg->mech != APR_LOCK_DEFAULT) { 344 newcfg = mxcfg; 345 } 346 else { 347 newcfg = defcfg; 348 } 349 350 if (!newcfg->none && mutex_needs_file(newcfg->mech) && !newcfg->dir) { 351 /* a file-based mutex mechanism was configured, but 352 * without a mutex file directory; go back through 353 * the chain to find the directory, store in new 354 * mutex cfg structure 355 */ 356 newcfg = apr_pmemdup(p, newcfg, sizeof *newcfg); 357 358 /* !true if dir not already set: mxcfg->set && defcfg->dir */ 359 if (defcfg->set && defcfg->dir) { 360 newcfg->dir = defcfg->dir; 361 } 362 else if (mxcfg->dir) { 363 newcfg->dir = mxcfg->dir; 364 } 365 else { 366 newcfg->dir = defcfg->dir; 367 } 368 } 369 370 return newcfg; 371} 372 373static void log_bad_create_options(server_rec *s, const char *type) 374{ 375 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00021) 376 "Invalid options were specified when creating the %s mutex", 377 type); 378} 379 380static void log_unknown_type(server_rec *s, const char *type) 381{ 382 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(00022) 383 "Can't create mutex of unknown type %s", type); 384} 385 386static void log_create_failure(apr_status_t rv, server_rec *s, const char *type, 387 const char *fname) 388{ 389 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00023) 390 "Couldn't create the %s mutex %s%s%s", type, 391 fname ? "(file " : "", 392 fname ? fname : "", 393 fname ? ")" : ""); 394} 395 396#ifdef AP_NEED_SET_MUTEX_PERMS 397static void log_perms_failure(apr_status_t rv, server_rec *s, const char *type) 398{ 399 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00024) 400 "Couldn't set permissions on the %s mutex; " 401 "check User and Group directives", 402 type); 403} 404#endif 405 406AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex, 407 const char **name, 408 const char *type, 409 const char *instance_id, 410 server_rec *s, apr_pool_t *p, 411 apr_int32_t options) 412{ 413 apr_status_t rv; 414 const char *fname; 415 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type); 416 417 if (options) { 418 log_bad_create_options(s, type); 419 return APR_EINVAL; 420 } 421 422 if (!mxcfg) { 423 log_unknown_type(s, type); 424 return APR_EINVAL; 425 } 426 427 if (mxcfg->none) { 428 *mutex = NULL; 429 return APR_SUCCESS; 430 } 431 432 fname = get_mutex_filename(p, mxcfg, type, instance_id); 433 434 rv = apr_global_mutex_create(mutex, fname, mxcfg->mech, p); 435 if (rv != APR_SUCCESS) { 436 log_create_failure(rv, s, type, fname); 437 return rv; 438 } 439 440 if (name) 441 *name = fname; 442 443#ifdef AP_NEED_SET_MUTEX_PERMS 444 rv = ap_unixd_set_global_mutex_perms(*mutex); 445 if (rv != APR_SUCCESS) { 446 log_perms_failure(rv, s, type); 447 } 448#endif 449 450 return rv; 451} 452 453AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex, 454 const char **name, 455 const char *type, 456 const char *instance_id, 457 server_rec *s, apr_pool_t *p, 458 apr_int32_t options) 459{ 460 apr_status_t rv; 461 const char *fname; 462 mutex_cfg_t *mxcfg = mxcfg_lookup(p, type); 463 464 if (options) { 465 log_bad_create_options(s, type); 466 return APR_EINVAL; 467 } 468 469 if (!mxcfg) { 470 log_unknown_type(s, type); 471 return APR_EINVAL; 472 } 473 474 if (mxcfg->none) { 475 *mutex = NULL; 476 return APR_SUCCESS; 477 } 478 479 fname = get_mutex_filename(p, mxcfg, type, instance_id); 480 481 rv = apr_proc_mutex_create(mutex, fname, mxcfg->mech, p); 482 if (rv != APR_SUCCESS) { 483 log_create_failure(rv, s, type, fname); 484 return rv; 485 } 486 487 if (name) 488 *name = fname; 489 490#ifdef AP_NEED_SET_MUTEX_PERMS 491 rv = ap_unixd_set_proc_mutex_perms(*mutex); 492 if (rv != APR_SUCCESS) { 493 log_perms_failure(rv, s, type); 494 } 495#endif 496 497 return rv; 498} 499 500AP_CORE_DECLARE(void) ap_dump_mutexes(apr_pool_t *p, server_rec *s, apr_file_t *out) 501{ 502 apr_hash_index_t *idx; 503 mutex_cfg_t *defcfg = apr_hash_get(mxcfg_by_type, "default", APR_HASH_KEY_STRING); 504 for (idx = apr_hash_first(p, mxcfg_by_type); idx; idx = apr_hash_next(idx)) 505 { 506 mutex_cfg_t *mxcfg; 507 const char *name, *mech; 508 const void *name_; 509 const char *dir = ""; 510 apr_hash_this(idx, &name_, NULL, NULL); 511 name = name_; 512 mxcfg = mxcfg_lookup(p, name); 513 if (mxcfg == defcfg && strcmp(name, "default") != 0) { 514 apr_file_printf(out, "Mutex %s: using_defaults\n", name); 515 continue; 516 } 517 if (mxcfg->none) { 518 apr_file_printf(out, "Mutex %s: none\n", name); 519 continue; 520 } 521 switch (mxcfg->mech) { 522 case APR_LOCK_DEFAULT: 523 mech = "default"; 524 break; 525#if APR_HAS_FCNTL_SERIALIZE 526 case APR_LOCK_FCNTL: 527 mech = "fcntl"; 528 break; 529#endif 530#if APR_HAS_FLOCK_SERIALIZE 531 case APR_LOCK_FLOCK: 532 mech = "flock"; 533 break; 534#endif 535#if APR_HAS_POSIXSEM_SERIALIZE 536 case APR_LOCK_POSIXSEM: 537 mech = "posixsem"; 538 break; 539#endif 540#if APR_HAS_SYSVSEM_SERIALIZE 541 case APR_LOCK_SYSVSEM: 542 mech = "sysvsem"; 543 break; 544#endif 545#if APR_HAS_PROC_PTHREAD_SERIALIZE 546 case APR_LOCK_PROC_PTHREAD: 547 mech = "pthread"; 548 break; 549#endif 550 default: 551 ap_assert(0); 552 } 553 554 if (mxcfg->dir) 555 dir = ap_server_root_relative(p, mxcfg->dir); 556 557 apr_file_printf(out, "Mutex %s: dir=\"%s\" mechanism=%s %s\n", name, dir, mech, 558 mxcfg->omit_pid ? "[OmitPid]" : ""); 559 } 560} 561