blob: 36565e13da7c29d374d2cdc0e334808a0a118374 [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
alaffincc2e5552000-07-27 17:13:18 +00004 * 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.
vapier45a8ba02009-07-20 10:59:32 +00007 *
alaffincc2e5552000-07-27 17:13:18 +00008 * 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.
vapier45a8ba02009-07-20 10:59:32 +000011 *
alaffincc2e5552000-07-27 17:13:18 +000012 * 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.
vapier45a8ba02009-07-20 10:59:32 +000018 *
alaffincc2e5552000-07-27 17:13:18 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
alaffincc2e5552000-07-27 17:13:18 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
alaffincc2e5552000-07-27 17:13:18 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32
vapier6daba922009-08-28 09:29:01 +000033/* $Id: tst_sig.c,v 1.13 2009/08/28 09:29:01 vapier 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
Khem Rajad33a3d2016-07-21 21:26:51 -070052 SIGCHLD as an expected signal if fork() is being used.
alaffincc2e5552000-07-27 17:13:18 +000053
54 handler - a pointer to the unexpected signal handler to
55 be executed after an unexpected signal has been
vapier45a8ba02009-07-20 10:59:32 +000056 detected. If handler is set to DEF_HANDLER, a
alaffincc2e5552000-07-27 17:13:18 +000057 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
vapier45a8ba02009-07-20 10:59:32 +000064 is set so that other user-defined handlers have
alaffincc2e5552000-07-27 17:13:18 +000065 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>
mridgee6508f82005-01-04 21:00:17 +000073#include <unistd.h>
alaffincc2e5552000-07-27 17:13:18 +000074#include "test.h"
75
76#define MAXMESG 150 /* size of mesg string sent to tst_res */
77
Cyril Hrubisab1d3b62014-06-02 18:33:17 +020078static void (*T_cleanup) ();
alaffincc2e5552000-07-27 17:13:18 +000079
vapier6daba922009-08-28 09:29:01 +000080static void def_handler(); /* default signal handler */
81static void (*tst_setup_signal(int, void (*)(int))) (int);
alaffincc2e5552000-07-27 17:13:18 +000082
83/****************************************************************************
84 * tst_sig() : set-up to catch unexpected signals. fork_flag is set to NOFORK
Khem Rajad33a3d2016-07-21 21:26:51 -070085 * if SIGCHLD is to be an "unexpected signal", otherwise it is set to
alaffincc2e5552000-07-27 17:13:18 +000086 * FORK. cleanup points to a cleanup routine to be executed before
87 * tst_exit is called (cleanup is set to NULL if no cleanup is desired).
88 * handler is a pointer to the signal handling routine (if handler is
89 * set to NULL, a default handler is used).
90 ***************************************************************************/
91
vapier6daba922009-08-28 09:29:01 +000092void tst_sig(int fork_flag, void (*handler) (), void (*cleanup) ())
alaffincc2e5552000-07-27 17:13:18 +000093{
alaffincc2e5552000-07-27 17:13:18 +000094 int sig;
mridgee6508f82005-01-04 21:00:17 +000095#ifdef _SC_SIGRT_MIN
vapier6daba922009-08-28 09:29:01 +000096 long sigrtmin, sigrtmax;
mridgee6508f82005-01-04 21:00:17 +000097#endif
alaffincc2e5552000-07-27 17:13:18 +000098
99 /*
100 * save T_cleanup and handler function pointers
101 */
vapier6daba922009-08-28 09:29:01 +0000102 T_cleanup = cleanup; /* used by default handler */
alaffincc2e5552000-07-27 17:13:18 +0000103
104 if (handler == DEF_HANDLER) {
105 /* use default handler */
106 handler = def_handler;
107 }
mridgee6508f82005-01-04 21:00:17 +0000108#ifdef _SC_SIGRT_MIN
vapier6daba922009-08-28 09:29:01 +0000109 sigrtmin = sysconf(_SC_SIGRT_MIN);
110 sigrtmax = sysconf(_SC_SIGRT_MAX);
mridgee6508f82005-01-04 21:00:17 +0000111#endif
112
alaffincc2e5552000-07-27 17:13:18 +0000113 /*
114 * now loop through all signals and set the handlers
115 */
116
117 for (sig = 1; sig < NSIG; sig++) {
vapier6daba922009-08-28 09:29:01 +0000118 /*
119 * SIGKILL is never unexpected.
Khem Rajad33a3d2016-07-21 21:26:51 -0700120 * SIGCHLD is only unexpected when
vapier6daba922009-08-28 09:29:01 +0000121 * no forking is being done.
122 * SIGINFO is used for file quotas and should be expected
123 */
alaffincc2e5552000-07-27 17:13:18 +0000124
mridgee6508f82005-01-04 21:00:17 +0000125#ifdef _SC_SIGRT_MIN
vapier6daba922009-08-28 09:29:01 +0000126 if (sig >= sigrtmin && sig <= sigrtmax)
127 continue;
mridgee6508f82005-01-04 21:00:17 +0000128#endif
129
vapier6daba922009-08-28 09:29:01 +0000130 switch (sig) {
131 case SIGKILL:
132 case SIGSTOP:
133 case SIGCONT:
mridgee6508f82005-01-04 21:00:17 +0000134#if !defined(_SC_SIGRT_MIN) && defined(__SIGRTMIN) && defined(__SIGRTMAX)
vapier6daba922009-08-28 09:29:01 +0000135 /* Ignore all real-time signals */
robbiew3e36a842003-11-25 16:04:28 +0000136 case __SIGRTMIN:
vapier6daba922009-08-28 09:29:01 +0000137 case __SIGRTMIN + 1:
138 case __SIGRTMIN + 2:
139 case __SIGRTMIN + 3:
140 case __SIGRTMIN + 4:
141 case __SIGRTMIN + 5:
142 case __SIGRTMIN + 6:
143 case __SIGRTMIN + 7:
144 case __SIGRTMIN + 8:
145 case __SIGRTMIN + 9:
146 case __SIGRTMIN + 10:
147 case __SIGRTMIN + 11:
148 case __SIGRTMIN + 12:
149 case __SIGRTMIN + 13:
150 case __SIGRTMIN + 14:
151 case __SIGRTMIN + 15:
robbiew79c135c2004-05-19 20:44:50 +0000152/* __SIGRTMIN is 37 on HPPA rather than 32 *
153 * as on i386, etc. */
154#if !defined(__hppa__)
vapier6daba922009-08-28 09:29:01 +0000155 case __SIGRTMAX - 15:
156 case __SIGRTMAX - 14:
157 case __SIGRTMAX - 13:
158 case __SIGRTMAX - 12:
159 case __SIGRTMAX - 11:
robbiew79c135c2004-05-19 20:44:50 +0000160#endif
vapier6daba922009-08-28 09:29:01 +0000161 case __SIGRTMAX - 10:
162 case __SIGRTMAX - 9:
163 case __SIGRTMAX - 8:
164 case __SIGRTMAX - 7:
165 case __SIGRTMAX - 6:
166 case __SIGRTMAX - 5:
167 case __SIGRTMAX - 4:
168 case __SIGRTMAX - 3:
169 case __SIGRTMAX - 2:
170 case __SIGRTMAX - 1:
robbiew3e36a842003-11-25 16:04:28 +0000171 case __SIGRTMAX:
mridgee6508f82005-01-04 21:00:17 +0000172#endif
alaffincc2e5552000-07-27 17:13:18 +0000173#ifdef SIGSWAP
robbiew3e36a842003-11-25 16:04:28 +0000174 case SIGSWAP:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175#endif /* SIGSWAP */
alaffincc2e5552000-07-27 17:13:18 +0000176
177#ifdef SIGCKPT
vapier6daba922009-08-28 09:29:01 +0000178 case SIGCKPT:
alaffincc2e5552000-07-27 17:13:18 +0000179#endif
180#ifdef SIGRESTART
vapier6daba922009-08-28 09:29:01 +0000181 case SIGRESTART:
alaffincc2e5552000-07-27 17:13:18 +0000182#endif
vapier6daba922009-08-28 09:29:01 +0000183 /*
184 * pthread-private signals SIGPTINTR and SIGPTRESCHED.
185 * Setting a handler for these signals is disallowed when
186 * the binary is linked against libpthread.
187 */
alaffincc2e5552000-07-27 17:13:18 +0000188#ifdef SIGPTINTR
vapier6daba922009-08-28 09:29:01 +0000189 case SIGPTINTR:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190#endif /* SIGPTINTR */
alaffincc2e5552000-07-27 17:13:18 +0000191#ifdef SIGPTRESCHED
vapier6daba922009-08-28 09:29:01 +0000192 case SIGPTRESCHED:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193#endif /* SIGPTRESCHED */
mridgee6508f82005-01-04 21:00:17 +0000194#ifdef _SIGRESERVE
vapier6daba922009-08-28 09:29:01 +0000195 case _SIGRESERVE:
mridgee6508f82005-01-04 21:00:17 +0000196#endif
197#ifdef _SIGDIL
vapier6daba922009-08-28 09:29:01 +0000198 case _SIGDIL:
mridgee6508f82005-01-04 21:00:17 +0000199#endif
200#ifdef _SIGCANCEL
vapier6daba922009-08-28 09:29:01 +0000201 case _SIGCANCEL:
mridgee6508f82005-01-04 21:00:17 +0000202#endif
203#ifdef _SIGGFAULT
vapier6daba922009-08-28 09:29:01 +0000204 case _SIGGFAULT:
mridgee6508f82005-01-04 21:00:17 +0000205#endif
vapier6daba922009-08-28 09:29:01 +0000206 break;
Cyril Hrubis0c1ba5c2014-06-11 10:53:13 +0200207
Khem Rajad33a3d2016-07-21 21:26:51 -0700208 case SIGCHLD:
Cyril Hrubis0c1ba5c2014-06-11 10:53:13 +0200209 if (fork_flag == FORK)
210 continue;
211
vapier6daba922009-08-28 09:29:01 +0000212 default:
213 if (tst_setup_signal(sig, handler) == SIG_ERR)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 tst_resm(TWARN | TERRNO,
215 "signal failed for signal %d", sig);
vapier6daba922009-08-28 09:29:01 +0000216 break;
217 }
Cyril Hrubisab1d3b62014-06-02 18:33:17 +0200218 }
alaffincc2e5552000-07-27 17:13:18 +0000219}
220
alaffincc2e5552000-07-27 17:13:18 +0000221/****************************************************************************
222 * def_handler() : default signal handler that is invoked when
223 * an unexpected signal is caught.
224 ***************************************************************************/
225
vapier6daba922009-08-28 09:29:01 +0000226static void def_handler(int sig)
alaffincc2e5552000-07-27 17:13:18 +0000227{
alaffincc2e5552000-07-27 17:13:18 +0000228 /*
vapier6daba922009-08-28 09:29:01 +0000229 * Break remaining test cases, do any cleanup, then exit
alaffincc2e5552000-07-27 17:13:18 +0000230 */
Garrett Cooper6a464fe2010-12-18 06:32:42 -0800231 tst_brkm(TBROK, T_cleanup,
Xiaoguang Wang1a6f68a2014-05-13 19:02:59 +0800232 "unexpected signal %s(%d) received (pid = %d).",
233 tst_strsig(sig), sig, getpid());
alaffincc2e5552000-07-27 17:13:18 +0000234}
alaffin3c8699d2000-09-07 14:34:44 +0000235
236/*
237 * tst_setup_signal - A function like signal(), but we have
238 * control over its personality.
239 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800240static void (*tst_setup_signal(int sig, void (*handler) (int))) (int) {
vapier6daba922009-08-28 09:29:01 +0000241 struct sigaction my_act, old_act;
242 int ret;
alaffin3c8699d2000-09-07 14:34:44 +0000243
vapier6daba922009-08-28 09:29:01 +0000244 my_act.sa_handler = handler;
245 my_act.sa_flags = SA_RESTART;
246 sigemptyset(&my_act.sa_mask);
alaffin3c8699d2000-09-07 14:34:44 +0000247
vapier6daba922009-08-28 09:29:01 +0000248 ret = sigaction(sig, &my_act, &old_act);
alaffin3c8699d2000-09-07 14:34:44 +0000249
vapier6daba922009-08-28 09:29:01 +0000250 if (ret == 0)
251 return old_act.sa_handler;
252 else
253 return SIG_ERR;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700254}