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.
20 #include "http_config.h"
21 #include "mpm_common.h"
23 #include "scoreboard.h"
24 #include "ap_listen.h"
25 #include "simple_types.h"
26 #include "simple_run.h"
27 #include "http_core.h"
29 /* Thie file contains the absolute minimal MPM API, to interface with httpd. */
31 ap_generation_t volatile ap_my_generation = 0;
32 server_rec *ap_server_conf = NULL;
35 int ap_mpm_run(apr_pool_t * pconf, apr_pool_t * plog, server_rec * s)
37 simple_core_t *sc = simple_core_get();
39 sc->mpm_state = AP_MPMQ_RUNNING;
41 if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
42 sc->mpm_state = AP_MPMQ_STOPPING;
46 return simple_main_loop(sc);
49 apr_status_t ap_mpm_query(int query_code, int *result)
51 simple_core_t *sc = simple_core_get();
54 case AP_MPMQ_IS_THREADED:
55 *result = AP_MPMQ_STATIC;
58 case AP_MPMQ_IS_FORKED:
59 *result = AP_MPMQ_DYNAMIC;
62 case AP_MPMQ_IS_ASYNC:
66 case AP_MPMQ_MAX_DAEMON_USED:
67 *result = sc->procmgr.proc_count;
70 case AP_MPMQ_HARD_LIMIT_DAEMONS:
71 *result = sc->procmgr.proc_count;
74 case AP_MPMQ_HARD_LIMIT_THREADS:
75 *result = sc->procmgr.thread_count;
78 case AP_MPMQ_MAX_THREADS:
79 *result = sc->procmgr.thread_count;
82 case AP_MPMQ_MAX_SPARE_DAEMONS:
83 *result = sc->procmgr.proc_count;
86 case AP_MPMQ_MIN_SPARE_DAEMONS:
87 *result = sc->procmgr.proc_count;
90 case AP_MPMQ_MIN_SPARE_THREADS:
91 case AP_MPMQ_MAX_SPARE_THREADS:
92 *result = sc->procmgr.thread_count;
95 case AP_MPMQ_MAX_REQUESTS_DAEMON:
96 *result = sc->procmgr.max_requests_per_child;
99 case AP_MPMQ_MAX_DAEMONS:
100 *result = sc->procmgr.proc_count;
103 case AP_MPMQ_MPM_STATE:
104 *result = sc->mpm_state;
114 simple_open_logs(apr_pool_t * p,
115 apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s)
121 nsock = ap_setup_listeners(s);
124 ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
126 "simple_open_logs: no listening sockets available, shutting down");
134 simple_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp)
138 simple_core_t *sc = simple_core_get();
142 run_debug = ap_exists_config_define("DEBUG");
145 sc->run_foreground = 1;
146 sc->run_single_process = 1;
149 sc->run_foreground = ap_exists_config_define("FOREGROUND");
152 if (sc->restart_num == 2) {
154 if (sc->run_foreground) {
155 rv = apr_proc_detach(APR_PROC_DETACH_FOREGROUND);
158 rv = apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
162 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
163 "simple_pre_config: apr_proc_detach(%s) failed",
164 sc->run_foreground ? "FOREGROUND" : "DAEMONIZE");
165 return HTTP_INTERNAL_SERVER_ERROR;
172 static void simple_process_start(process_rec * process)
176 /* this is our first 'real' entry point, so setup everything here. */
177 rv = simple_core_init(simple_core_get(), process->pool);
180 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
181 "simple_core_init: Fatal Error Encountered");
185 ap_mpm_rewrite_args(process);
188 #define SIMPLE_MAX_PROC (500000)
189 #define SIMPLE_MIN_PROC (1)
191 #define SIMPLE_MAX_THREADS (500000)
192 #define SIMPLE_MIN_THREADS (1)
195 simple_check_config(apr_pool_t * p, apr_pool_t * plog,
196 apr_pool_t * ptemp, server_rec * s)
198 simple_core_t *sc = simple_core_get();
200 if (sc->procmgr.proc_count > SIMPLE_MAX_PROC) {
201 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
202 "simple_check_config: SimpleProcCount must be less than %d",
207 if (sc->procmgr.proc_count < SIMPLE_MIN_PROC) {
208 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
209 "simple_check_config: SimpleProcCount must be more than %d",
214 if (sc->procmgr.thread_count > SIMPLE_MAX_THREADS) {
215 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
216 "simple_check_config: SimpleThreadCount must be less than %d",
221 if (sc->procmgr.thread_count < SIMPLE_MIN_THREADS) {
222 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
223 "simple_check_config: SimpleThreadCount must be more than %d",
231 static void simple_hooks(apr_pool_t * p)
233 static const char *const aszSucc[] = { "core.c", NULL };
235 ap_hook_open_logs(simple_open_logs, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
237 ap_hook_pre_config(simple_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
239 ap_hook_check_config(simple_check_config, NULL, NULL, APR_HOOK_MIDDLE);
243 static const char *set_proccount(cmd_parms * cmd, void *baton,
246 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
251 simple_core_get()->procmgr.proc_count = atoi(arg);
256 static const char *set_threadcount(cmd_parms * cmd, void *baton,
259 simple_core_t *sc = simple_core_get();
260 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
266 sc->procmgr.thread_count = atoi(arg);
271 static const command_rec simple_cmds[] = {
272 AP_INIT_TAKE1("SimpleProcCount", set_proccount, NULL, RSRC_CONF,
273 "Number of child processes launched at server startup"),
274 AP_INIT_TAKE1("SimpleThreadCount", set_threadcount, NULL, RSRC_CONF,
275 "Set the number of Worker Threads Per-Process"),
276 /* pqXXXXXXXXX: These do NOT belong in the MPM configuration commands. */
283 module AP_MODULE_DECLARE_DATA mpm_simple_module = {
285 simple_process_start, /* hook to run before apache parses args */
286 NULL, /* create per-directory config structure */
287 NULL, /* merge per-directory config structures */
288 NULL, /* create per-server config structure */
289 NULL, /* merge per-server config structures */
290 simple_cmds, /* command apr_table_t */
291 simple_hooks /* register_hooks */