]>
Commit | Line | Data |
---|---|---|
eb1f6bfa | 1 | // |
7962bc6a | 2 | // $Id$ |
eb1f6bfa | 3 | // |
0b96a9b3 | 4 | // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de> |
eb1f6bfa AJ |
5 | // File: signal.cc |
6 | // Date: Sat Feb 28 1998 | |
7 | // Compiler: gcc 2.7.2.x series | |
feec68a0 | 8 | // |
eb1f6bfa AJ |
9 | // Books: W. Richard Steven, "Advanced Programming in the UNIX Environment", |
10 | // Addison-Wesley, 1992. | |
feec68a0 | 11 | // |
eb1f6bfa | 12 | // (c) 1998 Lehrgebiet Rechnernetze und Verteilte Systeme |
0b96a9b3 | 13 | // Universit?t Hannover, Germany |
eb1f6bfa AJ |
14 | // |
15 | // Permission to use, copy, modify, distribute, and sell this software | |
16 | // and its documentation for any purpose is hereby granted without fee, | |
17 | // provided that (i) the above copyright notices and this permission | |
18 | // notice appear in all copies of the software and related documentation, | |
19 | // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte | |
20 | // Systeme and the University of Hannover may not be used in any | |
21 | // advertising or publicity relating to the software without the | |
22 | // specific, prior written permission of Lehrgebiet Rechnernetze und | |
23 | // Verteilte Systeme and the University of Hannover. | |
24 | // | |
25 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | |
26 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | |
27 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
28 | // | |
29 | // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR | |
30 | // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL, | |
31 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES | |
32 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT | |
33 | // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, | |
34 | // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
35 | // SOFTWARE. | |
36 | // | |
eb1f6bfa AJ |
37 | // Revision 1.3 1999/01/19 13:11:52 cached |
38 | // adaptations necessary for AIX. | |
39 | // | |
40 | // Revision 1.2 1999/01/19 11:00:50 voeckler | |
41 | // added psignal(int,const char*) compatibility function. | |
42 | // | |
43 | // Revision 1.1 1998/08/13 21:51:58 voeckler | |
44 | // Initial revision | |
45 | // | |
46 | // | |
47 | ||
d8b258a9 | 48 | #if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) |
eb1f6bfa AJ |
49 | #pragma implementation |
50 | #endif | |
51 | ||
f7f3304a | 52 | #include "squid.h" |
2ccf2eb2 AJ |
53 | #include "signal.hh" |
54 | ||
55 | //#include <sys/types.h> | |
eb1f6bfa AJ |
56 | #include <errno.h> |
57 | #include <string.h> | |
58 | #include <memory.h> | |
59 | #include <stdio.h> | |
60 | #include <unistd.h> | |
61 | #include <sys/wait.h> | |
2ccf2eb2 | 62 | //#include <signal.h> |
eb1f6bfa AJ |
63 | |
64 | SigFunc* | |
65 | Signal( int signo, SigFunc* newhandler, bool doInterrupt ) | |
feec68a0 A |
66 | // purpose: install reliable signals |
67 | // paramtr: signo (IN): signal for which a handler is to be installed | |
68 | // newhandler (IN): function pointer to the signal handler | |
69 | // doInterrupt (IN): interrupted system calls wanted! | |
70 | // returns: the old signal handler, or SIG_ERR in case of error. | |
eb1f6bfa | 71 | { |
feec68a0 | 72 | struct sigaction action, old; |
eb1f6bfa | 73 | |
feec68a0 A |
74 | memset( &old, 0, sizeof(old) ); |
75 | memset( &action, 0, sizeof(action) ); | |
eb1f6bfa | 76 | |
feec68a0 A |
77 | // action.sa_handler = newhandler; I HATE TYPE-OVERCORRECTNESS ! |
78 | memmove( &action.sa_handler, &newhandler, sizeof(SigFunc*) ); | |
79 | sigemptyset( &action.sa_mask ); | |
eb1f6bfa | 80 | |
feec68a0 A |
81 | if ( signo == SIGCHLD ) { |
82 | action.sa_flags |= SA_NOCLDSTOP; | |
eb1f6bfa AJ |
83 | |
84 | #ifdef SA_NODEFER | |
feec68a0 | 85 | action.sa_flags |= SA_NODEFER; // SYSV: don't block current signal |
eb1f6bfa | 86 | #endif |
feec68a0 | 87 | } |
eb1f6bfa | 88 | |
feec68a0 | 89 | if ( signo == SIGALRM || doInterrupt ) { |
eb1f6bfa | 90 | #ifdef SA_INTERRUPT |
feec68a0 | 91 | action.sa_flags |= SA_INTERRUPT; // SunOS, obsoleted by POSIX |
eb1f6bfa | 92 | #endif |
feec68a0 | 93 | } else { |
eb1f6bfa | 94 | #ifdef SA_RESTART |
feec68a0 | 95 | action.sa_flags |= SA_RESTART; // BSD, SVR4 |
eb1f6bfa | 96 | #endif |
feec68a0 | 97 | } |
eb1f6bfa | 98 | |
feec68a0 A |
99 | return ( sigaction( signo, &action, &old ) < 0 ) ? |
100 | (SigFunc*) SIG_ERR : | |
101 | (SigFunc*) old.sa_handler; | |
eb1f6bfa AJ |
102 | } |
103 | ||
104 | SIGRETTYPE | |
105 | sigChild( int signo ) | |
feec68a0 A |
106 | // purpose: supply ad hoc child handler with output on stderr |
107 | // paramtr: signo (IN): == SIGCHLD | |
108 | // returns: only if OS uses a return type for signal handler | |
109 | // seealso: Stevens, UNP, figure 5.11 *and* Stevens, APUE, figure 8.3 | |
eb1f6bfa | 110 | { |
feec68a0 A |
111 | pid_t pid; |
112 | int status = signo; // to stop GNU from complaining... | |
feec68a0 A |
113 | |
114 | int saveerr = errno; | |
115 | while ( (pid = waitpid( -1, &status, WNOHANG )) > 0 ) { | |
116 | if ( WIFEXITED(status) ) { | |
ec3c3187 | 117 | fprintf( stderr, "child (pid=%ld) reaped, status %d\n%c", |
8978bd9d | 118 | (long) pid, WEXITSTATUS(status), 0 ); |
feec68a0 | 119 | } else if ( WIFSIGNALED(status) ) { |
ec3c3187 | 120 | fprintf( stderr, "child (pid=%ld) died on signal %d%s\n%c", |
8978bd9d | 121 | (long) pid, WTERMSIG(status), |
eb1f6bfa | 122 | #ifdef WCOREDUMP |
8978bd9d | 123 | WCOREDUMP(status) ? " (core generated)" : "", |
eb1f6bfa | 124 | #else |
8978bd9d | 125 | "", |
eb1f6bfa | 126 | #endif |
8978bd9d | 127 | 0 ); |
feec68a0 | 128 | } else { |
ec3c3187 | 129 | fprintf( stderr, "detected dead child (pid=%ld), status %d\n%c", |
8978bd9d | 130 | (long) pid, status, 0 ); |
feec68a0 | 131 | } |
eb1f6bfa | 132 | } |
feec68a0 | 133 | errno = saveerr; |
eb1f6bfa AJ |
134 | |
135 | #if SIGRETTYPE != void | |
feec68a0 | 136 | return 0; |
eb1f6bfa AJ |
137 | #endif |
138 | } |