Adapt copyright for a new version
[ltp-debian.git] / lib / tst_sig.c
blobc4b514e0f224491d061e7d4ee8d465599dd490ce
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /* $Id: tst_sig.c,v 1.13 2009/08/28 09:29:01 vapier Exp $ */
35 /*****************************************************************************
36 OS Testing - Silicon Graphics, Inc.
38 FUNCTION IDENTIFIER : tst_sig Set up for unexpected signals.
40 AUTHOR : David D. Fenner
42 CO-PILOT : Bill Roske
44 DATE STARTED : 06/06/90
46 This module may be linked with c-modules requiring unexpected
47 signal handling. The parameters to tst_sig are as follows:
49 fork_flag - set to FORK or NOFORK depending upon whether the
50 calling program executes a fork() system call. It
51 is normally the case that the calling program treats
52 SIGCLD as an expected signal if fork() is being used.
54 handler - a pointer to the unexpected signal handler to
55 be executed after an unexpected signal has been
56 detected. If handler is set to DEF_HANDLER, a
57 default handler is used. This routine should be
58 declared as function returning an int.
60 cleanup - a pointer to a cleanup routine to be executed
61 by the unexpected signal handler before tst_exit is
62 called. This parameter is set to NULL if no cleanup
63 routine is required. An external variable, T_cleanup
64 is set so that other user-defined handlers have
65 access to the cleanup routine. This routine should be
66 declared as returning type void.
68 ***************************************************************************/
70 #include <errno.h>
71 #include <string.h>
72 #include <signal.h>
73 #include <unistd.h>
74 #include "test.h"
76 #define MAXMESG 150 /* size of mesg string sent to tst_res */
78 void (*T_cleanup) (); /* pointer to cleanup function */
80 /****************************************************************************
81 * STD_COPIES is defined in parse_opts.c but is externed here in order to
82 * test whether SIGCHILD should be ignored or not.
83 ***************************************************************************/
84 extern int STD_COPIES;
86 static void def_handler(); /* default signal handler */
87 static void (*tst_setup_signal(int, void (*)(int))) (int);
89 /****************************************************************************
90 * tst_sig() : set-up to catch unexpected signals. fork_flag is set to NOFORK
91 * if SIGCLD is to be an "unexpected signal", otherwise it is set to
92 * FORK. cleanup points to a cleanup routine to be executed before
93 * tst_exit is called (cleanup is set to NULL if no cleanup is desired).
94 * handler is a pointer to the signal handling routine (if handler is
95 * set to NULL, a default handler is used).
96 ***************************************************************************/
98 void tst_sig(int fork_flag, void (*handler) (), void (*cleanup) ())
100 int sig;
101 #ifdef _SC_SIGRT_MIN
102 long sigrtmin, sigrtmax;
103 #endif
106 * save T_cleanup and handler function pointers
108 T_cleanup = cleanup; /* used by default handler */
110 if (handler == DEF_HANDLER) {
111 /* use default handler */
112 handler = def_handler;
114 #ifdef _SC_SIGRT_MIN
115 sigrtmin = sysconf(_SC_SIGRT_MIN);
116 sigrtmax = sysconf(_SC_SIGRT_MAX);
117 #endif
120 * now loop through all signals and set the handlers
123 for (sig = 1; sig < NSIG; sig++) {
125 * SIGKILL is never unexpected.
126 * SIGCLD is only unexpected when
127 * no forking is being done.
128 * SIGINFO is used for file quotas and should be expected
131 #ifdef _SC_SIGRT_MIN
132 if (sig >= sigrtmin && sig <= sigrtmax)
133 continue;
134 #endif
136 switch (sig) {
137 case SIGKILL:
138 case SIGSTOP:
139 case SIGCONT:
140 #if !defined(_SC_SIGRT_MIN) && defined(__SIGRTMIN) && defined(__SIGRTMAX)
141 /* Ignore all real-time signals */
142 case __SIGRTMIN:
143 case __SIGRTMIN + 1:
144 case __SIGRTMIN + 2:
145 case __SIGRTMIN + 3:
146 case __SIGRTMIN + 4:
147 case __SIGRTMIN + 5:
148 case __SIGRTMIN + 6:
149 case __SIGRTMIN + 7:
150 case __SIGRTMIN + 8:
151 case __SIGRTMIN + 9:
152 case __SIGRTMIN + 10:
153 case __SIGRTMIN + 11:
154 case __SIGRTMIN + 12:
155 case __SIGRTMIN + 13:
156 case __SIGRTMIN + 14:
157 case __SIGRTMIN + 15:
158 /* __SIGRTMIN is 37 on HPPA rather than 32 *
159 * as on i386, etc. */
160 #if !defined(__hppa__)
161 case __SIGRTMAX - 15:
162 case __SIGRTMAX - 14:
163 case __SIGRTMAX - 13:
164 case __SIGRTMAX - 12:
165 case __SIGRTMAX - 11:
166 #endif
167 case __SIGRTMAX - 10:
168 case __SIGRTMAX - 9:
169 case __SIGRTMAX - 8:
170 case __SIGRTMAX - 7:
171 case __SIGRTMAX - 6:
172 case __SIGRTMAX - 5:
173 case __SIGRTMAX - 4:
174 case __SIGRTMAX - 3:
175 case __SIGRTMAX - 2:
176 case __SIGRTMAX - 1:
177 case __SIGRTMAX:
178 #endif
179 #ifdef CRAY
180 case SIGINFO:
181 case SIGRECOVERY: /* allow chkpnt/restart */
182 #endif /* CRAY */
184 #ifdef SIGSWAP
185 case SIGSWAP:
186 #endif /* SIGSWAP */
188 #ifdef SIGCKPT
189 case SIGCKPT:
190 #endif
191 #ifdef SIGRESTART
192 case SIGRESTART:
193 #endif
195 * pthread-private signals SIGPTINTR and SIGPTRESCHED.
196 * Setting a handler for these signals is disallowed when
197 * the binary is linked against libpthread.
199 #ifdef SIGPTINTR
200 case SIGPTINTR:
201 #endif /* SIGPTINTR */
202 #ifdef SIGPTRESCHED
203 case SIGPTRESCHED:
204 #endif /* SIGPTRESCHED */
205 #ifdef _SIGRESERVE
206 case _SIGRESERVE:
207 #endif
208 #ifdef _SIGDIL
209 case _SIGDIL:
210 #endif
211 #ifdef _SIGCANCEL
212 case _SIGCANCEL:
213 #endif
214 #ifdef _SIGGFAULT
215 case _SIGGFAULT:
216 #endif
217 break;
219 case SIGCLD:
220 if (fork_flag == FORK || STD_COPIES > 1)
221 continue;
223 default:
224 if (tst_setup_signal(sig, handler) == SIG_ERR)
225 tst_resm(TWARN | TERRNO,
226 "signal() failed for signal %d", sig);
227 break;
229 #ifdef __sgi
230 /* On irix (07/96), signal() fails when signo is 33 or higher */
231 if (sig + 1 >= 33)
232 break;
233 #endif /* __sgi */
235 } /* endfor */
238 /****************************************************************************
239 * def_handler() : default signal handler that is invoked when
240 * an unexpected signal is caught.
241 ***************************************************************************/
243 static void def_handler(int sig)
246 * Break remaining test cases, do any cleanup, then exit
248 tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig);
250 /* now cleanup and exit */
251 if (T_cleanup)
252 (*T_cleanup) ();
254 tst_exit();
258 * tst_setup_signal - A function like signal(), but we have
259 * control over its personality.
261 static void (*tst_setup_signal(int sig, void (*handler) (int))) (int)
263 struct sigaction my_act, old_act;
264 int ret;
266 my_act.sa_handler = handler;
267 my_act.sa_flags = SA_RESTART;
268 sigemptyset(&my_act.sa_mask);
270 ret = sigaction(sig, &my_act, &old_act);
272 if (ret == 0)
273 return old_act.sa_handler;
274 else
275 return SIG_ERR;