]> granicus.if.org Git - apache/blob - server/mpm_common.c
mpm-query hook: distinguish between DECLINED and APR_ENOTIMPL so that
[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_log.h"
41 #include "http_main.h"
42 #include "mpm_common.h"
43 #include "ap_mpm.h"
44 #include "ap_listen.h"
45 #include "util_mutex.h"
46
47 #include "scoreboard.h"
48
49 #ifdef HAVE_PWD_H
50 #include <pwd.h>
51 #endif
52 #ifdef HAVE_GRP_H
53 #include <grp.h>
54 #endif
55 #if APR_HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #if AP_ENABLE_EXCEPTION_HOOK
60 APR_HOOK_STRUCT(
61     APR_HOOK_LINK(fatal_exception)
62     APR_HOOK_LINK(monitor)
63     APR_HOOK_LINK(drop_privileges)
64     APR_HOOK_LINK(mpm)
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)
69 )
70 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
71                           (ap_exception_info_t *ei), (ei), OK, DECLINED)
72 #else
73 APR_HOOK_STRUCT(
74     APR_HOOK_LINK(monitor)
75     APR_HOOK_LINK(drop_privileges)
76     APR_HOOK_LINK(mpm)
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)
81 )
82 #endif
83 AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
84                           (apr_pool_t *p), (p), 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,
95                             (int childnum),
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,
101                             (void),
102                             (), NULL)
103
104 /* number of calls to wait_or_timeout between writable probes */
105 #ifndef INTERVAL_OF_WRITABLE_PROBES
106 #define INTERVAL_OF_WRITABLE_PROBES 10
107 #endif
108 static int wait_or_timeout_counter;
109
110 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
111                         apr_pool_t *p)
112 {
113     apr_status_t rv;
114
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);
119     }
120
121     rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
122     if (APR_STATUS_IS_EINTR(rv)) {
123         ret->pid = -1;
124         return;
125     }
126
127     if (APR_STATUS_IS_CHILD_DONE(rv)) {
128         return;
129     }
130
131     apr_sleep(1000000);
132     ret->pid = -1;
133     return;
134 }
135
136 #if defined(TCP_NODELAY)
137 void ap_sock_disable_nagle(apr_socket_t *s)
138 {
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.)
145      *
146      * In spite of these problems, failure here is not a shooting offense.
147      */
148     apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
149
150     if (status != APR_SUCCESS) {
151         ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
152                      "apr_socket_opt_set: (TCP_NODELAY)");
153     }
154 }
155 #endif
156
157 #ifdef HAVE_GETPWNAM
158 AP_DECLARE(uid_t) ap_uname2id(const char *name)
159 {
160     struct passwd *ent;
161
162     if (name[0] == '#')
163         return (atoi(&name[1]));
164
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);
168         exit(1);
169     }
170
171     return (ent->pw_uid);
172 }
173 #endif
174
175 #ifdef HAVE_GETGRNAM
176 AP_DECLARE(gid_t) ap_gname2id(const char *name)
177 {
178     struct group *ent;
179
180     if (name[0] == '#')
181         return (atoi(&name[1]));
182
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);
186         exit(1);
187     }
188
189     return (ent->gr_gid);
190 }
191 #endif
192
193 #ifndef HAVE_INITGROUPS
194 int initgroups(const char *name, gid_t basegid)
195 {
196 #if defined(_OSD_POSIX) || defined(WIN32) || defined(NETWARE)
197     return 0;
198 #else
199     gid_t groups[NGROUPS_MAX];
200     struct group *g;
201     int index = 0;
202
203     setgrent();
204
205     groups[index++] = basegid;
206
207     while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
208         if (g->gr_gid != basegid) {
209             char **names;
210
211             for (names = g->gr_mem; *names != NULL; ++names) {
212                 if (!strcmp(*names, name))
213                     groups[index++] = g->gr_gid;
214             }
215         }
216     }
217
218     endgrent();
219
220     return setgroups(index, groups);
221 #endif
222 }
223 #endif /* def NEED_INITGROUPS */
224
225 /* standard mpm configuration handling */
226 const char *ap_pid_fname = NULL;
227
228 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
229                                const char *arg)
230 {
231     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
232     if (err != NULL) {
233         return err;
234     }
235
236     if (cmd->server->is_virtual) {
237         return "PidFile directive not allowed in <VirtualHost>";
238     }
239
240     ap_pid_fname = arg;
241     return NULL;
242 }
243
244 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
245                                    const char *arg)
246 {
247     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
248     if (err != NULL) {
249         return err;
250     }
251
252     ap_scoreboard_fname = arg;
253     return NULL;
254 }
255
256 const char *ap_lock_fname = NULL;
257
258 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
259                                 const char *arg)
260 {
261     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
262     if (err != NULL) {
263         return err;
264     }
265
266     ap_lock_fname = arg;
267     return NULL;
268 }
269
270 int ap_max_requests_per_child = 0;
271
272 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
273                                     const char *arg)
274 {
275     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
276     if (err != NULL) {
277         return err;
278     }
279
280     ap_max_requests_per_child = atoi(arg);
281
282     return NULL;
283 }
284
285 char ap_coredump_dir[MAX_STRING_LEN];
286 int ap_coredumpdir_configured;
287
288 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
289                                    const char *arg)
290 {
291     apr_status_t rv;
292     apr_finfo_t finfo;
293     const char *fname;
294     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
295     if (err != NULL) {
296         return err;
297     }
298
299     fname = ap_server_root_relative(cmd->pool, arg);
300     if (!fname) {
301         return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
302                            arg, NULL);
303     }
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);
307     }
308     if (finfo.filetype != APR_DIR) {
309         return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
310                            " is not a directory", NULL);
311     }
312     apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
313     ap_coredumpdir_configured = 1;
314     return NULL;
315 }
316
317 int ap_graceful_shutdown_timeout = 0;
318
319 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
320                                           const char *arg)
321 {
322     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
323     if (err != NULL) {
324         return err;
325     }
326     ap_graceful_shutdown_timeout = atoi(arg);
327     return NULL;
328 }
329
330 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
331
332 const char *ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
333                                         void *dummy,
334                                         const char *arg)
335 {
336     apr_status_t rv;
337     const char *lockfile;
338     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
339     if (err != NULL) {
340         return err;
341     }
342
343     rv = ap_parse_mutex(arg, cmd->server->process->pool,
344                         &ap_accept_lock_mech, &lockfile);
345
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 ",
351                                arg, NULL);
352     }
353
354     if (lockfile && !ap_lock_fname)
355         ap_lock_fname = lockfile;
356     return NULL;
357 }
358
359 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
360
361 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
362                                     const char *arg)
363 {
364     long value;
365     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
366     if (err != NULL) {
367         return err;
368     }
369
370     value = strtol(arg, NULL, 0);
371     if (value < 0 || errno == ERANGE)
372         return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
373                            arg, NULL);
374
375     ap_max_mem_free = (apr_uint32_t)value * 1024;
376
377     return NULL;
378 }
379
380 apr_size_t ap_thread_stacksize = 0; /* use system default */
381
382 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
383                                         const char *arg)
384 {
385     long value;
386     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
387     if (err != NULL) {
388         return err;
389     }
390
391     value = strtol(arg, NULL, 0);
392     if (value < 0 || errno == ERANGE)
393         return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
394                            arg, NULL);
395
396     ap_thread_stacksize = (apr_size_t)value;
397
398     return NULL;
399 }
400
401 AP_DECLARE(int) ap_mpm_run(apr_pool_t *pconf, apr_pool_t *plog, server_rec *server_conf)
402 {
403     return ap_run_mpm(pconf, plog, server_conf);
404 }
405
406 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
407 {
408     apr_status_t rv;
409
410     if (ap_run_mpm_query(query_code, result, &rv) == DECLINED) {
411         rv = APR_EGENERAL;
412     }
413
414     return rv;
415 }
416
417 AP_DECLARE(apr_status_t) ap_mpm_note_child_killed(int childnum)
418 {
419     return ap_run_mpm_note_child_killed(childnum);
420 }
421
422 AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton)
423 {
424     return ap_run_mpm_register_timed_callback(t, cbfn, baton);
425 }
426
427 AP_DECLARE(const char *)ap_show_mpm(void)
428 {
429     const char *name = ap_run_mpm_get_name();
430
431     if (!name) {
432         name = "";
433     }
434
435     return name;
436 }