]> granicus.if.org Git - apache/blob - server/mpm_common.c
Introduce ap_(get|set)_core_module_config() functions/macros and use them
[apache] / server / mpm_common.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 /* The purpose of this file is to store the code that MOST mpm's will need
18  * this does not mean a function only goes into this file if every MPM needs
19  * it.  It means that if a function is needed by more than one MPM, and
20  * future maintenance would be served by making the code common, then the
21  * function belongs here.
22  *
23  * This is going in src/main because it is not platform specific, it is
24  * specific to multi-process servers, but NOT to Unix.  Which is why it
25  * does not belong in src/os/unix
26  */
27
28 #include "apr.h"
29 #include "apr_thread_proc.h"
30 #include "apr_signal.h"
31 #include "apr_strings.h"
32 #define APR_WANT_STRFUNC
33 #include "apr_want.h"
34 #include "apr_getopt.h"
35 #include "apr_optional.h"
36 #include "apr_allocator.h"
37
38 #include "httpd.h"
39 #include "http_config.h"
40 #include "http_core.h"
41 #include "http_log.h"
42 #include "http_main.h"
43 #include "mpm_common.h"
44 #include "ap_mpm.h"
45 #include "ap_listen.h"
46 #include "util_mutex.h"
47
48 #include "scoreboard.h"
49
50 #ifdef HAVE_PWD_H
51 #include <pwd.h>
52 #endif
53 #ifdef HAVE_GRP_H
54 #include <grp.h>
55 #endif
56 #if APR_HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
59
60 /* we know core's module_index is 0 */
61 #undef APLOG_MODULE_INDEX
62 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
63
64 #if AP_ENABLE_EXCEPTION_HOOK
65 APR_HOOK_STRUCT(
66     APR_HOOK_LINK(fatal_exception)
67     APR_HOOK_LINK(monitor)
68     APR_HOOK_LINK(drop_privileges)
69     APR_HOOK_LINK(mpm)
70     APR_HOOK_LINK(mpm_query)
71     APR_HOOK_LINK(mpm_register_timed_callback)
72     APR_HOOK_LINK(mpm_get_name)
73     APR_HOOK_LINK(end_generation)
74     APR_HOOK_LINK(child_status)
75 )
76 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
77                           (ap_exception_info_t *ei), (ei), OK, DECLINED)
78 #else
79 APR_HOOK_STRUCT(
80     APR_HOOK_LINK(monitor)
81     APR_HOOK_LINK(drop_privileges)
82     APR_HOOK_LINK(mpm)
83     APR_HOOK_LINK(mpm_query)
84     APR_HOOK_LINK(mpm_register_timed_callback)
85     APR_HOOK_LINK(mpm_get_name)
86     APR_HOOK_LINK(end_generation)
87     APR_HOOK_LINK(child_status)
88 )
89 #endif
90 AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
91                           (apr_pool_t *p, server_rec *s), (p, s), OK, DECLINED)
92 AP_IMPLEMENT_HOOK_RUN_ALL(int, drop_privileges,
93                           (apr_pool_t * pchild, server_rec * s),
94                           (pchild, s), OK, DECLINED)
95 AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm,
96                             (apr_pool_t *pconf, apr_pool_t *plog, server_rec *s),
97                             (pconf, plog, s), DECLINED)
98 AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm_query,
99                             (int query_code, int *result, apr_status_t *_rv),
100                             (query_code, result, _rv), DECLINED)
101 AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_register_timed_callback,
102                             (apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton),
103                             (t, cbfn, baton), APR_ENOTIMPL)
104 AP_IMPLEMENT_HOOK_VOID(end_generation,
105                        (server_rec *s, ap_generation_t gen),
106                        (s, gen))
107 AP_IMPLEMENT_HOOK_VOID(child_status,
108                        (server_rec *s, pid_t pid, ap_generation_t gen, int slot, mpm_child_status status),
109                        (s,pid,gen,slot,status))
110
111 /* hooks with no args are implemented last, after disabling APR hook probes */
112 #if defined(APR_HOOK_PROBES_ENABLED)
113 #undef APR_HOOK_PROBES_ENABLED
114 #undef APR_HOOK_PROBE_ENTRY
115 #define APR_HOOK_PROBE_ENTRY(ud,ns,name,args)
116 #undef APR_HOOK_PROBE_RETURN
117 #define APR_HOOK_PROBE_RETURN(ud,ns,name,rv,args)
118 #undef APR_HOOK_PROBE_INVOKE
119 #define APR_HOOK_PROBE_INVOKE(ud,ns,name,src,args)
120 #undef APR_HOOK_PROBE_COMPLETE
121 #define APR_HOOK_PROBE_COMPLETE(ud,ns,name,src,rv,args)
122 #undef APR_HOOK_INT_DCL_UD
123 #define APR_HOOK_INT_DCL_UD
124 #endif
125 AP_IMPLEMENT_HOOK_RUN_FIRST(const char *, mpm_get_name,
126                             (void),
127                             (), NULL)
128
129 typedef struct mpm_gen_info_t {
130     APR_RING_ENTRY(mpm_gen_info_t) link;
131     int gen;          /* which gen? */
132     int active;       /* number of active processes */
133 } mpm_gen_info_t;
134
135 APR_RING_HEAD(mpm_gen_info_head_t, mpm_gen_info_t);
136 static struct mpm_gen_info_head_t geninfo, unused_geninfo;
137 static int gen_head_init; /* yuck */
138
139 /* variables representing config directives implemented here */
140 const char *ap_pid_fname;
141 int ap_max_requests_per_child;
142 char ap_coredump_dir[MAX_STRING_LEN];
143 int ap_coredumpdir_configured;
144 int ap_graceful_shutdown_timeout;
145 apr_uint32_t ap_max_mem_free;
146 apr_size_t ap_thread_stacksize;
147
148 /* Set defaults for config directives implemented here.  This is
149  * called from core's pre-config hook, so MPMs which need to override
150  * one of these should run their pre-config hook after that of core.
151  */
152 void mpm_common_pre_config(apr_pool_t *pconf)
153 {
154     ap_pid_fname = DEFAULT_PIDLOG;
155     ap_max_requests_per_child = 0; /* unlimited */
156     apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
157     ap_coredumpdir_configured = 0;
158     ap_graceful_shutdown_timeout = 0; /* unlimited */
159     ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
160     ap_thread_stacksize = 0; /* use system default */
161 }
162
163 /* number of calls to wait_or_timeout between writable probes */
164 #ifndef INTERVAL_OF_WRITABLE_PROBES
165 #define INTERVAL_OF_WRITABLE_PROBES 10
166 #endif
167 static int wait_or_timeout_counter;
168
169 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
170                         apr_pool_t *p, server_rec *s)
171 {
172     apr_status_t rv;
173
174     ++wait_or_timeout_counter;
175     if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
176         wait_or_timeout_counter = 0;
177         ap_run_monitor(p, s);
178     }
179
180     rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
181     if (APR_STATUS_IS_EINTR(rv)) {
182         ret->pid = -1;
183         return;
184     }
185
186     if (APR_STATUS_IS_CHILD_DONE(rv)) {
187         return;
188     }
189
190     apr_sleep(apr_time_from_sec(1));
191     ret->pid = -1;
192     return;
193 }
194
195 #if defined(TCP_NODELAY)
196 void ap_sock_disable_nagle(apr_socket_t *s)
197 {
198     /* The Nagle algorithm says that we should delay sending partial
199      * packets in hopes of getting more data.  We don't want to do
200      * this; we are not telnet.  There are bad interactions between
201      * persistent connections and Nagle's algorithm that have very severe
202      * performance penalties.  (Failing to disable Nagle is not much of a
203      * problem with simple HTTP.)
204      *
205      * In spite of these problems, failure here is not a shooting offense.
206      */
207     apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
208
209     if (status != APR_SUCCESS) {
210         ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
211                      "apr_socket_opt_set: (TCP_NODELAY)");
212     }
213 }
214 #endif
215
216 #ifdef HAVE_GETPWNAM
217 AP_DECLARE(uid_t) ap_uname2id(const char *name)
218 {
219     struct passwd *ent;
220
221     if (name[0] == '#')
222         return (atoi(&name[1]));
223
224     if (!(ent = getpwnam(name))) {
225         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
226                      "%s: bad user name %s", ap_server_argv0, name);
227         exit(1);
228     }
229
230     return (ent->pw_uid);
231 }
232 #endif
233
234 #ifdef HAVE_GETGRNAM
235 AP_DECLARE(gid_t) ap_gname2id(const char *name)
236 {
237     struct group *ent;
238
239     if (name[0] == '#')
240         return (atoi(&name[1]));
241
242     if (!(ent = getgrnam(name))) {
243         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
244                      "%s: bad group name %s", ap_server_argv0, name);
245         exit(1);
246     }
247
248     return (ent->gr_gid);
249 }
250 #endif
251
252 #ifndef HAVE_INITGROUPS
253 int initgroups(const char *name, gid_t basegid)
254 {
255 #if defined(_OSD_POSIX) || defined(OS2) || defined(WIN32) || defined(NETWARE)
256     return 0;
257 #else
258     gid_t groups[NGROUPS_MAX];
259     struct group *g;
260     int index = 0;
261
262     setgrent();
263
264     groups[index++] = basegid;
265
266     while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
267         if (g->gr_gid != basegid) {
268             char **names;
269
270             for (names = g->gr_mem; *names != NULL; ++names) {
271                 if (!strcmp(*names, name))
272                     groups[index++] = g->gr_gid;
273             }
274         }
275     }
276
277     endgrent();
278
279     return setgroups(index, groups);
280 #endif
281 }
282 #endif /* def HAVE_INITGROUPS */
283
284 /* standard mpm configuration handling */
285
286 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
287                                const char *arg)
288 {
289     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
290     if (err != NULL) {
291         return err;
292     }
293
294     if (cmd->server->is_virtual) {
295         return "PidFile directive not allowed in <VirtualHost>";
296     }
297
298     ap_pid_fname = arg;
299     return NULL;
300 }
301
302 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
303                                     const char *arg)
304 {
305     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
306     if (err != NULL) {
307         return err;
308     }
309
310     if (!strcasecmp(cmd->cmd->name, "MaxRequestsPerChild")) {
311         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
312                      "MaxRequestsPerChild is deprecated, use "
313                      "MaxConnectionsPerChild instead.");
314     }
315
316     ap_max_requests_per_child = atoi(arg);
317
318     return NULL;
319 }
320
321 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
322                                    const char *arg)
323 {
324     apr_finfo_t finfo;
325     const char *fname;
326     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
327     if (err != NULL) {
328         return err;
329     }
330
331     fname = ap_server_root_relative(cmd->pool, arg);
332     if (!fname) {
333         return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
334                            arg, NULL);
335     }
336     if (apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool) != APR_SUCCESS) {
337         return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
338                            " does not exist", NULL);
339     }
340     if (finfo.filetype != APR_DIR) {
341         return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
342                            " is not a directory", NULL);
343     }
344     apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
345     ap_coredumpdir_configured = 1;
346     return NULL;
347 }
348
349 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
350                                           const char *arg)
351 {
352     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
353     if (err != NULL) {
354         return err;
355     }
356     ap_graceful_shutdown_timeout = atoi(arg);
357     return NULL;
358 }
359
360 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
361                                     const char *arg)
362 {
363     long value;
364     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
365     if (err != NULL) {
366         return err;
367     }
368
369     value = strtol(arg, NULL, 0);
370     if (value < 0 || errno == ERANGE)
371         return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
372                            arg, NULL);
373
374     ap_max_mem_free = (apr_uint32_t)value * 1024;
375
376     return NULL;
377 }
378
379 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
380                                         const char *arg)
381 {
382     long value;
383     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
384     if (err != NULL) {
385         return err;
386     }
387
388     value = strtol(arg, NULL, 0);
389     if (value < 0 || errno == ERANGE)
390         return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
391                            arg, NULL);
392
393     ap_thread_stacksize = (apr_size_t)value;
394
395     return NULL;
396 }
397
398 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
399 {
400     apr_status_t rv;
401
402     if (ap_run_mpm_query(query_code, result, &rv) == DECLINED) {
403         rv = APR_EGENERAL;
404     }
405
406     return rv;
407 }
408
409 /* core's child-status hook
410  * tracks number of remaining children per generation and
411  * runs the end-generation hook when a generation finishes
412  */
413 void ap_core_child_status(server_rec *s, pid_t pid,
414                           ap_generation_t gen, int slot,
415                           mpm_child_status status)
416 {
417     mpm_gen_info_t *cur;
418     const char *status_msg = "unknown status";
419
420     if (!gen_head_init) { /* where to run this? */
421         gen_head_init = 1;
422         APR_RING_INIT(&geninfo, mpm_gen_info_t, link);
423         APR_RING_INIT(&unused_geninfo, mpm_gen_info_t, link);
424     }
425
426     cur = APR_RING_FIRST(&geninfo);
427     while (cur != APR_RING_SENTINEL(&geninfo, mpm_gen_info_t, link) &&
428            cur->gen != gen) {
429         cur = APR_RING_NEXT(cur, link);
430     }
431
432     switch(status) {
433     case MPM_CHILD_STARTED:
434         status_msg = "started";
435         if (cur == APR_RING_SENTINEL(&geninfo, mpm_gen_info_t, link)) {
436             /* first child for this generation */
437             if (!APR_RING_EMPTY(&unused_geninfo, mpm_gen_info_t, link)) {
438                 cur = APR_RING_FIRST(&unused_geninfo);
439                 APR_RING_REMOVE(cur, link);
440             }
441             else {
442                 cur = apr_pcalloc(s->process->pool, sizeof *cur);
443             }
444             cur->gen = gen;
445             APR_RING_ELEM_INIT(cur, link);
446             APR_RING_INSERT_HEAD(&geninfo, cur, mpm_gen_info_t, link);
447         }
448         ++cur->active;
449         break;
450     case MPM_CHILD_EXITED:
451         status_msg = "exited";
452         if (cur == APR_RING_SENTINEL(&geninfo, mpm_gen_info_t, link)) {
453             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
454                          "no record of generation %d of exiting child %" APR_PID_T_FMT,
455                          gen, pid);
456         }
457         else {
458             --cur->active;
459             if (!cur->active) {
460                 ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, ap_server_conf,
461                              "end of generation %d", gen);
462                 ap_run_end_generation(ap_server_conf, gen);
463                 APR_RING_REMOVE(cur, link);
464                 APR_RING_INSERT_HEAD(&unused_geninfo, cur, mpm_gen_info_t, link);
465             }
466         }
467         break;
468     case MPM_CHILD_LOST_SLOT:
469         status_msg = "lost slot";
470         /* we don't track by slot, so it doesn't matter */
471         break;
472     }
473     ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, s,
474                  "mpm child %" APR_PID_T_FMT " (gen %d/slot %d) %s",
475                  pid, gen, slot, status_msg);
476 }
477
478 AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton)
479 {
480     return ap_run_mpm_register_timed_callback(t, cbfn, baton);
481 }
482
483 AP_DECLARE(const char *)ap_show_mpm(void)
484 {
485     const char *name = ap_run_mpm_get_name();
486
487     if (!name) {
488         name = "";
489     }
490
491     return name;
492 }
493
494 AP_DECLARE(const char *)ap_check_mpm(void)
495 {
496     static const char *last_mpm_name = NULL;
497
498     if (!_hooks.link_mpm || _hooks.link_mpm->nelts == 0)
499         return "No MPM loaded.";
500     else if (_hooks.link_mpm->nelts > 1)
501         return "More than one MPM loaded.";
502
503     if (last_mpm_name) {
504         if (strcmp(last_mpm_name, ap_show_mpm())) {
505             return "The MPM cannot be changed during restart.";
506         }
507     }
508     else {
509         last_mpm_name = apr_pstrdup(ap_pglobal, ap_show_mpm());
510     }
511
512     return NULL;
513 }