upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / threadproc / unix / signals.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 #define INCL_DOSEXCEPTIONS      /* for OS2 */
18 #include "apr_arch_threadproc.h"
19 #include "apr_private.h"
20 #include "apr_pools.h"
21 #include "apr_signal.h"
22 #include "apr_strings.h"
23
24 #include <assert.h>
25 #if APR_HAS_THREADS && APR_HAVE_PTHREAD_H
26 #include <pthread.h>
27 #endif
28
29 APR_DECLARE(apr_status_t) apr_proc_kill(apr_proc_t *proc, int signum)
30 {
31 #ifdef OS2
32     /* SIGTERM's don't work too well in OS/2 (only affects other EMX
33      * programs). CGIs may not be, esp. REXX scripts, so use a native
34      * call instead
35      */
36     if (signum == SIGTERM) {
37         return APR_OS2_STATUS(DosSendSignalException(proc->pid,
38                                                      XCPT_SIGNAL_BREAK));
39     }
40 #endif /* OS2 */
41
42     if (kill(proc->pid, signum) == -1) {
43         return errno;
44     }
45
46     return APR_SUCCESS;
47 }
48
49
50 #if APR_HAVE_SIGACTION
51
52 #ifdef DARWIN
53 static void avoid_zombies(int signo)
54 {
55     int exit_status;
56
57     while (waitpid(-1, &exit_status, WNOHANG) > 0) {
58         /* do nothing */
59     }
60 }
61 #endif /* DARWIN */
62
63 /*
64  * Replace standard signal() with the more reliable sigaction equivalent
65  * from W. Richard Stevens' "Advanced Programming in the UNIX Environment"
66  * (the version that does not automatically restart system calls).
67  */
68 APR_DECLARE(apr_sigfunc_t *) apr_signal(int signo, apr_sigfunc_t * func)
69 {
70     struct sigaction act, oact;
71
72     act.sa_handler = func;
73     sigemptyset(&act.sa_mask);
74     act.sa_flags = 0;
75 #ifdef SA_INTERRUPT             /* SunOS */
76     act.sa_flags |= SA_INTERRUPT;
77 #endif
78 #if defined(__osf__) && defined(__alpha)
79     /* XXX jeff thinks this should be enabled whenever SA_NOCLDWAIT is defined */
80
81     /* this is required on Tru64 to cause child processes to
82      * disappear gracefully - XPG4 compatible 
83      */
84     if ((signo == SIGCHLD) && (func == SIG_IGN)) {
85         act.sa_flags |= SA_NOCLDWAIT;
86     }
87 #endif
88 #ifdef DARWIN
89     /* ignoring SIGCHLD or leaving the default disposition doesn't avoid zombies,
90      * and there is no SA_NOCLDWAIT flag, so catch the signal and reap status in 
91      * the handler to avoid zombies
92      */
93     if ((signo == SIGCHLD) && (func == SIG_IGN)) {
94         act.sa_handler = avoid_zombies;
95     }
96 #endif
97     if (sigaction(signo, &act, &oact) < 0)
98         return SIG_ERR;
99     return oact.sa_handler;
100 }
101
102 #endif /* HAVE_SIGACTION */
103
104 /* AC_DECL_SYS_SIGLIST defines either of these symbols depending
105  * on the version of autoconf used. */
106 #if defined(SYS_SIGLIST_DECLARED) || HAVE_DECL_SYS_SIGLIST
107
108 void apr_signal_init(apr_pool_t *pglobal)
109 {
110 }
111 const char *apr_signal_description_get(int signum)
112 {
113     return sys_siglist[signum];
114 }
115
116 #else /* !(SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST) */
117
118 /* we need to roll our own signal description stuff */
119
120 #if defined(NSIG)
121 #define APR_NUMSIG NSIG
122 #elif defined(_NSIG)
123 #define APR_NUMSIG _NSIG
124 #elif defined(__NSIG)
125 #define APR_NUMSIG __NSIG
126 #else
127 #define APR_NUMSIG 33   /* breaks on OS/390 with < 33; 32 is o.k. for most */
128 #endif
129
130 static const char *signal_description[APR_NUMSIG];
131
132 #define store_desc(index, string) \
133         do { \
134             if (index >= APR_NUMSIG) { \
135                 assert(index < APR_NUMSIG); \
136             } \
137             else { \
138                 signal_description[index] = string; \
139             } \
140         } while (0)
141
142 void apr_signal_init(apr_pool_t *pglobal)
143 {
144     int sig;
145
146     store_desc(0, "Signal 0");
147
148 #ifdef SIGHUP
149     store_desc(SIGHUP, "Hangup");
150 #endif
151 #ifdef SIGINT
152     store_desc(SIGINT, "Interrupt");
153 #endif
154 #ifdef SIGQUIT
155     store_desc(SIGQUIT, "Quit");
156 #endif
157 #ifdef SIGILL
158     store_desc(SIGILL, "Illegal instruction");
159 #endif
160 #ifdef SIGTRAP
161     store_desc(SIGTRAP, "Trace/BPT trap");
162 #endif
163 #ifdef SIGIOT
164     store_desc(SIGIOT, "IOT instruction");
165 #endif
166 #ifdef SIGABRT
167     store_desc(SIGABRT, "Abort");
168 #endif
169 #ifdef SIGEMT
170     store_desc(SIGEMT, "Emulator trap");
171 #endif
172 #ifdef SIGFPE
173     store_desc(SIGFPE, "Arithmetic exception");
174 #endif
175 #ifdef SIGKILL
176     store_desc(SIGKILL, "Killed");
177 #endif
178 #ifdef SIGBUS
179     store_desc(SIGBUS, "Bus error");
180 #endif
181 #ifdef SIGSEGV
182     store_desc(SIGSEGV, "Segmentation fault");
183 #endif
184 #ifdef SIGSYS
185     store_desc(SIGSYS, "Bad system call");
186 #endif
187 #ifdef SIGPIPE
188     store_desc(SIGPIPE, "Broken pipe");
189 #endif
190 #ifdef SIGALRM
191     store_desc(SIGALRM, "Alarm clock");
192 #endif
193 #ifdef SIGTERM
194     store_desc(SIGTERM, "Terminated");
195 #endif
196 #ifdef SIGUSR1
197     store_desc(SIGUSR1, "User defined signal 1");
198 #endif
199 #ifdef SIGUSR2
200     store_desc(SIGUSR2, "User defined signal 2");
201 #endif
202 #ifdef SIGCLD
203     store_desc(SIGCLD, "Child status change");
204 #endif
205 #ifdef SIGCHLD
206     store_desc(SIGCHLD, "Child status change");
207 #endif
208 #ifdef SIGPWR
209     store_desc(SIGPWR, "Power-fail restart");
210 #endif
211 #ifdef SIGWINCH
212     store_desc(SIGWINCH, "Window changed");
213 #endif
214 #ifdef SIGURG
215     store_desc(SIGURG, "urgent socket condition");
216 #endif
217 #ifdef SIGPOLL
218     store_desc(SIGPOLL, "Pollable event occurred");
219 #endif
220 #ifdef SIGIO
221     store_desc(SIGIO, "socket I/O possible");
222 #endif
223 #ifdef SIGSTOP
224     store_desc(SIGSTOP, "Stopped (signal)");
225 #endif
226 #ifdef SIGTSTP
227     store_desc(SIGTSTP, "Stopped");
228 #endif
229 #ifdef SIGCONT
230     store_desc(SIGCONT, "Continued");
231 #endif
232 #ifdef SIGTTIN
233     store_desc(SIGTTIN, "Stopped (tty input)");
234 #endif
235 #ifdef SIGTTOU
236     store_desc(SIGTTOU, "Stopped (tty output)");
237 #endif
238 #ifdef SIGVTALRM
239     store_desc(SIGVTALRM, "virtual timer expired");
240 #endif
241 #ifdef SIGPROF
242     store_desc(SIGPROF, "profiling timer expired");
243 #endif
244 #ifdef SIGXCPU
245     store_desc(SIGXCPU, "exceeded cpu limit");
246 #endif
247 #ifdef SIGXFSZ
248     store_desc(SIGXFSZ, "exceeded file size limit");
249 #endif
250
251     for (sig = 0; sig < APR_NUMSIG; ++sig)
252         if (signal_description[sig] == NULL)
253             signal_description[sig] = apr_psprintf(pglobal, "signal #%d", sig);
254 }
255
256 const char *apr_signal_description_get(int signum)
257 {
258     return
259         signum < APR_NUMSIG
260         ? signal_description[signum]
261         : "unknown signal (number)";
262 }
263
264 #endif /* SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST */
265
266 #if APR_HAS_THREADS && (HAVE_SIGSUSPEND || APR_HAVE_SIGWAIT) && !defined(OS2)
267
268 static void remove_sync_sigs(sigset_t *sig_mask)
269 {
270 #ifdef SIGABRT
271     sigdelset(sig_mask, SIGABRT);
272 #endif
273 #ifdef SIGBUS
274     sigdelset(sig_mask, SIGBUS);
275 #endif
276 #ifdef SIGEMT
277     sigdelset(sig_mask, SIGEMT);
278 #endif
279 #ifdef SIGFPE
280     sigdelset(sig_mask, SIGFPE);
281 #endif
282 #ifdef SIGILL
283     sigdelset(sig_mask, SIGILL);
284 #endif
285 #ifdef SIGIOT
286     sigdelset(sig_mask, SIGIOT);
287 #endif
288 #ifdef SIGPIPE
289     sigdelset(sig_mask, SIGPIPE);
290 #endif
291 #ifdef SIGSEGV
292     sigdelset(sig_mask, SIGSEGV);
293 #endif
294 #ifdef SIGSYS
295     sigdelset(sig_mask, SIGSYS);
296 #endif
297 #ifdef SIGTRAP
298     sigdelset(sig_mask, SIGTRAP);
299 #endif
300
301 /* the rest of the signals removed from the mask in this function
302  * absolutely must be removed; you cannot block synchronous signals
303  * (requirement of pthreads API)
304  *
305  * SIGUSR2 is being removed from the mask for the convenience of
306  * Purify users (Solaris, HP-UX, SGI) since Purify uses SIGUSR2
307  */
308 #ifdef SIGUSR2
309     sigdelset(sig_mask, SIGUSR2);
310 #endif
311 }
312
313 APR_DECLARE(apr_status_t) apr_signal_thread(int(*signal_handler)(int signum))
314 {
315     sigset_t sig_mask;
316 #if APR_HAVE_SIGWAIT
317     int (*sig_func)(int signum) = (int (*)(int))signal_handler;
318 #endif
319
320     /* This thread will be the one responsible for handling signals */
321     sigfillset(&sig_mask);
322
323     /* On certain platforms, sigwait() returns EINVAL if any of various
324      * unblockable signals are included in the mask.  This was first 
325      * observed on AIX and Tru64.
326      */
327 #ifdef SIGKILL
328     sigdelset(&sig_mask, SIGKILL);
329 #endif
330 #ifdef SIGSTOP
331     sigdelset(&sig_mask, SIGSTOP);
332 #endif
333 #ifdef SIGCONT
334     sigdelset(&sig_mask, SIGCONT);
335 #endif
336 #ifdef SIGWAITING
337     sigdelset(&sig_mask, SIGWAITING);
338 #endif
339
340     /* no synchronous signals should be in the mask passed to sigwait() */
341     remove_sync_sigs(&sig_mask);
342
343     /* On AIX (4.3.3, at least), sigwait() won't wake up if the high-
344      * order bit of the second word of flags is turned on.  sigdelset()
345      * returns an error when trying to turn this off, so we'll turn it
346      * off manually.
347      *
348      * Note that the private fields differ between 32-bit and 64-bit
349      * and even between _ALL_SOURCE and !_ALL_SOURCE.  Except that on
350      * AIX 4.3 32-bit builds and 64-bit builds use the same definition.
351      *
352      * Applicable AIX fixes such that this is no longer needed:
353      *
354      * APAR IY23096 for AIX 51B, fix included in AIX 51C, and
355      * APAR IY24162 for 43X.
356      */
357 #if defined(_AIX)
358 #if defined(__64BIT__) && defined(_AIXVERSION_510)
359 #ifdef _ALL_SOURCE
360         sig_mask.ss_set[3] &= 0x7FFFFFFF;
361 #else /* not _ALL_SOURCE */
362         sig_mask.__ss_set[3] &= 0x7FFFFFFF;
363 #endif
364 #else /* not 64-bit build, or 64-bit build on 4.3 */
365 #ifdef _ALL_SOURCE
366         sig_mask.hisigs &= 0x7FFFFFFF;
367 #else /* not _ALL_SOURCE */
368         sig_mask.__hisigs &= 0x7FFFFFFF;
369 #endif
370 #endif
371 #endif /* _AIX */
372
373     while (1) {
374 #if APR_HAVE_SIGWAIT
375         int signal_received;
376
377         if (apr_sigwait(&sig_mask, &signal_received) != 0)
378         {
379             /* handle sigwait() error here */
380         }
381         
382         if (sig_func(signal_received) == 1) {
383             return APR_SUCCESS;
384         }
385 #elif HAVE_SIGSUSPEND
386         sigsuspend(&sig_mask);
387 #else
388 #error No apr_sigwait() and no sigsuspend()
389 #endif
390     }
391 }
392
393 APR_DECLARE(apr_status_t) apr_setup_signal_thread(void)
394 {
395     sigset_t sig_mask;
396     int rv;
397
398     /* All threads should mask out signals to be handled by
399      * the thread doing sigwait().
400      *
401      * No thread should ever block synchronous signals.
402      * See the Solaris man page for pthread_sigmask() for
403      * some information.  Solaris chooses to knock out such
404      * processes when a blocked synchronous signal is 
405      * delivered, skipping any registered signal handler.
406      * AIX doesn't call a signal handler either.  At least
407      * one level of linux+glibc does call the handler even
408      * when the synchronous signal is blocked.
409      */
410     sigfillset(&sig_mask);
411     remove_sync_sigs(&sig_mask);
412
413 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
414     if ((rv = sigprocmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
415         rv = errno;
416     }
417 #else
418     if ((rv = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
419 #ifdef PTHREAD_SETS_ERRNO
420         rv = errno;
421 #endif
422     }
423 #endif
424     return rv;
425 }
426
427 #endif
428
429 /* Deprecated */
430 const char *apr_signal_get_description(int signum)
431 {
432     return apr_signal_description_get(signum);
433 }