mod_example_ipc.c revision 5bfaaf573bacb45c1cf290ce85ecc676587e8a64
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* mod_example_ipc -- Apache sample module
*
* This module illustrates the use in an Apache 2.x module of the Interprocess
* Communications routines that come with APR. It is example code, and not meant
* to be used in a production server.
*
* To play with this sample module first compile it into a DSO file and install
* it into Apache's modules directory by running:
*
*
* Then activate it in Apache's httpd.conf file for instance for the URL
* /example_ipc in as follows:
*
* # httpd.conf
* LoadModule example_ipc_module modules/mod_example_ipc.so
* <Location /example_ipc>
* SetHandler example_ipc
* </Location>
*
* Then restart Apache via
*
*
* The module allocates a counter in shared memory, which is incremented by the
* request handler under a mutex. After installation, activate the handler by
* hitting the URL configured above with ab at various concurrency levels to see
* how mutex contention affects server performance.
*/
#include "apr.h"
#include "apr_strings.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "util_mutex.h"
#include "ap_config.h"
#endif
#include <unistd.h>
#endif
#define HTML_HEADER "<html>\n<head>\n<title>Mod_example_IPC Status Page " \
"</title>\n</head>\n<body>\n<h1>Mod_example_IPC Status</h1>\n"
#define HTML_FOOTER "</body>\n</html>\n"
/* Number of microseconds to camp out on the mutex */
#define CAMPOUT 10
/* Maximum number of times we camp out before giving up */
#define MAXCAMP 10
/* Number of microseconds the handler sits on the lock once acquired. */
#define SLEEPYTIME 1000
char *shmfilename; /* Shared memory file name, used on some systems */
static const char *exipc_mutex_type = "example-ipc-shm";
/* Data structure for shared memory block */
typedef struct exipc_data {
/* More fields if necessary */
} exipc_data;
/*
* Clean up the shared memory block. This function is registered as
* cleanup function for the configuration pool, which gets called
* on restarts. It assures that the new children will not talk to a stale
* shared memory segment.
*/
if (exipc_shm)
return apr_shm_destroy(exipc_shm);
return OK;
}
/*
* This routine is called in the parent; we must register our
* mutex type before the config is processed so that users can
* adjust the mutex settings using the Mutex directive.
*/
{
return OK;
}
/*
* This routine is called in the parent, so we'll set up the shared
* memory segment and mutex here.
*/
{
const char *tempdir;
/*
* Do nothing if we are not creating the final configuration.
* The parent process gets initialized a couple of times as the
* server starts up, and we don't want to create any more mutexes
* and shared memory segments than we're actually going to use.
*/
return OK;
/*
* The shared memory allocation routines take a file name.
* Depending on system-specific implementation of these
* routines, that file may or may not actually be created. We'd
* like to store those files in the operating system's designated
* temporary directory, which APR can point us to.
*/
if (APR_SUCCESS != rs) {
"Failed to find temporary directory");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Create the shared memory segment */
/*
* Create a unique filename using our pid. This information is
* stashed in the global variable so the children inherit it.
*/
(long int)getpid());
/* Now create that segment */
(const char *) shmfilename, pconf);
if (APR_SUCCESS != rs) {
"Failed to create shared memory segment on file %s",
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Created it, now let's zero it out */
/* Create global mutex */
s, pconf, 0);
if (APR_SUCCESS != rs) {
return HTTP_INTERNAL_SERVER_ERROR;
}
/*
* Destroy the shm segment when the configuration pool gets destroyed. This
* happens on server restarts. The parent will then (above) allocate a new
* shm segment that the new children will bind to.
*/
return OK;
}
/*
* This routine gets called when a child inits. We use it to attach
* to the shared memory segment, and reinitialize the mutex.
*/
{
/*
* Re-open the mutex for the child. Note we're reusing
* the mutex pointer global here.
*/
p);
if (APR_SUCCESS != rs) {
"Failed to reopen mutex %s in child",
/* There's really nothing else we can do here, since This
* routine doesn't return a status. If this ever goes wrong,
* it will turn Apache into a fork bomb. Let's hope it never
* will.
*/
}
}
/* The sample content handler */
static int exipc_handler(request_rec *r)
{
int gotlock = 0;
int camped;
return DECLINED;
}
/*
* The main function of the handler, aside from sending the
* status page to the client, is to increment the counter in
* the shared memory segment. This action needs to be mutexed
* out using the global mutex.
*/
/*
* First, acquire the lock. This code is a lot more involved than
* it usually needs to be, because the process based trylock
* routine is not implemented on unix platforms. I left it in to
* show how it would work if trylock worked, and for situations
* and platforms where trylock works.
*/
if (APR_STATUS_IS_EBUSY(rs)) {
} else if (APR_SUCCESS == rs) {
gotlock = 1;
break; /* Get out of the loop */
} else if (APR_STATUS_IS_ENOTIMPL(rs)) {
/* If it's not implemented, just hang in the mutex. */
startcamp = apr_time_now();
if (APR_SUCCESS == rs) {
gotlock = 1;
break; /* Out of the loop */
} else {
/* Some error, log and bail */
"Child %ld failed to acquire lock",
(long int)getpid());
break; /* Out of the loop without having the lock */
}
} else {
/* Some other error, log and bail */
"Child %ld failed to try and acquire lock",
(long int)getpid());
break; /* Out of the loop without having the lock */
}
/*
* The only way to get to this point is if the trylock worked
* and returned BUSY. So, bump the time and try again
*/
timecamped += CAMPOUT;
" microseconds",
(long int) getpid(), timecamped);
} /* Lock acquisition loop */
/* Sleep for a millisecond to make it a little harder for
* httpd children to acquire the lock.
*/
r->content_type = "text/html";
if (!r->header_only) {
ap_rputs(HTML_HEADER, r);
if (gotlock) {
/* Increment the counter */
/* Send a page with our pid and the new value of the counter. */
ap_rprintf(r, "<p>Lock acquired after %ld microseoncds.</p>\n",
(long int) timecamped);
ap_rputs("<table border=\"1\">\n", r);
ap_rprintf(r, "<tr><td>Child pid:</td><td>%d</td></tr>\n",
(int) getpid());
ap_rprintf(r, "<tr><td>Counter:</td><td>%u</td></tr>\n",
ap_rputs("</table>\n", r);
} else {
/*
* Send a page saying that we couldn't get the lock. Don't say
* what the counter is, because without the lock the value could
* race.
*/
ap_rprintf(r, "<p>Child %d failed to acquire lock "
"after camping out for %d microseconds.</p>\n",
(int) getpid(), (int) timecamped);
}
ap_rputs(HTML_FOOTER, r);
} /* r->header_only */
/* Release the lock */
if (gotlock)
/* Swallowing the result because what are we going to do with it at
* this stage?
*/
return OK;
}
static void exipc_register_hooks(apr_pool_t *p)
{
}
/* Dispatch list for API hooks */
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
exipc_register_hooks /* register hooks */
};