modules/up/src/Core/sys/Signal.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- sysSignalFromKernel
- Signal
- Signal
1 //
2 // $Id: Signal.cc,v 1.1.1.1 2000/03/10 16:32:20 engin Exp $
3 //
4 // Signal.cc
5 // Author: Ramesh Govindan <govindan@isi.edu>
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cerrno>
14
15 extern "C" {
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif // HAVE_UNISTD_H
19
20 #include <sys/types.h>
21 #include <signal.h>
22 }
23
24 #include "util/Types.hh"
25 #include "util/List.hh"
26 #include "util/Trail.hh"
27
28 #include "sys/Signal.hh"
29 #include "sched/Dispatcher.hh"
30
31 extern "C" {
32 #ifndef STDC_HEADERS
33 #ifdef HAVE_SIGVEC
34 extern void sigvec(...);
35 #endif
36 #ifdef HAVE_SIGACTION
37 extern int sigaction(...);
38 extern int sigemptyset(...);
39 #endif
40 #ifdef HAVE_SIGSET
41 extern int sigset(...);
42 #endif
43 #endif
44 }
45
46 // File local stuff
47 static struct _sigTypeToName {
48 int sigtype;
49 const char* signame;
50 } sigTypeToName[] = {
51 {SIGTERM, "terminate"},
52 {SIGHUP, "hup"},
53 {SIGINT, "int"},
54 {SIGQUIT, "quit"},
55 {SIGPIPE, "pipe"},
56 #ifdef SIGTTOU
57 {SIGTTOU, "ttou"},
58 #endif // SIGTTOU
59 #ifdef SIGTTIN
60 {SIGTTIN, "ttin"},
61 #endif // SIGTTIN
62 #ifdef SIGTSTP
63 {SIGTSTP, "tstp"},
64 #endif SIGTSTP
65 {SIGHUP, "hup"},
66 {0},
67 };
68 static TraceCode traceSignal("signal");
69
70 // The Signal class represents a specific signal.
71 // Usually, the main program:
72 // creates one or more Signal objects (of which three types
73 // are recognized - terminate, restart, dump)
74 // this automatically registers the object with the dispatcher
75
76 static void
77 sysSignalFromKernel(int sigtype)
/* [<][>][^][v][top][bottom][index][help] */
78 {
79 struct _sigTypeToName *st;
80
81 for (st = &sigTypeToName[0]; st->sigtype != 0; st++) {
82 if (st->sigtype == sigtype) {
83 TRACE(traceSignal,
84 "received %s signal, number %d\n",
85 st->signame,
86 st->sigtype);
87 #ifdef HAVE_SIGSET
88 // Re-arm signal
89 sigset(sigtype, sysSignalFromKernel);
90 #endif HAVE_SIGSET
91 dispatcher.signals.mark(st->signame);
92 return;
93 }
94 }
95
96 ERROR("received unrecognized signal %d\n",
97 sigtype);
98 return;
99 }
100
101 Signal::Signal(const char* p,
/* [<][>][^][v][top][bottom][index][help] */
102 const Handler* h)
103 : ListNode()
104 {
105 struct _sigTypeToName *st;
106
107 pending_ = false;
108 for (st = &sigTypeToName[0]; st->sigtype != 0; st++) {
109 if (!strcmp(st->signame, p)) {
110 name_ = st->signame;
111 if (h == NULL) {
112 goto Ignore;
113 }
114 handler_ = *h;
115 dispatcher.signals.append(this);
116 goto Arm;
117 }
118 }
119 ERROR("signal %s not defined on this system, ignoring\n", name_);
120 return;
121
122 Ignore:
123 {
124 #ifdef HAVE_SIGACTION
125 struct sigaction act;
126
127 TRACE(traceSignal,
128 "asking kernel to ignore signal number %d\n",
129 st->sigtype);
130
131 sigemptyset(&act.sa_mask);
132 act.sa_handler = SIG_IGN;
133 act.sa_flags = 0;
134
135 if (sigaction(st->sigtype, &act, NULL) < 0) {
136 FATAL("sigaction ignore failed on %d: %s\n",
137 st->sigtype,
138 strerror(errno));
139 }
140 #else // HAVE_SIGACTION
141 signal(st->sigtype, SIG_IGN);
142 #endif // HAVE_SIGACTION
143
144 return;
145 }
146
147 Arm:
148 {
149 #ifdef HAVE_SIGACTION
150 struct sigaction act;
151
152 sigemptyset(&act.sa_mask);
153 act.sa_handler = (void (*)(...)) sysSignalFromKernel;
154 act.sa_flags = 0;
155
156 TRACE(traceSignal,
157 "arming %s signal\n",
158 name_);
159
160 if (sigaction(st->sigtype, &act, NULL) < 0) {
161 FATAL("sigaction failed on %d: %s\n",
162 st->sigtype,
163 strerror(errno));
164 }
165 #endif // HAVE_SIGACTION
166
167 #ifdef HAVE_SIGVEC
168 struct sigvec vec, ovec;
169
170 memset((char*) &vec, 0, sizeof(vec));
171 vec.sv_handler = sysSignalFromKernel;
172
173 TRACE(traceSignal,
174 "arming %s signal\n",
175 name_);
176 if (sigvec(st->sigtype, &vec, &ovec) < 0) {
177 FATAL("sigvec failed on %d: %s\n",
178 st->sigtype,
179 strerror(errno));
180 }
181 #endif // HAVE_SIGVEC
182
183 #ifdef HAVE_SIGSET
184 TRACE(traceSignal,
185 "arming %s signal\n",
186 name_);
187 sigset(st->sigtype, sysSignalFromKernel);
188 #endif // HAVE_SIGSET
189
190 return;
191 }
192 }
193
194 Signal::~Signal() {
/* [<][>][^][v][top][bottom][index][help] */
195 dispatcher.signals.remove(this);
196 }
197 //
198 // Copyright (c) 1994 by the University of Southern California.
199 // All rights reserved.
200 //
201 // Permission to use, copy, modify, and distribute this software and
202 // its documentation in source and binary forms for lawful
203 // non-commercial purposes and without fee is hereby granted, provided
204 // that the above copyright notice appear in all copies and that both
205 // the copyright notice and this permission notice appear in supporting
206 // documentation, and that any documentation, advertising materials,
207 // and other materials related to such distribution and use acknowledge
208 // that the software was developed by the University of Southern
209 // California and/or Information Sciences Institute.
210 // The name of the University of Southern California may not
211 // be used to endorse or promote products derived from this software
212 // without specific prior written permission.
213 //
214 // THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
215 // ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE. THIS SOFTWARE IS
216 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
217 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
218 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
219 // NON-INFRINGEMENT.
220 //
221 // IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
222 // SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
223 // TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
224 // THE USE OR PERFORMANCE OF THIS SOFTWARE.
225 //
226 // Questions concerning this software should be directed to
227 // info-ra@isi.edu.
228 //