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.
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.
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
29 #include "apr_thread_proc.h"
30 #include "apr_signal.h"
31 #include "apr_strings.h"
32 #define APR_WANT_STRFUNC
34 #include "apr_getopt.h"
35 #include "apr_optional.h"
36 #include "apr_allocator.h"
39 #include "http_config.h"
41 #include "http_main.h"
42 #include "mpm_common.h"
44 #include "ap_listen.h"
45 #include "util_mutex.h"
47 #include "scoreboard.h"
59 #if AP_ENABLE_EXCEPTION_HOOK
61 APR_HOOK_LINK(fatal_exception)
62 APR_HOOK_LINK(monitor)
63 APR_HOOK_LINK(drop_privileges)
65 APR_HOOK_LINK(mpm_query)
66 APR_HOOK_LINK(mpm_note_child_killed)
67 APR_HOOK_LINK(mpm_register_timed_callback)
68 APR_HOOK_LINK(mpm_get_name)
70 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
71 (ap_exception_info_t *ei), (ei), OK, DECLINED)
74 APR_HOOK_LINK(monitor)
75 APR_HOOK_LINK(drop_privileges)
77 APR_HOOK_LINK(mpm_query)
78 APR_HOOK_LINK(mpm_note_child_killed)
79 APR_HOOK_LINK(mpm_register_timed_callback)
80 APR_HOOK_LINK(mpm_get_name)
83 AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
84 (apr_pool_t *p, server_rec *s), (p, s), OK, DECLINED)
85 AP_IMPLEMENT_HOOK_RUN_ALL(int, drop_privileges,
86 (apr_pool_t * pchild, server_rec * s),
87 (pchild, s), OK, DECLINED)
88 AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm,
89 (apr_pool_t *pconf, apr_pool_t *plog, server_rec *s),
90 (pconf, plog, s), DECLINED)
91 AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm_query,
92 (int query_code, int *result, apr_status_t *_rv),
93 (query_code, result, _rv), DECLINED)
94 AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_note_child_killed,
96 (childnum), APR_ENOTIMPL)
97 AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_register_timed_callback,
98 (apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton),
99 (t, cbfn, baton), APR_ENOTIMPL)
100 AP_IMPLEMENT_HOOK_RUN_FIRST(const char *, mpm_get_name,
104 /* number of calls to wait_or_timeout between writable probes */
105 #ifndef INTERVAL_OF_WRITABLE_PROBES
106 #define INTERVAL_OF_WRITABLE_PROBES 10
108 static int wait_or_timeout_counter;
110 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
111 apr_pool_t *p, server_rec *s)
115 ++wait_or_timeout_counter;
116 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
117 wait_or_timeout_counter = 0;
118 ap_run_monitor(p, s);
121 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
122 if (APR_STATUS_IS_EINTR(rv)) {
127 if (APR_STATUS_IS_CHILD_DONE(rv)) {
136 #if defined(TCP_NODELAY)
137 void ap_sock_disable_nagle(apr_socket_t *s)
139 /* The Nagle algorithm says that we should delay sending partial
140 * packets in hopes of getting more data. We don't want to do
141 * this; we are not telnet. There are bad interactions between
142 * persistent connections and Nagle's algorithm that have very severe
143 * performance penalties. (Failing to disable Nagle is not much of a
144 * problem with simple HTTP.)
146 * In spite of these problems, failure here is not a shooting offense.
148 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
150 if (status != APR_SUCCESS) {
151 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
152 "apr_socket_opt_set: (TCP_NODELAY)");
158 AP_DECLARE(uid_t) ap_uname2id(const char *name)
163 return (atoi(&name[1]));
165 if (!(ent = getpwnam(name))) {
166 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
167 "%s: bad user name %s", ap_server_argv0, name);
171 return (ent->pw_uid);
176 AP_DECLARE(gid_t) ap_gname2id(const char *name)
181 return (atoi(&name[1]));
183 if (!(ent = getgrnam(name))) {
184 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
185 "%s: bad group name %s", ap_server_argv0, name);
189 return (ent->gr_gid);
193 #ifndef HAVE_INITGROUPS
194 int initgroups(const char *name, gid_t basegid)
196 #if defined(_OSD_POSIX) || defined(OS2) || defined(WIN32) || defined(NETWARE)
199 gid_t groups[NGROUPS_MAX];
205 groups[index++] = basegid;
207 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
208 if (g->gr_gid != basegid) {
211 for (names = g->gr_mem; *names != NULL; ++names) {
212 if (!strcmp(*names, name))
213 groups[index++] = g->gr_gid;
220 return setgroups(index, groups);
223 #endif /* def NEED_INITGROUPS */
225 /* standard mpm configuration handling */
226 const char *ap_pid_fname = NULL;
228 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
231 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
236 if (cmd->server->is_virtual) {
237 return "PidFile directive not allowed in <VirtualHost>";
244 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
247 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
252 ap_scoreboard_fname = arg;
256 const char *ap_lock_fname = NULL;
258 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
261 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
270 int ap_max_requests_per_child = 0;
272 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
275 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
280 ap_max_requests_per_child = atoi(arg);
285 char ap_coredump_dir[MAX_STRING_LEN];
286 int ap_coredumpdir_configured;
288 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
294 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
299 fname = ap_server_root_relative(cmd->pool, arg);
301 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
304 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
305 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
306 " does not exist", NULL);
308 if (finfo.filetype != APR_DIR) {
309 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
310 " is not a directory", NULL);
312 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
313 ap_coredumpdir_configured = 1;
317 int ap_graceful_shutdown_timeout = 0;
319 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
322 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
326 ap_graceful_shutdown_timeout = atoi(arg);
330 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
332 const char *ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
337 const char *lockfile;
338 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
343 rv = ap_parse_mutex(arg, cmd->server->process->pool,
344 &ap_accept_lock_mech, &lockfile);
346 if ((rv == APR_ENOTIMPL) || (rv == APR_ENOLOCK)) {
347 return apr_pstrcat(cmd->pool, "Invalid AcceptMutex argument ", arg,
348 " (" AP_AVAILABLE_MUTEXES_STRING ")", NULL);
349 } else if (rv == APR_BADARG) {
350 return apr_pstrcat(cmd->pool, "Invalid AcceptMutex filepath ",
354 if (lockfile && !ap_lock_fname)
355 ap_lock_fname = lockfile;
359 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
361 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
365 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
370 value = strtol(arg, NULL, 0);
371 if (value < 0 || errno == ERANGE)
372 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
375 ap_max_mem_free = (apr_uint32_t)value * 1024;
380 apr_size_t ap_thread_stacksize = 0; /* use system default */
382 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
386 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
391 value = strtol(arg, NULL, 0);
392 if (value < 0 || errno == ERANGE)
393 return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
396 ap_thread_stacksize = (apr_size_t)value;
401 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
405 if (ap_run_mpm_query(query_code, result, &rv) == DECLINED) {
412 AP_DECLARE(apr_status_t) ap_mpm_note_child_killed(int childnum)
414 return ap_run_mpm_note_child_killed(childnum);
417 AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton)
419 return ap_run_mpm_register_timed_callback(t, cbfn, baton);
422 AP_DECLARE(const char *)ap_show_mpm(void)
424 const char *name = ap_run_mpm_get_name();