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 #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"
25 #if APR_HAS_THREADS && APR_HAVE_PTHREAD_H
29 APR_DECLARE(apr_status_t) apr_proc_kill(apr_proc_t *proc, int signum)
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
36 if (signum == SIGTERM) {
37 return APR_OS2_STATUS(DosSendSignalException(proc->pid,
42 if (kill(proc->pid, signum) == -1) {
50 #if APR_HAVE_SIGACTION
53 static void avoid_zombies(int signo)
57 while (waitpid(-1, &exit_status, WNOHANG) > 0) {
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).
68 APR_DECLARE(apr_sigfunc_t *) apr_signal(int signo, apr_sigfunc_t * func)
70 struct sigaction act, oact;
72 act.sa_handler = func;
73 sigemptyset(&act.sa_mask);
75 #ifdef SA_INTERRUPT /* SunOS */
76 act.sa_flags |= SA_INTERRUPT;
78 #if defined(__osf__) && defined(__alpha)
79 /* XXX jeff thinks this should be enabled whenever SA_NOCLDWAIT is defined */
81 /* this is required on Tru64 to cause child processes to
82 * disappear gracefully - XPG4 compatible
84 if ((signo == SIGCHLD) && (func == SIG_IGN)) {
85 act.sa_flags |= SA_NOCLDWAIT;
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
93 if ((signo == SIGCHLD) && (func == SIG_IGN)) {
94 act.sa_handler = avoid_zombies;
97 if (sigaction(signo, &act, &oact) < 0)
99 return oact.sa_handler;
102 #endif /* HAVE_SIGACTION */
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
108 void apr_signal_init(apr_pool_t *pglobal)
111 const char *apr_signal_description_get(int signum)
113 return sys_siglist[signum];
116 #else /* !(SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST) */
118 /* we need to roll our own signal description stuff */
121 #define APR_NUMSIG NSIG
123 #define APR_NUMSIG _NSIG
124 #elif defined(__NSIG)
125 #define APR_NUMSIG __NSIG
127 #define APR_NUMSIG 33 /* breaks on OS/390 with < 33; 32 is o.k. for most */
130 static const char *signal_description[APR_NUMSIG];
132 #define store_desc(index, string) \
134 if (index >= APR_NUMSIG) { \
135 assert(index < APR_NUMSIG); \
138 signal_description[index] = string; \
142 void apr_signal_init(apr_pool_t *pglobal)
146 store_desc(0, "Signal 0");
149 store_desc(SIGHUP, "Hangup");
152 store_desc(SIGINT, "Interrupt");
155 store_desc(SIGQUIT, "Quit");
158 store_desc(SIGILL, "Illegal instruction");
161 store_desc(SIGTRAP, "Trace/BPT trap");
164 store_desc(SIGIOT, "IOT instruction");
167 store_desc(SIGABRT, "Abort");
170 store_desc(SIGEMT, "Emulator trap");
173 store_desc(SIGFPE, "Arithmetic exception");
176 store_desc(SIGKILL, "Killed");
179 store_desc(SIGBUS, "Bus error");
182 store_desc(SIGSEGV, "Segmentation fault");
185 store_desc(SIGSYS, "Bad system call");
188 store_desc(SIGPIPE, "Broken pipe");
191 store_desc(SIGALRM, "Alarm clock");
194 store_desc(SIGTERM, "Terminated");
197 store_desc(SIGUSR1, "User defined signal 1");
200 store_desc(SIGUSR2, "User defined signal 2");
203 store_desc(SIGCLD, "Child status change");
206 store_desc(SIGCHLD, "Child status change");
209 store_desc(SIGPWR, "Power-fail restart");
212 store_desc(SIGWINCH, "Window changed");
215 store_desc(SIGURG, "urgent socket condition");
218 store_desc(SIGPOLL, "Pollable event occurred");
221 store_desc(SIGIO, "socket I/O possible");
224 store_desc(SIGSTOP, "Stopped (signal)");
227 store_desc(SIGTSTP, "Stopped");
230 store_desc(SIGCONT, "Continued");
233 store_desc(SIGTTIN, "Stopped (tty input)");
236 store_desc(SIGTTOU, "Stopped (tty output)");
239 store_desc(SIGVTALRM, "virtual timer expired");
242 store_desc(SIGPROF, "profiling timer expired");
245 store_desc(SIGXCPU, "exceeded cpu limit");
248 store_desc(SIGXFSZ, "exceeded file size limit");
251 for (sig = 0; sig < APR_NUMSIG; ++sig)
252 if (signal_description[sig] == NULL)
253 signal_description[sig] = apr_psprintf(pglobal, "signal #%d", sig);
256 const char *apr_signal_description_get(int signum)
260 ? signal_description[signum]
261 : "unknown signal (number)";
264 #endif /* SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST */
266 #if APR_HAS_THREADS && (HAVE_SIGSUSPEND || APR_HAVE_SIGWAIT) && !defined(OS2)
268 static void remove_sync_sigs(sigset_t *sig_mask)
271 sigdelset(sig_mask, SIGABRT);
274 sigdelset(sig_mask, SIGBUS);
277 sigdelset(sig_mask, SIGEMT);
280 sigdelset(sig_mask, SIGFPE);
283 sigdelset(sig_mask, SIGILL);
286 sigdelset(sig_mask, SIGIOT);
289 sigdelset(sig_mask, SIGPIPE);
292 sigdelset(sig_mask, SIGSEGV);
295 sigdelset(sig_mask, SIGSYS);
298 sigdelset(sig_mask, SIGTRAP);
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)
305 * SIGUSR2 is being removed from the mask for the convenience of
306 * Purify users (Solaris, HP-UX, SGI) since Purify uses SIGUSR2
309 sigdelset(sig_mask, SIGUSR2);
313 APR_DECLARE(apr_status_t) apr_signal_thread(int(*signal_handler)(int signum))
317 int (*sig_func)(int signum) = (int (*)(int))signal_handler;
320 /* This thread will be the one responsible for handling signals */
321 sigfillset(&sig_mask);
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.
328 sigdelset(&sig_mask, SIGKILL);
331 sigdelset(&sig_mask, SIGSTOP);
334 sigdelset(&sig_mask, SIGCONT);
337 sigdelset(&sig_mask, SIGWAITING);
340 /* no synchronous signals should be in the mask passed to sigwait() */
341 remove_sync_sigs(&sig_mask);
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
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.
352 * Applicable AIX fixes such that this is no longer needed:
354 * APAR IY23096 for AIX 51B, fix included in AIX 51C, and
355 * APAR IY24162 for 43X.
358 #if defined(__64BIT__) && defined(_AIXVERSION_510)
360 sig_mask.ss_set[3] &= 0x7FFFFFFF;
361 #else /* not _ALL_SOURCE */
362 sig_mask.__ss_set[3] &= 0x7FFFFFFF;
364 #else /* not 64-bit build, or 64-bit build on 4.3 */
366 sig_mask.hisigs &= 0x7FFFFFFF;
367 #else /* not _ALL_SOURCE */
368 sig_mask.__hisigs &= 0x7FFFFFFF;
377 if (apr_sigwait(&sig_mask, &signal_received) != 0)
379 /* handle sigwait() error here */
382 if (sig_func(signal_received) == 1) {
385 #elif HAVE_SIGSUSPEND
386 sigsuspend(&sig_mask);
388 #error No apr_sigwait() and no sigsuspend()
393 APR_DECLARE(apr_status_t) apr_setup_signal_thread(void)
398 /* All threads should mask out signals to be handled by
399 * the thread doing sigwait().
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.
410 sigfillset(&sig_mask);
411 remove_sync_sigs(&sig_mask);
413 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
414 if ((rv = sigprocmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
418 if ((rv = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
419 #ifdef PTHREAD_SETS_ERRNO
430 const char *apr_signal_get_description(int signum)
432 return apr_signal_description_get(signum);