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