1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#include "mpm.h"
#include "ap_mpm.h"
#include "httpd.h"
#include "http_config.h"
#include "mpm_common.h"
#include "http_log.h"
#include "scoreboard.h"
#include "ap_listen.h"
#include "simple_types.h"
#include "simple_run.h"
#include "http_core.h"
#include "simple_api.h"
/* Thie file contains the absolute minimal MPM API, to interface with httpd. */
ap_generation_t volatile ap_my_generation = 0;
server_rec *ap_server_conf = NULL;
APR_HOOK_STRUCT(APR_HOOK_LINK(simple_drop_privileges)
)
AP_IMPLEMENT_HOOK_RUN_ALL(int, simple_drop_privileges,
(apr_pool_t * pchild, server_rec * s),
(pchild, s), OK, DECLINED)
int ap_mpm_run(apr_pool_t * pconf, apr_pool_t * plog, server_rec * s)
{
simple_core_t *sc = simple_core_get();
sc->mpm_state = AP_MPMQ_RUNNING;
if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
sc->mpm_state = AP_MPMQ_STOPPING;
return 1;
}
return simple_main_loop(sc);
}
apr_status_t ap_mpm_query(int query_code, int *result)
{
simple_core_t *sc = simple_core_get();
switch (query_code) {
case AP_MPMQ_IS_THREADED:
*result = AP_MPMQ_STATIC;
return APR_SUCCESS;
break;
case AP_MPMQ_IS_FORKED:
*result = AP_MPMQ_DYNAMIC;
return APR_SUCCESS;
break;
case AP_MPMQ_IS_ASYNC:
*result = 1;
return APR_SUCCESS;
break;
case AP_MPMQ_MAX_DAEMON_USED:
*result = sc->procmgr.proc_count;
return APR_SUCCESS;
break;
case AP_MPMQ_HARD_LIMIT_DAEMONS:
*result = sc->procmgr.proc_count;
return APR_SUCCESS;
break;
case AP_MPMQ_HARD_LIMIT_THREADS:
*result = sc->procmgr.thread_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MAX_THREADS:
*result = sc->procmgr.thread_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MAX_SPARE_DAEMONS:
*result = sc->procmgr.proc_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MIN_SPARE_DAEMONS:
*result = sc->procmgr.proc_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MIN_SPARE_THREADS:
case AP_MPMQ_MAX_SPARE_THREADS:
*result = sc->procmgr.thread_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MAX_REQUESTS_DAEMON:
*result = sc->procmgr.max_requests_per_child;
return APR_SUCCESS;
break;
case AP_MPMQ_MAX_DAEMONS:
*result = sc->procmgr.proc_count;
return APR_SUCCESS;
break;
case AP_MPMQ_MPM_STATE:
*result = sc->mpm_state;
return APR_SUCCESS;
default:
break;
}
return APR_ENOTIMPL;
}
static int
simple_open_logs(apr_pool_t * p,
apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s)
{
int nsock;
ap_server_conf = s;
nsock = ap_setup_listeners(s);
if (nsock < 1) {
ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
s,
"simple_open_logs: no listening sockets available, shutting down");
return DONE;
}
return OK;
}
static int
simple_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp)
{
int run_debug;
apr_status_t rv;
simple_core_t *sc = simple_core_get();
sc->restart_num++;
run_debug = ap_exists_config_define("DEBUG");
if (run_debug) {
sc->run_foreground = 1;
sc->run_single_process = 1;
}
else {
sc->run_foreground = ap_exists_config_define("FOREGROUND");
}
if (sc->restart_num == 2) {
if (sc->run_foreground) {
rv = apr_proc_detach(APR_PROC_DETACH_FOREGROUND);
}
else {
rv = apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
if (rv) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
"simple_pre_config: apr_proc_detach(%s) failed",
sc->run_foreground ? "FOREGROUND" : "DAEMONIZE");
return HTTP_INTERNAL_SERVER_ERROR;
}
}
return OK;
}
static void simple_process_start(process_rec * process)
{
apr_status_t rv;
/* this is our first 'real' entry point, so setup everything here. */
rv = simple_core_init(simple_core_get(), process->pool);
if (rv) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
"simple_core_init: Fatal Error Encountered");
exit(EXIT_FAILURE);
}
ap_mpm_rewrite_args(process);
}
#define SIMPLE_MAX_PROC (500000)
#define SIMPLE_MIN_PROC (1)
#define SIMPLE_MAX_THREADS (500000)
#define SIMPLE_MIN_THREADS (1)
static int
simple_check_config(apr_pool_t * p, apr_pool_t * plog,
apr_pool_t * ptemp, server_rec * s)
{
simple_core_t *sc = simple_core_get();
if (sc->procmgr.proc_count > SIMPLE_MAX_PROC) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
"simple_check_config: SimpleProcCount must be less than %d",
SIMPLE_MAX_PROC);
return !OK;
}
if (sc->procmgr.proc_count < SIMPLE_MIN_PROC) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
"simple_check_config: SimpleProcCount must be more than %d",
SIMPLE_MIN_PROC);
return !OK;
}
if (sc->procmgr.thread_count > SIMPLE_MAX_THREADS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
"simple_check_config: SimpleThreadCount must be less than %d",
SIMPLE_MAX_THREADS);
return !OK;
}
if (sc->procmgr.thread_count < SIMPLE_MIN_THREADS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
"simple_check_config: SimpleThreadCount must be more than %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);
}
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,
simple_process_start, /* 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 */
};
|