simple_api.c revision 19af44d09017c09443615b57a479ea8d80ba0e4c
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* Licensed to the Apache Software Foundation (ASF) under one or more
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * contributor license agreements. See the NOTICE file distributed with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * this work for additional information regarding copyright ownership.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * The ASF licenses this file to You under the Apache License, Version 2.0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * (the "License"); you may not use this file except in compliance with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * the License. You may obtain a copy of the License at
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * http://www.apache.org/licenses/LICENSE-2.0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Unless required by applicable law or agreed to in writing, software
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * distributed under the License is distributed on an "AS IS" BASIS,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * See the License for the specific language governing permissions and
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * limitations under the License.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "ap_mpm.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "httpd.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "http_config.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "mpm_common.h"
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd#include "http_log.h"
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi#include "scoreboard.h"
27dcd8d81085fd60aadcd8a9bad35a607b26b758nilgun#include "ap_listen.h"
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd#include "simple_types.h"
e609c337f729875bc20e01096c7e610f45356f54nilgun#include "simple_run.h"
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung#include "http_core.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem/* This file contains the absolute minimal MPM API, to interface with httpd. */
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int simple_run(apr_pool_t * pconf, apr_pool_t * plog, server_rec * s)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd simple_core_t *sc = simple_core_get();
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->mpm_state = AP_MPMQ_RUNNING;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->mpm_state = AP_MPMQ_STOPPING;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return 1;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return simple_main_loop(sc);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic apr_status_t simple_query(int query_code, int *result)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd simple_core_t *sc = simple_core_get();
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd switch (query_code) {
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak case AP_MPMQ_IS_THREADED:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = AP_MPMQ_STATIC;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_IS_FORKED:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = AP_MPMQ_DYNAMIC;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_IS_ASYNC:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = 1;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak case AP_MPMQ_MAX_DAEMON_USED:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.proc_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_HARD_LIMIT_DAEMONS:
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak *result = sc->procmgr.proc_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak case AP_MPMQ_HARD_LIMIT_THREADS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.thread_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak case AP_MPMQ_MAX_THREADS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.thread_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MAX_SPARE_DAEMONS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.proc_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MIN_SPARE_DAEMONS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.proc_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MIN_SPARE_THREADS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MAX_SPARE_THREADS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.thread_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MAX_REQUESTS_DAEMON:
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak *result = sc->procmgr.max_requests_per_child;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MAX_DAEMONS:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->procmgr.proc_count;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_MPM_STATE:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = sc->mpm_state;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd case AP_MPMQ_GENERATION:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *result = 0;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd default:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd break;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak return APR_ENOTIMPL;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndsimple_open_logs(apr_pool_t * p,
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak int nsock;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd nsock = ap_setup_listeners(s);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (nsock < 1) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd s,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_open_logs: no listening sockets available, shutting down");
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak return DONE;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return OK;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniakstatic int
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndsimple_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp)
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd int run_debug;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_status_t rv;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd simple_core_t *sc;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak /* this is our first 'real' entry point, so setup everything here. */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = simple_core_init_once();
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (rv) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_core_init_once: Fatal Error Encountered");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return HTTP_INTERNAL_SERVER_ERROR;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak sc = simple_core_get();
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->restart_num++;
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd run_debug = ap_exists_config_define("DEBUG");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak if (run_debug) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->run_foreground = 1;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->run_single_process = 1;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak else {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->run_foreground = ap_exists_config_define("FOREGROUND");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (sc->restart_num == 2) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (sc->run_foreground) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = apr_proc_detach(APR_PROC_DETACH_FOREGROUND);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd else {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (rv) {
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_pre_config: apr_proc_detach(%s) failed",
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sc->run_foreground ? "FOREGROUND" : "DAEMONIZE");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return HTTP_INTERNAL_SERVER_ERROR;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return OK;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndsimple_check_config(apr_pool_t * p, apr_pool_t * plog,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_pool_t * ptemp, server_rec * s)
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd simple_core_t *sc = simple_core_get();
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (sc->procmgr.proc_count > SIMPLE_MAX_PROC) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_check_config: SimpleProcCount must be at most %d",
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd SIMPLE_MAX_PROC);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return !OK;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (sc->procmgr.proc_count < SIMPLE_MIN_PROC) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_check_config: SimpleProcCount must be at least %d",
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd SIMPLE_MIN_PROC);
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi return !OK;
27dcd8d81085fd60aadcd8a9bad35a607b26b758nilgun }
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
e609c337f729875bc20e01096c7e610f45356f54nilgun if (sc->procmgr.thread_count > SIMPLE_MAX_THREADS) {
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "simple_check_config: SimpleThreadCount must be at most %d",
9c1260efa52c82c2a58e5b5f20cd6902563d95f5rbowen SIMPLE_MAX_THREADS);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return !OK;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
if (sc->procmgr.thread_count < SIMPLE_MIN_THREADS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
"simple_check_config: SimpleThreadCount must be at least %d",
SIMPLE_MIN_THREADS);
return !OK;
}
return OK;
}
static void simple_hooks(apr_pool_t * p)
{
static const char *const aszSucc[] = { "core.c", NULL };
ap_hook_open_logs(simple_open_logs, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
ap_hook_pre_config(simple_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
ap_hook_check_config(simple_check_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_mpm(simple_run, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_mpm_query(simple_query, NULL, NULL, APR_HOOK_MIDDLE);
}
static const char *set_proccount(cmd_parms * cmd, void *baton,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
simple_core_get()->procmgr.proc_count = atoi(arg);
return NULL;
}
static const char *set_threadcount(cmd_parms * cmd, void *baton,
const char *arg)
{
simple_core_t *sc = simple_core_get();
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sc->procmgr.thread_count = atoi(arg);
return NULL;
}
static const command_rec simple_cmds[] = {
AP_INIT_TAKE1("SimpleProcCount", set_proccount, NULL, RSRC_CONF,
"Number of child processes launched at server startup"),
AP_INIT_TAKE1("SimpleThreadCount", set_threadcount, NULL, RSRC_CONF,
"Set the number of Worker Threads Per-Process"),
/* pqXXXXXXXXX: These do NOT belong in the MPM configuration commands. */
LISTEN_COMMANDS,
{NULL}
};
module AP_MODULE_DECLARE_DATA mpm_simple_module = {
MPM20_MODULE_STUFF,
NULL, /* hook to run before apache parses args */
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
simple_cmds, /* command apr_table_t */
simple_hooks /* register_hooks */
};