]> granicus.if.org Git - apache/blob - server/mpm/simple/simple_api.c
Switch all unix MPMs to use drop_privileges hook (mod_unixd) for startup
[apache] / server / mpm / simple / simple_api.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "mpm.h"
18 #include "ap_mpm.h"
19 #include "httpd.h"
20 #include "http_config.h"
21 #include "mpm_common.h"
22 #include "http_log.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"
28
29 /* Thie file contains the absolute minimal MPM API, to interface with httpd. */
30
31 ap_generation_t volatile ap_my_generation = 0;
32 server_rec *ap_server_conf = NULL;
33
34
35      int ap_mpm_run(apr_pool_t * pconf, apr_pool_t * plog, server_rec * s)
36 {
37     simple_core_t *sc = simple_core_get();
38
39     sc->mpm_state = AP_MPMQ_RUNNING;
40
41     if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
42         sc->mpm_state = AP_MPMQ_STOPPING;
43         return 1;
44     }
45
46     return simple_main_loop(sc);
47 }
48
49 apr_status_t ap_mpm_query(int query_code, int *result)
50 {
51     simple_core_t *sc = simple_core_get();
52
53     switch (query_code) {
54     case AP_MPMQ_IS_THREADED:
55         *result = AP_MPMQ_STATIC;
56         return APR_SUCCESS;
57         break;
58     case AP_MPMQ_IS_FORKED:
59         *result = AP_MPMQ_DYNAMIC;
60         return APR_SUCCESS;
61         break;
62     case AP_MPMQ_IS_ASYNC:
63         *result = 1;
64         return APR_SUCCESS;
65         break;
66     case AP_MPMQ_MAX_DAEMON_USED:
67         *result = sc->procmgr.proc_count;
68         return APR_SUCCESS;
69         break;
70     case AP_MPMQ_HARD_LIMIT_DAEMONS:
71         *result = sc->procmgr.proc_count;
72         return APR_SUCCESS;
73         break;
74     case AP_MPMQ_HARD_LIMIT_THREADS:
75         *result = sc->procmgr.thread_count;
76         return APR_SUCCESS;
77         break;
78     case AP_MPMQ_MAX_THREADS:
79         *result = sc->procmgr.thread_count;
80         return APR_SUCCESS;
81         break;
82     case AP_MPMQ_MAX_SPARE_DAEMONS:
83         *result = sc->procmgr.proc_count;
84         return APR_SUCCESS;
85         break;
86     case AP_MPMQ_MIN_SPARE_DAEMONS:
87         *result = sc->procmgr.proc_count;
88         return APR_SUCCESS;
89         break;
90     case AP_MPMQ_MIN_SPARE_THREADS:
91     case AP_MPMQ_MAX_SPARE_THREADS:
92         *result = sc->procmgr.thread_count;
93         return APR_SUCCESS;
94         break;
95     case AP_MPMQ_MAX_REQUESTS_DAEMON:
96         *result = sc->procmgr.max_requests_per_child;
97         return APR_SUCCESS;
98         break;
99     case AP_MPMQ_MAX_DAEMONS:
100         *result = sc->procmgr.proc_count;
101         return APR_SUCCESS;
102         break;
103     case AP_MPMQ_MPM_STATE:
104         *result = sc->mpm_state;
105         return APR_SUCCESS;
106     default:
107         break;
108     }
109
110     return APR_ENOTIMPL;
111 }
112
113 static int
114 simple_open_logs(apr_pool_t * p,
115                  apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s)
116 {
117     int nsock;
118
119     ap_server_conf = s;
120
121     nsock = ap_setup_listeners(s);
122
123     if (nsock < 1) {
124         ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
125                      s,
126                      "simple_open_logs: no listening sockets available, shutting down");
127         return DONE;
128     }
129
130     return OK;
131 }
132
133 static int
134 simple_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp)
135 {
136     int run_debug;
137     apr_status_t rv;
138     simple_core_t *sc = simple_core_get();
139
140     sc->restart_num++;
141
142     run_debug = ap_exists_config_define("DEBUG");
143
144     if (run_debug) {
145         sc->run_foreground = 1;
146         sc->run_single_process = 1;
147     }
148     else {
149         sc->run_foreground = ap_exists_config_define("FOREGROUND");
150     }
151
152     if (sc->restart_num == 2) {
153
154         if (sc->run_foreground) {
155             rv = apr_proc_detach(APR_PROC_DETACH_FOREGROUND);
156         }
157         else {
158             rv = apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
159         }
160
161         if (rv) {
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;
166         }
167     }
168
169     return OK;
170 }
171
172 static void simple_process_start(process_rec * process)
173 {
174     apr_status_t rv;
175
176     /* this is our first 'real' entry point, so setup everything here. */
177     rv = simple_core_init(simple_core_get(), process->pool);
178
179     if (rv) {
180         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
181                      "simple_core_init: Fatal Error Encountered");
182         exit(EXIT_FAILURE);
183     }
184
185     ap_mpm_rewrite_args(process);
186 }
187
188 #define SIMPLE_MAX_PROC (500000)
189 #define SIMPLE_MIN_PROC (1)
190
191 #define SIMPLE_MAX_THREADS (500000)
192 #define SIMPLE_MIN_THREADS (1)
193
194 static int
195 simple_check_config(apr_pool_t * p, apr_pool_t * plog,
196                     apr_pool_t * ptemp, server_rec * s)
197 {
198     simple_core_t *sc = simple_core_get();
199
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",
203                      SIMPLE_MAX_PROC);
204         return !OK;
205     }
206
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",
210                      SIMPLE_MIN_PROC);
211         return !OK;
212     }
213
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",
217                      SIMPLE_MAX_THREADS);
218         return !OK;
219     }
220
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",
224                      SIMPLE_MIN_THREADS);
225         return !OK;
226     }
227
228     return OK;
229 }
230
231 static void simple_hooks(apr_pool_t * p)
232 {
233     static const char *const aszSucc[] = { "core.c", NULL };
234
235     ap_hook_open_logs(simple_open_logs, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
236
237     ap_hook_pre_config(simple_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
238
239     ap_hook_check_config(simple_check_config, NULL, NULL, APR_HOOK_MIDDLE);
240
241 }
242
243 static const char *set_proccount(cmd_parms * cmd, void *baton,
244                                  const char *arg)
245 {
246     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
247     if (err != NULL) {
248         return err;
249     }
250
251     simple_core_get()->procmgr.proc_count = atoi(arg);
252     return NULL;
253 }
254
255
256 static const char *set_threadcount(cmd_parms * cmd, void *baton,
257                                    const char *arg)
258 {
259     simple_core_t *sc = simple_core_get();
260     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
261
262     if (err != NULL) {
263         return err;
264     }
265
266     sc->procmgr.thread_count = atoi(arg);
267
268     return NULL;
269 }
270
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. */
277     LISTEN_COMMANDS,
278     {NULL}
279 };
280
281
282
283 module AP_MODULE_DECLARE_DATA mpm_simple_module = {
284     MPM20_MODULE_STUFF,
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 */
292 };