1 /*---------------------------------------------------------------
2 * Copyright (c) 1999,2000,2001,2002,2003
3 * The Board of Trustees of the University of Illinois
5 *---------------------------------------------------------------
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software (Iperf) and associated
8 * documentation files (the "Software"), to deal in the Software
9 * without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute,
11 * sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do
13 * so, subject to the following conditions:
16 * Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and
18 * the following disclaimers.
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimers in the documentation and/or other materials
24 * provided with the distribution.
27 * Neither the names of the University of Illinois, NCSA,
28 * nor the names of its contributors may be used to endorse
29 * or promote products derived from this Software without
30 * specific prior written permission.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
36 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
38 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 * ________________________________________________________________
41 * National Laboratory for Applied Network Research
42 * National Center for Supercomputing Applications
43 * University of Illinois at Urbana-Champaign
44 * http://www.ncsa.uiuc.edu
45 * ________________________________________________________________
48 * by Mark Gates <mgates@nlanr.net>
49 * -------------------------------------------------------------------
50 * standard signal installer
51 * ------------------------------------------------------------------- */
62 /* list of signal handlers. _NSIG is number of signals defined. */
64 static SigfuncPtr handlers[ _NSIG ] = { 0};
66 /* -------------------------------------------------------------------
69 * dispatches the signal to appropriate signal handler. This emulates
70 * the signal handling of Unix.
72 * ------------------------------------------------------------------- */
74 BOOL WINAPI sig_dispatcher( DWORD type ) {
81 h = handlers[ SIGINT ];
84 case CTRL_CLOSE_EVENT:
85 case CTRL_LOGOFF_EVENT:
86 case CTRL_SHUTDOWN_EVENT:
88 h = handlers[ SIGTERM ];
96 // call the signal handler
104 /* -------------------------------------------------------------------
107 * installs a signal handler. I emulate Unix signals by storing the
108 * function pointers and dispatching events myself, using the
109 * sig_dispatcher above.
110 * ------------------------------------------------------------------- */
112 SigfuncPtr my_signal( int inSigno, SigfuncPtr inFunc ) {
113 SigfuncPtr old = NULL;
115 if ( inSigno >= 0 && inSigno < _NSIG ) {
116 old = handlers[ inSigno ];
117 handlers[ inSigno ] = inFunc;
121 } /* end my_signal */
123 #else /* not WIN32 */
125 /* -------------------------------------------------------------------
128 * installs a signal handler, and returns the old handler.
129 * This emulates the semi-standard signal() function in a
130 * standard way using the Posix sigaction function.
132 * from Stevens, 1998, section 5.8
133 * ------------------------------------------------------------------- */
135 SigfuncPtr my_signal( int inSigno, SigfuncPtr inFunc ) {
136 struct sigaction theNewAction, theOldAction;
138 assert( inFunc != NULL );
140 theNewAction.sa_handler = inFunc;
141 sigemptyset( &theNewAction.sa_mask );
142 theNewAction.sa_flags = 0;
144 if ( inSigno == SIGALRM ) {
146 theNewAction.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
150 theNewAction.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
154 if ( sigaction( inSigno, &theNewAction, &theOldAction ) < 0 ) {
157 return theOldAction.sa_handler;
159 } /* end my_signal */
161 #endif /* not WIN32 */
163 /* -------------------------------------------------------------------
166 * Quietly exits. This protects some against being called multiple
167 * times. (TODO: should use a mutex to ensure (num++ == 0) is atomic.)
168 * ------------------------------------------------------------------- */
170 void sig_exit( int inSigno ) {
179 } /* end extern "C" */