util_mutex.c revision cc0bf3af983afd28b83699bb17f53225752e589d
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* Licensed to the Apache Software Foundation (ASF) under one or more
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * contributor license agreements. See the NOTICE file distributed with
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * this work for additional information regarding copyright ownership.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * The ASF licenses this file to You under the Apache License, Version 2.0
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * (the "License"); you may not use this file except in compliance with
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * the License. You may obtain a copy of the License at
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * http://www.apache.org/licenses/LICENSE-2.0
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Unless required by applicable law or agreed to in writing, software
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * distributed under the License is distributed on an "AS IS" BASIS,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * See the License for the specific language governing permissions and
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * limitations under the License.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/*
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * util_mutex.c: Useful functions for determining allowable
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * mutexes and mutex settings
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_strings.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_lib.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#define APR_WANT_STRFUNC
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_want.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#define CORE_PRIVATE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "ap_config.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "httpd.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "http_main.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "http_config.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "util_mutex.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannconst char AP_DECLARE_DATA ap_all_available_mutexes_string[] =
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "Mutex mechanisms are: `none', `default'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FLOCK_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `flock:/path/to/file'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FCNTL_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `fcntl:/path/to/file'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `sysvsem'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#if APR_HAS_POSIXSEM_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `posixsem'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_PROC_PTHREAD_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `pthread'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FLOCK_SERIALIZE || APR_HAS_FCNTL_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `file:/path/to/file'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#if (APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)) || APR_HAS_POSIXSEM_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `sem'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann " ";
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannconst char AP_DECLARE_DATA ap_available_mutexes_string[] =
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "Mutex mechanisms are: `default'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FLOCK_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `flock:/path/to/file'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FCNTL_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `fcntl:/path/to/file'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `sysvsem'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_POSIXSEM_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `posixsem'"
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_PROC_PTHREAD_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `pthread'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FLOCK_SERIALIZE || APR_HAS_FCNTL_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `file:/path/to/file'"
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if (APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)) || APR_HAS_POSIXSEM_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ", `sem'"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann " ";
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel HoltmannAP_DECLARE(apr_status_t) ap_parse_mutex(const char *arg, apr_pool_t *pool,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_lockmech_e *mutexmech,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char **mutexfile)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Split arg into meth and file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann char *meth = apr_pstrdup(pool, arg);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann char *file = strchr(meth, ':');
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (file) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *(file++) = '\0';
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!*file) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!strcasecmp(meth, "none") || !strcasecmp(meth, "no")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_ENOLOCK;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* APR determines temporary filename unless overridden below,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * we presume file indicates an mutexfile is a file path
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * unless the method sets mutexfile=file and NULLs file
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexfile = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* NOTE: previously, 'yes' implied 'sem' */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!strcasecmp(meth, "default") || !strcasecmp(meth, "yes")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_DEFAULT;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FCNTL_SERIALIZE
7cfa80f07e94c3e48703d145ef03a73dd6e7b983Marcel Holtmann else if (!strcasecmp(meth, "fcntl") || !strcasecmp(meth, "file")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_FCNTL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_FLOCK_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else if (!strcasecmp(meth, "flock") || !strcasecmp(meth, "file")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_FLOCK;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_POSIXSEM_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else if (!strcasecmp(meth, "posixsem") || !strcasecmp(meth, "sem")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_POSIXSEM;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Posix/SysV semaphores aren't file based, use the literal name
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * if provided and fall back on APR's default if not. Today, APR
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * will ignore it, but once supported it has an absurdly short limit.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (file) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexfile = apr_pstrdup(pool, file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else if (!strcasecmp(meth, "sysvsem") || !strcasecmp(meth, "sem")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_SYSVSEM;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#if APR_HAS_PROC_PTHREAD_SERIALIZE
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else if (!strcasecmp(meth, "pthread")) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexmech = APR_LOCK_PROC_PTHREAD;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_ENOTIMPL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Unless the method above assumed responsibility for setting up
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * mutexfile and NULLing out file, presume it is a file we
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * are looking to use
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (file) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *mutexfile = ap_server_root_relative(pool, file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!*mutexfile) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_BADARG;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann