blob: db0a549239500cedb6e69324a78590d13e739773 [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32
nstrazd8522012001-06-01 19:46:13 +000033/* $Id: tst_sig.c,v 1.4 2001/06/01 19:46:13 nstraz Exp $ */
alaffincc2e5552000-07-27 17:13:18 +000034
35/*****************************************************************************
36 OS Testing - Silicon Graphics, Inc.
37
38 FUNCTION IDENTIFIER : tst_sig Set up for unexpected signals.
39
40 AUTHOR : David D. Fenner
41
42 CO-PILOT : Bill Roske
43
44 DATE STARTED : 06/06/90
45
46 This module may be linked with c-modules requiring unexpected
47 signal handling. The parameters to tst_sig are as follows:
48
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.
53
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.
59
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.
67
68***************************************************************************/
69
alaffincc2e5552000-07-27 17:13:18 +000070#include <errno.h>
71#include <string.h>
72#include <signal.h>
73#include "test.h"
74
75#define MAXMESG 150 /* size of mesg string sent to tst_res */
76
77void (*T_cleanup)(); /* pointer to cleanup function */
78
nstrazd8522012001-06-01 19:46:13 +000079/****************************************************************************
80 * STD_COPIES is defined in parse_opts.c but is externed here in order to
81 * test whether SIGCHILD should be ignored or not.
82 ***************************************************************************/
83extern int STD_COPIES;
84
alaffincc2e5552000-07-27 17:13:18 +000085extern int errno;
86static void def_handler(); /* default signal handler */
alaffin3c8699d2000-09-07 14:34:44 +000087static void (*tst_setup_signal( int, void (*)(int)))(int);
alaffincc2e5552000-07-27 17:13:18 +000088
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 ***************************************************************************/
97
98void
nstraz94181082000-08-30 18:43:38 +000099tst_sig(int fork_flag, void (*handler)(), void (*cleanup)())
alaffincc2e5552000-07-27 17:13:18 +0000100{
alaffincc2e5552000-07-27 17:13:18 +0000101 char mesg[MAXMESG]; /* message buffer for tst_res */
102 int sig;
103
104 /*
105 * save T_cleanup and handler function pointers
106 */
107 T_cleanup = cleanup; /* used by default handler */
108
109 if (handler == DEF_HANDLER) {
110 /* use default handler */
111 handler = def_handler;
112 }
113
114 /*
115 * now loop through all signals and set the handlers
116 */
117
118 for (sig = 1; sig < NSIG; sig++) {
119 /*
120 * SIGKILL is never unexpected.
121 * SIGCLD is only unexpected when
122 * no forking is being done.
123 * SIGINFO is used for file quotas and should be expected
124 */
125
126 switch (sig) {
127 case SIGKILL:
128 case SIGSTOP:
129 case SIGCONT:
130#ifdef CRAY
131 case SIGINFO:
132 case SIGRECOVERY: /* allow chkpnt/restart */
133#endif /* CRAY */
134
135#ifdef SIGSWAP
136 case SIGSWAP:
137#endif /* SIGSWAP */
138
139#ifdef SIGCKPT
140 case SIGCKPT:
141#endif
142#ifdef SIGRESTART
143 case SIGRESTART:
144#endif
145 /*
146 * pthread-private signals SIGPTINTR and SIGPTRESCHED.
147 * Setting a handler for these signals is disallowed when
148 * the binary is linked against libpthread.
149 */
150#ifdef SIGPTINTR
151 case SIGPTINTR:
152#endif /* SIGPTINTR */
153#ifdef SIGPTRESCHED
154 case SIGPTRESCHED:
155#endif /* SIGPTRESCHED */
156
157 break;
158
159 case SIGCLD:
nstrazd8522012001-06-01 19:46:13 +0000160 if ( fork_flag == FORK || STD_COPIES > 1)
alaffincc2e5552000-07-27 17:13:18 +0000161 continue;
162
163 default:
alaffin3c8699d2000-09-07 14:34:44 +0000164 if (tst_setup_signal(sig, handler) == SIG_ERR) {
alaffincc2e5552000-07-27 17:13:18 +0000165 (void) sprintf(mesg,
166 "signal() failed for signal %d. error:%d %s.",
167 sig, errno, strerror(errno));
168 tst_resm(TWARN, mesg);
169 }
170 break;
171 }
172#ifdef __sgi
173 /* On irix (07/96), signal() fails when signo is 33 or higher */
174 if ( sig+1 >= 33 )
175 break;
176#endif /* __sgi */
177
178 } /* endfor */
179}
180
181
182
183/****************************************************************************
184 * def_handler() : default signal handler that is invoked when
185 * an unexpected signal is caught.
186 ***************************************************************************/
187
188static void
nstraz94181082000-08-30 18:43:38 +0000189def_handler(int sig)
alaffincc2e5552000-07-27 17:13:18 +0000190{
alaffincc2e5552000-07-27 17:13:18 +0000191
192 /*
193 * Break remaining test cases, do any cleanup, then exit
194 */
alaffin3c8699d2000-09-07 14:34:44 +0000195 tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig);
alaffincc2e5552000-07-27 17:13:18 +0000196
197 /* now cleanup and exit */
198 if (T_cleanup) {
199 (*T_cleanup)();
200 }
201
202 tst_exit();
203}
alaffin3c8699d2000-09-07 14:34:44 +0000204
205/*
206 * tst_setup_signal - A function like signal(), but we have
207 * control over its personality.
208 */
209static void (*tst_setup_signal( int sig, void (*handler)(int)))(int)
210{
211 struct sigaction my_act,old_act;
212 int ret;
213
214 my_act.sa_handler = handler;
215 my_act.sa_flags = SA_RESTART;
216 sigemptyset(&my_act.sa_mask);
217
218 ret = sigaction(sig, &my_act, &old_act);
219
220 if ( ret == 0 )
221 return( old_act.sa_handler );
222 else
223 return( SIG_ERR );
224}
225