]> git.ipfire.org Git - thirdparty/apache/httpd.git/blob
1611326
[thirdparty/apache/httpd.git] /
1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2000-2004 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 *
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
57 */
58
59 /* The purpose of this file is to store the code that MOST mpm's will need
60 * this does not mean a function only goes into this file if every MPM needs
61 * it. It means that if a function is needed by more than one MPM, and
62 * future maintenance would be served by making the code common, then the
63 * function belongs here.
64 *
65 * This is going in src/main because it is not platform specific, it is
66 * specific to multi-process servers, but NOT to Unix. Which is why it
67 * does not belong in src/os/unix
68 */
69
70 #include "apr.h"
71 #include "apr_thread_proc.h"
72 #include "apr_signal.h"
73 #include "apr_strings.h"
74 #define APR_WANT_STRFUNC
75 #include "apr_want.h"
76 #include "apr_getopt.h"
77 #include "apr_optional.h"
78 #include "apr_allocator.h"
79
80 #include "httpd.h"
81 #include "http_config.h"
82 #include "http_log.h"
83 #include "http_main.h"
84 #include "mpm.h"
85 #include "mpm_common.h"
86 #include "ap_mpm.h"
87 #include "ap_listen.h"
88 #include "mpm_default.h"
89
90 #ifdef AP_MPM_WANT_SET_SCOREBOARD
91 #include "scoreboard.h"
92 #endif
93
94 #ifdef HAVE_PWD_H
95 #include <pwd.h>
96 #endif
97 #ifdef HAVE_GRP_H
98 #include <grp.h>
99 #endif
100
101 #ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
102 void ap_reclaim_child_processes(int terminate)
103 {
104 int i;
105 long int waittime = 1024 * 16; /* in usecs */
106 apr_status_t waitret;
107 int tries;
108 int not_dead_yet;
109 int max_daemons;
110
111 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
112
113 for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
114 /* don't want to hold up progress any more than
115 * necessary, but we need to allow children a few moments to exit.
116 * Set delay with an exponential backoff.
117 */
118 apr_sleep(waittime);
119 waittime = waittime * 4;
120
121 /* now see who is done */
122 not_dead_yet = 0;
123 for (i = 0; i < max_daemons; ++i) {
124 pid_t pid = MPM_CHILD_PID(i);
125 apr_proc_t proc;
126
127 if (pid == 0)
128 continue;
129
130 proc.pid = pid;
131 waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
132 if (waitret != APR_CHILD_NOTDONE) {
133 MPM_NOTE_CHILD_KILLED(i);
134 continue;
135 }
136
137 ++not_dead_yet;
138 switch (tries) {
139 case 1: /* 16ms */
140 case 2: /* 82ms */
141 case 3: /* 344ms */
142 case 4: /* 16ms */
143 break;
144
145 case 5: /* 82ms */
146 case 6: /* 344ms */
147 case 7: /* 1.4sec */
148 /* ok, now it's being annoying */
149 ap_log_error(APLOG_MARK, APLOG_WARNING,
150 0, ap_server_conf,
151 "child process %ld still did not exit, "
152 "sending a SIGTERM",
153 (long)pid);
154 kill(pid, SIGTERM);
155 break;
156
157 case 8: /* 6 sec */
158 /* die child scum */
159 ap_log_error(APLOG_MARK, APLOG_ERR,
160 0, ap_server_conf,
161 "child process %ld still did not exit, "
162 "sending a SIGKILL",
163 (long)pid);
164 #ifndef BEOS
165 kill(pid, SIGKILL);
166 #else
167 /* sending a SIGKILL kills the entire team on BeOS, and as
168 * httpd thread is part of that team it removes any chance
169 * of ever doing a restart. To counter this I'm changing to
170 * use a kinder, gentler way of killing a specific thread
171 * that is just as effective.
172 */
173 kill_thread(pid);
174 #endif
175 break;
176
177 case 9: /* 14 sec */
178 /* gave it our best shot, but alas... If this really
179 * is a child we are trying to kill and it really hasn't
180 * exited, we will likely fail to bind to the port
181 * after the restart.
182 */
183 ap_log_error(APLOG_MARK, APLOG_ERR,
184 0, ap_server_conf,
185 "could not make child process %ld exit, "
186 "attempting to continue anyway",
187 (long)pid);
188 break;
189 }
190 }
191
192 #if APR_HAS_OTHER_CHILD
193 apr_proc_other_child_check();
194 #endif
195
196 if (!not_dead_yet) {
197 /* nothing left to wait for */
198 break;
199 }
200 }
201 }
202 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
203
204 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
205
206 /* number of calls to wait_or_timeout between writable probes */
207 #ifndef INTERVAL_OF_WRITABLE_PROBES
208 #define INTERVAL_OF_WRITABLE_PROBES 10
209 #endif
210 static int wait_or_timeout_counter;
211
212 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
213 apr_pool_t *p)
214 {
215 apr_status_t rv;
216
217 ++wait_or_timeout_counter;
218 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
219 wait_or_timeout_counter = 0;
220 }
221
222 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
223 if (APR_STATUS_IS_EINTR(rv)) {
224 ret->pid = -1;
225 return;
226 }
227
228 if (APR_STATUS_IS_CHILD_DONE(rv)) {
229 return;
230 }
231
232 #ifdef NEED_WAITPID
233 if ((ret = reap_children(exitcode, status)) > 0) {
234 return;
235 }
236 #endif
237
238 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
239 ret->pid = -1;
240 return;
241 }
242 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
243
244 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
245 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
246 {
247 int signum = status;
248 const char *sigdesc = apr_signal_description_get(signum);
249
250 /* Child died... if it died due to a fatal error,
251 * we should simply bail out. The caller needs to
252 * check for bad rc from us and exit, running any
253 * appropriate cleanups.
254 *
255 * If the child died due to a resource shortage,
256 * the parent should limit the rate of forking
257 */
258 if (APR_PROC_CHECK_EXIT(why)) {
259 if (status == APEXIT_CHILDSICK) {
260 return status;
261 }
262
263 if (status == APEXIT_CHILDFATAL) {
264 ap_log_error(APLOG_MARK, APLOG_ALERT,
265 0, ap_server_conf,
266 "Child %" APR_PID_T_FMT
267 " returned a Fatal error..." APR_EOL_STR
268 "Apache is exiting!",
269 pid->pid);
270 return APEXIT_CHILDFATAL;
271 }
272
273 return 0;
274 }
275
276 if (APR_PROC_CHECK_SIGNALED(why)) {
277 switch (signum) {
278 case SIGTERM:
279 case SIGHUP:
280 case AP_SIG_GRACEFUL:
281 case SIGKILL:
282 break;
283
284 default:
285 if (APR_PROC_CHECK_CORE_DUMP(why)) {
286 ap_log_error(APLOG_MARK, APLOG_NOTICE,
287 0, ap_server_conf,
288 "child pid %ld exit signal %s (%d), "
289 "possible coredump in %s",
290 (long)pid->pid, sigdesc, signum,
291 ap_coredump_dir);
292 }
293 else {
294 ap_log_error(APLOG_MARK, APLOG_NOTICE,
295 0, ap_server_conf,
296 "child pid %ld exit signal %s (%d)",
297 (long)pid->pid, sigdesc, signum);
298 }
299 }
300 }
301 return 0;
302 }
303 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
304
305 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
306 void ap_sock_disable_nagle(apr_socket_t *s)
307 {
308 /* The Nagle algorithm says that we should delay sending partial
309 * packets in hopes of getting more data. We don't want to do
310 * this; we are not telnet. There are bad interactions between
311 * persistent connections and Nagle's algorithm that have very severe
312 * performance penalties. (Failing to disable Nagle is not much of a
313 * problem with simple HTTP.)
314 *
315 * In spite of these problems, failure here is not a shooting offense.
316 */
317 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
318
319 if (status != APR_SUCCESS) {
320 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
321 "apr_socket_opt_set: (TCP_NODELAY)");
322 }
323 }
324 #endif
325
326 #ifdef HAVE_GETPWNAM
327 AP_DECLARE(uid_t) ap_uname2id(const char *name)
328 {
329 struct passwd *ent;
330
331 if (name[0] == '#')
332 return (atoi(&name[1]));
333
334 if (!(ent = getpwnam(name))) {
335 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
336 "%s: bad user name %s", ap_server_argv0, name);
337 exit(1);
338 }
339
340 return (ent->pw_uid);
341 }
342 #endif
343
344 #ifdef HAVE_GETGRNAM
345 AP_DECLARE(gid_t) ap_gname2id(const char *name)
346 {
347 struct group *ent;
348
349 if (name[0] == '#')
350 return (atoi(&name[1]));
351
352 if (!(ent = getgrnam(name))) {
353 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
354 "%s: bad group name %s", ap_server_argv0, name);
355 exit(1);
356 }
357
358 return (ent->gr_gid);
359 }
360 #endif
361
362 #ifndef HAVE_INITGROUPS
363 int initgroups(const char *name, gid_t basegid)
364 {
365 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
366 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
367 return 0;
368 #else /* ndef QNX */
369 gid_t groups[NGROUPS_MAX];
370 struct group *g;
371 int index = 0;
372
373 setgrent();
374
375 groups[index++] = basegid;
376
377 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
378 if (g->gr_gid != basegid) {
379 char **names;
380
381 for (names = g->gr_mem; *names != NULL; ++names) {
382 if (!strcmp(*names, name))
383 groups[index++] = g->gr_gid;
384 }
385 }
386 }
387
388 endgrent();
389
390 return setgroups(index, groups);
391 #endif /* def QNX */
392 }
393 #endif /* def NEED_INITGROUPS */
394
395 #ifdef AP_MPM_USES_POD
396
397 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
398 {
399 apr_status_t rv;
400
401 *pod = apr_palloc(p, sizeof(**pod));
402 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
403 if (rv != APR_SUCCESS) {
404 return rv;
405 }
406
407 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
408 (*pod)->p = p;
409
410 apr_sockaddr_info_get(&(*pod)->sa, ap_listeners->bind_addr->hostname,
411 APR_UNSPEC, ap_listeners->bind_addr->port, 0, p);
412
413 /* close these before exec. */
414 apr_file_unset_inherit((*pod)->pod_in);
415 apr_file_unset_inherit((*pod)->pod_out);
416
417 return APR_SUCCESS;
418 }
419
420 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
421 {
422 char c;
423 apr_size_t len = 1;
424 apr_status_t rv;
425
426 rv = apr_file_read(pod->pod_in, &c, &len);
427
428 if ((rv == APR_SUCCESS) && (len == 1)) {
429 return APR_SUCCESS;
430 }
431
432 if (rv != APR_SUCCESS) {
433 return rv;
434 }
435
436 return AP_NORESTART;
437 }
438
439 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
440 {
441 apr_status_t rv;
442
443 rv = apr_file_close(pod->pod_out);
444 if (rv != APR_SUCCESS) {
445 return rv;
446 }
447
448 rv = apr_file_close(pod->pod_in);
449 if (rv != APR_SUCCESS) {
450 return rv;
451 }
452
453 return APR_SUCCESS;
454 }
455
456 static apr_status_t pod_signal_internal(ap_pod_t *pod)
457 {
458 apr_status_t rv;
459 char char_of_death = '!';
460 apr_size_t one = 1;
461
462 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
463 if (rv != APR_SUCCESS) {
464 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
465 "write pipe_of_death");
466 }
467
468 return rv;
469 }
470
471 /* This function connects to the server, then immediately closes the connection.
472 * This permits the MPM to skip the poll when there is only one listening
473 * socket, because it provides a alternate way to unblock an accept() when
474 * the pod is used.
475 */
476 static apr_status_t dummy_connection(ap_pod_t *pod)
477 {
478 apr_status_t rv;
479 apr_socket_t *sock;
480 apr_pool_t *p;
481
482 /* create a temporary pool for the socket. pconf stays around too long */
483 rv = apr_pool_create(&p, pod->p);
484 if (rv != APR_SUCCESS) {
485 return rv;
486 }
487
488 rv = apr_socket_create(&sock, pod->sa->family, SOCK_STREAM, p);
489 if (rv != APR_SUCCESS) {
490 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
491 "get socket to connect to listener");
492 return rv;
493 }
494
495 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
496 * queued connections before it starts blocking local connects...
497 * we need to keep from blocking too long and instead return an error,
498 * because the MPM won't want to hold up a graceful restart for a
499 * long time
500 */
501 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
502 if (rv != APR_SUCCESS) {
503 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
504 "set timeout on socket to connect to listener");
505 apr_socket_close(sock);
506 return rv;
507 }
508
509 rv = apr_connect(sock, pod->sa);
510 if (rv != APR_SUCCESS) {
511 int log_level = APLOG_WARNING;
512
513 if (APR_STATUS_IS_TIMEUP(rv)) {
514 /* probably some server processes bailed out already and there
515 * is nobody around to call accept and clear out the kernel
516 * connection queue; usually this is not worth logging
517 */
518 log_level = APLOG_DEBUG;
519 }
520
521 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
522 "connect to listener");
523 }
524
525 apr_socket_close(sock);
526 apr_pool_destroy(p);
527
528 return rv;
529 }
530
531 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
532 {
533 apr_status_t rv;
534
535 rv = pod_signal_internal(pod);
536 if (rv != APR_SUCCESS) {
537 return rv;
538 }
539
540 return dummy_connection(pod);
541 }
542
543 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
544 {
545 int i;
546 apr_status_t rv = APR_SUCCESS;
547
548 /* we don't write anything to the pod here... we assume
549 * that the would-be reader of the pod has another way to
550 * see that it is time to die once we wake it up
551 *
552 * writing lots of things to the pod at once is very
553 * problematic... we can fill the kernel pipe buffer and
554 * be blocked until somebody consumes some bytes or
555 * we hit a timeout... if we hit a timeout we can't just
556 * keep trying because maybe we'll never successfully
557 * write again... but then maybe we'll leave would-be
558 * readers stranded (a number of them could be tied up for
559 * a while serving time-consuming requests)
560 */
561 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
562 rv = dummy_connection(pod);
563 }
564 }
565 #endif /* #ifdef AP_MPM_USES_POD */
566
567 /* standard mpm configuration handling */
568 #ifdef AP_MPM_WANT_SET_PIDFILE
569 const char *ap_pid_fname = NULL;
570
571 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
572 const char *arg)
573 {
574 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
575 if (err != NULL) {
576 return err;
577 }
578
579 if (cmd->server->is_virtual) {
580 return "PidFile directive not allowed in <VirtualHost>";
581 }
582
583 ap_pid_fname = arg;
584 return NULL;
585 }
586 #endif
587
588 #ifdef AP_MPM_WANT_SET_SCOREBOARD
589 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
590 const char *arg)
591 {
592 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
593 if (err != NULL) {
594 return err;
595 }
596
597 ap_scoreboard_fname = arg;
598 return NULL;
599 }
600 #endif
601
602 #ifdef AP_MPM_WANT_SET_LOCKFILE
603 const char *ap_lock_fname = NULL;
604
605 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
606 const char *arg)
607 {
608 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
609 if (err != NULL) {
610 return err;
611 }
612
613 ap_lock_fname = arg;
614 return NULL;
615 }
616 #endif
617
618 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
619 int ap_max_requests_per_child = 0;
620
621 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
622 const char *arg)
623 {
624 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
625 if (err != NULL) {
626 return err;
627 }
628
629 ap_max_requests_per_child = atoi(arg);
630
631 return NULL;
632 }
633 #endif
634
635 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
636 char ap_coredump_dir[MAX_STRING_LEN];
637 int ap_coredumpdir_configured;
638
639 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
640 const char *arg)
641 {
642 apr_status_t rv;
643 apr_finfo_t finfo;
644 const char *fname;
645 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
646 if (err != NULL) {
647 return err;
648 }
649
650 fname = ap_server_root_relative(cmd->pool, arg);
651 if (!fname) {
652 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
653 arg, NULL);
654 }
655 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
656 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
657 " does not exist", NULL);
658 }
659 if (finfo.filetype != APR_DIR) {
660 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
661 " is not a directory", NULL);
662 }
663 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
664 ap_coredumpdir_configured = 1;
665 return NULL;
666 }
667 #endif
668
669 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
670 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
671
672 const char ap_valid_accept_mutex_string[] =
673 "Valid accept mutexes for this platform and MPM are: default"
674 #if APR_HAS_FLOCK_SERIALIZE
675 ", flock"
676 #endif
677 #if APR_HAS_FCNTL_SERIALIZE
678 ", fcntl"
679 #endif
680 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
681 ", sysvsem"
682 #endif
683 #if APR_HAS_POSIXSEM_SERIALIZE
684 ", posixsem"
685 #endif
686 #if APR_HAS_PROC_PTHREAD_SERIALIZE
687 ", pthread"
688 #endif
689 ".";
690
691 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
692 void *dummy,
693 const char *arg)
694 {
695 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
696 if (err != NULL) {
697 return err;
698 }
699
700 if (!strcasecmp(arg, "default")) {
701 ap_accept_lock_mech = APR_LOCK_DEFAULT;
702 }
703 #if APR_HAS_FLOCK_SERIALIZE
704 else if (!strcasecmp(arg, "flock")) {
705 ap_accept_lock_mech = APR_LOCK_FLOCK;
706 }
707 #endif
708 #if APR_HAS_FCNTL_SERIALIZE
709 else if (!strcasecmp(arg, "fcntl")) {
710 ap_accept_lock_mech = APR_LOCK_FCNTL;
711 }
712 #endif
713
714 /* perchild can't use SysV sems because the permissions on the accept
715 * mutex can't be set to allow all processes to use the mutex and
716 * at the same time keep all users from being able to dink with the
717 * mutex
718 */
719 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
720 else if (!strcasecmp(arg, "sysvsem")) {
721 ap_accept_lock_mech = APR_LOCK_SYSVSEM;
722 }
723 #endif
724 #if APR_HAS_POSIXSEM_SERIALIZE
725 else if (!strcasecmp(arg, "posixsem")) {
726 ap_accept_lock_mech = APR_LOCK_POSIXSEM;
727 }
728 #endif
729 #if APR_HAS_PROC_PTHREAD_SERIALIZE
730 else if (!strcasecmp(arg, "pthread")) {
731 ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
732 }
733 #endif
734 else {
735 return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; ",
736 ap_valid_accept_mutex_string, NULL);
737 }
738 return NULL;
739 }
740
741 #endif
742
743 #ifdef AP_MPM_WANT_SIGNAL_SERVER
744
745 static const char *dash_k_arg;
746
747 static int send_signal(pid_t pid, int sig)
748 {
749 if (kill(pid, sig) < 0) {
750 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
751 "sending signal to server");
752 return 1;
753 }
754 return 0;
755 }
756
757 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
758 {
759 apr_status_t rv;
760 pid_t otherpid;
761 int running = 0;
762 int have_pid_file = 0;
763 const char *status;
764
765 *exit_status = 0;
766
767 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
768 if (rv != APR_SUCCESS) {
769 if (rv != APR_ENOENT) {
770 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
771 "Error retrieving pid file %s", ap_pid_fname);
772 *exit_status = 1;
773 return 1;
774 }
775 status = "httpd (no pid file) not running";
776 }
777 else {
778 have_pid_file = 1;
779 if (kill(otherpid, 0) == 0) {
780 running = 1;
781 status = apr_psprintf(pconf,
782 "httpd (pid %" APR_PID_T_FMT ") already "
783 "running", otherpid);
784 }
785 else {
786 status = apr_psprintf(pconf,
787 "httpd (pid %" APR_PID_T_FMT "?) not running",
788 otherpid);
789 }
790 }
791
792 if (!strcmp(dash_k_arg, "start")) {
793 if (running) {
794 printf("%s\n", status);
795 return 1;
796 }
797 }
798
799 if (!strcmp(dash_k_arg, "stop")) {
800 if (!running) {
801 printf("%s\n", status);
802 }
803 else {
804 send_signal(otherpid, SIGTERM);
805 }
806 return 1;
807 }
808
809 if (!strcmp(dash_k_arg, "restart")) {
810 if (!running) {
811 printf("httpd not running, trying to start\n");
812 }
813 else {
814 *exit_status = send_signal(otherpid, SIGHUP);
815 return 1;
816 }
817 }
818
819 if (!strcmp(dash_k_arg, "graceful")) {
820 if (!running) {
821 printf("httpd not running, trying to start\n");
822 }
823 else {
824 *exit_status = send_signal(otherpid, SIGUSR1);
825 return 1;
826 }
827 }
828
829 return 0;
830 }
831
832 void ap_mpm_rewrite_args(process_rec *process)
833 {
834 apr_array_header_t *mpm_new_argv;
835 apr_status_t rv;
836 apr_getopt_t *opt;
837 char optbuf[3];
838 const char *optarg;
839 int fixed_args;
840
841 mpm_new_argv = apr_array_make(process->pool, process->argc,
842 sizeof(const char **));
843 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
844 fixed_args = mpm_new_argv->nelts;
845 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
846 opt->errfn = NULL;
847 optbuf[0] = '-';
848 /* option char returned by apr_getopt() will be stored in optbuf[1] */
849 optbuf[2] = '\0';
850 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
851 optbuf + 1, &optarg)) == APR_SUCCESS) {
852 switch(optbuf[1]) {
853 case 'k':
854 if (!dash_k_arg) {
855 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
856 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful")) {
857 dash_k_arg = optarg;
858 break;
859 }
860 }
861 default:
862 *(const char **)apr_array_push(mpm_new_argv) =
863 apr_pstrdup(process->pool, optbuf);
864 if (optarg) {
865 *(const char **)apr_array_push(mpm_new_argv) = optarg;
866 }
867 }
868 }
869
870 /* back up to capture the bad argument */
871 if (rv == APR_BADCH || rv == APR_BADARG) {
872 opt->ind--;
873 }
874
875 while (opt->ind < opt->argc) {
876 *(const char **)apr_array_push(mpm_new_argv) =
877 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
878 }
879
880 process->argc = mpm_new_argv->nelts;
881 process->argv = (const char * const *)mpm_new_argv->elts;
882
883 if (dash_k_arg) {
884 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
885 }
886 }
887
888 #endif /* AP_MPM_WANT_SIGNAL_SERVER */
889
890 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
891 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
892
893 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
894 const char *arg)
895 {
896 long value;
897 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
898 if (err != NULL) {
899 return err;
900 }
901
902 value = strtol(arg, NULL, 0);
903 if (value < 0 || errno == ERANGE)
904 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
905 arg, NULL);
906
907 ap_max_mem_free = (apr_uint32_t)value * 1024;
908
909 return NULL;
910 }
911
912 #endif /* AP_MPM_WANT_SET_MAX_MEM_FREE */